mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Use runtime_data in iaqualink (#144988)
This commit is contained in:
parent
b8df9c7e97
commit
e74aeeab1a
@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Awaitable, Callable, Coroutine
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from functools import wraps
|
||||
import logging
|
||||
@ -19,11 +20,6 @@ from iaqualink.device import (
|
||||
)
|
||||
from iaqualink.exception import AqualinkServiceException
|
||||
|
||||
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
||||
from homeassistant.components.climate import DOMAIN as CLIMATE_DOMAIN
|
||||
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
@ -48,21 +44,27 @@ PLATFORMS = [
|
||||
Platform.SWITCH,
|
||||
]
|
||||
|
||||
type AqualinkConfigEntry = ConfigEntry[AqualinkRuntimeData]
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
@dataclass
|
||||
class AqualinkRuntimeData:
|
||||
"""Runtime data for Aqualink."""
|
||||
|
||||
client: AqualinkClient
|
||||
# These will contain the initialized devices
|
||||
binary_sensors: list[AqualinkBinarySensor]
|
||||
lights: list[AqualinkLight]
|
||||
sensors: list[AqualinkSensor]
|
||||
switches: list[AqualinkSwitch]
|
||||
thermostats: list[AqualinkThermostat]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: AqualinkConfigEntry) -> bool:
|
||||
"""Set up Aqualink from a config entry."""
|
||||
username = entry.data[CONF_USERNAME]
|
||||
password = entry.data[CONF_PASSWORD]
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
|
||||
# These will contain the initialized devices
|
||||
binary_sensors = hass.data[DOMAIN][BINARY_SENSOR_DOMAIN] = []
|
||||
climates = hass.data[DOMAIN][CLIMATE_DOMAIN] = []
|
||||
lights = hass.data[DOMAIN][LIGHT_DOMAIN] = []
|
||||
sensors = hass.data[DOMAIN][SENSOR_DOMAIN] = []
|
||||
switches = hass.data[DOMAIN][SWITCH_DOMAIN] = []
|
||||
|
||||
aqualink = AqualinkClient(username, password, httpx_client=get_async_client(hass))
|
||||
try:
|
||||
await aqualink.login()
|
||||
@ -90,6 +92,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
await aqualink.close()
|
||||
return False
|
||||
|
||||
runtime_data = AqualinkRuntimeData(
|
||||
aqualink, binary_sensors=[], lights=[], sensors=[], switches=[], thermostats=[]
|
||||
)
|
||||
for system in systems:
|
||||
try:
|
||||
devices = await system.get_devices()
|
||||
@ -101,36 +106,35 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
for dev in devices.values():
|
||||
if isinstance(dev, AqualinkThermostat):
|
||||
climates += [dev]
|
||||
runtime_data.thermostats += [dev]
|
||||
elif isinstance(dev, AqualinkLight):
|
||||
lights += [dev]
|
||||
runtime_data.lights += [dev]
|
||||
elif isinstance(dev, AqualinkSwitch):
|
||||
switches += [dev]
|
||||
runtime_data.switches += [dev]
|
||||
elif isinstance(dev, AqualinkBinarySensor):
|
||||
binary_sensors += [dev]
|
||||
runtime_data.binary_sensors += [dev]
|
||||
elif isinstance(dev, AqualinkSensor):
|
||||
sensors += [dev]
|
||||
runtime_data.sensors += [dev]
|
||||
|
||||
platforms = []
|
||||
if binary_sensors:
|
||||
_LOGGER.debug("Got %s binary sensors: %s", len(binary_sensors), binary_sensors)
|
||||
platforms.append(Platform.BINARY_SENSOR)
|
||||
if climates:
|
||||
_LOGGER.debug("Got %s climates: %s", len(climates), climates)
|
||||
platforms.append(Platform.CLIMATE)
|
||||
if lights:
|
||||
_LOGGER.debug("Got %s lights: %s", len(lights), lights)
|
||||
platforms.append(Platform.LIGHT)
|
||||
if sensors:
|
||||
_LOGGER.debug("Got %s sensors: %s", len(sensors), sensors)
|
||||
platforms.append(Platform.SENSOR)
|
||||
if switches:
|
||||
_LOGGER.debug("Got %s switches: %s", len(switches), switches)
|
||||
platforms.append(Platform.SWITCH)
|
||||
_LOGGER.debug(
|
||||
"Got %s binary sensors: %s",
|
||||
len(runtime_data.binary_sensors),
|
||||
runtime_data.binary_sensors,
|
||||
)
|
||||
_LOGGER.debug("Got %s lights: %s", len(runtime_data.lights), runtime_data.lights)
|
||||
_LOGGER.debug("Got %s sensors: %s", len(runtime_data.sensors), runtime_data.sensors)
|
||||
_LOGGER.debug(
|
||||
"Got %s switches: %s", len(runtime_data.switches), runtime_data.switches
|
||||
)
|
||||
_LOGGER.debug(
|
||||
"Got %s thermostats: %s",
|
||||
len(runtime_data.thermostats),
|
||||
runtime_data.thermostats,
|
||||
)
|
||||
|
||||
hass.data[DOMAIN]["client"] = aqualink
|
||||
entry.runtime_data = runtime_data
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, platforms)
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
async def _async_systems_update(_: datetime) -> None:
|
||||
"""Refresh internal state for all systems."""
|
||||
@ -161,18 +165,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: AqualinkConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
aqualink = hass.data[DOMAIN]["client"]
|
||||
await aqualink.close()
|
||||
|
||||
platforms_to_unload = [
|
||||
platform for platform in PLATFORMS if platform in hass.data[DOMAIN]
|
||||
]
|
||||
|
||||
del hass.data[DOMAIN]
|
||||
|
||||
return await hass.config_entries.async_unload_platforms(entry, platforms_to_unload)
|
||||
await entry.runtime_data.client.close()
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
|
||||
def refresh_system[_AqualinkEntityT: AqualinkEntity, **_P](
|
||||
|
@ -5,15 +5,13 @@ from __future__ import annotations
|
||||
from iaqualink.device import AqualinkBinarySensor
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
DOMAIN as BINARY_SENSOR_DOMAIN,
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
from . import AqualinkConfigEntry
|
||||
from .entity import AqualinkEntity
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
@ -21,14 +19,14 @@ PARALLEL_UPDATES = 0
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: AqualinkConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up discovered binary sensors."""
|
||||
async_add_entities(
|
||||
(
|
||||
HassAqualinkBinarySensor(dev)
|
||||
for dev in hass.data[DOMAIN][BINARY_SENSOR_DOMAIN]
|
||||
for dev in config_entry.runtime_data.binary_sensors
|
||||
),
|
||||
True,
|
||||
)
|
||||
|
@ -9,19 +9,16 @@ from iaqualink.device import AqualinkThermostat
|
||||
from iaqualink.systems.iaqua.device import AqualinkState
|
||||
|
||||
from homeassistant.components.climate import (
|
||||
DOMAIN as CLIMATE_DOMAIN,
|
||||
ClimateEntity,
|
||||
ClimateEntityFeature,
|
||||
HVACAction,
|
||||
HVACMode,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from . import refresh_system
|
||||
from .const import DOMAIN
|
||||
from . import AqualinkConfigEntry, refresh_system
|
||||
from .entity import AqualinkEntity
|
||||
from .utils import await_or_reraise
|
||||
|
||||
@ -32,12 +29,12 @@ PARALLEL_UPDATES = 0
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: AqualinkConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up discovered switches."""
|
||||
async_add_entities(
|
||||
(HassAqualinkThermostat(dev) for dev in hass.data[DOMAIN][CLIMATE_DOMAIN]),
|
||||
(HassAqualinkThermostat(dev) for dev in config_entry.runtime_data.thermostats),
|
||||
True,
|
||||
)
|
||||
|
||||
|
@ -9,17 +9,14 @@ from iaqualink.device import AqualinkLight
|
||||
from homeassistant.components.light import (
|
||||
ATTR_BRIGHTNESS,
|
||||
ATTR_EFFECT,
|
||||
DOMAIN as LIGHT_DOMAIN,
|
||||
ColorMode,
|
||||
LightEntity,
|
||||
LightEntityFeature,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from . import refresh_system
|
||||
from .const import DOMAIN
|
||||
from . import AqualinkConfigEntry, refresh_system
|
||||
from .entity import AqualinkEntity
|
||||
from .utils import await_or_reraise
|
||||
|
||||
@ -28,12 +25,12 @@ PARALLEL_UPDATES = 0
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: AqualinkConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up discovered lights."""
|
||||
async_add_entities(
|
||||
(HassAqualinkLight(dev) for dev in hass.data[DOMAIN][LIGHT_DOMAIN]),
|
||||
(HassAqualinkLight(dev) for dev in config_entry.runtime_data.lights),
|
||||
True,
|
||||
)
|
||||
|
||||
|
@ -4,17 +4,12 @@ from __future__ import annotations
|
||||
|
||||
from iaqualink.device import AqualinkSensor
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
DOMAIN as SENSOR_DOMAIN,
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
||||
from homeassistant.const import UnitOfTemperature
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
from . import AqualinkConfigEntry
|
||||
from .entity import AqualinkEntity
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
@ -22,12 +17,12 @@ PARALLEL_UPDATES = 0
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: AqualinkConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up discovered sensors."""
|
||||
async_add_entities(
|
||||
(HassAqualinkSensor(dev) for dev in hass.data[DOMAIN][SENSOR_DOMAIN]),
|
||||
(HassAqualinkSensor(dev) for dev in config_entry.runtime_data.sensors),
|
||||
True,
|
||||
)
|
||||
|
||||
|
@ -6,13 +6,11 @@ from typing import Any
|
||||
|
||||
from iaqualink.device import AqualinkSwitch
|
||||
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN, SwitchEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from . import refresh_system
|
||||
from .const import DOMAIN
|
||||
from . import AqualinkConfigEntry, refresh_system
|
||||
from .entity import AqualinkEntity
|
||||
from .utils import await_or_reraise
|
||||
|
||||
@ -21,12 +19,12 @@ PARALLEL_UPDATES = 0
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: AqualinkConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up discovered switches."""
|
||||
async_add_entities(
|
||||
(HassAqualinkSwitch(dev) for dev in hass.data[DOMAIN][SWITCH_DOMAIN]),
|
||||
(HassAqualinkSwitch(dev) for dev in config_entry.runtime_data.switches),
|
||||
True,
|
||||
)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user