1 """Support for Hydrawise sprinkler binary sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from datetime
import datetime
9 from pydrawise
import Zone
10 import voluptuous
as vol
13 BinarySensorDeviceClass,
15 BinarySensorEntityDescription,
23 from .const
import DOMAIN, SERVICE_RESUME, SERVICE_START_WATERING, SERVICE_SUSPEND
24 from .coordinator
import HydrawiseUpdateCoordinators
25 from .entity
import HydrawiseEntity
28 @dataclass(frozen=True, kw_only=True)
30 """Describes Hydrawise binary sensor."""
32 value_fn: Callable[[HydrawiseBinarySensor], bool |
None]
33 always_available: bool =
False
36 CONTROLLER_BINARY_SENSORS: tuple[HydrawiseBinarySensorEntityDescription, ...] = (
39 device_class=BinarySensorDeviceClass.CONNECTIVITY,
41 lambda status_sensor: status_sensor.coordinator.last_update_success
42 and status_sensor.controller.online
45 always_available=
True,
49 RAIN_SENSOR_BINARY_SENSOR: tuple[HydrawiseBinarySensorEntityDescription, ...] = (
52 translation_key=
"rain_sensor",
53 device_class=BinarySensorDeviceClass.MOISTURE,
54 value_fn=
lambda rain_sensor: rain_sensor.sensor.status.active,
58 ZONE_BINARY_SENSORS: tuple[HydrawiseBinarySensorEntityDescription, ...] = (
61 translation_key=
"watering",
62 device_class=BinarySensorDeviceClass.RUNNING,
64 lambda watering_sensor: watering_sensor.zone.scheduled_runs.current_run
70 SCHEMA_START_WATERING: VolDictType = {
71 vol.Optional(
"duration"): vol.All(vol.Coerce(int), vol.Range(min=0, max=90)),
73 SCHEMA_SUSPEND: VolDictType = {
74 vol.Required(
"until"): cv.datetime,
80 config_entry: ConfigEntry,
81 async_add_entities: AddEntitiesCallback,
83 """Set up the Hydrawise binary_sensor platform."""
84 coordinators: HydrawiseUpdateCoordinators = hass.data[DOMAIN][config_entry.entry_id]
85 entities: list[HydrawiseBinarySensor] = []
86 for controller
in coordinators.main.data.controllers.values():
89 for description
in CONTROLLER_BINARY_SENSORS
98 for sensor
in controller.sensors
99 for description
in RAIN_SENSOR_BINARY_SENSOR
100 if "rain sensor" in sensor.model.name.lower()
104 coordinators.main, description, controller, zone_id=zone.id
106 for zone
in controller.zones
107 for description
in ZONE_BINARY_SENSORS
110 platform = entity_platform.async_get_current_platform()
111 platform.async_register_entity_service(SERVICE_RESUME,
None,
"resume")
112 platform.async_register_entity_service(
113 SERVICE_START_WATERING, SCHEMA_START_WATERING,
"start_watering"
115 platform.async_register_entity_service(SERVICE_SUSPEND, SCHEMA_SUSPEND,
"suspend")
119 """A sensor implementation for Hydrawise device."""
121 entity_description: HydrawiseBinarySensorEntityDescription
124 """Update state attributes."""
129 """Set the entity availability."""
132 return super().available
136 """A binary sensor for a Hydrawise irrigation zone.
138 This is only used for irrigation zones, as they have special methods for
139 service actions that don't apply to other binary sensors.
145 """Start watering in the irrigation zone."""
146 await self.coordinator.api.start_zone(
147 self.
zonezone, custom_run_duration=
int((duration
or 0) * 60)
150 async
def suspend(self, until: datetime) ->
None:
151 """Suspend automatic watering in the irrigation zone."""
152 await self.coordinator.api.suspend_zone(self.
zonezone, until=until)
155 """Resume automatic watering in the irrigation zone."""
156 await self.coordinator.api.resume_zone(self.
zonezone)
None start_watering(self, int|None duration=None)
None suspend(self, datetime until)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)