From bf866732c97aee55f74f67e2260cd2f7b2a59168 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 26 Feb 2024 16:30:53 -1000 Subject: [PATCH] Make unifiprotect fallback update an eager task (#111580) --- homeassistant/components/unifiprotect/data.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/unifiprotect/data.py b/homeassistant/components/unifiprotect/data.py index 5565581290c..2825c2a4f3c 100644 --- a/homeassistant/components/unifiprotect/data.py +++ b/homeassistant/components/unifiprotect/data.py @@ -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)