Skip to content

Commit

Permalink
Clean up binary sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaskistler committed May 1, 2024
1 parent a6b271a commit e0bd1a9
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions homeassistant/components/hydrawise/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from __future__ import annotations

from pydrawise.schema import Sensor, Zone
from typing import Any

from pydrawise import Sensor, Zone

from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
Expand All @@ -19,8 +21,7 @@

CONTROLLER_BINARY_SENSORS: tuple[BinarySensorEntityDescription, ...] = (
BinarySensorEntityDescription(
key="status",
device_class=BinarySensorDeviceClass.CONNECTIVITY,
key="status", device_class=BinarySensorDeviceClass.CONNECTIVITY
),
)

Expand Down Expand Up @@ -80,11 +81,15 @@ class HydrawiseBinarySensor(HydrawiseEntity, BinarySensorEntity):

def _update_attrs(self) -> None:
"""Update state attributes."""
if self.entity_description.key == "status":
self._attr_is_on = self.coordinator.last_update_success
elif self.entity_description.key == "is_watering":
zone: Zone = self.zone
self._attr_is_on = zone.scheduled_runs.current_run is not None
elif self.entity_description.key == "rain_sensor":
sensor: Sensor = self.sensor
self._attr_is_on = sensor.status.active
self._attr_is_on = getattr(self, f"_get_{self.entity_description.key}")()

def _get_status(self) -> bool:
return self.coordinator.last_update_success

def _get_rain_sensor(self) -> Any:
sensor: Sensor = self.sensor
return sensor.status.active

def _get_is_watering(self) -> bool:
zone: Zone = self.zone
return zone.scheduled_runs.current_run is not None

0 comments on commit e0bd1a9

Please sign in to comment.