From 0ab66a4ed171c751f4ae7623ac98e10eb6b906ad Mon Sep 17 00:00:00 2001 From: Mick Vleeshouwer Date: Tue, 7 Jan 2025 19:06:57 +0100 Subject: [PATCH] Improve logic for event polling duration in Overkiz (#133617) --- homeassistant/components/overkiz/__init__.py | 15 +++++++---- homeassistant/components/overkiz/const.py | 1 + .../components/overkiz/coordinator.py | 25 ++++++++++++++----- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/overkiz/__init__.py b/homeassistant/components/overkiz/__init__.py index 2b4a0367bf7..51efb52e55d 100644 --- a/homeassistant/components/overkiz/__init__.py +++ b/homeassistant/components/overkiz/__init__.py @@ -41,6 +41,7 @@ from .const import ( PLATFORMS, UPDATE_INTERVAL, UPDATE_INTERVAL_ALL_ASSUMED_STATE, + UPDATE_INTERVAL_LOCAL, ) from .coordinator import OverkizDataUpdateCoordinator @@ -116,13 +117,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: OverkizDataConfigEntry) if coordinator.is_stateless: LOGGER.debug( - ( - "All devices have an assumed state. Update interval has been reduced" - " to: %s" - ), + "All devices have an assumed state. Update interval has been reduced to: %s", UPDATE_INTERVAL_ALL_ASSUMED_STATE, ) - coordinator.update_interval = UPDATE_INTERVAL_ALL_ASSUMED_STATE + coordinator.set_update_interval(UPDATE_INTERVAL_ALL_ASSUMED_STATE) + + if api_type == APIType.LOCAL: + LOGGER.debug( + "Devices connect via Local API. Update interval has been reduced to: %s", + UPDATE_INTERVAL_LOCAL, + ) + coordinator.set_update_interval(UPDATE_INTERVAL_LOCAL) platforms: defaultdict[Platform, list[Device]] = defaultdict(list) diff --git a/homeassistant/components/overkiz/const.py b/homeassistant/components/overkiz/const.py index 1a89fecf9c0..41b567500a9 100644 --- a/homeassistant/components/overkiz/const.py +++ b/homeassistant/components/overkiz/const.py @@ -44,6 +44,7 @@ DEFAULT_SERVER: Final = Server.SOMFY_EUROPE DEFAULT_HOST: Final = "gateway-xxxx-xxxx-xxxx.local:8443" UPDATE_INTERVAL: Final = timedelta(seconds=30) +UPDATE_INTERVAL_LOCAL: Final = timedelta(seconds=5) UPDATE_INTERVAL_ALL_ASSUMED_STATE: Final = timedelta(minutes=60) PLATFORMS: list[Platform] = [ diff --git a/homeassistant/components/overkiz/coordinator.py b/homeassistant/components/overkiz/coordinator.py index 17068d26b7c..484ef138cf7 100644 --- a/homeassistant/components/overkiz/coordinator.py +++ b/homeassistant/components/overkiz/coordinator.py @@ -26,7 +26,7 @@ from homeassistant.helpers import device_registry as dr from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.util.decorator import Registry -from .const import DOMAIN, LOGGER, UPDATE_INTERVAL +from .const import DOMAIN, IGNORED_OVERKIZ_DEVICES, LOGGER EVENT_HANDLERS: Registry[ str, Callable[[OverkizDataUpdateCoordinator, Event], Coroutine[Any, Any, None]] @@ -36,6 +36,8 @@ EVENT_HANDLERS: Registry[ class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]): """Class to manage fetching data from Overkiz platform.""" + _default_update_interval: timedelta + def __init__( self, hass: HomeAssistant, @@ -45,7 +47,7 @@ class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]): client: OverkizClient, devices: list[Device], places: Place | None, - update_interval: timedelta | None = None, + update_interval: timedelta, config_entry_id: str, ) -> None: """Initialize global data updater.""" @@ -59,12 +61,17 @@ class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]): self.data = {} self.client = client self.devices: dict[str, Device] = {d.device_url: d for d in devices} - self.is_stateless = all( - device.protocol in (Protocol.RTS, Protocol.INTERNAL) for device in devices - ) self.executions: dict[str, dict[str, str]] = {} self.areas = self._places_to_area(places) if places else None self.config_entry_id = config_entry_id + self._default_update_interval = update_interval + + self.is_stateless = all( + device.protocol in (Protocol.RTS, Protocol.INTERNAL) + for device in devices + if device.widget not in IGNORED_OVERKIZ_DEVICES + and device.ui_class not in IGNORED_OVERKIZ_DEVICES + ) async def _async_update_data(self) -> dict[str, Device]: """Fetch Overkiz data via event listener.""" @@ -102,8 +109,9 @@ class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]): if event_handler := EVENT_HANDLERS.get(event.name): await event_handler(self, event) + # Restore the default update interval if no executions are pending if not self.executions: - self.update_interval = UPDATE_INTERVAL + self.update_interval = self._default_update_interval return self.devices @@ -124,6 +132,11 @@ class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]): return areas + def set_update_interval(self, update_interval: timedelta) -> None: + """Set the update interval and store this value.""" + self.update_interval = update_interval + self._default_update_interval = update_interval + @EVENT_HANDLERS.register(EventName.DEVICE_AVAILABLE) async def on_device_available(