2020-05-10 02:16:21 +00:00
|
|
|
"""The BSB-Lan integration."""
|
2024-03-08 18:15:59 +00:00
|
|
|
|
2022-10-18 10:06:51 +00:00
|
|
|
import dataclasses
|
2020-05-10 02:16:21 +00:00
|
|
|
|
2024-08-12 08:57:51 +00:00
|
|
|
from bsblan import BSBLAN, BSBLANConfig, Device, Info, StaticState
|
2020-05-10 02:16:21 +00:00
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-03 16:51:30 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_USERNAME,
|
|
|
|
Platform,
|
|
|
|
)
|
2020-05-10 02:16:21 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
|
2024-10-24 15:53:06 +00:00
|
|
|
from .const import CONF_PASSKEY
|
2023-08-30 11:14:30 +00:00
|
|
|
from .coordinator import BSBLanUpdateCoordinator
|
2020-05-10 02:16:21 +00:00
|
|
|
|
2024-09-13 12:04:00 +00:00
|
|
|
PLATFORMS = [Platform.CLIMATE, Platform.SENSOR]
|
2021-04-27 06:46:49 +00:00
|
|
|
|
2024-10-24 15:53:06 +00:00
|
|
|
type BSBLanConfigEntry = ConfigEntry[BSBLanData]
|
|
|
|
|
2020-05-10 02:16:21 +00:00
|
|
|
|
2022-10-18 10:06:51 +00:00
|
|
|
@dataclasses.dataclass
|
2024-08-23 06:42:36 +00:00
|
|
|
class BSBLanData:
|
2022-10-18 10:06:51 +00:00
|
|
|
"""BSBLan data stored in the Home Assistant data object."""
|
|
|
|
|
2023-11-06 20:43:02 +00:00
|
|
|
coordinator: BSBLanUpdateCoordinator
|
2022-10-18 10:06:51 +00:00
|
|
|
client: BSBLAN
|
|
|
|
device: Device
|
|
|
|
info: Info
|
2022-11-30 15:36:33 +00:00
|
|
|
static: StaticState
|
2022-10-18 10:06:51 +00:00
|
|
|
|
|
|
|
|
2024-10-24 15:53:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: BSBLanConfigEntry) -> bool:
|
2020-05-10 02:16:21 +00:00
|
|
|
"""Set up BSB-Lan from a config entry."""
|
|
|
|
|
2024-08-12 08:57:51 +00:00
|
|
|
# create config using BSBLANConfig
|
|
|
|
config = BSBLANConfig(
|
|
|
|
host=entry.data[CONF_HOST],
|
2020-05-10 02:16:21 +00:00
|
|
|
passkey=entry.data[CONF_PASSKEY],
|
|
|
|
port=entry.data[CONF_PORT],
|
2020-11-30 19:13:16 +00:00
|
|
|
username=entry.data.get(CONF_USERNAME),
|
|
|
|
password=entry.data.get(CONF_PASSWORD),
|
2020-05-10 02:16:21 +00:00
|
|
|
)
|
|
|
|
|
2024-08-12 08:57:51 +00:00
|
|
|
# create BSBLAN client
|
|
|
|
session = async_get_clientsession(hass)
|
|
|
|
bsblan = BSBLAN(config, session)
|
|
|
|
|
|
|
|
# Create and perform first refresh of the coordinator
|
2023-08-30 11:14:30 +00:00
|
|
|
coordinator = BSBLanUpdateCoordinator(hass, entry, bsblan)
|
2022-10-18 10:06:51 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
|
2024-08-12 08:57:51 +00:00
|
|
|
# Fetch all required data concurrently
|
2022-10-18 10:06:51 +00:00
|
|
|
device = await bsblan.device()
|
|
|
|
info = await bsblan.info()
|
2022-11-30 15:36:33 +00:00
|
|
|
static = await bsblan.static_values()
|
2024-08-12 08:57:51 +00:00
|
|
|
|
2024-10-24 15:53:06 +00:00
|
|
|
entry.runtime_data = BSBLanData(
|
2022-10-18 10:06:51 +00:00
|
|
|
client=bsblan,
|
|
|
|
coordinator=coordinator,
|
|
|
|
device=device,
|
|
|
|
info=info,
|
2022-11-30 15:36:33 +00:00
|
|
|
static=static,
|
2022-10-18 10:06:51 +00:00
|
|
|
)
|
2020-05-10 02:16:21 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2020-05-10 02:16:21 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2024-10-24 15:53:06 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: BSBLanConfigEntry) -> bool:
|
2022-10-18 10:06:51 +00:00
|
|
|
"""Unload BSBLAN config entry."""
|
2024-10-24 15:53:06 +00:00
|
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|