Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The AirNow integration."""
2 
3 import datetime
4 import logging
5 
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.const import (
8  CONF_API_KEY,
9  CONF_LATITUDE,
10  CONF_LONGITUDE,
11  CONF_RADIUS,
12  Platform,
13 )
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers import device_registry as dr, entity_registry as er
16 from homeassistant.helpers.aiohttp_client import async_get_clientsession
17 
18 from .coordinator import AirNowDataUpdateCoordinator
19 
20 _LOGGER = logging.getLogger(__name__)
21 PLATFORMS = [Platform.SENSOR]
22 
23 type AirNowConfigEntry = ConfigEntry[AirNowDataUpdateCoordinator]
24 
25 
26 async def async_setup_entry(hass: HomeAssistant, entry: AirNowConfigEntry) -> bool:
27  """Set up AirNow from a config entry."""
28  api_key = entry.data[CONF_API_KEY]
29  latitude = entry.data[CONF_LATITUDE]
30  longitude = entry.data[CONF_LONGITUDE]
31 
32  # Station Radius is a user-configurable option
33  distance = entry.options[CONF_RADIUS]
34 
35  # Reports are published hourly but update twice per hour
36  update_interval = datetime.timedelta(minutes=30)
37 
38  # Setup the Coordinator
39  session = async_get_clientsession(hass)
40  coordinator = AirNowDataUpdateCoordinator(
41  hass, session, api_key, latitude, longitude, distance, update_interval
42  )
43 
44  # Sync with Coordinator
45  await coordinator.async_config_entry_first_refresh()
46 
47  # Store Entity and Initialize Platforms
48  entry.runtime_data = coordinator
49 
50  # Listen for option changes
51  entry.async_on_unload(entry.add_update_listener(update_listener))
52 
53  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
54 
55  # Clean up unused device entries with no entities
56  device_registry = dr.async_get(hass)
57  entity_registry = er.async_get(hass)
58 
59  device_entries = dr.async_entries_for_config_entry(
60  device_registry, config_entry_id=entry.entry_id
61  )
62  for dev in device_entries:
63  dev_entities = er.async_entries_for_device(
64  entity_registry, dev.id, include_disabled_entities=True
65  )
66  if not dev_entities:
67  device_registry.async_remove_device(dev.id)
68 
69  return True
70 
71 
72 async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
73  """Migrate old entry."""
74  _LOGGER.debug("Migrating from version %s", entry.version)
75 
76  if entry.version == 1:
77  new_options = {CONF_RADIUS: entry.data[CONF_RADIUS]}
78  new_data = entry.data.copy()
79  del new_data[CONF_RADIUS]
80 
81  hass.config_entries.async_update_entry(
82  entry, data=new_data, options=new_options, version=2
83  )
84 
85  _LOGGER.info("Migration to version %s successful", entry.version)
86 
87  return True
88 
89 
90 async def async_unload_entry(hass: HomeAssistant, entry: AirNowConfigEntry) -> bool:
91  """Unload a config entry."""
92  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
93 
94 
95 async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
96  """Handle options update."""
97  await hass.config_entries.async_reload(entry.entry_id)
bool async_unload_entry(HomeAssistant hass, AirNowConfigEntry entry)
Definition: __init__.py:90
bool async_setup_entry(HomeAssistant hass, AirNowConfigEntry entry)
Definition: __init__.py:26
None update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:95
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:72
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)