Use shorthand attributes in Rachio (#99823)
This commit is contained in:
parent
2a3ebbc26c
commit
d9b48b03f7
2 changed files with 32 additions and 74 deletions
|
@ -178,16 +178,6 @@ def _create_entities(hass: HomeAssistant, config_entry: ConfigEntry) -> list[Ent
|
|||
class RachioSwitch(RachioDevice, SwitchEntity):
|
||||
"""Represent a Rachio state that can be toggled."""
|
||||
|
||||
def __init__(self, controller):
|
||||
"""Initialize a new Rachio switch."""
|
||||
super().__init__(controller)
|
||||
self._state = None
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return whether the switch is currently on."""
|
||||
return self._state
|
||||
|
||||
@callback
|
||||
def _async_handle_any_update(self, *args, **kwargs) -> None:
|
||||
"""Determine whether an update event applies to this device."""
|
||||
|
@ -219,9 +209,9 @@ class RachioStandbySwitch(RachioSwitch):
|
|||
def _async_handle_update(self, *args, **kwargs) -> None:
|
||||
"""Update the state using webhook data."""
|
||||
if args[0][0][KEY_SUBTYPE] == SUBTYPE_SLEEP_MODE_ON:
|
||||
self._state = True
|
||||
self._attr_is_on = True
|
||||
elif args[0][0][KEY_SUBTYPE] == SUBTYPE_SLEEP_MODE_OFF:
|
||||
self._state = False
|
||||
self._attr_is_on = False
|
||||
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
@ -236,7 +226,7 @@ class RachioStandbySwitch(RachioSwitch):
|
|||
async def async_added_to_hass(self) -> None:
|
||||
"""Subscribe to updates."""
|
||||
if KEY_ON in self._controller.init_data:
|
||||
self._state = not self._controller.init_data[KEY_ON]
|
||||
self._attr_is_on = not self._controller.init_data[KEY_ON]
|
||||
|
||||
self.async_on_remove(
|
||||
async_dispatcher_connect(
|
||||
|
@ -274,20 +264,20 @@ class RachioRainDelay(RachioSwitch):
|
|||
if args[0][0][KEY_SUBTYPE] == SUBTYPE_RAIN_DELAY_ON:
|
||||
endtime = parse_datetime(args[0][0][KEY_RAIN_DELAY_END])
|
||||
_LOGGER.debug("Rain delay expires at %s", endtime)
|
||||
self._state = True
|
||||
self._attr_is_on = True
|
||||
assert endtime is not None
|
||||
self._cancel_update = async_track_point_in_utc_time(
|
||||
self.hass, self._delay_expiration, endtime
|
||||
)
|
||||
elif args[0][0][KEY_SUBTYPE] == SUBTYPE_RAIN_DELAY_OFF:
|
||||
self._state = False
|
||||
self._attr_is_on = False
|
||||
|
||||
self.async_write_ha_state()
|
||||
|
||||
@callback
|
||||
def _delay_expiration(self, *args) -> None:
|
||||
"""Trigger when a rain delay expires."""
|
||||
self._state = False
|
||||
self._attr_is_on = False
|
||||
self._cancel_update = None
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
@ -304,12 +294,12 @@ class RachioRainDelay(RachioSwitch):
|
|||
async def async_added_to_hass(self) -> None:
|
||||
"""Subscribe to updates."""
|
||||
if KEY_RAIN_DELAY in self._controller.init_data:
|
||||
self._state = self._controller.init_data[
|
||||
self._attr_is_on = self._controller.init_data[
|
||||
KEY_RAIN_DELAY
|
||||
] / 1000 > as_timestamp(now())
|
||||
|
||||
# If the controller was in a rain delay state during a reboot, this re-sets the timer
|
||||
if self._state is True:
|
||||
if self._attr_is_on is True:
|
||||
delay_end = utc_from_timestamp(
|
||||
self._controller.init_data[KEY_RAIN_DELAY] / 1000
|
||||
)
|
||||
|
@ -330,19 +320,22 @@ class RachioRainDelay(RachioSwitch):
|
|||
class RachioZone(RachioSwitch):
|
||||
"""Representation of one zone of sprinklers connected to the Rachio Iro."""
|
||||
|
||||
_attr_icon = "mdi:water"
|
||||
|
||||
def __init__(self, person, controller, data, current_schedule):
|
||||
"""Initialize a new Rachio Zone."""
|
||||
self.id = data[KEY_ID]
|
||||
self._zone_name = data[KEY_NAME]
|
||||
self._attr_name = data[KEY_NAME]
|
||||
self._zone_number = data[KEY_ZONE_NUMBER]
|
||||
self._zone_enabled = data[KEY_ENABLED]
|
||||
self._entity_picture = data.get(KEY_IMAGE_URL)
|
||||
self._attr_entity_picture = data.get(KEY_IMAGE_URL)
|
||||
self._person = person
|
||||
self._shade_type = data.get(KEY_CUSTOM_SHADE, {}).get(KEY_NAME)
|
||||
self._zone_type = data.get(KEY_CUSTOM_CROP, {}).get(KEY_NAME)
|
||||
self._slope_type = data.get(KEY_CUSTOM_SLOPE, {}).get(KEY_NAME)
|
||||
self._summary = ""
|
||||
self._current_schedule = current_schedule
|
||||
self._attr_unique_id = f"{controller.controller_id}-zone-{self.id}"
|
||||
super().__init__(controller)
|
||||
|
||||
def __str__(self):
|
||||
|
@ -354,31 +347,11 @@ class RachioZone(RachioSwitch):
|
|||
"""How the Rachio API refers to the zone."""
|
||||
return self.id
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the friendly name of the zone."""
|
||||
return self._zone_name
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique id by combining controller id and zone number."""
|
||||
return f"{self._controller.controller_id}-zone-{self.zone_id}"
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Return the icon to display."""
|
||||
return "mdi:water"
|
||||
|
||||
@property
|
||||
def zone_is_enabled(self) -> bool:
|
||||
"""Return whether the zone is allowed to run."""
|
||||
return self._zone_enabled
|
||||
|
||||
@property
|
||||
def entity_picture(self):
|
||||
"""Return the entity picture to use in the frontend, if any."""
|
||||
return self._entity_picture
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> dict[str, Any]:
|
||||
"""Return the optional state attributes."""
|
||||
|
@ -424,7 +397,7 @@ class RachioZone(RachioSwitch):
|
|||
|
||||
def set_moisture_percent(self, percent) -> None:
|
||||
"""Set the zone moisture percent."""
|
||||
_LOGGER.debug("Setting %s moisture to %s percent", self._zone_name, percent)
|
||||
_LOGGER.debug("Setting %s moisture to %s percent", self.name, percent)
|
||||
self._controller.rachio.zone.set_moisture_percent(self.id, percent / 100)
|
||||
|
||||
@callback
|
||||
|
@ -436,19 +409,19 @@ class RachioZone(RachioSwitch):
|
|||
self._summary = args[0][KEY_SUMMARY]
|
||||
|
||||
if args[0][KEY_SUBTYPE] == SUBTYPE_ZONE_STARTED:
|
||||
self._state = True
|
||||
self._attr_is_on = True
|
||||
elif args[0][KEY_SUBTYPE] in [
|
||||
SUBTYPE_ZONE_STOPPED,
|
||||
SUBTYPE_ZONE_COMPLETED,
|
||||
SUBTYPE_ZONE_PAUSED,
|
||||
]:
|
||||
self._state = False
|
||||
self._attr_is_on = False
|
||||
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Subscribe to updates."""
|
||||
self._state = self.zone_id == self._current_schedule.get(KEY_ZONE_ID)
|
||||
self._attr_is_on = self.zone_id == self._current_schedule.get(KEY_ZONE_ID)
|
||||
|
||||
self.async_on_remove(
|
||||
async_dispatcher_connect(
|
||||
|
@ -463,24 +436,17 @@ class RachioSchedule(RachioSwitch):
|
|||
def __init__(self, person, controller, data, current_schedule):
|
||||
"""Initialize a new Rachio Schedule."""
|
||||
self._schedule_id = data[KEY_ID]
|
||||
self._schedule_name = data[KEY_NAME]
|
||||
self._duration = data[KEY_DURATION]
|
||||
self._schedule_enabled = data[KEY_ENABLED]
|
||||
self._summary = data[KEY_SUMMARY]
|
||||
self.type = data.get(KEY_TYPE, SCHEDULE_TYPE_FIXED)
|
||||
self._current_schedule = current_schedule
|
||||
self._attr_unique_id = (
|
||||
f"{controller.controller_id}-schedule-{self._schedule_id}"
|
||||
)
|
||||
self._attr_name = f"{data[KEY_NAME]} Schedule"
|
||||
super().__init__(controller)
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the friendly name of the schedule."""
|
||||
return f"{self._schedule_name} Schedule"
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique id by combining controller id and schedule."""
|
||||
return f"{self._controller.controller_id}-schedule-{self._schedule_id}"
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Return the icon to display."""
|
||||
|
@ -521,18 +487,20 @@ class RachioSchedule(RachioSwitch):
|
|||
with suppress(KeyError):
|
||||
if args[0][KEY_SCHEDULE_ID] == self._schedule_id:
|
||||
if args[0][KEY_SUBTYPE] in [SUBTYPE_SCHEDULE_STARTED]:
|
||||
self._state = True
|
||||
self._attr_is_on = True
|
||||
elif args[0][KEY_SUBTYPE] in [
|
||||
SUBTYPE_SCHEDULE_STOPPED,
|
||||
SUBTYPE_SCHEDULE_COMPLETED,
|
||||
]:
|
||||
self._state = False
|
||||
self._attr_is_on = False
|
||||
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Subscribe to updates."""
|
||||
self._state = self._schedule_id == self._current_schedule.get(KEY_SCHEDULE_ID)
|
||||
self._attr_is_on = self._schedule_id == self._current_schedule.get(
|
||||
KEY_SCHEDULE_ID
|
||||
)
|
||||
|
||||
self.async_on_remove(
|
||||
async_dispatcher_connect(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue