Add flow and rain sensor support to Hydrawise (#116303)

* Add flow and rain sensor support to Hydrawise

* Address comments

* Cleanup

* Review comments

* Address review comments

* Added tests

* Add icon translations

* Add snapshot tests

* Clean up binary sensor

* Mypy cleanup

* Another mypy error

* Reviewer feedback

* Clear next_cycle sensor when the value is unknown

* Reviewer feedback

* Reviewer feedback

* Remove assert

* Restructure switches, sensors, and binary sensors

* Reviewer feedback

* Reviewer feedback
This commit is contained in:
Thomas Kistler 2024-05-07 12:26:10 -07:00 committed by GitHub
parent a3248ccff9
commit 14fcf7be8e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 1307 additions and 149 deletions

View file

@ -2,6 +2,9 @@
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
@ -15,22 +18,40 @@ from .const import DOMAIN
from .coordinator import HydrawiseDataUpdateCoordinator
from .entity import HydrawiseEntity
BINARY_SENSOR_STATUS = BinarySensorEntityDescription(
key="status",
device_class=BinarySensorDeviceClass.CONNECTIVITY,
)
BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
BinarySensorEntityDescription(
key="is_watering",
translation_key="watering",
device_class=BinarySensorDeviceClass.MOISTURE,
@dataclass(frozen=True, kw_only=True)
class HydrawiseBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Describes Hydrawise binary sensor."""
value_fn: Callable[[HydrawiseBinarySensor], bool | None]
CONTROLLER_BINARY_SENSORS: tuple[HydrawiseBinarySensorEntityDescription, ...] = (
HydrawiseBinarySensorEntityDescription(
key="status",
device_class=BinarySensorDeviceClass.CONNECTIVITY,
value_fn=lambda status_sensor: status_sensor.coordinator.last_update_success,
),
)
BINARY_SENSOR_KEYS: list[str] = [
desc.key for desc in (BINARY_SENSOR_STATUS, *BINARY_SENSOR_TYPES)
]
RAIN_SENSOR_BINARY_SENSOR: tuple[HydrawiseBinarySensorEntityDescription, ...] = (
HydrawiseBinarySensorEntityDescription(
key="rain_sensor",
translation_key="rain_sensor",
device_class=BinarySensorDeviceClass.MOISTURE,
value_fn=lambda rain_sensor: rain_sensor.sensor.status.active,
),
)
ZONE_BINARY_SENSORS: tuple[HydrawiseBinarySensorEntityDescription, ...] = (
HydrawiseBinarySensorEntityDescription(
key="is_watering",
translation_key="watering",
device_class=BinarySensorDeviceClass.RUNNING,
value_fn=lambda watering_sensor: watering_sensor.zone.scheduled_runs.current_run
is not None,
),
)
async def async_setup_entry(
@ -42,15 +63,27 @@ async def async_setup_entry(
coordinator: HydrawiseDataUpdateCoordinator = hass.data[DOMAIN][
config_entry.entry_id
]
entities = []
entities: list[HydrawiseBinarySensor] = []
for controller in coordinator.data.controllers.values():
entities.append(
HydrawiseBinarySensor(coordinator, BINARY_SENSOR_STATUS, controller)
entities.extend(
HydrawiseBinarySensor(coordinator, description, controller)
for description in CONTROLLER_BINARY_SENSORS
)
entities.extend(
HydrawiseBinarySensor(coordinator, description, controller, zone)
HydrawiseBinarySensor(
coordinator,
description,
controller,
sensor_id=sensor.id,
)
for sensor in controller.sensors
for description in RAIN_SENSOR_BINARY_SENSOR
if "rain sensor" in sensor.model.name.lower()
)
entities.extend(
HydrawiseBinarySensor(coordinator, description, controller, zone_id=zone.id)
for zone in controller.zones
for description in BINARY_SENSOR_TYPES
for description in ZONE_BINARY_SENSORS
)
async_add_entities(entities)
@ -58,10 +91,8 @@ async def async_setup_entry(
class HydrawiseBinarySensor(HydrawiseEntity, BinarySensorEntity):
"""A sensor implementation for Hydrawise device."""
entity_description: HydrawiseBinarySensorEntityDescription
def _update_attrs(self) -> None:
"""Update state attributes."""
if self.entity_description.key == "status":
self._attr_is_on = self.coordinator.last_update_success
elif self.entity_description.key == "is_watering":
assert self.zone is not None
self._attr_is_on = self.zone.scheduled_runs.current_run is not None
self._attr_is_on = self.entity_description.value_fn(self)