From 62ce373457fbcb66d760849f3010f86bde490378 Mon Sep 17 00:00:00 2001 From: bakonyiferenc <54292419+bakonyiferenc@users.noreply.github.com> Date: Sat, 23 Mar 2024 13:54:08 +0100 Subject: [PATCH] Fixing issue #116 (#117) * Fix issue #116 * Format code with black --- .../sagemcom_fast/device_tracker.py | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/custom_components/sagemcom_fast/device_tracker.py b/custom_components/sagemcom_fast/device_tracker.py index e22954f..70b49f8 100644 --- a/custom_components/sagemcom_fast/device_tracker.py +++ b/custom_components/sagemcom_fast/device_tracker.py @@ -4,7 +4,7 @@ from homeassistant.components.device_tracker import SourceType from homeassistant.components.device_tracker.config_entry import ScannerEntity from homeassistant.config_entries import ConfigEntry -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.restore_state import RestoreEntity @@ -24,11 +24,22 @@ async def async_setup_entry( ) -> None: """Set up device tracker from config entry.""" data: HomeAssistantSagemcomFastData = hass.data[DOMAIN][entry.entry_id] - - async_add_entities( - SagemcomScannerEntity(data.coordinator, idx, entry.entry_id) - for idx, device in data.coordinator.data.items() - ) + tracked: dict[str, SagemcomScannerEntity] = {} + + @callback + def async_update_router() -> None: + """Update the values of the router.""" + newly_discovered: list[SagemcomScannerEntity] = [] + for idx, device in data.coordinator.data.items(): + if idx not in tracked: + tracked[idx] = SagemcomScannerEntity( + data.coordinator, idx, entry.entry_id + ) + newly_discovered.append(tracked[idx]) + async_add_entities(newly_discovered) + + entry.async_on_unload(data.coordinator.async_add_listener(async_update_router)) + async_update_router() class SagemcomScannerEntity(