Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions homeassistant/components/unifiprotect/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -282,14 +282,24 @@ def _async_process_updates(self, updates: Bootstrap | None) -> None:
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]
) -> CALLBACK_TYPE:
"""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)

Expand Down