Reduce UniFi polling for empty handlers (#182880)

This commit is contained in:
Robert Svensson
2026-09-25 16:13:59 +02:00
committed by GitHub
parent 6b3ce83a91
commit eb80f9606d
2 changed files with 14 additions and 3 deletions
@@ -15,6 +15,7 @@ if TYPE_CHECKING:
from .hub.hub import UnifiHub
POLL_INTERVAL = timedelta(seconds=10)
IDLE_POLL_INTERVAL = timedelta(minutes=10)
class UnifiDataUpdateCoordinator[HandlerT: APIHandler](
@@ -69,6 +70,11 @@ class UnifiDataUpdateCoordinator[HandlerT: APIHandler](
)
raise UpdateFailed(str(err)) from err
if self.update_interval is not None:
self.update_interval = (
POLL_INTERVAL if self._handler.items() else IDLE_POLL_INTERVAL
)
@callback
def _async_handle_update(self, event: ItemEvent, obj_id: str) -> None:
"""Notify listeners which object changed on a websocket update."""
+8 -3
View File
@@ -12,7 +12,7 @@ from aiounifi.models.message import MessageKey
import pytest
from homeassistant.components.unifi.const import CONF_BLOCK_CLIENT, DOMAIN
from homeassistant.components.unifi.coordinator import POLL_INTERVAL
from homeassistant.components.unifi.coordinator import IDLE_POLL_INTERVAL, POLL_INTERVAL
from homeassistant.components.unifi.errors import AuthenticationRequired, CannotConnect
from homeassistant.components.unifi.hub import get_unifi_api
from homeassistant.config_entries import ConfigEntryState
@@ -85,7 +85,7 @@ async def test_coordinators_preserve_handler_update_sources(
api.traffic_routes,
):
coordinator = loader.get_data_update_coordinator(handler)
assert coordinator.update_interval == POLL_INTERVAL
assert coordinator.update_interval == IDLE_POLL_INTERVAL
async def test_get_data_update_coordinator_requires_registered_handler(
@@ -110,6 +110,11 @@ async def test_polling_coordinator_refreshes_after_interval(
api.object_oriented_network_configs
)
assert coordinator.update_interval == IDLE_POLL_INTERVAL
with patch.object(coordinator.handler, "items", return_value=[("id", object())]):
await coordinator.async_refresh()
assert coordinator.update_interval == POLL_INTERVAL
with patch.object(
@@ -157,7 +162,7 @@ async def test_endpoint_not_found_disables_object_oriented_network_config_pollin
await hass.async_block_till_done()
assert mock_update.call_count == 2
assert traffic_rules_coordinator.update_interval == POLL_INTERVAL
assert traffic_rules_coordinator.update_interval == IDLE_POLL_INTERVAL
assert (
caplog.text.count(
"UniFi ObjectOrientedNetworkConfigs endpoint is unavailable; disabling polling"