Skip to content

Commit

Permalink
Improve logic for event polling duration in Overkiz (#133617)
Browse files Browse the repository at this point in the history
  • Loading branch information
iMicknl authored Jan 7, 2025
1 parent 3b13c5b commit 0ab66a4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
15 changes: 10 additions & 5 deletions homeassistant/components/overkiz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
PLATFORMS,
UPDATE_INTERVAL,
UPDATE_INTERVAL_ALL_ASSUMED_STATE,
UPDATE_INTERVAL_LOCAL,
)
from .coordinator import OverkizDataUpdateCoordinator

Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/overkiz/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
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] = [
Expand Down
25 changes: 19 additions & 6 deletions homeassistant/components/overkiz/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
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]]
Expand All @@ -36,6 +36,8 @@
class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]):
"""Class to manage fetching data from Overkiz platform."""

_default_update_interval: timedelta

def __init__(
self,
hass: HomeAssistant,
Expand All @@ -45,7 +47,7 @@ def __init__(
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."""
Expand All @@ -59,12 +61,17 @@ def __init__(
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."""
Expand Down Expand Up @@ -102,8 +109,9 @@ async def _async_update_data(self) -> 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

Expand All @@ -124,6 +132,11 @@ def _places_to_area(self, place: Place) -> dict[str, str]:

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(
Expand Down

0 comments on commit 0ab66a4

Please sign in to comment.