From 19d8b8a6ff2e312b1698ce05a9bd543c6cbe2b1a Mon Sep 17 00:00:00 2001 From: Milan Meulemans Date: Wed, 16 Feb 2022 13:06:11 +0100 Subject: [PATCH] Add binary sensor platform to Aseko (#66643) --- .coveragerc | 1 + .../components/aseko_pool_live/__init__.py | 2 +- .../aseko_pool_live/binary_sensor.py | 95 +++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/aseko_pool_live/binary_sensor.py diff --git a/.coveragerc b/.coveragerc index 32cb61a4921..a419628d50a 100644 --- a/.coveragerc +++ b/.coveragerc @@ -77,6 +77,7 @@ omit = homeassistant/components/aruba/device_tracker.py homeassistant/components/arwn/sensor.py homeassistant/components/aseko_pool_live/__init__.py + homeassistant/components/aseko_pool_live/binary_sensor.py homeassistant/components/aseko_pool_live/entity.py homeassistant/components/aseko_pool_live/sensor.py homeassistant/components/asterisk_cdr/mailbox.py diff --git a/homeassistant/components/aseko_pool_live/__init__.py b/homeassistant/components/aseko_pool_live/__init__.py index 697157bd866..213d0dabc91 100644 --- a/homeassistant/components/aseko_pool_live/__init__.py +++ b/homeassistant/components/aseko_pool_live/__init__.py @@ -17,7 +17,7 @@ from .const import DOMAIN _LOGGER = logging.getLogger(__name__) -PLATFORMS: list[str] = [Platform.SENSOR] +PLATFORMS: list[str] = [Platform.BINARY_SENSOR, Platform.SENSOR] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: diff --git a/homeassistant/components/aseko_pool_live/binary_sensor.py b/homeassistant/components/aseko_pool_live/binary_sensor.py new file mode 100644 index 00000000000..f67ea58bfc4 --- /dev/null +++ b/homeassistant/components/aseko_pool_live/binary_sensor.py @@ -0,0 +1,95 @@ +"""Support for Aseko Pool Live binary sensors.""" +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass + +from aioaseko import Unit + +from homeassistant.components.binary_sensor import ( + BinarySensorDeviceClass, + BinarySensorEntity, + BinarySensorEntityDescription, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import AsekoDataUpdateCoordinator +from .const import DOMAIN +from .entity import AsekoEntity + + +@dataclass +class AsekoBinarySensorDescriptionMixin: + """Mixin for required keys.""" + + value_fn: Callable[[Unit], bool] + + +@dataclass +class AsekoBinarySensorEntityDescription( + BinarySensorEntityDescription, AsekoBinarySensorDescriptionMixin +): + """Describes a Aseko binary sensor entity.""" + + +UNIT_BINARY_SENSORS: tuple[AsekoBinarySensorEntityDescription, ...] = ( + AsekoBinarySensorEntityDescription( + key="water_flow", + name="Water Flow", + icon="mdi:waves-arrow-right", + value_fn=lambda unit: unit.water_flow, + ), + AsekoBinarySensorEntityDescription( + key="has_alarm", + name="Alarm", + value_fn=lambda unit: unit.has_alarm, + device_class=BinarySensorDeviceClass.SAFETY, + ), + AsekoBinarySensorEntityDescription( + key="has_error", + name="Error", + value_fn=lambda unit: unit.has_error, + device_class=BinarySensorDeviceClass.PROBLEM, + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up the Aseko Pool Live binary sensors.""" + data: list[tuple[Unit, AsekoDataUpdateCoordinator]] = hass.data[DOMAIN][ + config_entry.entry_id + ] + entities: list[BinarySensorEntity] = [] + for unit, coordinator in data: + for description in UNIT_BINARY_SENSORS: + entities.append(AsekoUnitBinarySensorEntity(unit, coordinator, description)) + async_add_entities(entities) + + +class AsekoUnitBinarySensorEntity(AsekoEntity, BinarySensorEntity): + """Representation of a unit water flow binary sensor entity.""" + + entity_description: AsekoBinarySensorEntityDescription + + def __init__( + self, + unit: Unit, + coordinator: AsekoDataUpdateCoordinator, + entity_description: AsekoBinarySensorEntityDescription, + ) -> None: + """Initialize the unit binary sensor.""" + super().__init__(unit, coordinator) + self.entity_description = entity_description + self._attr_name = f"{self._device_name} {entity_description.name}" + self._attr_unique_id = f"{self._unit.serial_number}_{entity_description.key}" + + @property + def is_on(self) -> bool: + """Return the state of the sensor.""" + return self.entity_description.value_fn(self._unit)