Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve device tracking after reboot #117

Merged
merged 2 commits into from
Mar 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions custom_components/sagemcom_fast/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
Loading