mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Use shorthand attributes in Rachio (#99823)
This commit is contained in:
parent
2a3ebbc26c
commit
d9b48b03f7
@ -59,16 +59,6 @@ class RachioControllerBinarySensor(RachioDevice, BinarySensorEntity):
|
|||||||
|
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
|
|
||||||
def __init__(self, controller):
|
|
||||||
"""Set up a new Rachio controller binary sensor."""
|
|
||||||
super().__init__(controller)
|
|
||||||
self._state = None
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self) -> bool:
|
|
||||||
"""Return whether the sensor has a 'true' value."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_handle_any_update(self, *args, **kwargs) -> None:
|
def _async_handle_any_update(self, *args, **kwargs) -> None:
|
||||||
"""Determine whether an update event applies to this device."""
|
"""Determine whether an update event applies to this device."""
|
||||||
@ -98,15 +88,15 @@ class RachioControllerOnlineBinarySensor(RachioControllerBinarySensor):
|
|||||||
def _async_handle_update(self, *args, **kwargs) -> None:
|
def _async_handle_update(self, *args, **kwargs) -> None:
|
||||||
"""Handle an update to the state of this sensor."""
|
"""Handle an update to the state of this sensor."""
|
||||||
if args[0][0][KEY_SUBTYPE] in (SUBTYPE_ONLINE, SUBTYPE_COLD_REBOOT):
|
if args[0][0][KEY_SUBTYPE] in (SUBTYPE_ONLINE, SUBTYPE_COLD_REBOOT):
|
||||||
self._state = True
|
self._attr_is_on = True
|
||||||
elif args[0][0][KEY_SUBTYPE] == SUBTYPE_OFFLINE:
|
elif args[0][0][KEY_SUBTYPE] == SUBTYPE_OFFLINE:
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
|
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Subscribe to updates."""
|
"""Subscribe to updates."""
|
||||||
self._state = self._controller.init_data[KEY_STATUS] == STATUS_ONLINE
|
self._attr_is_on = self._controller.init_data[KEY_STATUS] == STATUS_ONLINE
|
||||||
|
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
async_dispatcher_connect(
|
async_dispatcher_connect(
|
||||||
@ -132,15 +122,15 @@ class RachioRainSensor(RachioControllerBinarySensor):
|
|||||||
def _async_handle_update(self, *args, **kwargs) -> None:
|
def _async_handle_update(self, *args, **kwargs) -> None:
|
||||||
"""Handle an update to the state of this sensor."""
|
"""Handle an update to the state of this sensor."""
|
||||||
if args[0][0][KEY_SUBTYPE] == SUBTYPE_RAIN_SENSOR_DETECTION_ON:
|
if args[0][0][KEY_SUBTYPE] == SUBTYPE_RAIN_SENSOR_DETECTION_ON:
|
||||||
self._state = True
|
self._attr_is_on = True
|
||||||
elif args[0][0][KEY_SUBTYPE] == SUBTYPE_RAIN_SENSOR_DETECTION_OFF:
|
elif args[0][0][KEY_SUBTYPE] == SUBTYPE_RAIN_SENSOR_DETECTION_OFF:
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
|
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Subscribe to updates."""
|
"""Subscribe to updates."""
|
||||||
self._state = self._controller.init_data[KEY_RAIN_SENSOR_TRIPPED]
|
self._attr_is_on = self._controller.init_data[KEY_RAIN_SENSOR_TRIPPED]
|
||||||
|
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
async_dispatcher_connect(
|
async_dispatcher_connect(
|
||||||
|
@ -178,16 +178,6 @@ def _create_entities(hass: HomeAssistant, config_entry: ConfigEntry) -> list[Ent
|
|||||||
class RachioSwitch(RachioDevice, SwitchEntity):
|
class RachioSwitch(RachioDevice, SwitchEntity):
|
||||||
"""Represent a Rachio state that can be toggled."""
|
"""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
|
@callback
|
||||||
def _async_handle_any_update(self, *args, **kwargs) -> None:
|
def _async_handle_any_update(self, *args, **kwargs) -> None:
|
||||||
"""Determine whether an update event applies to this device."""
|
"""Determine whether an update event applies to this device."""
|
||||||
@ -219,9 +209,9 @@ class RachioStandbySwitch(RachioSwitch):
|
|||||||
def _async_handle_update(self, *args, **kwargs) -> None:
|
def _async_handle_update(self, *args, **kwargs) -> None:
|
||||||
"""Update the state using webhook data."""
|
"""Update the state using webhook data."""
|
||||||
if args[0][0][KEY_SUBTYPE] == SUBTYPE_SLEEP_MODE_ON:
|
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:
|
elif args[0][0][KEY_SUBTYPE] == SUBTYPE_SLEEP_MODE_OFF:
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
|
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@ -236,7 +226,7 @@ class RachioStandbySwitch(RachioSwitch):
|
|||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Subscribe to updates."""
|
"""Subscribe to updates."""
|
||||||
if KEY_ON in self._controller.init_data:
|
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(
|
self.async_on_remove(
|
||||||
async_dispatcher_connect(
|
async_dispatcher_connect(
|
||||||
@ -274,20 +264,20 @@ class RachioRainDelay(RachioSwitch):
|
|||||||
if args[0][0][KEY_SUBTYPE] == SUBTYPE_RAIN_DELAY_ON:
|
if args[0][0][KEY_SUBTYPE] == SUBTYPE_RAIN_DELAY_ON:
|
||||||
endtime = parse_datetime(args[0][0][KEY_RAIN_DELAY_END])
|
endtime = parse_datetime(args[0][0][KEY_RAIN_DELAY_END])
|
||||||
_LOGGER.debug("Rain delay expires at %s", endtime)
|
_LOGGER.debug("Rain delay expires at %s", endtime)
|
||||||
self._state = True
|
self._attr_is_on = True
|
||||||
assert endtime is not None
|
assert endtime is not None
|
||||||
self._cancel_update = async_track_point_in_utc_time(
|
self._cancel_update = async_track_point_in_utc_time(
|
||||||
self.hass, self._delay_expiration, endtime
|
self.hass, self._delay_expiration, endtime
|
||||||
)
|
)
|
||||||
elif args[0][0][KEY_SUBTYPE] == SUBTYPE_RAIN_DELAY_OFF:
|
elif args[0][0][KEY_SUBTYPE] == SUBTYPE_RAIN_DELAY_OFF:
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
|
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _delay_expiration(self, *args) -> None:
|
def _delay_expiration(self, *args) -> None:
|
||||||
"""Trigger when a rain delay expires."""
|
"""Trigger when a rain delay expires."""
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
self._cancel_update = None
|
self._cancel_update = None
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@ -304,12 +294,12 @@ class RachioRainDelay(RachioSwitch):
|
|||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Subscribe to updates."""
|
"""Subscribe to updates."""
|
||||||
if KEY_RAIN_DELAY in self._controller.init_data:
|
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
|
KEY_RAIN_DELAY
|
||||||
] / 1000 > as_timestamp(now())
|
] / 1000 > as_timestamp(now())
|
||||||
|
|
||||||
# If the controller was in a rain delay state during a reboot, this re-sets the timer
|
# 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(
|
delay_end = utc_from_timestamp(
|
||||||
self._controller.init_data[KEY_RAIN_DELAY] / 1000
|
self._controller.init_data[KEY_RAIN_DELAY] / 1000
|
||||||
)
|
)
|
||||||
@ -330,19 +320,22 @@ class RachioRainDelay(RachioSwitch):
|
|||||||
class RachioZone(RachioSwitch):
|
class RachioZone(RachioSwitch):
|
||||||
"""Representation of one zone of sprinklers connected to the Rachio Iro."""
|
"""Representation of one zone of sprinklers connected to the Rachio Iro."""
|
||||||
|
|
||||||
|
_attr_icon = "mdi:water"
|
||||||
|
|
||||||
def __init__(self, person, controller, data, current_schedule):
|
def __init__(self, person, controller, data, current_schedule):
|
||||||
"""Initialize a new Rachio Zone."""
|
"""Initialize a new Rachio Zone."""
|
||||||
self.id = data[KEY_ID]
|
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_number = data[KEY_ZONE_NUMBER]
|
||||||
self._zone_enabled = data[KEY_ENABLED]
|
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._person = person
|
||||||
self._shade_type = data.get(KEY_CUSTOM_SHADE, {}).get(KEY_NAME)
|
self._shade_type = data.get(KEY_CUSTOM_SHADE, {}).get(KEY_NAME)
|
||||||
self._zone_type = data.get(KEY_CUSTOM_CROP, {}).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._slope_type = data.get(KEY_CUSTOM_SLOPE, {}).get(KEY_NAME)
|
||||||
self._summary = ""
|
self._summary = ""
|
||||||
self._current_schedule = current_schedule
|
self._current_schedule = current_schedule
|
||||||
|
self._attr_unique_id = f"{controller.controller_id}-zone-{self.id}"
|
||||||
super().__init__(controller)
|
super().__init__(controller)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
@ -354,31 +347,11 @@ class RachioZone(RachioSwitch):
|
|||||||
"""How the Rachio API refers to the zone."""
|
"""How the Rachio API refers to the zone."""
|
||||||
return self.id
|
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
|
@property
|
||||||
def zone_is_enabled(self) -> bool:
|
def zone_is_enabled(self) -> bool:
|
||||||
"""Return whether the zone is allowed to run."""
|
"""Return whether the zone is allowed to run."""
|
||||||
return self._zone_enabled
|
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
|
@property
|
||||||
def extra_state_attributes(self) -> dict[str, Any]:
|
def extra_state_attributes(self) -> dict[str, Any]:
|
||||||
"""Return the optional state attributes."""
|
"""Return the optional state attributes."""
|
||||||
@ -424,7 +397,7 @@ class RachioZone(RachioSwitch):
|
|||||||
|
|
||||||
def set_moisture_percent(self, percent) -> None:
|
def set_moisture_percent(self, percent) -> None:
|
||||||
"""Set the zone moisture percent."""
|
"""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)
|
self._controller.rachio.zone.set_moisture_percent(self.id, percent / 100)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@ -436,19 +409,19 @@ class RachioZone(RachioSwitch):
|
|||||||
self._summary = args[0][KEY_SUMMARY]
|
self._summary = args[0][KEY_SUMMARY]
|
||||||
|
|
||||||
if args[0][KEY_SUBTYPE] == SUBTYPE_ZONE_STARTED:
|
if args[0][KEY_SUBTYPE] == SUBTYPE_ZONE_STARTED:
|
||||||
self._state = True
|
self._attr_is_on = True
|
||||||
elif args[0][KEY_SUBTYPE] in [
|
elif args[0][KEY_SUBTYPE] in [
|
||||||
SUBTYPE_ZONE_STOPPED,
|
SUBTYPE_ZONE_STOPPED,
|
||||||
SUBTYPE_ZONE_COMPLETED,
|
SUBTYPE_ZONE_COMPLETED,
|
||||||
SUBTYPE_ZONE_PAUSED,
|
SUBTYPE_ZONE_PAUSED,
|
||||||
]:
|
]:
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
|
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Subscribe to updates."""
|
"""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(
|
self.async_on_remove(
|
||||||
async_dispatcher_connect(
|
async_dispatcher_connect(
|
||||||
@ -463,24 +436,17 @@ class RachioSchedule(RachioSwitch):
|
|||||||
def __init__(self, person, controller, data, current_schedule):
|
def __init__(self, person, controller, data, current_schedule):
|
||||||
"""Initialize a new Rachio Schedule."""
|
"""Initialize a new Rachio Schedule."""
|
||||||
self._schedule_id = data[KEY_ID]
|
self._schedule_id = data[KEY_ID]
|
||||||
self._schedule_name = data[KEY_NAME]
|
|
||||||
self._duration = data[KEY_DURATION]
|
self._duration = data[KEY_DURATION]
|
||||||
self._schedule_enabled = data[KEY_ENABLED]
|
self._schedule_enabled = data[KEY_ENABLED]
|
||||||
self._summary = data[KEY_SUMMARY]
|
self._summary = data[KEY_SUMMARY]
|
||||||
self.type = data.get(KEY_TYPE, SCHEDULE_TYPE_FIXED)
|
self.type = data.get(KEY_TYPE, SCHEDULE_TYPE_FIXED)
|
||||||
self._current_schedule = current_schedule
|
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)
|
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
|
@property
|
||||||
def icon(self) -> str:
|
def icon(self) -> str:
|
||||||
"""Return the icon to display."""
|
"""Return the icon to display."""
|
||||||
@ -521,18 +487,20 @@ class RachioSchedule(RachioSwitch):
|
|||||||
with suppress(KeyError):
|
with suppress(KeyError):
|
||||||
if args[0][KEY_SCHEDULE_ID] == self._schedule_id:
|
if args[0][KEY_SCHEDULE_ID] == self._schedule_id:
|
||||||
if args[0][KEY_SUBTYPE] in [SUBTYPE_SCHEDULE_STARTED]:
|
if args[0][KEY_SUBTYPE] in [SUBTYPE_SCHEDULE_STARTED]:
|
||||||
self._state = True
|
self._attr_is_on = True
|
||||||
elif args[0][KEY_SUBTYPE] in [
|
elif args[0][KEY_SUBTYPE] in [
|
||||||
SUBTYPE_SCHEDULE_STOPPED,
|
SUBTYPE_SCHEDULE_STOPPED,
|
||||||
SUBTYPE_SCHEDULE_COMPLETED,
|
SUBTYPE_SCHEDULE_COMPLETED,
|
||||||
]:
|
]:
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
|
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Subscribe to updates."""
|
"""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(
|
self.async_on_remove(
|
||||||
async_dispatcher_connect(
|
async_dispatcher_connect(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user