Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The AccuWeather component."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from accuweather import AccuWeather
8 
9 from homeassistant.components.sensor import DOMAIN as SENSOR_PLATFORM
10 from homeassistant.const import CONF_API_KEY, CONF_NAME, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers import entity_registry as er
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 
15 from .const import DOMAIN, UPDATE_INTERVAL_DAILY_FORECAST, UPDATE_INTERVAL_OBSERVATION
16 from .coordinator import (
17  AccuWeatherConfigEntry,
18  AccuWeatherDailyForecastDataUpdateCoordinator,
19  AccuWeatherData,
20  AccuWeatherObservationDataUpdateCoordinator,
21 )
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 PLATFORMS = [Platform.SENSOR, Platform.WEATHER]
26 
27 
28 async def async_setup_entry(hass: HomeAssistant, entry: AccuWeatherConfigEntry) -> bool:
29  """Set up AccuWeather as config entry."""
30  api_key: str = entry.data[CONF_API_KEY]
31  name: str = entry.data[CONF_NAME]
32 
33  location_key = entry.unique_id
34 
35  _LOGGER.debug("Using location_key: %s", location_key)
36 
37  websession = async_get_clientsession(hass)
38  accuweather = AccuWeather(api_key, websession, location_key=location_key)
39 
40  coordinator_observation = AccuWeatherObservationDataUpdateCoordinator(
41  hass,
42  entry,
43  accuweather,
44  name,
45  "observation",
46  UPDATE_INTERVAL_OBSERVATION,
47  )
48 
49  coordinator_daily_forecast = AccuWeatherDailyForecastDataUpdateCoordinator(
50  hass,
51  entry,
52  accuweather,
53  name,
54  "daily forecast",
55  UPDATE_INTERVAL_DAILY_FORECAST,
56  )
57 
58  await coordinator_observation.async_config_entry_first_refresh()
59  await coordinator_daily_forecast.async_config_entry_first_refresh()
60 
61  entry.runtime_data = AccuWeatherData(
62  coordinator_observation=coordinator_observation,
63  coordinator_daily_forecast=coordinator_daily_forecast,
64  )
65 
66  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
67 
68  # Remove ozone sensors from registry if they exist
69  ent_reg = er.async_get(hass)
70  for day in range(5):
71  unique_id = f"{location_key}-ozone-{day}"
72  if entity_id := ent_reg.async_get_entity_id(SENSOR_PLATFORM, DOMAIN, unique_id):
73  _LOGGER.debug("Removing ozone sensor entity %s", entity_id)
74  ent_reg.async_remove(entity_id)
75 
76  return True
77 
78 
80  hass: HomeAssistant, entry: AccuWeatherConfigEntry
81 ) -> bool:
82  """Unload a config entry."""
83  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, AccuWeatherConfigEntry entry)
Definition: __init__.py:28
bool async_unload_entry(HomeAssistant hass, AccuWeatherConfigEntry entry)
Definition: __init__.py:81
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)