Fix lookin falling back to polling too quickly (#93227)

This commit is contained in:
J. Nick Koston 2023-05-20 14:41:16 -05:00 committed by GitHub
parent cbee514c2a
commit fc7a421a48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 14 deletions

View file

@ -3,7 +3,6 @@ from __future__ import annotations
import asyncio import asyncio
from collections.abc import Callable, Coroutine from collections.abc import Callable, Coroutine
from datetime import timedelta
import logging import logging
from typing import Any from typing import Any
@ -26,7 +25,13 @@ from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession 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 .coordinator import LookinDataUpdateCoordinator, LookinPushCoordinator
from .models import LookinData from .models import LookinData
@ -107,9 +112,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
push_coordinator, push_coordinator,
name=entry.title, name=entry.title,
update_method=lookin_protocol.get_meteo_sensor, update_method=lookin_protocol.get_meteo_sensor,
update_interval=timedelta( update_interval=METEO_UPDATE_INTERVAL, # Updates are pushed (fallback is polling)
minutes=5
), # Updates are pushed (fallback is polling)
) )
await meteo_coordinator.async_config_entry_first_refresh() await meteo_coordinator.async_config_entry_first_refresh()
@ -127,9 +130,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
push_coordinator, push_coordinator,
name=f"{entry.title} {uuid}", name=f"{entry.title} {uuid}",
update_method=updater, update_method=updater,
update_interval=timedelta( update_interval=REMOTE_UPDATE_INTERVAL, # Updates are pushed (fallback is polling)
seconds=60
), # Updates are pushed (fallback is polling)
) )
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
device_coordinators[uuid] = coordinator device_coordinators[uuid] = coordinator

View file

@ -1,6 +1,7 @@
"""The lookin integration constants.""" """The lookin integration constants."""
from __future__ import annotations from __future__ import annotations
from datetime import timedelta
from typing import Final from typing import Final
from homeassistant.const import Platform from homeassistant.const import Platform
@ -22,3 +23,12 @@ TYPE_TO_PLATFORM = {
"03": Platform.LIGHT, "03": Platform.LIGHT,
"EF": Platform.CLIMATE, "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
)

View file

@ -10,12 +10,11 @@ from typing import TypeVar
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import NEVER_TIME, POLLING_FALLBACK_SECONDS
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_DataT = TypeVar("_DataT") _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: class LookinPushCoordinator:
"""Keep track of when the last push update was.""" """Keep track of when the last push update was."""
@ -32,9 +31,7 @@ class LookinPushCoordinator:
def active(self, interval: timedelta) -> bool: def active(self, interval: timedelta) -> bool:
"""Check if the last push update was recently.""" """Check if the last push update was recently."""
time_since_last_update = time.monotonic() - self.last_update time_since_last_update = time.monotonic() - self.last_update
is_active = ( is_active = time_since_last_update < POLLING_FALLBACK_SECONDS
time_since_last_update < interval.total_seconds() * ACTIVE_UPDATES_INTERVAL
)
_LOGGER.debug( _LOGGER.debug(
"%s: push updates active: %s (time_since_last_update=%s)", "%s: push updates active: %s (time_since_last_update=%s)",
self.name, self.name,