Skip to content

Commit

Permalink
Fix lookin falling back to polling too quickly (#93227)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed May 20, 2023
1 parent cbee514 commit fc7a421
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
17 changes: 9 additions & 8 deletions homeassistant/components/lookin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import asyncio
from collections.abc import Callable, Coroutine
from datetime import timedelta
import logging
from typing import Any

Expand All @@ -26,7 +25,13 @@
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

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

Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions homeassistant/components/lookin/const.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,3 +23,12 @@
"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
)
9 changes: 3 additions & 6 deletions homeassistant/components/lookin/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
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."""
Expand All @@ -32,9 +31,7 @@ def update(self) -> None:
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,
Expand Down

0 comments on commit fc7a421

Please sign in to comment.