2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Hydrawise sprinkler binary sensors."""
|
2024-03-08 13:52:48 +00:00
|
|
|
|
2021-09-27 15:35:09 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-05-07 19:26:10 +00:00
|
|
|
from collections.abc import Callable
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2021-09-27 15:35:09 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-14 12:06:55 +00:00
|
|
|
BinarySensorDeviceClass,
|
2021-09-27 15:35:09 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
BinarySensorEntityDescription,
|
|
|
|
)
|
2023-09-23 22:03:07 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-10-30 14:18:59 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-01-03 10:32:26 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2023-10-30 14:18:59 +00:00
|
|
|
from .const import DOMAIN
|
2023-05-24 12:07:37 +00:00
|
|
|
from .coordinator import HydrawiseDataUpdateCoordinator
|
2023-05-22 13:47:32 +00:00
|
|
|
from .entity import HydrawiseEntity
|
2018-05-26 16:42:52 +00:00
|
|
|
|
2024-05-07 19:26:10 +00:00
|
|
|
|
|
|
|
@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,
|
|
|
|
),
|
2021-09-27 15:35:09 +00:00
|
|
|
)
|
|
|
|
|
2024-05-07 19:26:10 +00:00
|
|
|
RAIN_SENSOR_BINARY_SENSOR: tuple[HydrawiseBinarySensorEntityDescription, ...] = (
|
|
|
|
HydrawiseBinarySensorEntityDescription(
|
|
|
|
key="rain_sensor",
|
|
|
|
translation_key="rain_sensor",
|
2021-12-14 12:06:55 +00:00
|
|
|
device_class=BinarySensorDeviceClass.MOISTURE,
|
2024-05-07 19:26:10 +00:00
|
|
|
value_fn=lambda rain_sensor: rain_sensor.sensor.status.active,
|
2021-09-27 15:35:09 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2024-05-07 19:26:10 +00:00
|
|
|
ZONE_BINARY_SENSORS: tuple[HydrawiseBinarySensorEntityDescription, ...] = (
|
|
|
|
HydrawiseBinarySensorEntityDescription(
|
|
|
|
key="is_watering",
|
|
|
|
translation_key="watering",
|
|
|
|
device_class=BinarySensorDeviceClass.RUNNING,
|
2024-05-18 08:26:22 +00:00
|
|
|
value_fn=(
|
|
|
|
lambda watering_sensor: watering_sensor.zone.scheduled_runs.current_run
|
|
|
|
is not None
|
|
|
|
),
|
2024-05-07 19:26:10 +00:00
|
|
|
),
|
|
|
|
)
|
2021-09-27 15:35:09 +00:00
|
|
|
|
2023-09-23 22:03:07 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the Hydrawise binary_sensor platform."""
|
|
|
|
coordinator: HydrawiseDataUpdateCoordinator = hass.data[DOMAIN][
|
|
|
|
config_entry.entry_id
|
|
|
|
]
|
2024-05-07 19:26:10 +00:00
|
|
|
entities: list[HydrawiseBinarySensor] = []
|
2024-01-02 11:46:39 +00:00
|
|
|
for controller in coordinator.data.controllers.values():
|
2024-05-07 19:26:10 +00:00
|
|
|
entities.extend(
|
|
|
|
HydrawiseBinarySensor(coordinator, description, controller)
|
|
|
|
for description in CONTROLLER_BINARY_SENSORS
|
|
|
|
)
|
|
|
|
entities.extend(
|
|
|
|
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()
|
2021-09-27 15:35:09 +00:00
|
|
|
)
|
2024-03-13 16:25:27 +00:00
|
|
|
entities.extend(
|
2024-05-07 19:26:10 +00:00
|
|
|
HydrawiseBinarySensor(coordinator, description, controller, zone_id=zone.id)
|
2024-03-13 16:25:27 +00:00
|
|
|
for zone in controller.zones
|
2024-05-07 19:26:10 +00:00
|
|
|
for description in ZONE_BINARY_SENSORS
|
2024-03-13 16:25:27 +00:00
|
|
|
)
|
2023-09-23 22:03:07 +00:00
|
|
|
async_add_entities(entities)
|
2018-05-26 16:42:52 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class HydrawiseBinarySensor(HydrawiseEntity, BinarySensorEntity):
|
2018-05-26 16:42:52 +00:00
|
|
|
"""A sensor implementation for Hydrawise device."""
|
|
|
|
|
2024-05-07 19:26:10 +00:00
|
|
|
entity_description: HydrawiseBinarySensorEntityDescription
|
|
|
|
|
2023-10-30 14:18:59 +00:00
|
|
|
def _update_attrs(self) -> None:
|
|
|
|
"""Update state attributes."""
|
2024-05-07 19:26:10 +00:00
|
|
|
self._attr_is_on = self.entity_description.value_fn(self)
|