mirror of
https://github.com/home-assistant/core.git
synced 2026-04-20 08:29:39 +02:00
Use runtime_data in VeSync (#159720)
Co-authored-by: Joostlek <joostlek@outlook.com>
This commit is contained in:
@@ -9,7 +9,6 @@ from pyvesync.utils.errors import (
|
||||
VeSyncServerError,
|
||||
)
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
@@ -18,8 +17,8 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.device_registry import DeviceEntry
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .const import DOMAIN, VS_COORDINATOR, VS_MANAGER
|
||||
from .coordinator import VeSyncDataCoordinator
|
||||
from .const import DOMAIN
|
||||
from .coordinator import VesyncConfigEntry, VeSyncDataCoordinator
|
||||
from .services import async_setup_services
|
||||
|
||||
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
||||
@@ -47,7 +46,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, config_entry: VesyncConfigEntry
|
||||
) -> bool:
|
||||
"""Set up Vesync as config entry."""
|
||||
username = config_entry.data[CONF_USERNAME]
|
||||
password = config_entry.data[CONF_PASSWORD]
|
||||
@@ -75,31 +76,24 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
||||
translation_domain=DOMAIN, translation_key="api_response_error"
|
||||
) from err
|
||||
|
||||
hass.data[DOMAIN] = {}
|
||||
hass.data[DOMAIN][VS_MANAGER] = manager
|
||||
|
||||
coordinator = VeSyncDataCoordinator(hass, config_entry, manager)
|
||||
|
||||
# Store coordinator at domain level since only single integration instance is permitted.
|
||||
hass.data[DOMAIN][VS_COORDINATOR] = coordinator
|
||||
await manager.update()
|
||||
await manager.check_firmware()
|
||||
|
||||
config_entry.runtime_data = VeSyncDataCoordinator(hass, config_entry, manager)
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: VesyncConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if unload_ok:
|
||||
hass.data.pop(DOMAIN)
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
|
||||
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
async def async_migrate_entry(
|
||||
hass: HomeAssistant, config_entry: VesyncConfigEntry
|
||||
) -> bool:
|
||||
"""Migrate old entry."""
|
||||
_LOGGER.debug(
|
||||
"Migrating VeSync config entry: %s minor version: %s",
|
||||
@@ -134,10 +128,10 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
|
||||
|
||||
|
||||
async def async_remove_config_entry_device(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
|
||||
hass: HomeAssistant, config_entry: VesyncConfigEntry, device_entry: DeviceEntry
|
||||
) -> bool:
|
||||
"""Remove a config entry from a device."""
|
||||
manager = hass.data[DOMAIN][VS_MANAGER]
|
||||
manager = config_entry.runtime_data.manager
|
||||
await manager.get_devices()
|
||||
for dev in manager.devices:
|
||||
if isinstance(dev.sub_device_no, int):
|
||||
|
||||
@@ -7,20 +7,20 @@ from dataclasses import dataclass
|
||||
import logging
|
||||
|
||||
from pyvesync.base_devices.vesyncbasedevice import VeSyncBaseDevice
|
||||
from pyvesync.device_container import DeviceContainer
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .common import rgetattr
|
||||
from .const import DOMAIN, VS_COORDINATOR, VS_DEVICES, VS_DISCOVERY, VS_MANAGER
|
||||
from .coordinator import VeSyncDataCoordinator
|
||||
from .const import VS_DEVICES, VS_DISCOVERY
|
||||
from .coordinator import VesyncConfigEntry, VeSyncDataCoordinator
|
||||
from .entity import VeSyncBaseEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -56,12 +56,12 @@ SENSOR_DESCRIPTIONS: tuple[VeSyncBinarySensorEntityDescription, ...] = (
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: VesyncConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up binary_sensor platform."""
|
||||
|
||||
coordinator = hass.data[DOMAIN][VS_COORDINATOR]
|
||||
coordinator = config_entry.runtime_data
|
||||
|
||||
@callback
|
||||
def discover(devices: list[VeSyncBaseDevice]) -> None:
|
||||
@@ -73,13 +73,13 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
_setup_entities(
|
||||
hass.data[DOMAIN][VS_MANAGER].devices, async_add_entities, coordinator
|
||||
config_entry.runtime_data.manager.devices, async_add_entities, coordinator
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def _setup_entities(
|
||||
devices: list[VeSyncBaseDevice],
|
||||
devices: DeviceContainer | list[VeSyncBaseDevice],
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
coordinator: VeSyncDataCoordinator,
|
||||
) -> None:
|
||||
|
||||
@@ -22,8 +22,6 @@ Energy history is weekly/monthly/yearly and can be updated a lot more infrequent
|
||||
in this case every 6 hours.
|
||||
"""
|
||||
VS_DEVICES = "devices"
|
||||
VS_COORDINATOR = "coordinator"
|
||||
VS_MANAGER = "manager"
|
||||
VS_LISTENERS = "listeners"
|
||||
VS_NUMBERS = "numbers"
|
||||
|
||||
|
||||
@@ -15,18 +15,20 @@ from .const import UPDATE_INTERVAL, UPDATE_INTERVAL_ENERGY
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
type VesyncConfigEntry = ConfigEntry[VeSyncDataCoordinator]
|
||||
|
||||
|
||||
class VeSyncDataCoordinator(DataUpdateCoordinator[None]):
|
||||
"""Class representing data coordinator for VeSync devices."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
config_entry: VesyncConfigEntry
|
||||
update_time: datetime | None = None
|
||||
|
||||
def __init__(
|
||||
self, hass: HomeAssistant, config_entry: ConfigEntry, manager: VeSync
|
||||
self, hass: HomeAssistant, config_entry: VesyncConfigEntry, manager: VeSync
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
self._manager = manager
|
||||
self.manager = manager
|
||||
|
||||
super().__init__(
|
||||
hass,
|
||||
@@ -48,9 +50,9 @@ class VeSyncDataCoordinator(DataUpdateCoordinator[None]):
|
||||
async def _async_update_data(self) -> None:
|
||||
"""Fetch data from API endpoint."""
|
||||
|
||||
await self._manager.update_all_devices()
|
||||
await self.manager.update_all_devices()
|
||||
|
||||
if self.should_update_energy():
|
||||
self.update_time = datetime.now()
|
||||
for outlet in self._manager.devices.outlets:
|
||||
for outlet in self.manager.devices.outlets:
|
||||
await outlet.update_energy()
|
||||
|
||||
@@ -7,21 +7,21 @@ from typing import Any, cast
|
||||
from pyvesync import VeSync
|
||||
|
||||
from homeassistant.components.diagnostics import REDACTED
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.device_registry import DeviceEntry
|
||||
|
||||
from .const import DOMAIN, VS_MANAGER
|
||||
from .const import DOMAIN
|
||||
from .coordinator import VesyncConfigEntry
|
||||
|
||||
KEYS_TO_REDACT = {"manager", "uuid", "mac_id"}
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry
|
||||
hass: HomeAssistant, config_entry: VesyncConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
manager: VeSync = hass.data[DOMAIN][VS_MANAGER]
|
||||
manager: VeSync = config_entry.runtime_data.manager
|
||||
|
||||
return {
|
||||
DOMAIN: {
|
||||
@@ -39,10 +39,10 @@ async def async_get_config_entry_diagnostics(
|
||||
|
||||
|
||||
async def async_get_device_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry, device: DeviceEntry
|
||||
hass: HomeAssistant, config_entry: VesyncConfigEntry, device: DeviceEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a device entry."""
|
||||
manager: VeSync = hass.data[DOMAIN][VS_MANAGER]
|
||||
manager: VeSync = config_entry.runtime_data.manager
|
||||
vesync_device_id = next(iden[1] for iden in device.identifiers if iden[0] == DOMAIN)
|
||||
|
||||
def get_vesync_unique_id(dev: Any) -> str:
|
||||
|
||||
@@ -6,9 +6,9 @@ import logging
|
||||
from typing import Any
|
||||
|
||||
from pyvesync.base_devices.vesyncbasedevice import VeSyncBaseDevice
|
||||
from pyvesync.device_container import DeviceContainer
|
||||
|
||||
from homeassistant.components.fan import FanEntity, FanEntityFeature
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
@@ -20,8 +20,6 @@ from homeassistant.util.percentage import (
|
||||
|
||||
from .common import is_fan, is_purifier, rgetattr
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
VS_COORDINATOR,
|
||||
VS_DEVICES,
|
||||
VS_DISCOVERY,
|
||||
VS_FAN_MODE_ADVANCED_SLEEP,
|
||||
@@ -32,9 +30,8 @@ from .const import (
|
||||
VS_FAN_MODE_PRESET_LIST_HA,
|
||||
VS_FAN_MODE_SLEEP,
|
||||
VS_FAN_MODE_TURBO,
|
||||
VS_MANAGER,
|
||||
)
|
||||
from .coordinator import VeSyncDataCoordinator
|
||||
from .coordinator import VesyncConfigEntry, VeSyncDataCoordinator
|
||||
from .entity import VeSyncBaseEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -53,12 +50,12 @@ VS_TO_HA_MODE_MAP = {
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: VesyncConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the VeSync fan platform."""
|
||||
|
||||
coordinator = hass.data[DOMAIN][VS_COORDINATOR]
|
||||
coordinator = config_entry.runtime_data
|
||||
|
||||
@callback
|
||||
def discover(devices: list[VeSyncBaseDevice]) -> None:
|
||||
@@ -70,13 +67,13 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
_setup_entities(
|
||||
hass.data[DOMAIN][VS_MANAGER].devices, async_add_entities, coordinator
|
||||
config_entry.runtime_data.manager.devices, async_add_entities, coordinator
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def _setup_entities(
|
||||
devices: list[VeSyncBaseDevice],
|
||||
devices: DeviceContainer | list[VeSyncBaseDevice],
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
coordinator: VeSyncDataCoordinator,
|
||||
) -> None:
|
||||
|
||||
@@ -12,24 +12,20 @@ from homeassistant.components.humidifier import (
|
||||
HumidifierEntity,
|
||||
HumidifierEntityFeature,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
VS_COORDINATOR,
|
||||
VS_DEVICES,
|
||||
VS_DISCOVERY,
|
||||
VS_HUMIDIFIER_MODE_AUTO,
|
||||
VS_HUMIDIFIER_MODE_HUMIDITY,
|
||||
VS_HUMIDIFIER_MODE_MANUAL,
|
||||
VS_HUMIDIFIER_MODE_SLEEP,
|
||||
VS_MANAGER,
|
||||
)
|
||||
from .coordinator import VeSyncDataCoordinator
|
||||
from .coordinator import VesyncConfigEntry, VeSyncDataCoordinator
|
||||
from .entity import VeSyncBaseEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -45,12 +41,12 @@ VS_TO_HA_MODE_MAP = {
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: VesyncConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the VeSync humidifier platform."""
|
||||
|
||||
coordinator = hass.data[DOMAIN][VS_COORDINATOR]
|
||||
coordinator = config_entry.runtime_data
|
||||
|
||||
@callback
|
||||
def discover(devices: list[VeSyncBaseDevice]) -> None:
|
||||
@@ -62,7 +58,7 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
_setup_entities(
|
||||
hass.data[DOMAIN][VS_MANAGER].devices.humidifiers,
|
||||
config_entry.runtime_data.manager.devices.humidifiers,
|
||||
async_add_entities,
|
||||
coordinator,
|
||||
)
|
||||
|
||||
@@ -6,6 +6,7 @@ from typing import Any
|
||||
from pyvesync.base_devices.bulb_base import VeSyncBulb
|
||||
from pyvesync.base_devices.switch_base import VeSyncSwitch
|
||||
from pyvesync.base_devices.vesyncbasedevice import VeSyncBaseDevice
|
||||
from pyvesync.device_container import DeviceContainer
|
||||
|
||||
from homeassistant.components.light import (
|
||||
ATTR_BRIGHTNESS,
|
||||
@@ -13,14 +14,13 @@ from homeassistant.components.light import (
|
||||
ColorMode,
|
||||
LightEntity,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.util import color as color_util
|
||||
|
||||
from .const import DOMAIN, VS_COORDINATOR, VS_DEVICES, VS_DISCOVERY, VS_MANAGER
|
||||
from .coordinator import VeSyncDataCoordinator
|
||||
from .const import VS_DEVICES, VS_DISCOVERY
|
||||
from .coordinator import VesyncConfigEntry, VeSyncDataCoordinator
|
||||
from .entity import VeSyncBaseEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -30,12 +30,12 @@ MIN_MIREDS = 153 # 1,000,000 divided by 6500 Kelvin = 153 Mireds
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: VesyncConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up lights."""
|
||||
|
||||
coordinator = hass.data[DOMAIN][VS_COORDINATOR]
|
||||
coordinator = config_entry.runtime_data
|
||||
|
||||
@callback
|
||||
def discover(devices: list[VeSyncBaseDevice]) -> None:
|
||||
@@ -47,13 +47,13 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
_setup_entities(
|
||||
hass.data[DOMAIN][VS_MANAGER].devices, async_add_entities, coordinator
|
||||
config_entry.runtime_data.manager.devices, async_add_entities, coordinator
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def _setup_entities(
|
||||
devices: list[VeSyncBaseDevice],
|
||||
devices: DeviceContainer | list[VeSyncBaseDevice],
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
coordinator: VeSyncDataCoordinator,
|
||||
) -> None:
|
||||
|
||||
@@ -5,21 +5,21 @@ from dataclasses import dataclass
|
||||
import logging
|
||||
|
||||
from pyvesync.base_devices.vesyncbasedevice import VeSyncBaseDevice
|
||||
from pyvesync.device_container import DeviceContainer
|
||||
|
||||
from homeassistant.components.number import (
|
||||
NumberEntity,
|
||||
NumberEntityDescription,
|
||||
NumberMode,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .common import is_humidifier
|
||||
from .const import DOMAIN, VS_COORDINATOR, VS_DEVICES, VS_DISCOVERY, VS_MANAGER
|
||||
from .coordinator import VeSyncDataCoordinator
|
||||
from .const import VS_DEVICES, VS_DISCOVERY
|
||||
from .coordinator import VesyncConfigEntry, VeSyncDataCoordinator
|
||||
from .entity import VeSyncBaseEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -53,12 +53,12 @@ NUMBER_DESCRIPTIONS: list[VeSyncNumberEntityDescription] = [
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: VesyncConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up number entities."""
|
||||
|
||||
coordinator = hass.data[DOMAIN][VS_COORDINATOR]
|
||||
coordinator = config_entry.runtime_data
|
||||
|
||||
@callback
|
||||
def discover(devices: list[VeSyncBaseDevice]) -> None:
|
||||
@@ -70,13 +70,13 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
_setup_entities(
|
||||
hass.data[DOMAIN][VS_MANAGER].devices, async_add_entities, coordinator
|
||||
config_entry.runtime_data.manager.devices, async_add_entities, coordinator
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def _setup_entities(
|
||||
devices: list[VeSyncBaseDevice],
|
||||
devices: DeviceContainer | list[VeSyncBaseDevice],
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
coordinator: VeSyncDataCoordinator,
|
||||
) -> None:
|
||||
|
||||
@@ -5,9 +5,9 @@ from dataclasses import dataclass
|
||||
import logging
|
||||
|
||||
from pyvesync.base_devices.vesyncbasedevice import VeSyncBaseDevice
|
||||
from pyvesync.device_container import DeviceContainer
|
||||
|
||||
from homeassistant.components.select import SelectEntity, SelectEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
@@ -15,7 +15,6 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .common import is_humidifier, is_outlet, is_purifier
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
HUMIDIFIER_NIGHT_LIGHT_LEVEL_BRIGHT,
|
||||
HUMIDIFIER_NIGHT_LIGHT_LEVEL_DIM,
|
||||
HUMIDIFIER_NIGHT_LIGHT_LEVEL_OFF,
|
||||
@@ -25,12 +24,10 @@ from .const import (
|
||||
PURIFIER_NIGHT_LIGHT_LEVEL_DIM,
|
||||
PURIFIER_NIGHT_LIGHT_LEVEL_OFF,
|
||||
PURIFIER_NIGHT_LIGHT_LEVEL_ON,
|
||||
VS_COORDINATOR,
|
||||
VS_DEVICES,
|
||||
VS_DISCOVERY,
|
||||
VS_MANAGER,
|
||||
)
|
||||
from .coordinator import VeSyncDataCoordinator
|
||||
from .coordinator import VesyncConfigEntry, VeSyncDataCoordinator
|
||||
from .entity import VeSyncBaseEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -107,12 +104,12 @@ SELECT_DESCRIPTIONS: list[VeSyncSelectEntityDescription] = [
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: VesyncConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up select entities."""
|
||||
|
||||
coordinator = hass.data[DOMAIN][VS_COORDINATOR]
|
||||
coordinator = config_entry.runtime_data
|
||||
|
||||
@callback
|
||||
def discover(devices: list[VeSyncBaseDevice]) -> None:
|
||||
@@ -124,13 +121,13 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
_setup_entities(
|
||||
hass.data[DOMAIN][VS_MANAGER].devices, async_add_entities, coordinator
|
||||
config_entry.runtime_data.manager.devices, async_add_entities, coordinator
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def _setup_entities(
|
||||
devices: list[VeSyncBaseDevice],
|
||||
devices: DeviceContainer | list[VeSyncBaseDevice],
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
coordinator: VeSyncDataCoordinator,
|
||||
) -> None:
|
||||
|
||||
@@ -7,6 +7,7 @@ from dataclasses import dataclass
|
||||
import logging
|
||||
|
||||
from pyvesync.base_devices.vesyncbasedevice import VeSyncBaseDevice
|
||||
from pyvesync.device_container import DeviceContainer
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
@@ -14,7 +15,6 @@ from homeassistant.components.sensor import (
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||
PERCENTAGE,
|
||||
@@ -30,8 +30,8 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
from .common import is_humidifier, is_outlet, rgetattr
|
||||
from .const import DOMAIN, VS_COORDINATOR, VS_DEVICES, VS_DISCOVERY, VS_MANAGER
|
||||
from .coordinator import VeSyncDataCoordinator
|
||||
from .const import VS_DEVICES, VS_DISCOVERY
|
||||
from .coordinator import VesyncConfigEntry, VeSyncDataCoordinator
|
||||
from .entity import VeSyncBaseEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -154,12 +154,12 @@ SENSORS: tuple[VeSyncSensorEntityDescription, ...] = (
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: VesyncConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up switches."""
|
||||
|
||||
coordinator = hass.data[DOMAIN][VS_COORDINATOR]
|
||||
coordinator = config_entry.runtime_data
|
||||
|
||||
@callback
|
||||
def discover(devices: list[VeSyncBaseDevice]) -> None:
|
||||
@@ -171,13 +171,13 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
_setup_entities(
|
||||
hass.data[DOMAIN][VS_MANAGER].devices, async_add_entities, coordinator
|
||||
config_entry.runtime_data.manager.devices, async_add_entities, coordinator
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def _setup_entities(
|
||||
devices: list[VeSyncBaseDevice],
|
||||
devices: DeviceContainer | list[VeSyncBaseDevice],
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
coordinator: VeSyncDataCoordinator,
|
||||
) -> None:
|
||||
|
||||
@@ -5,7 +5,8 @@ from homeassistant.core import HomeAssistant, ServiceCall, callback
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
|
||||
from .const import DOMAIN, SERVICE_UPDATE_DEVS, VS_DEVICES, VS_DISCOVERY, VS_MANAGER
|
||||
from . import VesyncConfigEntry
|
||||
from .const import DOMAIN, SERVICE_UPDATE_DEVS, VS_DEVICES, VS_DISCOVERY
|
||||
|
||||
|
||||
@callback
|
||||
@@ -21,13 +22,13 @@ async def async_new_device_discovery(call: ServiceCall) -> None:
|
||||
"""Discover and add new devices."""
|
||||
|
||||
entries = call.hass.config_entries.async_entries(DOMAIN)
|
||||
entry = entries[0] if entries else None
|
||||
config_entry: VesyncConfigEntry | None = entries[0] if entries else None
|
||||
|
||||
if not entry:
|
||||
if not config_entry:
|
||||
raise ServiceValidationError("Entry not found")
|
||||
if entry.state is not ConfigEntryState.LOADED:
|
||||
if config_entry.state is not ConfigEntryState.LOADED:
|
||||
raise ServiceValidationError("Entry not loaded")
|
||||
manager = call.hass.data[DOMAIN][VS_MANAGER]
|
||||
manager = config_entry.runtime_data.manager
|
||||
known_devices = list(manager.devices)
|
||||
await manager.get_devices()
|
||||
new_devices = [device for device in manager.devices if device not in known_devices]
|
||||
|
||||
@@ -6,21 +6,21 @@ import logging
|
||||
from typing import Any, Final
|
||||
|
||||
from pyvesync.base_devices.vesyncbasedevice import VeSyncBaseDevice
|
||||
from pyvesync.device_container import DeviceContainer
|
||||
|
||||
from homeassistant.components.switch import (
|
||||
SwitchDeviceClass,
|
||||
SwitchEntity,
|
||||
SwitchEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .common import is_outlet, is_wall_switch, rgetattr
|
||||
from .const import DOMAIN, VS_COORDINATOR, VS_DEVICES, VS_DISCOVERY, VS_MANAGER
|
||||
from .coordinator import VeSyncDataCoordinator
|
||||
from .const import VS_DEVICES, VS_DISCOVERY
|
||||
from .coordinator import VesyncConfigEntry, VeSyncDataCoordinator
|
||||
from .entity import VeSyncBaseEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -69,12 +69,12 @@ SENSOR_DESCRIPTIONS: Final[tuple[VeSyncSwitchEntityDescription, ...]] = (
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: VesyncConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up switch platform."""
|
||||
|
||||
coordinator = hass.data[DOMAIN][VS_COORDINATOR]
|
||||
coordinator = config_entry.runtime_data
|
||||
|
||||
@callback
|
||||
def discover(devices: list[VeSyncBaseDevice]) -> None:
|
||||
@@ -86,13 +86,13 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
_setup_entities(
|
||||
hass.data[DOMAIN][VS_MANAGER].devices, async_add_entities, coordinator
|
||||
config_entry.runtime_data.manager.devices, async_add_entities, coordinator
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def _setup_entities(
|
||||
devices: list[VeSyncBaseDevice],
|
||||
devices: DeviceContainer | list[VeSyncBaseDevice],
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
coordinator: VeSyncDataCoordinator,
|
||||
) -> None:
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
"""Update entity for VeSync.."""
|
||||
|
||||
from pyvesync.base_devices.vesyncbasedevice import VeSyncBaseDevice
|
||||
from pyvesync.device_container import DeviceContainer
|
||||
|
||||
from homeassistant.components.update import UpdateDeviceClass, UpdateEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import DOMAIN, VS_COORDINATOR, VS_DEVICES, VS_DISCOVERY, VS_MANAGER
|
||||
from .coordinator import VeSyncDataCoordinator
|
||||
from .const import VS_DEVICES, VS_DISCOVERY
|
||||
from .coordinator import VesyncConfigEntry, VeSyncDataCoordinator
|
||||
from .entity import VeSyncBaseEntity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: VesyncConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up update entity."""
|
||||
coordinator = hass.data[DOMAIN][VS_COORDINATOR]
|
||||
coordinator = config_entry.runtime_data
|
||||
|
||||
@callback
|
||||
def discover(devices: list[VeSyncBaseDevice]) -> None:
|
||||
@@ -31,13 +31,13 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
_setup_entities(
|
||||
hass.data[DOMAIN][VS_MANAGER].devices, async_add_entities, coordinator
|
||||
config_entry.runtime_data.manager.devices, async_add_entities, coordinator
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def _setup_entities(
|
||||
devices: list[VeSyncBaseDevice],
|
||||
devices: DeviceContainer | list[VeSyncBaseDevice],
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
coordinator: VeSyncDataCoordinator,
|
||||
) -> None:
|
||||
|
||||
@@ -14,7 +14,7 @@ from homeassistant.components.vesync import (
|
||||
async_remove_config_entry_device,
|
||||
async_setup_entry,
|
||||
)
|
||||
from homeassistant.components.vesync.const import DOMAIN, VS_MANAGER
|
||||
from homeassistant.components.vesync.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
@@ -72,8 +72,6 @@ async def test_async_setup_entry__no_devices(
|
||||
]
|
||||
|
||||
assert manager.login.call_count == 1
|
||||
assert hass.data[DOMAIN][VS_MANAGER] == manager
|
||||
assert not hass.data[DOMAIN][VS_MANAGER].devices
|
||||
|
||||
|
||||
async def test_async_setup_entry__loads_fans(
|
||||
@@ -100,8 +98,6 @@ async def test_async_setup_entry__loads_fans(
|
||||
Platform.UPDATE,
|
||||
]
|
||||
assert manager.login.call_count == 1
|
||||
assert hass.data[DOMAIN][VS_MANAGER] == manager
|
||||
assert list(hass.data[DOMAIN][VS_MANAGER].devices) == [fan]
|
||||
|
||||
|
||||
async def test_migrate_config_entry(
|
||||
@@ -118,17 +114,17 @@ async def test_migrate_config_entry(
|
||||
suggested_object_id="switch",
|
||||
)
|
||||
|
||||
humidifer: er.RegistryEntry = entity_registry.async_get_or_create(
|
||||
domain="humidifer",
|
||||
humidifier: er.RegistryEntry = entity_registry.async_get_or_create(
|
||||
domain="humidifier",
|
||||
platform="vesync",
|
||||
unique_id="humidifer",
|
||||
unique_id="humidifier",
|
||||
config_entry=switch_old_id_config_entry,
|
||||
suggested_object_id="humidifer",
|
||||
suggested_object_id="humidifier",
|
||||
)
|
||||
|
||||
assert switch.unique_id == "switch"
|
||||
assert switch_old_id_config_entry.minor_version == 1
|
||||
assert humidifer.unique_id == "humidifer"
|
||||
assert humidifier.unique_id == "humidifier"
|
||||
|
||||
await hass.config_entries.async_setup(switch_old_id_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
@@ -139,10 +135,10 @@ async def test_migrate_config_entry(
|
||||
assert migrated_switch is not None
|
||||
assert migrated_switch.entity_id.startswith("switch")
|
||||
assert migrated_switch.unique_id == "switch-device_status"
|
||||
# Confirm humidifer was not impacted
|
||||
migrated_humidifer = entity_registry.async_get(humidifer.entity_id)
|
||||
assert migrated_humidifer is not None
|
||||
assert migrated_humidifer.unique_id == "humidifer"
|
||||
# Confirm humidifier was not impacted
|
||||
migrated_humidifier = entity_registry.async_get(humidifier.entity_id)
|
||||
assert migrated_humidifier is not None
|
||||
assert migrated_humidifier.unique_id == "humidifier"
|
||||
|
||||
# Assert that entity exists in the switch domain
|
||||
switch_entities = [
|
||||
@@ -150,10 +146,10 @@ async def test_migrate_config_entry(
|
||||
]
|
||||
assert len(switch_entities) == 2
|
||||
|
||||
humidifer_entities = [
|
||||
e for e in entity_registry.entities.values() if e.domain == "humidifer"
|
||||
humidifier_entities = [
|
||||
e for e in entity_registry.entities.values() if e.domain == "humidifier"
|
||||
]
|
||||
assert len(humidifer_entities) == 1
|
||||
assert len(humidifier_entities) == 2
|
||||
|
||||
|
||||
async def test_async_remove_config_entry_device_positive(
|
||||
|
||||
@@ -6,11 +6,7 @@ import pytest
|
||||
from pyvesync import VeSync
|
||||
|
||||
from homeassistant.components.vesync import async_setup
|
||||
from homeassistant.components.vesync.const import (
|
||||
DOMAIN,
|
||||
SERVICE_UPDATE_DEVS,
|
||||
VS_MANAGER,
|
||||
)
|
||||
from homeassistant.components.vesync.const import DOMAIN, SERVICE_UPDATE_DEVS
|
||||
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
@@ -61,7 +57,6 @@ async def test_async_new_device_discovery(
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
assert not hass.data[DOMAIN][VS_MANAGER].devices
|
||||
|
||||
# Simulate the manager discovering a new fan when get_devices is called
|
||||
manager.get_devices = AsyncMock(
|
||||
|
||||
Reference in New Issue
Block a user