Refactor Debouncer usage in august (#111102)

This commit is contained in:
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

View File

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