From ec9cfe99904badfb10b2a3aec9ae5658287a8399 Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Tue, 1 Dec 2026 17:11:25 +0100 Subject: [PATCH] Skip hosts update in FRITZ!Box tools when mesh topology is not accessible (#192601) Co-authored-by: Claude Opus 5.5 --- homeassistant/components/fritz/coordinator.py | 10 ++- tests/components/fritz/test_coordinator.py | 66 +++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/fritz/coordinator.py b/homeassistant/components/fritz/coordinator.py index 7421e10528a0..71db3577407e 100644 --- a/homeassistant/components/fritz/coordinator.py +++ b/homeassistant/components/fritz/coordinator.py @@ -168,7 +168,7 @@ class FritzBoxTools(DataUpdateCoordinator[UpdateCoordinatorDataType]): self.mesh_wifi_uplink = False self.device_conn_type: str | None = None self.device_is_router: bool = False - self.hosts: dict[str, Device] = {} + self.hosts: dict[str, Device] | None = None self.password = password self.port = port self.username = username @@ -424,7 +424,7 @@ class FritzBoxTools(DataUpdateCoordinator[UpdateCoordinatorDataType]): "port_mappings": {}, } self.connection.clear_cache() - self.hosts = {} + self.hosts = None try: await self.async_update_device_info() @@ -702,12 +702,12 @@ class FritzBoxTools(DataUpdateCoordinator[UpdateCoordinatorDataType]): consider_home = _default_consider_home new_device = False - self.hosts = await self._async_update_hosts_info() if not self.fritz_status.device_has_mesh_support or ( self._options and self._options.get(CONF_OLD_DISCOVERY, DEFAULT_CONF_OLD_DISCOVERY) ): + self.hosts = await self._async_update_hosts_info() LOGGER.debug( "Using old hosts discovery method. (Mesh not supported or user option)" ) @@ -759,6 +759,7 @@ class FritzBoxTools(DataUpdateCoordinator[UpdateCoordinatorDataType]): self.mesh_role = MeshRoles(node["mesh_role"]) # second get all client devices + self.hosts = await self._async_update_hosts_info() for node in topology.get("nodes", []): if node["is_meshed"]: continue @@ -837,6 +838,9 @@ class FritzBoxTools(DataUpdateCoordinator[UpdateCoordinatorDataType]): LOGGER.debug("Device tracker cleanup triggered") device_hosts = {self.mac: Device(True, "", "", "", "", None)} if self.device_discovery_enabled: + if self.hosts is None: + # Hosts were not fetched in this update, so clients can't be judged stale + return device_hosts.update(self.hosts) entity_reg: er.EntityRegistry = er.async_get(self.hass) config_entry = self.config_entry diff --git a/tests/components/fritz/test_coordinator.py b/tests/components/fritz/test_coordinator.py index ff27907bd327..0003dcac84ec 100644 --- a/tests/components/fritz/test_coordinator.py +++ b/tests/components/fritz/test_coordinator.py @@ -11,6 +11,7 @@ from fritzconnection.core.exceptions import ( FritzConnectionException, FritzSecurityError, ) +from fritzconnection.lib.fritzhosts import FritzHosts from fritzconnection.lib.fritzstatus import FritzStatus from fritzconnection.lib.fritztools import ArgumentNamespace import pytest @@ -773,6 +774,71 @@ async def test_async_trigger_cleanup_preserves_fritz_device( assert fritz_device_after.id == fritz_device.id +@pytest.mark.usefixtures("fc_class_mock", "fs_class_mock") +async def test_hosts_not_fetched_without_mesh_topology_access( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + fh_class_mock: type[FritzHosts], +) -> None: + """Test hosts are never fetched when the device has no access to the mesh topology.""" + fh_class_mock.get_mesh_topology.side_effect = FritzActionError( + "Error 404: Device has no access to topology information." + ) + + entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) + entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done(wait_background_tasks=True) + assert entry.state is ConfigEntryState.LOADED + assert hass.states.async_entity_ids("device_tracker") == [] + + freezer.tick(SCAN_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + # The topology is retried on every update, but hosts are never fetched + fh_class_mock.get_hosts_attributes.assert_not_called() + assert fh_class_mock.get_mesh_topology.call_count == 2 + assert hass.states.async_entity_ids("device_tracker") == [] + + +@pytest.mark.usefixtures("fc_class_mock", "fs_class_mock") +async def test_cleanup_keeps_clients_on_mesh_topology_error( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, + freezer: FrozenDateTimeFactory, + fh_class_mock: type[FritzHosts], +) -> None: + """Test a failing mesh topology fetch does not remove tracked clients.""" + fh_class_mock.get_hosts_attributes.return_value = [ + MOCK_HOST_PRINTER, + MOCK_HOST_FRITZBOX, + ] + + entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) + entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done(wait_background_tasks=True) + assert entry.state is ConfigEntryState.LOADED + assert entity_registry.async_get("device_tracker.printer") + assert hass.states.get("switch.printer_internet_access") + + fh_class_mock.get_mesh_topology.side_effect = FritzActionError( + "Error 500: Device has no access to topology information." + ) + freezer.tick(SCAN_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + assert device_registry.async_get_device_by_connection( + (dr.CONNECTION_NETWORK_MAC, "aa:bb:cc:00:11:22"), entry.entry_id + ) + assert entity_registry.async_get("device_tracker.printer") + assert entity_registry.async_get("switch.printer_internet_access") + assert hass.states.get("switch.printer_internet_access") + + async def test_old_discovery_does_not_self_reference_box( hass: HomeAssistant, device_registry: dr.DeviceRegistry,