diff --git a/homeassistant/components/lookin/__init__.py b/homeassistant/components/lookin/__init__.py index a8f22fb17ca..c16d7f34f0f 100644 --- a/homeassistant/components/lookin/__init__.py +++ b/homeassistant/components/lookin/__init__.py @@ -3,7 +3,6 @@ from __future__ import annotations import asyncio from collections.abc import Callable, Coroutine -from datetime import timedelta import logging from typing import Any @@ -26,7 +25,13 @@ from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import device_registry as dr from homeassistant.helpers.aiohttp_client import async_get_clientsession -from .const import DOMAIN, PLATFORMS, TYPE_TO_PLATFORM +from .const import ( + DOMAIN, + METEO_UPDATE_INTERVAL, + PLATFORMS, + REMOTE_UPDATE_INTERVAL, + TYPE_TO_PLATFORM, +) from .coordinator import LookinDataUpdateCoordinator, LookinPushCoordinator from .models import LookinData @@ -107,9 +112,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: push_coordinator, name=entry.title, update_method=lookin_protocol.get_meteo_sensor, - update_interval=timedelta( - minutes=5 - ), # Updates are pushed (fallback is polling) + update_interval=METEO_UPDATE_INTERVAL, # Updates are pushed (fallback is polling) ) await meteo_coordinator.async_config_entry_first_refresh() @@ -127,9 +130,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: push_coordinator, name=f"{entry.title} {uuid}", update_method=updater, - update_interval=timedelta( - seconds=60 - ), # Updates are pushed (fallback is polling) + update_interval=REMOTE_UPDATE_INTERVAL, # Updates are pushed (fallback is polling) ) await coordinator.async_config_entry_first_refresh() device_coordinators[uuid] = coordinator diff --git a/homeassistant/components/lookin/const.py b/homeassistant/components/lookin/const.py index a48e3ad01a8..8eb96dcefd8 100644 --- a/homeassistant/components/lookin/const.py +++ b/homeassistant/components/lookin/const.py @@ -1,6 +1,7 @@ """The lookin integration constants.""" from __future__ import annotations +from datetime import timedelta from typing import Final from homeassistant.const import Platform @@ -22,3 +23,12 @@ TYPE_TO_PLATFORM = { "03": Platform.LIGHT, "EF": Platform.CLIMATE, } + +NEVER_TIME = -120.0 # Time that will never match time.monotonic() +ACTIVE_UPDATES_INTERVAL = 4 # Consider active for 4x the update interval +METEO_UPDATE_INTERVAL = timedelta(minutes=5) +REMOTE_UPDATE_INTERVAL = timedelta(seconds=60) +POLLING_FALLBACK_SECONDS = ( + max(REMOTE_UPDATE_INTERVAL, METEO_UPDATE_INTERVAL).total_seconds() + * ACTIVE_UPDATES_INTERVAL +) diff --git a/homeassistant/components/lookin/coordinator.py b/homeassistant/components/lookin/coordinator.py index 94c4a70f3ca..1bdbb36dd71 100644 --- a/homeassistant/components/lookin/coordinator.py +++ b/homeassistant/components/lookin/coordinator.py @@ -10,12 +10,11 @@ from typing import TypeVar from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.update_coordinator import DataUpdateCoordinator +from .const import NEVER_TIME, POLLING_FALLBACK_SECONDS + _LOGGER = logging.getLogger(__name__) _DataT = TypeVar("_DataT") -NEVER_TIME = -120.0 # Time that will never match time.monotonic() -ACTIVE_UPDATES_INTERVAL = 3 # Consider active for 3x the update interval - class LookinPushCoordinator: """Keep track of when the last push update was.""" @@ -32,9 +31,7 @@ class LookinPushCoordinator: def active(self, interval: timedelta) -> bool: """Check if the last push update was recently.""" time_since_last_update = time.monotonic() - self.last_update - is_active = ( - time_since_last_update < interval.total_seconds() * ACTIVE_UPDATES_INTERVAL - ) + is_active = time_since_last_update < POLLING_FALLBACK_SECONDS _LOGGER.debug( "%s: push updates active: %s (time_since_last_update=%s)", self.name,