Skip to content

Commit

Permalink
Add reachable and battery low ambisense device binary sensor (#219)
Browse files Browse the repository at this point in the history
* feat: add reachable and low battery binary sensor for ambisense devices
  • Loading branch information
thomasgermain authored Oct 30, 2024
1 parent df4bf38 commit e3c0d5f
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
78 changes: 76 additions & 2 deletions custom_components/mypyllant/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
33 changes: 32 additions & 1 deletion custom_components/mypyllant/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e3c0d5f

Please sign in to comment.