diff --git a/homeassistant/components/unifi/coordinator.py b/homeassistant/components/unifi/coordinator.py index daee12d4f157..eaf60da4a659 100644 --- a/homeassistant/components/unifi/coordinator.py +++ b/homeassistant/components/unifi/coordinator.py @@ -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.""" diff --git a/tests/components/unifi/test_hub.py b/tests/components/unifi/test_hub.py index d1242cf03870..b956e8e9e457 100644 --- a/tests/components/unifi/test_hub.py +++ b/tests/components/unifi/test_hub.py @@ -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"