2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Hydrawise sprinkler binary sensors."""
|
2021-09-27 15:35:09 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-11-15 23:59:37 +00:00
|
|
|
from pydrawise.schema import Zone
|
2018-05-26 16:42:52 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-09-27 15:35:09 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
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
|
2018-05-26 16:42:52 +00:00
|
|
|
from homeassistant.const import CONF_MONITORED_CONDITIONS
|
2023-10-30 14:18:59 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-03-21 05:56:46 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-03 10:32:26 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
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
|
|
|
# Deprecated since Home Assistant 2023.10.0
|
|
|
|
# Can be removed completely in 2024.4.0
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
2021-09-27 15:35:09 +00:00
|
|
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=BINARY_SENSOR_KEYS): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(BINARY_SENSOR_KEYS)]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2018-05-26 16:42:52 +00:00
|
|
|
|
|
|
|
|
2022-01-03 10:32:26 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2018-05-26 16:42:52 +00:00
|
|
|
"""Set up a sensor for a Hydrawise device."""
|
2023-09-23 22:03:07 +00:00
|
|
|
# We don't need to trigger import flow from here as it's triggered from `__init__.py`
|
2023-10-06 12:42:08 +00:00
|
|
|
return # pragma: no cover
|
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
|
|
|
)
|
2023-11-15 23:59:37 +00:00
|
|
|
for zone in controller.zones:
|
|
|
|
for description in BINARY_SENSOR_TYPES:
|
|
|
|
entities.append(
|
|
|
|
HydrawiseBinarySensor(coordinator, description, controller, zone)
|
2023-05-24 12:07:37 +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."""
|
|
|
|
|
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":
|
2023-11-15 23:59:37 +00:00
|
|
|
zone: Zone = self.zone
|
|
|
|
self._attr_is_on = zone.scheduled_runs.current_run is not None
|