Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The GIOS component."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
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 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_STATION_ID, DOMAIN
16 from .coordinator import GiosDataUpdateCoordinator
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 PLATFORMS = [Platform.SENSOR]
21 
22 type GiosConfigEntry = ConfigEntry[GiosData]
23 
24 
25 @dataclass
26 class GiosData:
27  """Data for GIOS integration."""
28 
29  coordinator: GiosDataUpdateCoordinator
30 
31 
32 async def async_setup_entry(hass: HomeAssistant, entry: GiosConfigEntry) -> bool:
33  """Set up GIOS as config entry."""
34  station_id: int = entry.data[CONF_STATION_ID]
35  _LOGGER.debug("Using station_id: %d", station_id)
36 
37  # We used to use int as config_entry unique_id, convert this to str.
38  if isinstance(entry.unique_id, int):
39  hass.config_entries.async_update_entry(entry, unique_id=str(station_id)) # type: ignore[unreachable]
40 
41  # We used to use int in device_entry identifiers, convert this to str.
42  device_registry = dr.async_get(hass)
43  old_ids = (DOMAIN, station_id)
44  device_entry = device_registry.async_get_device(identifiers={old_ids}) # type: ignore[arg-type]
45  if device_entry and entry.entry_id in device_entry.config_entries:
46  new_ids = (DOMAIN, str(station_id))
47  device_registry.async_update_device(device_entry.id, new_identifiers={new_ids})
48 
49  websession = async_get_clientsession(hass)
50 
51  coordinator = GiosDataUpdateCoordinator(hass, websession, station_id)
52  await coordinator.async_config_entry_first_refresh()
53 
54  entry.runtime_data = GiosData(coordinator)
55 
56  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
57 
58  # Remove air_quality entities from registry if they exist
59  ent_reg = er.async_get(hass)
60  unique_id = str(coordinator.gios.station_id)
61  if entity_id := ent_reg.async_get_entity_id(
62  AIR_QUALITY_PLATFORM, DOMAIN, unique_id
63  ):
64  _LOGGER.debug("Removing deprecated air_quality entity %s", entity_id)
65  ent_reg.async_remove(entity_id)
66 
67  return True
68 
69 
70 async def async_unload_entry(hass: HomeAssistant, entry: GiosConfigEntry) -> bool:
71  """Unload a config entry."""
72  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, GiosConfigEntry entry)
Definition: __init__.py:70
bool async_setup_entry(HomeAssistant hass, GiosConfigEntry entry)
Definition: __init__.py:32
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)