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