Add battery binary sensor to Rachio hose timer (#115810)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Brian Rogers 2024-04-18 14:26:09 -04:00 committed by GitHub
parent d48bd9b016
commit 3a461c32ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 3 deletions

View File

@ -2,6 +2,7 @@
from abc import abstractmethod
import logging
from typing import Any
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
@ -15,16 +16,21 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import (
DOMAIN as DOMAIN_RACHIO,
KEY_BATTERY_STATUS,
KEY_DEVICE_ID,
KEY_LOW,
KEY_RAIN_SENSOR_TRIPPED,
KEY_REPORTED_STATE,
KEY_STATE,
KEY_STATUS,
KEY_SUBTYPE,
SIGNAL_RACHIO_CONTROLLER_UPDATE,
SIGNAL_RACHIO_RAIN_SENSOR_UPDATE,
STATUS_ONLINE,
)
from .coordinator import RachioUpdateCoordinator
from .device import RachioPerson
from .entity import RachioDevice
from .entity import RachioDevice, RachioHoseTimerEntity
from .webhooks import (
SUBTYPE_COLD_REBOOT,
SUBTYPE_OFFLINE,
@ -52,6 +58,11 @@ def _create_entities(hass: HomeAssistant, config_entry: ConfigEntry) -> list[Ent
for controller in person.controllers:
entities.append(RachioControllerOnlineBinarySensor(controller))
entities.append(RachioRainSensor(controller))
entities.extend(
RachioHoseTimerBattery(valve, base_station.coordinator)
for base_station in person.base_stations
for valve in base_station.coordinator.data.values()
)
return entities
@ -140,3 +151,24 @@ class RachioRainSensor(RachioControllerBinarySensor):
self._async_handle_any_update,
)
)
class RachioHoseTimerBattery(RachioHoseTimerEntity, BinarySensorEntity):
"""Represents a battery sensor for a smart hose timer."""
_attr_device_class = BinarySensorDeviceClass.BATTERY
def __init__(
self, data: dict[str, Any], coordinator: RachioUpdateCoordinator
) -> None:
"""Initialize a smart hose timer battery sensor."""
super().__init__(data, coordinator)
self._attr_unique_id = f"{self.id}-battery"
@callback
def _update_attr(self) -> None:
"""Handle updated coordinator data."""
data = self.coordinator.data[self.id]
self._static_attrs = data[KEY_STATE][KEY_REPORTED_STATE]
self._attr_is_on = self._static_attrs[KEY_BATTERY_STATUS] == KEY_LOW

View File

@ -57,6 +57,7 @@ KEY_CONNECTED = "connected"
KEY_CURRENT_STATUS = "lastWateringAction"
KEY_DETECT_FLOW = "detectFlow"
KEY_BATTERY_STATUS = "batteryStatus"
KEY_LOW = "LOW"
KEY_REASON = "reason"
KEY_DEFAULT_RUNTIME = "defaultRuntimeSeconds"
KEY_DURATION_SECONDS = "durationSeconds"

View File

@ -70,6 +70,7 @@ class RachioHoseTimerEntity(CoordinatorEntity[RachioUpdateCoordinator]):
manufacturer=DEFAULT_NAME,
configuration_url="https://app.rach.io",
)
self._update_attr()
@property
def available(self) -> bool:

View File

@ -548,8 +548,6 @@ class RachioValve(RachioHoseTimerEntity, SwitchEntity):
self._person = person
self._base = base
self._attr_unique_id = f"{self.id}-valve"
self._static_attrs = data[KEY_STATE][KEY_REPORTED_STATE]
self._attr_is_on = KEY_CURRENT_STATUS in self._static_attrs
def turn_on(self, **kwargs: Any) -> None:
"""Turn on this valve."""