Improve logic for event polling duration in Overkiz (#133617)

pull/135021/head
Mick Vleeshouwer 2025-01-07 19:06:57 +01:00 committed by GitHub
parent 3b13c5bfdd
commit 0ab66a4ed1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 11 deletions

View File

@ -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)

View File

@ -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] = [

View File

@ -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(