From ac416f7e07a0434d9a17abfd9c276ebeecf69bfc Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Mon, 4 Mar 2024 01:05:28 -0800 Subject: [PATCH] Add rainbird request debouncer and immediately update entity switch state (#112152) --- homeassistant/components/rainbird/coordinator.py | 8 ++++++++ homeassistant/components/rainbird/switch.py | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/homeassistant/components/rainbird/coordinator.py b/homeassistant/components/rainbird/coordinator.py index 22aaf2d11a0..70365c2f095 100644 --- a/homeassistant/components/rainbird/coordinator.py +++ b/homeassistant/components/rainbird/coordinator.py @@ -19,6 +19,7 @@ from pyrainbird.data import ModelAndVersion, Schedule from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.helpers.debounce import Debouncer from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed @@ -29,6 +30,10 @@ UPDATE_INTERVAL = datetime.timedelta(minutes=1) # changes, so we refresh it less often. CALENDAR_UPDATE_INTERVAL = datetime.timedelta(minutes=15) +# The valves state are not immediately reflected after issuing a command. We add +# small delay to give additional time to reflect the new state. +DEBOUNCER_COOLDOWN = 5 + # Rainbird devices can only accept a single request at a time CONECTION_LIMIT = 1 @@ -71,6 +76,9 @@ class RainbirdUpdateCoordinator(DataUpdateCoordinator[RainbirdDeviceState]): _LOGGER, name=name, update_interval=UPDATE_INTERVAL, + request_refresh_debouncer=Debouncer( + hass, _LOGGER, cooldown=DEBOUNCER_COOLDOWN, immediate=False + ), ) self._controller = controller self._unique_id = unique_id diff --git a/homeassistant/components/rainbird/switch.py b/homeassistant/components/rainbird/switch.py index da3979a27fd..810a6fbb721 100644 --- a/homeassistant/components/rainbird/switch.py +++ b/homeassistant/components/rainbird/switch.py @@ -103,6 +103,10 @@ class RainBirdSwitch(CoordinatorEntity[RainbirdUpdateCoordinator], SwitchEntity) except RainbirdApiException as err: raise HomeAssistantError("Rain Bird device failure") from err + # The device reflects the old state for a few moments. Update the + # state manually and trigger a refresh after a short debounced delay. + self.coordinator.data.active_zones.add(self._zone) + self.async_write_ha_state() await self.coordinator.async_request_refresh() async def async_turn_off(self, **kwargs): @@ -115,6 +119,11 @@ class RainBirdSwitch(CoordinatorEntity[RainbirdUpdateCoordinator], SwitchEntity) ) from err except RainbirdApiException as err: raise HomeAssistantError("Rain Bird device failure") from err + + # The device reflects the old state for a few moments. Update the + # state manually and trigger a refresh after a short debounced delay. + self.coordinator.data.active_zones.remove(self._zone) + self.async_write_ha_state() await self.coordinator.async_request_refresh() @property