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
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-09-27 15:35:09 +00:00
|
|
|
BINARY_SENSOR_STATUS = BinarySensorEntityDescription(
|
|
|
|
key="status",
|
2021-12-14 12:06:55 +00:00
|
|
|
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
2021-09-27 15:35:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
|
|
|
|
BinarySensorEntityDescription(
|
|
|
|
key="is_watering",
|
2023-09-26 12:34:43 +00:00
|
|
|
translation_key="watering",
|
2021-12-14 12:06:55 +00:00
|
|
|
device_class=BinarySensorDeviceClass.MOISTURE,
|
2021-09-27 15:35:09 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
BINARY_SENSOR_KEYS: list[str] = [
|
|
|
|
desc.key for desc in (BINARY_SENSOR_STATUS, *BINARY_SENSOR_TYPES)
|
|
|
|
]
|
|
|
|
|
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
|
|
|
|
]
|
2023-11-15 23:59:37 +00:00
|
|
|
entities = []
|
2024-01-02 11:46:39 +00:00
|
|
|
for controller in coordinator.data.controllers.values():
|
2023-11-15 23:59:37 +00:00
|
|
|
entities.append(
|
|
|
|
HydrawiseBinarySensor(coordinator, BINARY_SENSOR_STATUS, controller)
|
2021-09-27 15:35:09 +00:00
|
|
|
)
|
2024-03-13 16:25:27 +00:00
|
|
|
entities.extend(
|
|
|
|
HydrawiseBinarySensor(coordinator, description, controller, zone)
|
|
|
|
for zone in controller.zones
|
|
|
|
for description in BINARY_SENSOR_TYPES
|
|
|
|
)
|
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."""
|
|
|
|
|
2023-10-30 14:18:59 +00:00
|
|
|
def _update_attrs(self) -> None:
|
|
|
|
"""Update state attributes."""
|
2021-09-27 15:35:09 +00:00
|
|
|
if self.entity_description.key == "status":
|
2023-09-06 14:51:27 +00:00
|
|
|
self._attr_is_on = self.coordinator.last_update_success
|
2021-09-27 15:35:09 +00:00
|
|
|
elif self.entity_description.key == "is_watering":
|
2024-05-02 05:56:27 +00:00
|
|
|
assert self.zone is not None
|
|
|
|
self._attr_is_on = self.zone.scheduled_runs.current_run is not None
|