mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Improve logic for event polling duration in Overkiz (#133617)
This commit is contained in:
parent
3b13c5bfdd
commit
0ab66a4ed1
@ -41,6 +41,7 @@ from .const import (
|
|||||||
PLATFORMS,
|
PLATFORMS,
|
||||||
UPDATE_INTERVAL,
|
UPDATE_INTERVAL,
|
||||||
UPDATE_INTERVAL_ALL_ASSUMED_STATE,
|
UPDATE_INTERVAL_ALL_ASSUMED_STATE,
|
||||||
|
UPDATE_INTERVAL_LOCAL,
|
||||||
)
|
)
|
||||||
from .coordinator import OverkizDataUpdateCoordinator
|
from .coordinator import OverkizDataUpdateCoordinator
|
||||||
|
|
||||||
@ -116,13 +117,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: OverkizDataConfigEntry)
|
|||||||
|
|
||||||
if coordinator.is_stateless:
|
if coordinator.is_stateless:
|
||||||
LOGGER.debug(
|
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,
|
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)
|
platforms: defaultdict[Platform, list[Device]] = defaultdict(list)
|
||||||
|
|
||||||
|
@ -44,6 +44,7 @@ DEFAULT_SERVER: Final = Server.SOMFY_EUROPE
|
|||||||
DEFAULT_HOST: Final = "gateway-xxxx-xxxx-xxxx.local:8443"
|
DEFAULT_HOST: Final = "gateway-xxxx-xxxx-xxxx.local:8443"
|
||||||
|
|
||||||
UPDATE_INTERVAL: Final = timedelta(seconds=30)
|
UPDATE_INTERVAL: Final = timedelta(seconds=30)
|
||||||
|
UPDATE_INTERVAL_LOCAL: Final = timedelta(seconds=5)
|
||||||
UPDATE_INTERVAL_ALL_ASSUMED_STATE: Final = timedelta(minutes=60)
|
UPDATE_INTERVAL_ALL_ASSUMED_STATE: Final = timedelta(minutes=60)
|
||||||
|
|
||||||
PLATFORMS: list[Platform] = [
|
PLATFORMS: list[Platform] = [
|
||||||
|
@ -26,7 +26,7 @@ from homeassistant.helpers import device_registry as dr
|
|||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
from homeassistant.util.decorator import Registry
|
from homeassistant.util.decorator import Registry
|
||||||
|
|
||||||
from .const import DOMAIN, LOGGER, UPDATE_INTERVAL
|
from .const import DOMAIN, IGNORED_OVERKIZ_DEVICES, LOGGER
|
||||||
|
|
||||||
EVENT_HANDLERS: Registry[
|
EVENT_HANDLERS: Registry[
|
||||||
str, Callable[[OverkizDataUpdateCoordinator, Event], Coroutine[Any, Any, None]]
|
str, Callable[[OverkizDataUpdateCoordinator, Event], Coroutine[Any, Any, None]]
|
||||||
@ -36,6 +36,8 @@ EVENT_HANDLERS: Registry[
|
|||||||
class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]):
|
class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]):
|
||||||
"""Class to manage fetching data from Overkiz platform."""
|
"""Class to manage fetching data from Overkiz platform."""
|
||||||
|
|
||||||
|
_default_update_interval: timedelta
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
@ -45,7 +47,7 @@ class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]):
|
|||||||
client: OverkizClient,
|
client: OverkizClient,
|
||||||
devices: list[Device],
|
devices: list[Device],
|
||||||
places: Place | None,
|
places: Place | None,
|
||||||
update_interval: timedelta | None = None,
|
update_interval: timedelta,
|
||||||
config_entry_id: str,
|
config_entry_id: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize global data updater."""
|
"""Initialize global data updater."""
|
||||||
@ -59,12 +61,17 @@ class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]):
|
|||||||
self.data = {}
|
self.data = {}
|
||||||
self.client = client
|
self.client = client
|
||||||
self.devices: dict[str, Device] = {d.device_url: d for d in devices}
|
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.executions: dict[str, dict[str, str]] = {}
|
||||||
self.areas = self._places_to_area(places) if places else None
|
self.areas = self._places_to_area(places) if places else None
|
||||||
self.config_entry_id = config_entry_id
|
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]:
|
async def _async_update_data(self) -> dict[str, Device]:
|
||||||
"""Fetch Overkiz data via event listener."""
|
"""Fetch Overkiz data via event listener."""
|
||||||
@ -102,8 +109,9 @@ class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]):
|
|||||||
if event_handler := EVENT_HANDLERS.get(event.name):
|
if event_handler := EVENT_HANDLERS.get(event.name):
|
||||||
await event_handler(self, event)
|
await event_handler(self, event)
|
||||||
|
|
||||||
|
# Restore the default update interval if no executions are pending
|
||||||
if not self.executions:
|
if not self.executions:
|
||||||
self.update_interval = UPDATE_INTERVAL
|
self.update_interval = self._default_update_interval
|
||||||
|
|
||||||
return self.devices
|
return self.devices
|
||||||
|
|
||||||
@ -124,6 +132,11 @@ class OverkizDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Device]]):
|
|||||||
|
|
||||||
return areas
|
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)
|
@EVENT_HANDLERS.register(EventName.DEVICE_AVAILABLE)
|
||||||
async def on_device_available(
|
async def on_device_available(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user