Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The IMGW-PIB integration."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 import logging
7 
8 from aiohttp import ClientError
9 from imgw_pib import ImgwPib
10 from imgw_pib.exceptions import ApiError
11 
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import Platform
14 from homeassistant.core import HomeAssistant
15 from homeassistant.exceptions import ConfigEntryNotReady
16 from homeassistant.helpers.aiohttp_client import async_get_clientsession
17 
18 from .const import CONF_STATION_ID
19 from .coordinator import ImgwPibDataUpdateCoordinator
20 
21 PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 type ImgwPibConfigEntry = ConfigEntry[ImgwPibData]
26 
27 
28 @dataclass
30  """Data for the IMGW-PIB integration."""
31 
32  coordinator: ImgwPibDataUpdateCoordinator
33 
34 
35 async def async_setup_entry(hass: HomeAssistant, entry: ImgwPibConfigEntry) -> bool:
36  """Set up IMGW-PIB from a config entry."""
37  station_id: str = entry.data[CONF_STATION_ID]
38 
39  _LOGGER.debug("Using hydrological station ID: %s", station_id)
40 
41  client_session = async_get_clientsession(hass)
42 
43  try:
44  imgwpib = await ImgwPib.create(
45  client_session, hydrological_station_id=station_id
46  )
47  except (ClientError, TimeoutError, ApiError) as err:
48  raise ConfigEntryNotReady from err
49 
50  coordinator = ImgwPibDataUpdateCoordinator(hass, imgwpib, station_id)
51  await coordinator.async_config_entry_first_refresh()
52 
53  entry.runtime_data = ImgwPibData(coordinator)
54 
55  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
56 
57  return True
58 
59 
60 async def async_unload_entry(hass: HomeAssistant, entry: ImgwPibConfigEntry) -> bool:
61  """Unload a config entry."""
62  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, ImgwPibConfigEntry entry)
Definition: __init__.py:35
bool async_unload_entry(HomeAssistant hass, ImgwPibConfigEntry entry)
Definition: __init__.py:60
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)