Skip to content

Commit

Permalink
Fix cleanup of old orphan device entries in AVM Fritz!Tools (#123516)
Browse files Browse the repository at this point in the history
fix cleanup of old orphan device entries
  • Loading branch information
mib1185 authored and frenck committed Aug 10, 2024
1 parent dfb5946 commit 4a75c55
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions homeassistant/components/fritz/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,26 +653,23 @@ async def async_trigger_cleanup(self) -> None:
entities: list[er.RegistryEntry] = er.async_entries_for_config_entry(
entity_reg, config_entry.entry_id
)

orphan_macs: set[str] = set()
for entity in entities:
entry_mac = entity.unique_id.split("_")[0]
if (
entity.domain == DEVICE_TRACKER_DOMAIN
or "_internet_access" in entity.unique_id
) and entry_mac not in device_hosts:
_LOGGER.info("Removing orphan entity entry %s", entity.entity_id)
orphan_macs.add(entry_mac)
entity_reg.async_remove(entity.entity_id)

device_reg = dr.async_get(self.hass)
orphan_connections = {
(CONNECTION_NETWORK_MAC, dr.format_mac(mac)) for mac in orphan_macs
valid_connections = {
(CONNECTION_NETWORK_MAC, dr.format_mac(mac)) for mac in device_hosts
}
for device in dr.async_entries_for_config_entry(
device_reg, config_entry.entry_id
):
if any(con in device.connections for con in orphan_connections):
if not any(con in device.connections for con in valid_connections):
_LOGGER.debug("Removing obsolete device entry %s", device.name)
device_reg.async_update_device(
device.id, remove_config_entry_id=config_entry.entry_id
Expand Down

0 comments on commit 4a75c55

Please sign in to comment.