Refactor Debouncer usage in august ()

pull/111034/head
J. Nick Koston 2024-02-22 11:23:14 -10:00 committed by GitHub
parent 78f3f67e3a
commit 36c11119cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 9 deletions
homeassistant/components/august

View File

@ -1,7 +1,6 @@
"""Consume the august activity stream."""
from __future__ import annotations
import asyncio
from datetime import datetime
from functools import partial
import logging
@ -63,11 +62,10 @@ class ActivityStream(AugustSubscriberMixin):
self._update_debounce: dict[str, Debouncer] = {}
self._update_debounce_jobs: dict[str, HassJob] = {}
async def _async_update_house_id_later(
self, debouncer: Debouncer, _: datetime
) -> None:
@callback
def _async_update_house_id_later(self, debouncer: Debouncer, _: datetime) -> None:
"""Call a debouncer from async_call_later."""
await debouncer.async_call()
debouncer.async_schedule_call()
async def async_setup(self) -> None:
"""Token refresh check and catch up the activity stream."""
@ -128,9 +126,9 @@ class ActivityStream(AugustSubscriberMixin):
_LOGGER.debug("Skipping update because pubnub is connected")
return
_LOGGER.debug("Start retrieving device activities")
await asyncio.gather(
*(debouncer.async_call() for debouncer in self._update_debounce.values())
)
# Await in sequence to avoid hammering the API
for debouncer in self._update_debounce.values():
await debouncer.async_call()
@callback
def async_schedule_house_id_refresh(self, house_id: str) -> None:
@ -139,7 +137,7 @@ class ActivityStream(AugustSubscriberMixin):
_async_cancel_future_scheduled_updates(future_updates)
debouncer = self._update_debounce[house_id]
self._hass.async_create_task(debouncer.async_call())
debouncer.async_schedule_call()
# Schedule two updates past the debounce time
# to ensure we catch the case where the activity
# api does not update right away and we need to poll