Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Airly integration."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 
8 from homeassistant.components.air_quality import DOMAIN as AIR_QUALITY_PLATFORM
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers import device_registry as dr, entity_registry as er
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 
15 from .const import CONF_USE_NEAREST, DOMAIN, MIN_UPDATE_INTERVAL
16 from .coordinator import AirlyDataUpdateCoordinator
17 
18 PLATFORMS = [Platform.SENSOR]
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 type AirlyConfigEntry = ConfigEntry[AirlyDataUpdateCoordinator]
23 
24 
25 async def async_setup_entry(hass: HomeAssistant, entry: AirlyConfigEntry) -> bool:
26  """Set up Airly as config entry."""
27  api_key = entry.data[CONF_API_KEY]
28  latitude = entry.data[CONF_LATITUDE]
29  longitude = entry.data[CONF_LONGITUDE]
30  use_nearest = entry.data.get(CONF_USE_NEAREST, False)
31 
32  # For backwards compat, set unique ID
33  if entry.unique_id is None:
34  hass.config_entries.async_update_entry(
35  entry, unique_id=f"{latitude}-{longitude}"
36  )
37 
38  # identifiers in device_info should use tuple[str, str] type, but latitude and
39  # longitude are float, so we convert old device entries to use correct types
40  # We used to use a str 3-tuple here sometime, convert that to a 2-tuple too.
41  device_registry = dr.async_get(hass)
42  old_ids = (DOMAIN, latitude, longitude)
43  for old_ids in (
44  (DOMAIN, latitude, longitude),
45  (
46  DOMAIN,
47  str(latitude),
48  str(longitude),
49  ),
50  ):
51  device_entry = device_registry.async_get_device(identifiers={old_ids}) # type: ignore[arg-type]
52  if device_entry and entry.entry_id in device_entry.config_entries:
53  new_ids = (DOMAIN, f"{latitude}-{longitude}")
54  device_registry.async_update_device(
55  device_entry.id, new_identifiers={new_ids}
56  )
57 
58  websession = async_get_clientsession(hass)
59 
60  update_interval = timedelta(minutes=MIN_UPDATE_INTERVAL)
61 
62  coordinator = AirlyDataUpdateCoordinator(
63  hass, websession, api_key, latitude, longitude, update_interval, use_nearest
64  )
65  await coordinator.async_config_entry_first_refresh()
66 
67  entry.runtime_data = coordinator
68 
69  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
70 
71  # Remove air_quality entities from registry if they exist
72  ent_reg = er.async_get(hass)
73  unique_id = f"{coordinator.latitude}-{coordinator.longitude}"
74  if entity_id := ent_reg.async_get_entity_id(
75  AIR_QUALITY_PLATFORM, DOMAIN, unique_id
76  ):
77  _LOGGER.debug("Removing deprecated air_quality entity %s", entity_id)
78  ent_reg.async_remove(entity_id)
79 
80  return True
81 
82 
83 async def async_unload_entry(hass: HomeAssistant, entry: AirlyConfigEntry) -> bool:
84  """Unload a config entry."""
85  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, AirlyConfigEntry entry)
Definition: __init__.py:83
bool async_setup_entry(HomeAssistant hass, AirlyConfigEntry entry)
Definition: __init__.py:25
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)