Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The BSB-Lan integration."""
2 
3 import dataclasses
4 
5 from bsblan import BSBLAN, BSBLANConfig, Device, Info, StaticState
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import (
9  CONF_HOST,
10  CONF_PASSWORD,
11  CONF_PORT,
12  CONF_USERNAME,
13  Platform,
14 )
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.aiohttp_client import async_get_clientsession
17 
18 from .const import CONF_PASSKEY
19 from .coordinator import BSBLanUpdateCoordinator
20 
21 PLATFORMS = [Platform.CLIMATE, Platform.SENSOR, Platform.WATER_HEATER]
22 
23 type BSBLanConfigEntry = ConfigEntry[BSBLanData]
24 
25 
26 @dataclasses.dataclass
27 class BSBLanData:
28  """BSBLan data stored in the Home Assistant data object."""
29 
30  coordinator: BSBLanUpdateCoordinator
31  client: BSBLAN
32  device: Device
33  info: Info
34  static: StaticState
35 
36 
37 async def async_setup_entry(hass: HomeAssistant, entry: BSBLanConfigEntry) -> bool:
38  """Set up BSB-Lan from a config entry."""
39 
40  # create config using BSBLANConfig
41  config = BSBLANConfig(
42  host=entry.data[CONF_HOST],
43  passkey=entry.data[CONF_PASSKEY],
44  port=entry.data[CONF_PORT],
45  username=entry.data.get(CONF_USERNAME),
46  password=entry.data.get(CONF_PASSWORD),
47  )
48 
49  # create BSBLAN client
50  session = async_get_clientsession(hass)
51  bsblan = BSBLAN(config, session)
52 
53  # Create and perform first refresh of the coordinator
54  coordinator = BSBLanUpdateCoordinator(hass, entry, bsblan)
55  await coordinator.async_config_entry_first_refresh()
56 
57  # Fetch all required data concurrently
58  device = await bsblan.device()
59  info = await bsblan.info()
60  static = await bsblan.static_values()
61 
62  entry.runtime_data = BSBLanData(
63  client=bsblan,
64  coordinator=coordinator,
65  device=device,
66  info=info,
67  static=static,
68  )
69 
70  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
71 
72  return True
73 
74 
75 async def async_unload_entry(hass: HomeAssistant, entry: BSBLanConfigEntry) -> bool:
76  """Unload BSBLAN config entry."""
77  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, BSBLanConfigEntry entry)
Definition: __init__.py:37
bool async_unload_entry(HomeAssistant hass, BSBLanConfigEntry entry)
Definition: __init__.py:75
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)