mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Remove side effects from rachio switch init (#34799)
* Remove side effects from rachio switch init * Remove useless inits
This commit is contained in:
parent
a6ee2995bc
commit
f656f352a3
@ -88,13 +88,9 @@ def _create_entities(hass, config_entry):
|
|||||||
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, poll=True):
|
def __init__(self, controller):
|
||||||
"""Initialize a new Rachio switch."""
|
"""Initialize a new Rachio switch."""
|
||||||
super().__init__(controller)
|
super().__init__(controller)
|
||||||
|
|
||||||
if poll:
|
|
||||||
self._state = self._poll_update()
|
|
||||||
else:
|
|
||||||
self._state = None
|
self._state = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -107,10 +103,6 @@ class RachioSwitch(RachioDevice, SwitchEntity):
|
|||||||
"""Return whether the switch is currently on."""
|
"""Return whether the switch is currently on."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def _poll_update(self, data=None) -> bool:
|
|
||||||
"""Poll the API."""
|
|
||||||
|
|
||||||
@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."""
|
||||||
@ -129,11 +121,6 @@ class RachioSwitch(RachioDevice, SwitchEntity):
|
|||||||
class RachioStandbySwitch(RachioSwitch):
|
class RachioStandbySwitch(RachioSwitch):
|
||||||
"""Representation of a standby status/button."""
|
"""Representation of a standby status/button."""
|
||||||
|
|
||||||
def __init__(self, controller):
|
|
||||||
"""Instantiate a new Rachio standby mode switch."""
|
|
||||||
super().__init__(controller, poll=True)
|
|
||||||
self._poll_update(controller.init_data)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Return the name of the standby switch."""
|
"""Return the name of the standby switch."""
|
||||||
@ -149,13 +136,6 @@ class RachioStandbySwitch(RachioSwitch):
|
|||||||
"""Return an icon for the standby switch."""
|
"""Return an icon for the standby switch."""
|
||||||
return "mdi:power"
|
return "mdi:power"
|
||||||
|
|
||||||
def _poll_update(self, data=None) -> bool:
|
|
||||||
"""Request the state from the API."""
|
|
||||||
if data is None:
|
|
||||||
data = self._controller.rachio.device.get(self._controller.controller_id)[1]
|
|
||||||
|
|
||||||
return not data[KEY_ON]
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
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."""
|
||||||
@ -176,6 +156,9 @@ class RachioStandbySwitch(RachioSwitch):
|
|||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Subscribe to updates."""
|
"""Subscribe to updates."""
|
||||||
|
if KEY_ON in self._controller.init_data:
|
||||||
|
self._state = not self._controller.init_data[KEY_ON]
|
||||||
|
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
async_dispatcher_connect(
|
async_dispatcher_connect(
|
||||||
self.hass,
|
self.hass,
|
||||||
@ -188,11 +171,6 @@ class RachioStandbySwitch(RachioSwitch):
|
|||||||
class RachioRainDelay(RachioSwitch):
|
class RachioRainDelay(RachioSwitch):
|
||||||
"""Representation of a rain delay status/switch."""
|
"""Representation of a rain delay status/switch."""
|
||||||
|
|
||||||
def __init__(self, controller):
|
|
||||||
"""Instantiate a new Rachio rain delay switch."""
|
|
||||||
super().__init__(controller, poll=True)
|
|
||||||
self._poll_update(controller.init_data)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Return the name of the switch."""
|
"""Return the name of the switch."""
|
||||||
@ -208,18 +186,6 @@ class RachioRainDelay(RachioSwitch):
|
|||||||
"""Return an icon for rain delay."""
|
"""Return an icon for rain delay."""
|
||||||
return "mdi:camera-timer"
|
return "mdi:camera-timer"
|
||||||
|
|
||||||
def _poll_update(self, data=None) -> bool:
|
|
||||||
"""Request the state from the API."""
|
|
||||||
# API returns either 0 or current UNIX time when rain delay was canceled
|
|
||||||
# depending if it was done from the app or via the API
|
|
||||||
if data is None:
|
|
||||||
data = self._controller.rachio.device.get(self._controller.controller_id)[1]
|
|
||||||
|
|
||||||
try:
|
|
||||||
return data[KEY_RAIN_DELAY] / 1000 > as_timestamp(now())
|
|
||||||
except KeyError:
|
|
||||||
return False
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
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."""
|
||||||
@ -242,6 +208,11 @@ class RachioRainDelay(RachioSwitch):
|
|||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Subscribe to updates."""
|
"""Subscribe to updates."""
|
||||||
|
if KEY_RAIN_DELAY in self._controller.init_data:
|
||||||
|
self._state = self._controller.init_data[
|
||||||
|
KEY_RAIN_DELAY
|
||||||
|
] / 1000 > as_timestamp(now())
|
||||||
|
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
async_dispatcher_connect(
|
async_dispatcher_connect(
|
||||||
self.hass,
|
self.hass,
|
||||||
@ -266,8 +237,7 @@ class RachioZone(RachioSwitch):
|
|||||||
self._zone_type = data.get(KEY_CUSTOM_CROP, {}).get(KEY_NAME)
|
self._zone_type = data.get(KEY_CUSTOM_CROP, {}).get(KEY_NAME)
|
||||||
self._summary = ""
|
self._summary = ""
|
||||||
self._current_schedule = current_schedule
|
self._current_schedule = current_schedule
|
||||||
super().__init__(controller, poll=False)
|
super().__init__(controller)
|
||||||
self._state = self.zone_id == self._current_schedule.get(KEY_ZONE_ID)
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""Display the zone as a string."""
|
"""Display the zone as a string."""
|
||||||
@ -336,11 +306,6 @@ class RachioZone(RachioSwitch):
|
|||||||
"""Stop watering all zones."""
|
"""Stop watering all zones."""
|
||||||
self._controller.stop_watering()
|
self._controller.stop_watering()
|
||||||
|
|
||||||
def _poll_update(self, data=None) -> bool:
|
|
||||||
"""Poll the API to check whether the zone is running."""
|
|
||||||
self._current_schedule = self._controller.current_schedule
|
|
||||||
return self.zone_id == self._current_schedule.get(KEY_ZONE_ID)
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_handle_update(self, *args, **kwargs) -> None:
|
def _async_handle_update(self, *args, **kwargs) -> None:
|
||||||
"""Handle incoming webhook zone data."""
|
"""Handle incoming webhook zone data."""
|
||||||
@ -358,6 +323,8 @@ class RachioZone(RachioSwitch):
|
|||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Subscribe to updates."""
|
"""Subscribe to updates."""
|
||||||
|
self._state = self.zone_id == self._current_schedule.get(KEY_ZONE_ID)
|
||||||
|
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
async_dispatcher_connect(
|
async_dispatcher_connect(
|
||||||
self.hass, SIGNAL_RACHIO_ZONE_UPDATE, self._async_handle_update
|
self.hass, SIGNAL_RACHIO_ZONE_UPDATE, self._async_handle_update
|
||||||
@ -376,8 +343,7 @@ class RachioSchedule(RachioSwitch):
|
|||||||
self._schedule_enabled = data[KEY_ENABLED]
|
self._schedule_enabled = data[KEY_ENABLED]
|
||||||
self._summary = data[KEY_SUMMARY]
|
self._summary = data[KEY_SUMMARY]
|
||||||
self._current_schedule = current_schedule
|
self._current_schedule = current_schedule
|
||||||
super().__init__(controller, poll=False)
|
super().__init__(controller)
|
||||||
self._state = self._schedule_id == self._current_schedule.get(KEY_SCHEDULE_ID)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
@ -420,11 +386,6 @@ class RachioSchedule(RachioSwitch):
|
|||||||
"""Stop watering all zones."""
|
"""Stop watering all zones."""
|
||||||
self._controller.stop_watering()
|
self._controller.stop_watering()
|
||||||
|
|
||||||
def _poll_update(self, data=None) -> bool:
|
|
||||||
"""Poll the API to check whether the schedule is running."""
|
|
||||||
self._current_schedule = self._controller.current_schedule
|
|
||||||
return self._schedule_id == self._current_schedule.get(KEY_SCHEDULE_ID)
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_handle_update(self, *args, **kwargs) -> None:
|
def _async_handle_update(self, *args, **kwargs) -> None:
|
||||||
"""Handle incoming webhook schedule data."""
|
"""Handle incoming webhook schedule data."""
|
||||||
@ -445,6 +406,8 @@ class RachioSchedule(RachioSwitch):
|
|||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Subscribe to updates."""
|
"""Subscribe to updates."""
|
||||||
|
self._state = self._schedule_id == self._current_schedule.get(KEY_SCHEDULE_ID)
|
||||||
|
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
async_dispatcher_connect(
|
async_dispatcher_connect(
|
||||||
self.hass, SIGNAL_RACHIO_SCHEDULE_UPDATE, self._async_handle_update
|
self.hass, SIGNAL_RACHIO_SCHEDULE_UPDATE, self._async_handle_update
|
||||||
|
Loading…
x
Reference in New Issue
Block a user