Add rainbird request debouncer and immediately update entity switch state (#112152)
This commit is contained in:
parent
2c5510df30
commit
ac416f7e07
2 changed files with 17 additions and 0 deletions
|
@ -19,6 +19,7 @@ from pyrainbird.data import ModelAndVersion, Schedule
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.debounce import Debouncer
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
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.
|
# changes, so we refresh it less often.
|
||||||
CALENDAR_UPDATE_INTERVAL = datetime.timedelta(minutes=15)
|
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
|
# Rainbird devices can only accept a single request at a time
|
||||||
CONECTION_LIMIT = 1
|
CONECTION_LIMIT = 1
|
||||||
|
|
||||||
|
@ -71,6 +76,9 @@ class RainbirdUpdateCoordinator(DataUpdateCoordinator[RainbirdDeviceState]):
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
name=name,
|
name=name,
|
||||||
update_interval=UPDATE_INTERVAL,
|
update_interval=UPDATE_INTERVAL,
|
||||||
|
request_refresh_debouncer=Debouncer(
|
||||||
|
hass, _LOGGER, cooldown=DEBOUNCER_COOLDOWN, immediate=False
|
||||||
|
),
|
||||||
)
|
)
|
||||||
self._controller = controller
|
self._controller = controller
|
||||||
self._unique_id = unique_id
|
self._unique_id = unique_id
|
||||||
|
|
|
@ -103,6 +103,10 @@ class RainBirdSwitch(CoordinatorEntity[RainbirdUpdateCoordinator], SwitchEntity)
|
||||||
except RainbirdApiException as err:
|
except RainbirdApiException as err:
|
||||||
raise HomeAssistantError("Rain Bird device failure") from 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()
|
await self.coordinator.async_request_refresh()
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs):
|
async def async_turn_off(self, **kwargs):
|
||||||
|
@ -115,6 +119,11 @@ class RainBirdSwitch(CoordinatorEntity[RainbirdUpdateCoordinator], SwitchEntity)
|
||||||
) from err
|
) from err
|
||||||
except RainbirdApiException as err:
|
except RainbirdApiException as err:
|
||||||
raise HomeAssistantError("Rain Bird device failure") from 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()
|
await self.coordinator.async_request_refresh()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
Loading…
Add table
Reference in a new issue