mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Add dynamic device/entity creation to lunatone (#186430)
This commit is contained in:
@@ -8,7 +8,7 @@ from lunatone_rest_api_client import Auth, DALIScan, Devices, Info, Sensors
|
||||
from lunatone_rest_api_client.models import DeviceCapabilities
|
||||
|
||||
from homeassistant.const import CONF_URL, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import ConfigEntryError
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
@@ -83,13 +83,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: LunatoneConfigEntry) ->
|
||||
coordinator_info = LunatoneInfoDataUpdateCoordinator(hass, entry, info_api)
|
||||
await coordinator_info.async_config_entry_first_refresh()
|
||||
|
||||
if info_api.data is None:
|
||||
info_data = info_api.data
|
||||
if info_data is None:
|
||||
raise ConfigEntryError(
|
||||
translation_domain=DOMAIN, translation_key="missing_device_info"
|
||||
)
|
||||
|
||||
if info_api.data.uid is not None:
|
||||
new_unique_id = info_api.data.uid.replace("-", "")
|
||||
if info_data.uid is not None:
|
||||
new_unique_id = info_data.uid.replace("-", "")
|
||||
if new_unique_id != entry.unique_id:
|
||||
await _update_unique_id(hass, entry, new_unique_id)
|
||||
coordinator_info.previous_devices = {
|
||||
@@ -97,48 +98,55 @@ async def async_setup_entry(hass: HomeAssistant, entry: LunatoneConfigEntry) ->
|
||||
for line_id in coordinator_info.data.lines
|
||||
}
|
||||
|
||||
assert entry.unique_id
|
||||
entry_unique_id = entry.unique_id
|
||||
assert entry_unique_id is not None
|
||||
|
||||
device_registry = dr.async_get(hass)
|
||||
|
||||
device_registry.async_get_or_create(
|
||||
config_entry_id=entry.entry_id,
|
||||
identifiers={(DOMAIN, entry.unique_id)},
|
||||
name=info_api.data.name,
|
||||
identifiers={(DOMAIN, entry_unique_id)},
|
||||
name=info_data.name,
|
||||
manufacturer=MANUFACTURER,
|
||||
sw_version=info_api.data.version,
|
||||
sw_version=info_data.version,
|
||||
hw_version=coordinator_info.data.device.pcb,
|
||||
configuration_url=entry.data[CONF_URL],
|
||||
serial_number=str(info_api.data.device.serial),
|
||||
model=info_api.data.product_name,
|
||||
serial_number=str(info_data.device.serial),
|
||||
model=info_data.product_name,
|
||||
model_id=(
|
||||
f"{coordinator_info.data.device.article_number}{coordinator_info.data.article_suffix}"
|
||||
),
|
||||
)
|
||||
|
||||
for line_id, line_info in info_api.data.lines.items():
|
||||
line_unique_id = f"{entry.unique_id}-line{line_id}"
|
||||
@callback
|
||||
def _async_register_devices() -> None:
|
||||
info_data = coordinator_info.data
|
||||
for line_id, line_info in info_data.lines.items():
|
||||
line_unique_id = f"{entry_unique_id}-line{line_id}"
|
||||
|
||||
device_info = dr.DeviceInfo(
|
||||
identifiers={(DOMAIN, line_unique_id)},
|
||||
translation_key="dali_line",
|
||||
translation_placeholders={"line_id": str(line_id)},
|
||||
via_device_id=dr.async_get_device_id_by_identifier(
|
||||
hass, (DOMAIN, entry.unique_id), config_entry_id=entry.entry_id
|
||||
),
|
||||
)
|
||||
if line_info.device.serial != info_api.data.device.serial:
|
||||
device_info.serial_number = str(line_info.device.serial)
|
||||
device_info.hw_version = line_info.device.pcb
|
||||
device_info.model_id = (
|
||||
f"{line_info.device.article_number}{line_info.device.article_info}"
|
||||
device_info = dr.DeviceInfo(
|
||||
identifiers={(DOMAIN, line_unique_id)},
|
||||
translation_key="dali_line",
|
||||
translation_placeholders={"line_id": str(line_id)},
|
||||
via_device_id=dr.async_get_device_id_by_identifier(
|
||||
hass, (DOMAIN, entry_unique_id), config_entry_id=entry.entry_id
|
||||
),
|
||||
)
|
||||
if line_info.device.serial != info_data.device.serial:
|
||||
device_info.serial_number = str(line_info.device.serial)
|
||||
device_info.hw_version = line_info.device.pcb
|
||||
device_info.model_id = (
|
||||
f"{line_info.device.article_number}{line_info.device.article_info}"
|
||||
)
|
||||
|
||||
device_registry.async_get_or_create_from_info(
|
||||
config_entry_id=entry.entry_id, device_info=device_info
|
||||
)
|
||||
|
||||
device_registry.async_get_or_create_from_info(
|
||||
config_entry_id=entry.entry_id, device_info=device_info
|
||||
)
|
||||
_async_register_devices()
|
||||
entry.async_on_unload(coordinator_info.async_add_listener(_async_register_devices))
|
||||
|
||||
devices_api = Devices(auth_api, info_api.data.version)
|
||||
devices_api = Devices(auth_api, info_data.version)
|
||||
coordinator_devices = LunatoneDevicesDataUpdateCoordinator(hass, entry, devices_api)
|
||||
await coordinator_devices.async_config_entry_first_refresh()
|
||||
|
||||
@@ -147,7 +155,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: LunatoneConfigEntry) ->
|
||||
await coordinator_scan.async_config_entry_first_refresh()
|
||||
|
||||
coordinator_sensors = None
|
||||
if DeviceCapabilities.SENSORS in info_api.data.device_capabilities:
|
||||
if DeviceCapabilities.SENSORS in info_data.device_capabilities:
|
||||
sensors_api = Sensors(auth_api)
|
||||
coordinator_sensors = LunatoneSensorsDataUpdateCoordinator(
|
||||
hass, entry, sensors_api
|
||||
@@ -159,7 +167,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: LunatoneConfigEntry) ->
|
||||
devices = find_devices(
|
||||
device_registry,
|
||||
entry.entry_id,
|
||||
re.compile(rf"{re.escape(entry.unique_id)}-line\d+-d24-address\d+"),
|
||||
re.compile(rf"{re.escape(entry_unique_id)}-line\d+-d24-address\d+"),
|
||||
)
|
||||
remove_devices(device_registry, entry.entry_id, devices)
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
"""Platform for Lunatone binary sensor integration."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from functools import partial
|
||||
from typing import override
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
@@ -31,23 +33,55 @@ async def async_setup_entry(
|
||||
coordinator_info = config_entry.runtime_data.coordinator_info
|
||||
coordinator_scan = config_entry.runtime_data.coordinator_scan
|
||||
|
||||
assert config_entry.unique_id is not None
|
||||
config_entry_unique_id = config_entry.unique_id
|
||||
assert config_entry_unique_id is not None
|
||||
|
||||
entities: list[BinarySensorEntity] = [
|
||||
LunatoneDALIScanStatus(
|
||||
coordinator_scan, coordinator_info, config_entry.unique_id
|
||||
)
|
||||
]
|
||||
entities.extend(
|
||||
async_add_entities(
|
||||
[
|
||||
LunatoneDALIScanStatus(
|
||||
coordinator_scan, coordinator_info, config_entry.unique_id, int(line_id)
|
||||
coordinator_scan, coordinator_info, config_entry_unique_id
|
||||
)
|
||||
for line_id in coordinator_info.data.lines
|
||||
]
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
def _add_entities(
|
||||
entity_builder: dict[str, Callable[[], BinarySensorEntity]],
|
||||
known_entities: set[str],
|
||||
) -> None:
|
||||
current_entities = entity_builder.keys()
|
||||
new_entities = current_entities - known_entities
|
||||
stale_entities = known_entities - current_entities
|
||||
|
||||
if new_entities:
|
||||
async_add_entities(
|
||||
[
|
||||
entity_builder[entity_unique_id]()
|
||||
for entity_unique_id in new_entities
|
||||
]
|
||||
)
|
||||
known_entities.update(new_entities)
|
||||
known_entities.difference_update(stale_entities)
|
||||
|
||||
known_entities: set[str] = set()
|
||||
|
||||
@callback
|
||||
def _check_entities() -> None:
|
||||
entity_builder: dict[str, Callable[[], BinarySensorEntity]] = {}
|
||||
for line_id in map(int, coordinator_info.data.lines):
|
||||
unique_id = LunatoneDALIScanStatus.construct_unique_id(
|
||||
config_entry_unique_id, line_id
|
||||
)
|
||||
entity_builder[unique_id] = partial(
|
||||
LunatoneDALIScanStatus,
|
||||
coordinator_scan,
|
||||
coordinator_info,
|
||||
config_entry_unique_id,
|
||||
line_id,
|
||||
)
|
||||
_add_entities(entity_builder, known_entities)
|
||||
|
||||
_check_entities()
|
||||
config_entry.async_on_unload(coordinator_info.async_add_listener(_check_entities))
|
||||
|
||||
|
||||
class LunatoneDALIScanStatus(
|
||||
@@ -77,10 +111,31 @@ class LunatoneDALIScanStatus(
|
||||
if line is not None:
|
||||
device_unique_id += f"-line{line}"
|
||||
|
||||
self._attr_unique_id = f"{device_unique_id}-scan-progress"
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, device_unique_id)},
|
||||
identifiers={
|
||||
(DOMAIN, self.construct_device_unique_id(config_entry_unique_id, line))
|
||||
},
|
||||
)
|
||||
self._attr_unique_id = self.construct_unique_id(config_entry_unique_id, line)
|
||||
|
||||
@staticmethod
|
||||
def construct_device_unique_id(
|
||||
config_entry_unique_id: str, line_id: int | None = None
|
||||
) -> str:
|
||||
"""Returns the unique_id of the parent device."""
|
||||
suffix = f"-line{line_id}" if line_id is not None else ""
|
||||
return f"{config_entry_unique_id}{suffix}"
|
||||
|
||||
@staticmethod
|
||||
def construct_unique_id(
|
||||
config_entry_unique_id: str, line_id: int | None = None
|
||||
) -> str:
|
||||
"""Returns the unique_id for a device ID."""
|
||||
return f"{
|
||||
LunatoneDALIScanStatus.construct_device_unique_id(
|
||||
config_entry_unique_id, line_id
|
||||
)
|
||||
}-scan-progress"
|
||||
|
||||
@override
|
||||
async def async_added_to_hass(self) -> None:
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
"""Platform for Lunatone light integration."""
|
||||
|
||||
from collections.abc import Callable
|
||||
import contextlib
|
||||
from functools import partial
|
||||
from typing import Any, override
|
||||
|
||||
import aiohttp
|
||||
@@ -40,26 +42,76 @@ async def async_setup_entry(
|
||||
"""Set up the Lunatone Light platform."""
|
||||
coordinator_info = config_entry.runtime_data.coordinator_info
|
||||
coordinator_devices = config_entry.runtime_data.coordinator_devices
|
||||
auth_api = coordinator_info.info_api.auth
|
||||
|
||||
assert config_entry.unique_id is not None
|
||||
config_entry_unique_id = config_entry.unique_id
|
||||
assert config_entry_unique_id is not None
|
||||
|
||||
entities: list[LightEntity] = [
|
||||
LunatoneLineBroadcastLight(
|
||||
coordinator_devices,
|
||||
coordinator_info,
|
||||
DALIBroadcast(auth_api, int(line_id)),
|
||||
config_entry.unique_id,
|
||||
)
|
||||
for line_id in coordinator_info.data.lines
|
||||
]
|
||||
entities.extend(
|
||||
LunatoneLight(coordinator_devices, line_id, device_id, config_entry.unique_id)
|
||||
for line_id, devices in coordinator_devices.data.items()
|
||||
for device_id in devices
|
||||
def _add_entities(
|
||||
entity_builder: dict[str, Callable[[], LightEntity]], known_entities: set[str]
|
||||
) -> None:
|
||||
current_entities = entity_builder.keys()
|
||||
new_entities = current_entities - known_entities
|
||||
stale_entities = known_entities - current_entities
|
||||
|
||||
if new_entities:
|
||||
async_add_entities(
|
||||
[
|
||||
entity_builder[entity_unique_id]()
|
||||
for entity_unique_id in new_entities
|
||||
]
|
||||
)
|
||||
known_entities.update(new_entities)
|
||||
known_entities.difference_update(stale_entities)
|
||||
|
||||
known_broadcast_entities: set[str] = set()
|
||||
|
||||
@callback
|
||||
def _check_broadcast_entities() -> None:
|
||||
entity_builder: dict[str, Callable[[], LightEntity]] = {}
|
||||
for line_id in map(int, coordinator_info.data.lines):
|
||||
unique_id = LunatoneLineBroadcastLight.construct_unique_id(
|
||||
config_entry_unique_id, line_id
|
||||
)
|
||||
entity_builder[unique_id] = partial(
|
||||
LunatoneLineBroadcastLight,
|
||||
coordinator_devices,
|
||||
coordinator_info,
|
||||
DALIBroadcast(coordinator_info.info_api.auth, line_id),
|
||||
config_entry_unique_id,
|
||||
)
|
||||
|
||||
_add_entities(entity_builder, known_broadcast_entities)
|
||||
|
||||
_check_broadcast_entities()
|
||||
config_entry.async_on_unload(
|
||||
coordinator_info.async_add_listener(_check_broadcast_entities)
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
known_light_entities: set[str] = set()
|
||||
|
||||
@callback
|
||||
def _check_light_entities() -> None:
|
||||
entity_builder: dict[str, Callable[[], LightEntity]] = {}
|
||||
for line_id, devices in coordinator_devices.data.items():
|
||||
for device_id in devices:
|
||||
unique_id = LunatoneLight.construct_unique_id(
|
||||
config_entry_unique_id, device_id
|
||||
)
|
||||
entity_builder[unique_id] = partial(
|
||||
LunatoneLight,
|
||||
coordinator_devices,
|
||||
coordinator_info,
|
||||
line_id,
|
||||
device_id,
|
||||
config_entry_unique_id,
|
||||
)
|
||||
|
||||
_add_entities(entity_builder, known_light_entities)
|
||||
|
||||
_check_light_entities()
|
||||
config_entry.async_on_unload(
|
||||
coordinator_devices.async_add_listener(_check_light_entities)
|
||||
)
|
||||
|
||||
|
||||
class LunatoneLight(
|
||||
@@ -78,18 +130,20 @@ class LunatoneLight(
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: LunatoneDevicesDataUpdateCoordinator,
|
||||
coordinator_devices: LunatoneDevicesDataUpdateCoordinator,
|
||||
coordinator_info: LunatoneInfoDataUpdateCoordinator,
|
||||
line_id: int,
|
||||
device_id: int,
|
||||
config_entry_unique_id: str,
|
||||
) -> None:
|
||||
"""Initialize a Lunatone light."""
|
||||
super().__init__(coordinator)
|
||||
super().__init__(coordinator_devices)
|
||||
self._coordinator_info = coordinator_info
|
||||
self._line_id = line_id
|
||||
self._device_id = device_id
|
||||
self._device = self.coordinator.data[line_id][device_id]
|
||||
|
||||
unique_id = f"{config_entry_unique_id}-device{device_id}"
|
||||
unique_id = self.construct_unique_id(config_entry_unique_id, device_id)
|
||||
|
||||
device_info = dr.DeviceInfo(
|
||||
identifiers={(DOMAIN, unique_id)},
|
||||
@@ -105,6 +159,43 @@ class LunatoneLight(
|
||||
self._attr_device_info = device_info
|
||||
self._attr_unique_id = unique_id
|
||||
|
||||
@staticmethod
|
||||
def construct_unique_id(config_entry_unique_id: str, device_id: int) -> str:
|
||||
"""Returns the unique_id for a device ID."""
|
||||
return f"{config_entry_unique_id}-device{device_id}"
|
||||
|
||||
@override
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Register callbacks."""
|
||||
await super().async_added_to_hass()
|
||||
self.async_on_remove(
|
||||
self._coordinator_info.async_add_listener(self._handle_info_update)
|
||||
)
|
||||
|
||||
@callback
|
||||
def _handle_info_update(self) -> None:
|
||||
config_entry_id = self.coordinator.config_entry.entry_id
|
||||
device_registry = dr.async_get(self.hass)
|
||||
linked_device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{self.coordinator.config_entry.unique_id}-line{self._line_id}"),
|
||||
config_entry_id,
|
||||
)
|
||||
|
||||
if (
|
||||
linked_device
|
||||
and self.registry_entry
|
||||
and self.registry_entry.device_id is not None
|
||||
):
|
||||
device = device_registry.async_get(
|
||||
self.registry_entry.device_id, include_child_devices=False
|
||||
)
|
||||
if device and device.via_device_id != linked_device.id:
|
||||
device_registry.async_update_device(
|
||||
device.id, via_device_id=linked_device.id
|
||||
)
|
||||
|
||||
self.async_write_ha_state()
|
||||
|
||||
@property
|
||||
@override
|
||||
def available(self) -> bool:
|
||||
@@ -264,9 +355,16 @@ class LunatoneLineBroadcastLight(
|
||||
self._coordinator_info = coordinator_info
|
||||
self._broadcast = broadcast
|
||||
|
||||
line_unique_id = f"{config_entry_unique_id}-line{broadcast.line}"
|
||||
self._attr_device_info = dr.DeviceInfo(identifiers={(DOMAIN, line_unique_id)})
|
||||
self._attr_unique_id = line_unique_id
|
||||
assert broadcast.line is not None
|
||||
|
||||
unique_id = self.construct_unique_id(config_entry_unique_id, broadcast.line)
|
||||
self._attr_device_info = dr.DeviceInfo(identifiers={(DOMAIN, unique_id)})
|
||||
self._attr_unique_id = unique_id
|
||||
|
||||
@staticmethod
|
||||
def construct_unique_id(config_entry_unique_id: str, line_id: int) -> str:
|
||||
"""Returns the unique_id for a device ID."""
|
||||
return f"{config_entry_unique_id}-line{line_id}"
|
||||
|
||||
@override
|
||||
async def async_added_to_hass(self) -> None:
|
||||
|
||||
@@ -26,10 +26,7 @@ rules:
|
||||
docs-triggers:
|
||||
status: exempt
|
||||
comment: This integration does not have any triggers.
|
||||
entity-event-setup:
|
||||
status: exempt
|
||||
comment: |
|
||||
Entities of this integration does not explicitly subscribe to events.
|
||||
entity-event-setup: done
|
||||
entity-unique-id: done
|
||||
has-entity-name: done
|
||||
runtime-data: done
|
||||
@@ -65,7 +62,7 @@ rules:
|
||||
docs-supported-functions: done
|
||||
docs-troubleshooting: todo
|
||||
docs-use-cases: todo
|
||||
dynamic-devices: todo
|
||||
dynamic-devices: done
|
||||
entity-category: done
|
||||
entity-device-class: done
|
||||
entity-disabled-by-default: todo
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
"""Platform for Lunatone sensor integration."""
|
||||
|
||||
from collections.abc import Callable
|
||||
import contextlib
|
||||
from functools import partial
|
||||
from typing import Final, override
|
||||
|
||||
from lunatone_rest_api_client import Sensor
|
||||
@@ -19,7 +21,7 @@ from homeassistant.const import (
|
||||
UnitOfRatio,
|
||||
UnitOfTemperature,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
@@ -104,31 +106,76 @@ async def async_setup_entry(
|
||||
coordinator_info = config_entry.runtime_data.coordinator_info
|
||||
coordinator_sensors = config_entry.runtime_data.coordinator_sensors
|
||||
|
||||
assert config_entry.unique_id is not None
|
||||
config_entry_unique_id = config_entry.unique_id
|
||||
assert config_entry_unique_id is not None
|
||||
|
||||
entities: list[SensorEntity] = [
|
||||
LunatoneDALILineStatusSensor(coordinator_info, line_id, config_entry.unique_id)
|
||||
for line_id in coordinator_info.data.lines
|
||||
]
|
||||
def _add_entities(
|
||||
entity_builder: dict[str, Callable[[], SensorEntity]], known_entities: set[str]
|
||||
) -> None:
|
||||
current_entities = entity_builder.keys()
|
||||
new_entities = current_entities - known_entities
|
||||
stale_entities = known_entities - current_entities
|
||||
|
||||
if new_entities:
|
||||
async_add_entities(
|
||||
[
|
||||
entity_builder[entity_unique_id]()
|
||||
for entity_unique_id in new_entities
|
||||
]
|
||||
)
|
||||
known_entities.update(new_entities)
|
||||
known_entities.difference_update(stale_entities)
|
||||
|
||||
known_line_status_entities: set[str] = set()
|
||||
|
||||
@callback
|
||||
def _check_line_status_entities() -> None:
|
||||
entity_builder: dict[str, Callable[[], SensorEntity]] = {}
|
||||
for line_id in map(int, coordinator_info.data.lines):
|
||||
unique_id = LunatoneDALILineStatusSensor.construct_unique_id(
|
||||
config_entry_unique_id, line_id
|
||||
)
|
||||
entity_builder[unique_id] = partial(
|
||||
LunatoneDALILineStatusSensor,
|
||||
coordinator_info,
|
||||
line_id,
|
||||
config_entry_unique_id,
|
||||
)
|
||||
_add_entities(entity_builder, known_line_status_entities)
|
||||
|
||||
_check_line_status_entities()
|
||||
config_entry.async_on_unload(
|
||||
coordinator_info.async_add_listener(_check_line_status_entities)
|
||||
)
|
||||
|
||||
if coordinator_sensors is not None:
|
||||
entities.extend(
|
||||
LunatoneSensor(
|
||||
coordinator_sensors,
|
||||
description,
|
||||
sensor_id,
|
||||
config_entry.unique_id,
|
||||
)
|
||||
for sensor_id, sensor in coordinator_sensors.data.items()
|
||||
if (
|
||||
description := (
|
||||
known_sensor_entities: set[str] = set()
|
||||
|
||||
@callback
|
||||
def _check_sensor_entities() -> None:
|
||||
entity_builder: dict[str, Callable[[], SensorEntity]] = {}
|
||||
for sensor_id, sensor in coordinator_sensors.data.items():
|
||||
if description := (
|
||||
SPECIAL_SENSOR_TYPES.get((sensor.data.name, sensor.data.type))
|
||||
or SENSOR_TYPES.get(sensor.data.type)
|
||||
)
|
||||
)
|
||||
)
|
||||
):
|
||||
unique_id = LunatoneSensor.construct_unique_id(
|
||||
config_entry_unique_id, sensor_id, description.key
|
||||
)
|
||||
entity_builder[unique_id] = partial(
|
||||
LunatoneSensor,
|
||||
coordinator_sensors,
|
||||
coordinator_info,
|
||||
description,
|
||||
sensor_id,
|
||||
config_entry_unique_id,
|
||||
)
|
||||
_add_entities(entity_builder, known_sensor_entities)
|
||||
|
||||
async_add_entities(entities)
|
||||
_check_sensor_entities()
|
||||
config_entry.async_on_unload(
|
||||
coordinator_sensors.async_add_listener(_check_sensor_entities)
|
||||
)
|
||||
|
||||
|
||||
class LunatoneSensor(
|
||||
@@ -140,51 +187,98 @@ class LunatoneSensor(
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: LunatoneSensorsDataUpdateCoordinator,
|
||||
coordinator_sensors: LunatoneSensorsDataUpdateCoordinator,
|
||||
coordinator_info: LunatoneInfoDataUpdateCoordinator,
|
||||
description: SensorEntityDescription,
|
||||
sensor_id: int,
|
||||
config_entry_unique_id: str,
|
||||
) -> None:
|
||||
"""Initialize a Lunatone Sensor."""
|
||||
super().__init__(coordinator)
|
||||
super().__init__(coordinator_sensors)
|
||||
self.entity_description = description
|
||||
|
||||
self._config_entry_unique_id = config_entry_unique_id
|
||||
self._coordinator_info = coordinator_info
|
||||
self._sensor_id = sensor_id
|
||||
self._line_id: int | None = None
|
||||
|
||||
self._attr_unique_id = (
|
||||
f"{self._config_entry_unique_id}-sensor{sensor_id}-{description.key}"
|
||||
self._attr_unique_id = self.construct_unique_id(
|
||||
config_entry_unique_id, sensor_id, description.key
|
||||
)
|
||||
device_info = dr.DeviceInfo(
|
||||
identifiers={(DOMAIN, self._config_entry_unique_id)},
|
||||
identifiers={(DOMAIN, config_entry_unique_id)},
|
||||
)
|
||||
if (
|
||||
self.sensor.data.address_type == SensorAddressType.DALI
|
||||
and self.sensor.data.dali_sensor_address
|
||||
):
|
||||
self._line_id = self.sensor.data.dali_sensor_address.line
|
||||
linked_device_unique_id = f"{config_entry_unique_id}-line{self._line_id}"
|
||||
device_info = dr.DeviceInfo(
|
||||
identifiers={
|
||||
(
|
||||
DOMAIN,
|
||||
f"{self._config_entry_unique_id}"
|
||||
f"-line{self.sensor.data.dali_sensor_address.line}"
|
||||
f"-d24-address{self.sensor.data.dali_sensor_address.address}",
|
||||
(
|
||||
f"{linked_device_unique_id}"
|
||||
f"-d24-address{self.sensor.data.dali_sensor_address.address}"
|
||||
),
|
||||
)
|
||||
},
|
||||
translation_key="dali_sensor",
|
||||
translation_placeholders={
|
||||
"line_id": str(self.sensor.data.dali_sensor_address.line),
|
||||
"line_id": str(self._line_id),
|
||||
"dali_address": str(self.sensor.data.dali_sensor_address.address),
|
||||
},
|
||||
)
|
||||
with contextlib.suppress(ValueError):
|
||||
device_info.via_device_id = dr.async_get_device_id_by_identifier(
|
||||
self.coordinator.hass,
|
||||
(DOMAIN, str(self._config_entry_unique_id)),
|
||||
(DOMAIN, f"{linked_device_unique_id}"),
|
||||
config_entry_id=self.coordinator.config_entry.entry_id,
|
||||
)
|
||||
self._attr_device_info = device_info
|
||||
|
||||
@staticmethod
|
||||
def construct_unique_id(
|
||||
config_entry_unique_id: str, sensor_id: int, key: str
|
||||
) -> str:
|
||||
"""Returns the unique_id for a device ID."""
|
||||
return f"{config_entry_unique_id}-sensor{sensor_id}-{key}"
|
||||
|
||||
@override
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Register callbacks."""
|
||||
await super().async_added_to_hass()
|
||||
if self._line_id is not None:
|
||||
self.async_on_remove(
|
||||
self._coordinator_info.async_add_listener(self._handle_info_update)
|
||||
)
|
||||
|
||||
@callback
|
||||
def _handle_info_update(self) -> None:
|
||||
assert self._line_id is not None
|
||||
|
||||
config_entry_id = self.coordinator.config_entry.entry_id
|
||||
device_registry = dr.async_get(self.hass)
|
||||
linked_device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{self.coordinator.config_entry.unique_id}-line{self._line_id}"),
|
||||
config_entry_id,
|
||||
)
|
||||
|
||||
if (
|
||||
linked_device
|
||||
and self.registry_entry
|
||||
and self.registry_entry.device_id is not None
|
||||
):
|
||||
device = device_registry.async_get(
|
||||
self.registry_entry.device_id, include_child_devices=False
|
||||
)
|
||||
if device and device.via_device_id != linked_device.id:
|
||||
device_registry.async_update_device(
|
||||
device.id, via_device_id=linked_device.id
|
||||
)
|
||||
|
||||
self.async_write_ha_state()
|
||||
|
||||
@property
|
||||
def sensor(self) -> Sensor:
|
||||
"""Return the sensor data."""
|
||||
@@ -218,7 +312,7 @@ class LunatoneDALILineStatusSensor(
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: LunatoneInfoDataUpdateCoordinator,
|
||||
line_id: str,
|
||||
line_id: int,
|
||||
config_entry_unique_id: str,
|
||||
) -> None:
|
||||
"""Initialize a Lunatone DALI line status sensor."""
|
||||
@@ -227,25 +321,43 @@ class LunatoneDALILineStatusSensor(
|
||||
self._config_entry_unique_id = config_entry_unique_id
|
||||
self._line_id = line_id
|
||||
|
||||
line_unique_id = f"{config_entry_unique_id}-line{line_id}"
|
||||
# Name must match the light platform, either of them may create the device
|
||||
self._attr_device_info = dr.DeviceInfo(
|
||||
identifiers={(DOMAIN, line_unique_id)},
|
||||
identifiers={
|
||||
(
|
||||
DOMAIN,
|
||||
self.construct_device_unique_id(config_entry_unique_id, line_id),
|
||||
)
|
||||
},
|
||||
translation_key="dali_line",
|
||||
translation_placeholders={"line_id": str(line_id)},
|
||||
)
|
||||
self._attr_unique_id = f"{line_unique_id}-status"
|
||||
self._attr_unique_id = self.construct_unique_id(config_entry_unique_id, line_id)
|
||||
|
||||
@staticmethod
|
||||
def construct_device_unique_id(
|
||||
config_entry_unique_id: str, line_id: int | None = None
|
||||
) -> str:
|
||||
"""Returns the unique_id of the parent device."""
|
||||
return f"{config_entry_unique_id}-line{line_id}"
|
||||
|
||||
@staticmethod
|
||||
def construct_unique_id(
|
||||
config_entry_unique_id: str, line_id: int | None = None
|
||||
) -> str:
|
||||
"""Returns the unique_id for a device ID."""
|
||||
return f"{config_entry_unique_id}-line{line_id}-status"
|
||||
|
||||
@property
|
||||
@override
|
||||
def available(self) -> bool:
|
||||
return super().available and self._line_id in self.coordinator.data.lines
|
||||
return super().available and str(self._line_id) in self.coordinator.data.lines
|
||||
|
||||
@property
|
||||
@override
|
||||
def native_value(self) -> str:
|
||||
"""Return the value of the sensor."""
|
||||
line_data = self.coordinator.data.lines.get(self._line_id)
|
||||
line_data = self.coordinator.data.lines.get(str(self._line_id))
|
||||
return (
|
||||
DALI_LINE_STATUS_SENSOR_MAPPING[line_data.line_status]
|
||||
if line_data is not None
|
||||
|
||||
@@ -51,6 +51,7 @@ async def test_sensor_value_update(
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test the Lunatone DALI scan status value update."""
|
||||
fast_update_interval = timedelta(seconds=1)
|
||||
scan_states = iter(
|
||||
(
|
||||
(ScanState.ADDRESSING, ScanLineState.ADDRESSING),
|
||||
@@ -62,7 +63,13 @@ async def test_sensor_value_update(
|
||||
|
||||
coordinator = mock_config_entry.runtime_data.coordinator_scan
|
||||
|
||||
async def fake_update():
|
||||
def get_entity_states() -> dict[str, str]:
|
||||
return {
|
||||
entity.entity_id: entity.state
|
||||
for entity in hass.states.async_all(Platform.BINARY_SENSOR)
|
||||
}
|
||||
|
||||
async def fake_update() -> None:
|
||||
scan_state, line_scan_state = next(scan_states)
|
||||
scan_data = deepcopy(mock_lunatone_scan.data)
|
||||
scan_data.status = scan_state
|
||||
@@ -71,27 +78,27 @@ async def test_sensor_value_update(
|
||||
|
||||
mock_lunatone_scan.async_update.side_effect = fake_update
|
||||
|
||||
entities = hass.states.async_all(Platform.BINARY_SENSOR)
|
||||
assert all(entity.state == STATE_OFF for entity in entities)
|
||||
assert coordinator.update_interval == timedelta(seconds=10)
|
||||
entity_states = get_entity_states()
|
||||
assert all(state == STATE_OFF for state in entity_states.values())
|
||||
assert coordinator.update_interval == DEFAULT_SCAN_UPDATE_INTERVAL
|
||||
|
||||
freezer.tick(DEFAULT_SCAN_UPDATE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
entities = hass.states.async_all(Platform.BINARY_SENSOR)
|
||||
assert entities[0].state == STATE_ON
|
||||
assert entities[1].state == STATE_ON
|
||||
assert entities[2].state == STATE_OFF
|
||||
assert coordinator.update_interval == timedelta(seconds=1)
|
||||
entity_states = get_entity_states()
|
||||
assert entity_states["binary_sensor.test_scan_status"] == STATE_ON
|
||||
assert entity_states["binary_sensor.dali_line_0_scan_status"] == STATE_ON
|
||||
assert entity_states["binary_sensor.dali_line_1_scan_status"] == STATE_OFF
|
||||
assert coordinator.update_interval == fast_update_interval
|
||||
|
||||
freezer.tick(timedelta(seconds=1))
|
||||
freezer.tick(fast_update_interval)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
entities = hass.states.async_all(Platform.BINARY_SENSOR)
|
||||
assert all(entity.state == STATE_OFF for entity in entities)
|
||||
assert coordinator.update_interval == timedelta(seconds=10)
|
||||
entity_states = get_entity_states()
|
||||
assert all(state == STATE_OFF for state in entity_states.values())
|
||||
assert coordinator.update_interval == DEFAULT_SCAN_UPDATE_INTERVAL
|
||||
|
||||
|
||||
async def test_line_scan_status_removed_with_line(
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""Tests for the Lunatone integration."""
|
||||
|
||||
from copy import deepcopy
|
||||
from unittest.mock import AsyncMock, PropertyMock
|
||||
|
||||
import aiohttp
|
||||
@@ -30,6 +31,47 @@ from . import (
|
||||
)
|
||||
|
||||
|
||||
async def test_line_added_after_info_update(
|
||||
hass: HomeAssistant,
|
||||
mock_lunatone_info: AsyncMock,
|
||||
mock_lunatone_devices: AsyncMock,
|
||||
mock_lunatone_sensors: AsyncMock,
|
||||
mock_lunatone_scan: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test an added line creates its device and entities."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
async def fake_update() -> None:
|
||||
info_data = deepcopy(mock_lunatone_info.data)
|
||||
info_data.lines["2"] = deepcopy(info_data.lines["1"])
|
||||
mock_lunatone_info.data = info_data
|
||||
|
||||
mock_lunatone_info.async_update.side_effect = fake_update
|
||||
|
||||
freezer.tick(DEFAULT_INFO_UPDATE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
line_device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{mock_config_entry.unique_id}-line2"), mock_config_entry.entry_id
|
||||
)
|
||||
assert line_device is not None
|
||||
|
||||
assert mock_config_entry.unique_id
|
||||
main_device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, mock_config_entry.unique_id), mock_config_entry.entry_id
|
||||
)
|
||||
assert main_device is not None
|
||||
assert line_device.via_device_id == main_device.id
|
||||
|
||||
assert hass.states.get("light.dali_line_2")
|
||||
assert hass.states.get("sensor.dali_line_2_status")
|
||||
assert hass.states.get("binary_sensor.dali_line_2_scan_status")
|
||||
|
||||
|
||||
async def test_load_unload_config_entry(
|
||||
hass: HomeAssistant,
|
||||
mock_lunatone_info: AsyncMock,
|
||||
|
||||
@@ -5,7 +5,8 @@ from unittest.mock import AsyncMock
|
||||
|
||||
import aiohttp
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
from lunatone_rest_api_client.models import LineStatus
|
||||
from lunatone_rest_api_client.models import DeviceData, FeaturesStatus, LineStatus
|
||||
from lunatone_rest_api_client.models.common import Status
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry, async_fire_time_changed
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
@@ -17,7 +18,11 @@ from homeassistant.components.light import (
|
||||
ATTR_RGBW_COLOR,
|
||||
DOMAIN as LIGHT_DOMAIN,
|
||||
)
|
||||
from homeassistant.components.lunatone.coordinator import DEFAULT_INFO_UPDATE_INTERVAL
|
||||
from homeassistant.components.lunatone.const import DOMAIN
|
||||
from homeassistant.components.lunatone.coordinator import (
|
||||
DEFAULT_DEVICES_UPDATE_INTERVAL,
|
||||
DEFAULT_INFO_UPDATE_INTERVAL,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
SERVICE_TURN_OFF,
|
||||
@@ -56,6 +61,126 @@ async def test_setup(
|
||||
assert entity_state == snapshot(name=f"{entity_entry.entity_id}-state")
|
||||
|
||||
|
||||
async def test_device_added_after_setup(
|
||||
hass: HomeAssistant,
|
||||
mock_lunatone_info: AsyncMock,
|
||||
mock_lunatone_devices: AsyncMock,
|
||||
mock_lunatone_sensors: AsyncMock,
|
||||
mock_lunatone_scan: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test a device added after setup creates an entity."""
|
||||
entity_id = "light.device_1"
|
||||
device = mock_lunatone_devices.data.devices.pop(0)
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
entity = hass.states.get(entity_id)
|
||||
assert entity is None
|
||||
|
||||
mock_lunatone_devices.data.devices.append(device)
|
||||
|
||||
freezer.tick(DEFAULT_DEVICES_UPDATE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
entity = hass.states.get(entity_id)
|
||||
assert entity
|
||||
|
||||
|
||||
async def test_device_removed_after_setup(
|
||||
hass: HomeAssistant,
|
||||
mock_lunatone_info: AsyncMock,
|
||||
mock_lunatone_devices: AsyncMock,
|
||||
mock_lunatone_sensors: AsyncMock,
|
||||
mock_lunatone_scan: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test that removing a device after setup removes its entity."""
|
||||
entity_id = "light.device_6"
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert hass.states.get(entity_id)
|
||||
assert entity_registry.async_get(entity_id)
|
||||
|
||||
mock_lunatone_devices.data.devices.pop()
|
||||
|
||||
freezer.tick(DEFAULT_DEVICES_UPDATE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
assert hass.states.get(entity_id) is None
|
||||
assert entity_registry.async_get(entity_id) is None
|
||||
|
||||
|
||||
async def test_light_added_before_line_device(
|
||||
hass: HomeAssistant,
|
||||
mock_lunatone_info: AsyncMock,
|
||||
mock_lunatone_devices: AsyncMock,
|
||||
mock_lunatone_sensors: AsyncMock,
|
||||
mock_lunatone_scan: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test a light is added before its line device and linked later."""
|
||||
entity_id = "light.device_7"
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
new_device_data = DeviceData(
|
||||
id=7,
|
||||
name="Device 7",
|
||||
line=2,
|
||||
address=0,
|
||||
features=FeaturesStatus(switchable=Status[bool](status=False)),
|
||||
)
|
||||
mock_lunatone_devices.data.devices.append(new_device_data)
|
||||
|
||||
freezer.tick(DEFAULT_DEVICES_UPDATE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
state = hass.states.get(entity_id)
|
||||
assert state
|
||||
|
||||
light_device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{mock_config_entry.unique_id}-device7"),
|
||||
mock_config_entry.entry_id,
|
||||
)
|
||||
assert light_device
|
||||
assert light_device.via_device_id is None
|
||||
|
||||
info_data = deepcopy(mock_lunatone_info.data)
|
||||
info_data.lines["2"] = deepcopy(info_data.lines["1"])
|
||||
mock_lunatone_info.data = info_data
|
||||
|
||||
freezer.tick(DEFAULT_INFO_UPDATE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
state = hass.states.get(entity_id)
|
||||
assert state
|
||||
|
||||
assert mock_config_entry.unique_id
|
||||
|
||||
line_device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{mock_config_entry.unique_id}-line2"),
|
||||
mock_config_entry.entry_id,
|
||||
)
|
||||
assert line_device
|
||||
|
||||
light_device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{mock_config_entry.unique_id}-device7"), mock_config_entry.entry_id
|
||||
)
|
||||
assert light_device
|
||||
assert light_device.via_device_id == line_device.id
|
||||
|
||||
|
||||
async def test_turn_on_off(
|
||||
hass: HomeAssistant,
|
||||
mock_lunatone_info: AsyncMock,
|
||||
|
||||
@@ -1,18 +1,28 @@
|
||||
"""Tests for the lights provided by the Lunatone integration."""
|
||||
|
||||
from copy import deepcopy
|
||||
from datetime import timedelta
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
from lunatone_rest_api_client.models import LineStatus
|
||||
from lunatone_rest_api_client.models import (
|
||||
LineStatus,
|
||||
SensorAddressType,
|
||||
SensorDaliAddress,
|
||||
SensorData,
|
||||
SensorType,
|
||||
)
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry, async_fire_time_changed
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.lunatone.const import DOMAIN
|
||||
from homeassistant.components.lunatone.coordinator import (
|
||||
DEFAULT_INFO_UPDATE_INTERVAL,
|
||||
DEFAULT_SENSORS_UPDATE_INTERVAL,
|
||||
)
|
||||
from homeassistant.const import STATE_UNKNOWN, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
@@ -74,7 +84,7 @@ async def test_sensor_value_update(
|
||||
assert entity
|
||||
assert entity.state == STATE_UNKNOWN
|
||||
|
||||
freezer.tick(timedelta(seconds=40))
|
||||
freezer.tick(DEFAULT_SENSORS_UPDATE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -83,6 +93,158 @@ async def test_sensor_value_update(
|
||||
assert entity.state == expected_state
|
||||
|
||||
|
||||
async def test_sensor_added_after_update(
|
||||
hass: HomeAssistant,
|
||||
mock_lunatone_info: AsyncMock,
|
||||
mock_lunatone_devices: AsyncMock,
|
||||
mock_lunatone_sensors: AsyncMock,
|
||||
mock_lunatone_scan: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test a DALI sensor added after setup creates its entity and device."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
sensor_data = SensorData(
|
||||
id=5,
|
||||
name="Test Sensor 5",
|
||||
type=SensorType.TEMPERATURE,
|
||||
addressType=SensorAddressType.DALI,
|
||||
daliSensorAddress=SensorDaliAddress(line=1, address=3, instanceNumber=0),
|
||||
)
|
||||
mock_lunatone_sensors.data.sensors.append(sensor_data)
|
||||
|
||||
freezer.tick(DEFAULT_SENSORS_UPDATE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
entity = entity_registry.async_get("sensor.dali_line_1_a32_temperature")
|
||||
assert entity is not None
|
||||
|
||||
sensor_device = device_registry.async_get_device_by_identifier(
|
||||
(
|
||||
DOMAIN,
|
||||
f"{mock_config_entry.unique_id}-line1-d24-address3",
|
||||
),
|
||||
mock_config_entry.entry_id,
|
||||
)
|
||||
assert sensor_device is not None
|
||||
assert entity.device_id == sensor_device.id
|
||||
|
||||
|
||||
async def test_sensor_removed_after_setup(
|
||||
hass: HomeAssistant,
|
||||
mock_lunatone_info: AsyncMock,
|
||||
mock_lunatone_devices: AsyncMock,
|
||||
mock_lunatone_sensors: AsyncMock,
|
||||
mock_lunatone_scan: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test a sensor removed after setup removes its entity."""
|
||||
entity_id = "sensor.dali_line_0_a02_temperature"
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
state = hass.states.get(entity_id)
|
||||
assert state
|
||||
|
||||
entity_entry = entity_registry.async_get(entity_id)
|
||||
assert entity_entry
|
||||
|
||||
mock_lunatone_sensors.data.sensors = [
|
||||
sensor_data
|
||||
for sensor_data in mock_lunatone_sensors.data.sensors
|
||||
if sensor_data.dali_sensor_address is None
|
||||
]
|
||||
|
||||
freezer.tick(DEFAULT_SENSORS_UPDATE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
state = hass.states.get(entity_id)
|
||||
assert state is None
|
||||
|
||||
entity_entry = entity_registry.async_get(entity_id)
|
||||
assert entity_entry is None
|
||||
|
||||
|
||||
async def test_sensor_added_before_line_device(
|
||||
hass: HomeAssistant,
|
||||
mock_lunatone_info: AsyncMock,
|
||||
mock_lunatone_devices: AsyncMock,
|
||||
mock_lunatone_sensors: AsyncMock,
|
||||
mock_lunatone_scan: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test a sensor is added before its line device and linked later."""
|
||||
entity_id = "sensor.dali_line_2_a02_illuminance"
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
new_sensor_data = SensorData(
|
||||
id=5,
|
||||
name="Test Sensor 5",
|
||||
type=SensorType.LIGHT,
|
||||
addressType=SensorAddressType.DALI,
|
||||
daliSensorAddress=SensorDaliAddress(line=2, address=0, instanceNumber=0),
|
||||
)
|
||||
mock_lunatone_sensors.data.sensors.append(new_sensor_data)
|
||||
|
||||
freezer.tick(DEFAULT_SENSORS_UPDATE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
state = hass.states.get(entity_id)
|
||||
assert state
|
||||
|
||||
sensor_device = device_registry.async_get_device_by_identifier(
|
||||
(
|
||||
DOMAIN,
|
||||
(
|
||||
f"{mock_config_entry.unique_id}"
|
||||
f"-line{new_sensor_data.dali_sensor_address.line}"
|
||||
f"-d24-address{new_sensor_data.dali_sensor_address.address}"
|
||||
),
|
||||
),
|
||||
mock_config_entry.entry_id,
|
||||
)
|
||||
assert sensor_device
|
||||
assert sensor_device.via_device_id is None
|
||||
|
||||
info_data = deepcopy(mock_lunatone_info.data)
|
||||
info_data.lines["2"] = deepcopy(info_data.lines["1"])
|
||||
mock_lunatone_info.data = info_data
|
||||
|
||||
freezer.tick(DEFAULT_INFO_UPDATE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
state = hass.states.get(entity_id)
|
||||
assert state
|
||||
|
||||
assert mock_config_entry.unique_id
|
||||
|
||||
line_device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{mock_config_entry.unique_id}-line2"),
|
||||
mock_config_entry.entry_id,
|
||||
)
|
||||
assert line_device
|
||||
|
||||
sensor_device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{mock_config_entry.unique_id}-line2-d24-address0"),
|
||||
mock_config_entry.entry_id,
|
||||
)
|
||||
assert sensor_device
|
||||
assert sensor_device.via_device_id == line_device.id
|
||||
|
||||
|
||||
async def test_dali_line_status_value_update(
|
||||
hass: HomeAssistant,
|
||||
mock_lunatone_info: AsyncMock,
|
||||
@@ -90,17 +252,17 @@ async def test_dali_line_status_value_update(
|
||||
mock_lunatone_sensors: AsyncMock,
|
||||
mock_lunatone_scan: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test the Lunatone sensor value update."""
|
||||
line_id = 0
|
||||
line_id = "0"
|
||||
entity_id = f"sensor.dali_line_{line_id}_status"
|
||||
|
||||
line_statuses = iter((LineStatus.NO_POWER, LineStatus.OK))
|
||||
line_status = iter((LineStatus.NO_POWER, LineStatus.OK))
|
||||
|
||||
async def fake_update() -> None:
|
||||
info_data = deepcopy(mock_lunatone_info.data)
|
||||
info_data.lines[str(line_id)].line_status = next(line_statuses)
|
||||
info_data.lines[str(line_id)].line_status = next(line_status)
|
||||
mock_lunatone_info.data = info_data
|
||||
|
||||
mock_lunatone_info.async_update.side_effect = fake_update
|
||||
@@ -111,7 +273,7 @@ async def test_dali_line_status_value_update(
|
||||
assert entity
|
||||
assert entity.state == "no_power"
|
||||
|
||||
freezer.tick(timedelta(seconds=60))
|
||||
freezer.tick(DEFAULT_INFO_UPDATE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user