diff --git a/custom_components/mypyllant/binary_sensor.py b/custom_components/mypyllant/binary_sensor.py index c34d053..3d96355 100644 --- a/custom_components/mypyllant/binary_sensor.py +++ b/custom_components/mypyllant/binary_sensor.py @@ -13,11 +13,16 @@ from homeassistant.helpers.entity import DeviceInfo, EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity -from myPyllant.models import System +from myPyllant.models import System, AmbisenseDevice from . import SystemCoordinator from .const import DOMAIN -from .utils import EntityList, ZoneCoordinatorEntity, CircuitEntity +from .utils import ( + EntityList, + ZoneCoordinatorEntity, + AmbisenseDeviceCoordinatorEntity, + CircuitEntity, +) _LOGGER = logging.getLogger(__name__) @@ -51,6 +56,21 @@ async def async_setup_entry( sensors.append( lambda: ZoneIsManualCoolingActive(index, zone_index, coordinator) ) + if system.ambisense_rooms: + for room_index, room in enumerate(system.ambisense_rooms): + for device in room.room_configuration.devices: + if device.unreach is not None: + sensors.append( + lambda: AmbisenseDeviceLowBattery( + index, room_index, device, coordinator + ) + ) + if device.low_bat is not None: + sensors.append( + lambda: AmbisenseDeviceUnreachable( + index, room_index, device, coordinator + ) + ) async_add_entities(sensors) # type: ignore @@ -268,3 +288,57 @@ def name(self) -> str: @property def unique_id(self) -> str: return f"{DOMAIN} {self.id_infix}_manual_cooling_active" + + +class AmbisenseDeviceLowBattery(AmbisenseDeviceCoordinatorEntity, BinarySensorEntity): + def __init__( + self, + system_index: int, + room_index: int, + device: AmbisenseDevice, + coordinator: SystemCoordinator, + ) -> None: + super().__init__(system_index, room_index, device, coordinator) + + @property + def is_on(self) -> bool | None: + return self.device.low_bat + + @property + def unique_id(self) -> str: + return self.unique_id_fragment + "_low_bat" + + @property + def name(self) -> str: + return f"{self.name_prefix} battery low" + + @property + def device_class(self) -> BinarySensorDeviceClass: + return BinarySensorDeviceClass.BATTERY + + +class AmbisenseDeviceUnreachable(AmbisenseDeviceCoordinatorEntity, BinarySensorEntity): + def __init__( + self, + system_index: int, + room_index: int, + device: AmbisenseDevice, + coordinator: SystemCoordinator, + ) -> None: + super().__init__(system_index, room_index, device, coordinator) + + @property + def is_on(self) -> bool | None: + return not self.device.unreach + + @property + def unique_id(self) -> str: + return self.unique_id_fragment + "_unreach" + + @property + def name(self) -> str: + return f"{self.name_prefix} reachable" + + @property + def device_class(self) -> BinarySensorDeviceClass: + return BinarySensorDeviceClass.CONNECTIVITY diff --git a/custom_components/mypyllant/utils.py b/custom_components/mypyllant/utils.py index d19c519..4fb613c 100644 --- a/custom_components/mypyllant/utils.py +++ b/custom_components/mypyllant/utils.py @@ -21,7 +21,14 @@ if typing.TYPE_CHECKING: from custom_components.mypyllant.coordinator import SystemCoordinator - from myPyllant.models import System, DomesticHotWater, Zone, AmbisenseRoom, Circuit + from myPyllant.models import ( + System, + DomesticHotWater, + Zone, + AmbisenseRoom, + AmbisenseDevice, + Circuit, + ) logger = logging.getLogger(__name__) @@ -350,6 +357,30 @@ def unique_id(self) -> str: return f"{DOMAIN}_{self.id_infix}_climate" +class AmbisenseDeviceCoordinatorEntity(AmbisenseCoordinatorEntity): + def __init__( + self, + system_index: int, + room_index: int, + device: AmbisenseDevice, + coordinator: SystemCoordinator, + ) -> None: + super().__init__(system_index, room_index, coordinator) + self.device = device + + @property + def name_prefix(self) -> str: + return f"{self.system.home.home_name or self.system.home.nomenclature} {self.device.name}" + + @property + def id_infix(self) -> str: + return f"{self.system.id}_room_{self.room_index}_device_{self.device.sgtin}" + + @property + def unique_id_fragment(self) -> str: + return f"{DOMAIN}_{self.id_infix}" + + class CircuitEntity(CoordinatorEntity): def __init__( self,