Make unifiprotect fallback update an eager task (#111580)

This commit is contained in:
J. Nick Koston 2024-02-26 16:30:53 -10:00 committed by GitHub
parent 86ced5a85f
commit bf866732c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,7 +2,7 @@
from __future__ import annotations
from collections.abc import Callable, Generator, Iterable
from datetime import timedelta
from datetime import datetime, timedelta
import logging
from typing import Any, cast
@ -282,6 +282,16 @@ class ProtectData:
for device in self.get_by_types(DEVICES_THAT_ADOPT):
self._async_signal_device_update(device)
@callback
def _async_poll(self, now: datetime) -> None:
"""Poll the Protect API.
If the websocket is connected, most of the time
this will be a no-op. If the websocket is disconnected,
this will trigger a reconnect and refresh.
"""
self._hass.async_create_task(self.async_refresh(), eager_start=True)
@callback
def async_subscribe_device_id(
self, mac: str, update_callback: Callable[[ProtectDeviceType], None]
@ -289,7 +299,7 @@ class ProtectData:
"""Add an callback subscriber."""
if not self._subscriptions:
self._unsub_interval = async_track_time_interval(
self._hass, self.async_refresh, self._update_interval
self._hass, self._async_poll, self._update_interval
)
self._subscriptions.setdefault(mac, []).append(update_callback)