diff --git a/homeassistant/components/waze_travel_time/const.py b/homeassistant/components/waze_travel_time/const.py index 42ec0182d3a5..9840c38dcc18 100644 --- a/homeassistant/components/waze_travel_time/const.py +++ b/homeassistant/components/waze_travel_time/const.py @@ -7,6 +7,11 @@ from homeassistant.util.hass_dict import HassKey DOMAIN = "waze_travel_time" SEMAPHORE_KEY: HassKey[asyncio.Semaphore] = HassKey(DOMAIN) +ROUTING_REQUEST_QUOTA = 100 +ROUTING_QUOTA_WINDOW_MINUTES = 120 +ROUTING_QUOTA_RESERVE = 0.1 +MIN_UPDATE_INTERVAL_MINUTES = 5 + CONF_BASE_COORDINATES = "base_coordinates" CONF_DESTINATION = "destination" CONF_ORIGIN = "origin" diff --git a/homeassistant/components/waze_travel_time/coordinator.py b/homeassistant/components/waze_travel_time/coordinator.py index 0fc5bb048dd5..bb48b5a35791 100644 --- a/homeassistant/components/waze_travel_time/coordinator.py +++ b/homeassistant/components/waze_travel_time/coordinator.py @@ -1,10 +1,11 @@ """The Waze Travel Time data coordinator.""" import asyncio -from collections.abc import Collection +from collections.abc import Callable, Collection from dataclasses import dataclass from datetime import timedelta import logging +from math import ceil, floor from typing import Literal, override import httpx2 @@ -12,9 +13,15 @@ from pywaze.route_calculator import CalcRoutesResponse, WazeRouteCalculator, WRC from homeassistant.config_entries import ConfigEntry from homeassistant.const import UnitOfLength -from homeassistant.core import HomeAssistant +from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback +from homeassistant.helpers.issue_registry import ( + IssueSeverity, + async_create_issue, + async_delete_issue, +) from homeassistant.helpers.location import find_coordinates from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed +from homeassistant.util.hass_dict import HassKey from homeassistant.util.unit_conversion import DistanceConverter from .const import ( @@ -32,13 +39,19 @@ from .const import ( CONF_VEHICLE_TYPE, DOMAIN, IMPERIAL_UNITS, + MIN_UPDATE_INTERVAL_MINUTES, + ROUTING_QUOTA_RESERVE, + ROUTING_QUOTA_WINDOW_MINUTES, + ROUTING_REQUEST_QUOTA, SEMAPHORE_KEY, ) from .helpers import base_coordinates_to_tuple _LOGGER = logging.getLogger(__name__) -SCAN_INTERVAL = timedelta(minutes=5) +POLLING_COORDINATORS: HassKey[set[WazeTravelTimeCoordinator]] = HassKey( + f"{DOMAIN}_polling_coordinators" +) type WazeTravelTimeConfigEntry = ConfigEntry[WazeTravelTimeCoordinator] @@ -193,12 +206,89 @@ class WazeTravelTimeCoordinator(DataUpdateCoordinator[WazeTravelTimeData]): _LOGGER, name=DOMAIN, config_entry=config_entry, - update_interval=SCAN_INTERVAL, + update_interval=timedelta(minutes=MIN_UPDATE_INTERVAL_MINUTES), ) self.client = client self._origin = config_entry.data[CONF_ORIGIN] self._destination = config_entry.data[CONF_DESTINATION] + @callback + @override + def async_add_listener( + self, update_callback: CALLBACK_TYPE, context: object = None + ) -> Callable[[], None]: + """Update intervals when this coordinator starts calling the API.""" + remove_listener = super().async_add_listener(update_callback, context) + if not self.config_entry.pref_disable_polling: + coordinators = self.hass.data.setdefault(POLLING_COORDINATORS, set()) + if self not in coordinators: + coordinators.add(self) + self._async_update_intervals() + return remove_listener + + @callback + @override + def _unschedule_refresh(self) -> None: + """Update intervals when this coordinator stops calling the API.""" + super()._unschedule_refresh() + self._async_remove_polling_coordinator() + + @override + async def async_shutdown(self) -> None: + """Update intervals when the config_entry is unloaded or setup fails.""" + await super().async_shutdown() + self._async_remove_polling_coordinator() + + @callback + def _async_remove_polling_coordinator(self) -> None: + """Remove this coordinator from the interval calculation.""" + if coordinators := self.hass.data.get(POLLING_COORDINATORS): + coordinators.discard(self) + self._async_update_intervals() + + @callback + def _async_update_intervals(self) -> None: + """Apply the shared routing budget to all automatically polled routes. + + Automatic polling reserves 10% of an empirically observed routing quota per + public IP. Each route update makes one routing request, including alternatives. + Manual refreshes, get_travel_times service calls, startup requests, and other + consumers sharing the public IP can still exhaust the quota. Dynamic intervals + are preventive and do not guarantee protection against 429 responses. + """ + coordinators = self.hass.data[POLLING_COORDINATORS] + request_budget = ROUTING_REQUEST_QUOTA * (1 - ROUTING_QUOTA_RESERVE) + if len(coordinators) > request_budget: + async_create_issue( + self.hass, + DOMAIN, + "too_many_polling_routes", + is_fixable=False, + severity=IssueSeverity.WARNING, + translation_key="too_many_polling_routes", + translation_placeholders={"max_routes": str(floor(request_budget))}, + learn_more_url="https://www.home-assistant.io/integrations/waze_travel_time/#defining-a-custom-polling-interval", + ) + else: + async_delete_issue(self.hass, DOMAIN, "too_many_polling_routes") + + if not coordinators: + return + + polling_rounds = floor(request_budget / len(coordinators)) + if polling_rounds > 0: + # Keep another round beyond the window boundary, allowing for timer rounding. + minutes = floor(ROUTING_QUOTA_WINDOW_MINUTES / polling_rounds) + 1 + else: + minutes = ceil( + ROUTING_QUOTA_WINDOW_MINUTES * len(coordinators) / request_budget + ) + interval = timedelta(minutes=max(MIN_UPDATE_INTERVAL_MINUTES, minutes)) + for coordinator in coordinators: + if coordinator.update_interval != interval: + coordinator.update_interval = interval + coordinator._schedule_refresh() # noqa: SLF001 + @override async def _async_update_data(self) -> WazeTravelTimeData: """Get the latest data from Waze.""" diff --git a/homeassistant/components/waze_travel_time/strings.json b/homeassistant/components/waze_travel_time/strings.json index 74eac621208c..f42cb441150e 100644 --- a/homeassistant/components/waze_travel_time/strings.json +++ b/homeassistant/components/waze_travel_time/strings.json @@ -29,6 +29,12 @@ "message": "An error occurred while retrieving data from Waze" } }, + "issues": { + "too_many_polling_routes": { + "description": "More than {max_routes} Waze Travel Time entries are polling automatically. Updating all these routes once exceeds the request budget for a two-hour window and may lead to Waze blocking updates.\n\nTo reduce the number of automatically polled routes, remove entries you no longer need, or disable automatic polling for some entries:\n1. Go to Settings > Devices & services and find the Waze Travel Time entry.\n2. Open the entry menu, select System options, and turn off automatic polling.\n3. Use the `homeassistant.update_entity` action in an automation to update these routes only when needed.\n\nThe warning will clear when no more than {max_routes} routes poll automatically. On-demand updates and other requests from your public IP can still exceed the limit.", + "title": "Too many Waze routes polling automatically" + } + }, "options": { "step": { "init": { diff --git a/tests/components/waze_travel_time/test_coordinator.py b/tests/components/waze_travel_time/test_coordinator.py new file mode 100644 index 000000000000..01af48d0407c --- /dev/null +++ b/tests/components/waze_travel_time/test_coordinator.py @@ -0,0 +1,414 @@ +"""Test automatic Waze polling intervals.""" + +from datetime import timedelta +from unittest.mock import AsyncMock + +from freezegun.api import FrozenDateTimeFactory +import pytest +from pytest_homeassistant.common import MockConfigEntry, async_fire_time_changed +from pywaze.route_calculator import WRCError + +from homeassistant.components.waze_travel_time.config_flow import WazeConfigFlow +from homeassistant.components.waze_travel_time.const import DEFAULT_OPTIONS, DOMAIN +from homeassistant.config_entries import ConfigEntryDisabler, ConfigEntryState +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er, issue_registry as ir +from homeassistant.setup import async_setup_component + +from .const import MOCK_CONFIG + + +@pytest.fixture +def route_entries() -> list[MockConfigEntry]: + """Create independently configured routes.""" + return [ + MockConfigEntry( + domain=DOMAIN, + data=MOCK_CONFIG, + options=DEFAULT_OPTIONS, + version=WazeConfigFlow.VERSION, + minor_version=WazeConfigFlow.MINOR_VERSION, + ) + for _ in range(91) + ] + + +@pytest.mark.parametrize( + ("route_count", "minutes"), + [ + pytest.param(3, 5, id="minimum_interval"), + pytest.param(4, 6, id="above_minimum_interval"), + pytest.param(10, 14, id="multiple_rounds"), + pytest.param(90, 121, id="exactly_one_round"), + pytest.param(91, 122, id="more_than_reserved_budget"), + ], +) +async def test_polling_interval( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + mock_update: AsyncMock, + route_entries: list[MockConfigEntry], + route_count: int, + minutes: int, +) -> None: + """All loaded routes share the interval, including concurrent startup.""" + entries = route_entries[:route_count] + for entry in entries: + entry.add_to_hass(hass) + assert await async_setup_component(hass, DOMAIN, {}) + await hass.async_block_till_done() + assert mock_update.call_count == route_count + assert all( + entry.runtime_data.update_interval == timedelta(minutes=minutes) + for entry in entries + ) + + mock_update.reset_mock() + freezer.tick(timedelta(minutes=minutes - 1)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + mock_update.assert_not_called() + + freezer.tick(timedelta(minutes=1, seconds=1)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + assert mock_update.call_count == route_count + + +@pytest.mark.parametrize( + ("route_count", "expected_max_requests"), + [ + pytest.param(17, 85, id="seventeen"), + pytest.param(45, 90, id="exactly_two_rounds"), + pytest.param(90, 90, id="exactly_one_round"), + ], +) +async def test_polling_round_budget( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + mock_update: AsyncMock, + route_entries: list[MockConfigEntry], + route_count: int, + expected_max_requests: int, +) -> None: + """Repeated polling rounds never exceed the budget within a two-hour window.""" + for entry in route_entries[:route_count]: + entry.add_to_hass(hass) + assert await async_setup_component(hass, DOMAIN, {}) + await hass.async_block_till_done(wait_background_tasks=True) + assert mock_update.call_count == route_count + + # Include startup requests, then observe every minute for two quota windows. + request_minutes = [0] * route_count + max_requests = route_count + previous_requests = route_count + for minute in range(1, 241): + freezer.tick(timedelta(minutes=1)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + new_requests = mock_update.call_count - previous_requests + request_minutes.extend([minute] * new_requests) + previous_requests = mock_update.call_count + + # Count both window boundaries to catch an extra round at the reset time. + requests_in_window = sum( + request_minute >= minute - 120 for request_minute in request_minutes + ) + assert requests_in_window <= 90 + max_requests = max(max_requests, requests_in_window) + + assert max_requests == expected_max_requests + + +async def test_entry_lifecycle( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + mock_update: AsyncMock, + route_entries: list[MockConfigEntry], + entity_registry: er.EntityRegistry, +) -> None: + """Adding, unloading, disabling, enabling and removing recalculate intervals.""" + # One active route uses the minimum interval. + first, *others = route_entries[:4] + first.add_to_hass(hass) + assert await hass.config_entries.async_setup(first.entry_id) + await hass.async_block_till_done() + coordinator = first.runtime_data + assert coordinator.update_interval == timedelta(minutes=5) + + # A second listener must not count this route twice. + remove_listener = coordinator.async_add_listener(lambda: None) + + # Adding routes updates the already-running coordinator as well as new ones. + for entry in others: + entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + assert all( + entry.runtime_data.update_interval == timedelta(minutes=6) + for entry in route_entries[:4] + ) + + # Removing the extra listener must leave the route participating in polling. + remove_listener() + assert coordinator.update_interval == timedelta(minutes=6) + + # Unloading the fourth route restores the interval for three active routes. + last = others[-1] + assert await hass.config_entries.async_unload(last.entry_id) + await hass.async_block_till_done() + assert coordinator.update_interval == timedelta(minutes=5) + + # The remaining timers must use five minutes, not their previous six minutes. + mock_update.reset_mock() + freezer.tick(timedelta(minutes=5, seconds=1)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + assert mock_update.call_count == 3 + + # Loading the route again must count it exactly once. + assert await hass.config_entries.async_setup(last.entry_id) + await hass.async_block_till_done() + assert coordinator.update_interval == timedelta(minutes=6) + + # Disabling and enabling the entry must remove and restore its polling cost. + assert await hass.config_entries.async_set_disabled_by( + last.entry_id, ConfigEntryDisabler.USER + ) + await hass.async_block_till_done() + assert coordinator.update_interval == timedelta(minutes=5) + + assert await hass.config_entries.async_set_disabled_by(last.entry_id, None) + await hass.async_block_till_done() + assert coordinator.update_interval == timedelta(minutes=6) + + # A loaded entry stops participating when its only sensor is disabled. + entity_id = entity_registry.async_get_entity_id("sensor", DOMAIN, last.entry_id) + assert entity_id is not None + entity_registry.async_update_entity( + entity_id, disabled_by=er.RegistryEntryDisabler.USER + ) + await hass.async_block_till_done() + assert last.state is ConfigEntryState.LOADED + assert coordinator.update_interval == timedelta(minutes=5) + + # Enabling the sensor and reloading restores automatic polling. + entity_registry.async_update_entity(entity_id, disabled_by=None) + assert await hass.config_entries.async_reload(last.entry_id) + await hass.async_block_till_done() + assert coordinator.update_interval == timedelta(minutes=6) + + # Removing a route adjusts the interval without reloading the other entries. + assert await hass.config_entries.async_remove(last.entry_id) + await hass.async_block_till_done() + assert coordinator.update_interval == timedelta(minutes=5) + assert first.runtime_data is coordinator + + +@pytest.mark.usefixtures("mock_update") +@pytest.mark.parametrize( + ("disable_polling", "disable_sensor"), + [ + pytest.param(True, None, id="polling_disabled"), + pytest.param(False, er.RegistryEntryDisabler.USER, id="sensor_disabled"), + ], +) +async def test_too_many_polling_routes_issue( + hass: HomeAssistant, + route_entries: list[MockConfigEntry], + entity_registry: er.EntityRegistry, + issue_registry: ir.IssueRegistry, + disable_polling: bool, + disable_sensor: er.RegistryEntryDisabler | None, +) -> None: + """Warn only above the automatic polling budget and clear on lifecycle changes.""" + # Ninety active routes fit one round; a non-polling entry must not trigger a warning. + for entry in route_entries[:90]: + entry.add_to_hass(hass) + extra = route_entries[90] + extra.add_to_hass(hass) + hass.config_entries.async_update_entry(extra, pref_disable_polling=disable_polling) + entity_registry.async_get_or_create( + "sensor", + DOMAIN, + extra.entry_id, + config_entry=extra, + disabled_by=disable_sensor, + ) + assert await async_setup_component(hass, DOMAIN, {}) + await hass.async_block_till_done() + assert not issue_registry.issues + + # Enabling the ninety-first route creates one shared, actionable warning. + hass.config_entries.async_update_entry(extra, pref_disable_polling=False) + entity_id = entity_registry.async_get_entity_id("sensor", DOMAIN, extra.entry_id) + assert entity_id is not None + entity_registry.async_update_entity(entity_id, disabled_by=None) + assert await hass.config_entries.async_reload(extra.entry_id) + await hass.async_block_till_done() + issue = issue_registry.async_get_issue(DOMAIN, "too_many_polling_routes") + assert issue is not None + assert issue.severity is ir.IssueSeverity.WARNING + assert not issue.is_fixable + assert issue.translation_key == "too_many_polling_routes" + assert issue.translation_placeholders == {"max_routes": "90"} + assert ( + issue.learn_more_url + == "https://www.home-assistant.io/integrations/waze_travel_time/#defining-a-custom-polling-interval" + ) + assert len(issue_registry.issues) == 1 + + # Reloading must not duplicate the warning; unloading must clear it. + assert await hass.config_entries.async_reload(extra.entry_id) + await hass.async_block_till_done() + assert len(issue_registry.issues) == 1 + assert await hass.config_entries.async_unload(extra.entry_id) + await hass.async_block_till_done() + assert not issue_registry.issues + assert await hass.config_entries.async_setup(extra.entry_id) + await hass.async_block_till_done() + assert len(issue_registry.issues) == 1 + + # Disabling automatic polling is one of the actions recommended by the Repair. + hass.config_entries.async_update_entry(extra, pref_disable_polling=True) + assert await hass.config_entries.async_reload(extra.entry_id) + await hass.async_block_till_done() + assert not issue_registry.issues + hass.config_entries.async_update_entry(extra, pref_disable_polling=False) + assert await hass.config_entries.async_reload(extra.entry_id) + await hass.async_block_till_done() + assert len(issue_registry.issues) == 1 + + # Disabling the only sensor stops polling even though its entry stays loaded. + entity_registry.async_update_entity( + entity_id, disabled_by=er.RegistryEntryDisabler.USER + ) + await hass.async_block_till_done() + assert extra.state is ConfigEntryState.LOADED + assert not issue_registry.issues + entity_registry.async_update_entity(entity_id, disabled_by=None) + assert await hass.config_entries.async_reload(extra.entry_id) + await hass.async_block_till_done() + assert len(issue_registry.issues) == 1 + + # Disabling or removing an entry also resolves the warning. + assert await hass.config_entries.async_set_disabled_by( + extra.entry_id, ConfigEntryDisabler.USER + ) + await hass.async_block_till_done() + assert not issue_registry.issues + assert await hass.config_entries.async_set_disabled_by(extra.entry_id, None) + await hass.async_block_till_done() + assert len(issue_registry.issues) == 1 + assert await hass.config_entries.async_remove(extra.entry_id) + await hass.async_block_till_done() + assert not issue_registry.issues + + +@pytest.mark.parametrize( + ("disable_polling", "disable_sensor"), + [ + pytest.param(True, None, id="polling_disabled"), + pytest.param(False, er.RegistryEntryDisabler.USER, id="sensor_disabled"), + ], +) +async def test_non_polling_entry( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + mock_update: AsyncMock, + route_entries: list[MockConfigEntry], + entity_registry: er.EntityRegistry, + disable_polling: bool, + disable_sensor: er.RegistryEntryDisabler | None, +) -> None: + """Routes with polling disabled or no enabled entities do not share the budget.""" + for entry in route_entries[:3]: + entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(entry.entry_id) + extra = route_entries[3] + extra.add_to_hass(hass) + hass.config_entries.async_update_entry(extra, pref_disable_polling=disable_polling) + entity_registry.async_get_or_create( + "sensor", + DOMAIN, + extra.entry_id, + config_entry=extra, + disabled_by=disable_sensor, + ) + assert await hass.config_entries.async_setup(extra.entry_id) + await hass.async_block_till_done() + assert extra.state is ConfigEntryState.LOADED + assert mock_update.call_count == 4 # Startup still fetches the route. + assert all( + entry.runtime_data.update_interval == timedelta(minutes=5) + for entry in route_entries[:3] + ) + + mock_update.reset_mock() + freezer.tick(timedelta(minutes=5, seconds=1)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + assert mock_update.call_count == 3 + + # Enabling the route uses the normal entry reload lifecycle. + hass.config_entries.async_update_entry(extra, pref_disable_polling=False) + entity_id = entity_registry.async_get_entity_id("sensor", DOMAIN, extra.entry_id) + assert entity_id is not None + entity_registry.async_update_entity(entity_id, disabled_by=None) + assert await hass.config_entries.async_reload(extra.entry_id) + await hass.async_block_till_done() + assert all( + entry.runtime_data.update_interval == timedelta(minutes=6) + for entry in route_entries[:4] + ) + + +async def test_setup_retry( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + mock_update: AsyncMock, + route_entries: list[MockConfigEntry], +) -> None: + """Failed setup and retries never leave stale or duplicate polling routes.""" + # Three active routes provide the five-minute baseline. + for entry in route_entries[:3]: + entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + coordinator = route_entries[0].runtime_data + extra = route_entries[3] + extra.add_to_hass(hass) + + # Failed setup must not add the fourth route to the polling count. + mock_update.side_effect = WRCError("Unavailable") + assert not await hass.config_entries.async_setup(extra.entry_id) + await hass.async_block_till_done() + assert extra.state is ConfigEntryState.SETUP_RETRY + assert coordinator.update_interval == timedelta(minutes=5) + + # Advance past the first retry delay, including its random offset. + mock_update.reset_mock() + freezer.tick(timedelta(seconds=6)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + mock_update.assert_called_once() + assert extra.state is ConfigEntryState.SETUP_RETRY + assert coordinator.update_interval == timedelta(minutes=5) + + # The next automatic retry succeeds and must count the fourth route once. + mock_update.side_effect = None + freezer.tick(timedelta(seconds=11)) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + assert extra.state is ConfigEntryState.LOADED + assert coordinator.update_interval == timedelta(minutes=6) + + # Reloading must replace the registration, not add a duplicate. + assert await hass.config_entries.async_reload(extra.entry_id) + await hass.async_block_till_done() + assert coordinator.update_interval == timedelta(minutes=6) + + # Unloading must remove the route without leaving a stale registration. + assert await hass.config_entries.async_unload(extra.entry_id) + await hass.async_block_till_done() + assert coordinator.update_interval == timedelta(minutes=5)