Fix cleanup of old orphan device entries in AVM Fritz!Tools (#123516)

fix cleanup of old orphan device entries
This commit is contained in:
Michael 2024-08-10 18:01:15 +02:00 committed by GitHub
parent ace6385f5e
commit 257742de46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -652,8 +652,6 @@ class FritzBoxTools(DataUpdateCoordinator[UpdateCoordinatorDataType]):
entities: list[er.RegistryEntry] = er.async_entries_for_config_entry( entities: list[er.RegistryEntry] = er.async_entries_for_config_entry(
entity_reg, config_entry.entry_id entity_reg, config_entry.entry_id
) )
orphan_macs: set[str] = set()
for entity in entities: for entity in entities:
entry_mac = entity.unique_id.split("_")[0] entry_mac = entity.unique_id.split("_")[0]
if ( if (
@ -661,17 +659,16 @@ class FritzBoxTools(DataUpdateCoordinator[UpdateCoordinatorDataType]):
or "_internet_access" in entity.unique_id or "_internet_access" in entity.unique_id
) and entry_mac not in device_hosts: ) and entry_mac not in device_hosts:
_LOGGER.info("Removing orphan entity entry %s", entity.entity_id) _LOGGER.info("Removing orphan entity entry %s", entity.entity_id)
orphan_macs.add(entry_mac)
entity_reg.async_remove(entity.entity_id) entity_reg.async_remove(entity.entity_id)
device_reg = dr.async_get(self.hass) device_reg = dr.async_get(self.hass)
orphan_connections = { valid_connections = {
(CONNECTION_NETWORK_MAC, dr.format_mac(mac)) for mac in orphan_macs (CONNECTION_NETWORK_MAC, dr.format_mac(mac)) for mac in device_hosts
} }
for device in dr.async_entries_for_config_entry( for device in dr.async_entries_for_config_entry(
device_reg, config_entry.entry_id 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) _LOGGER.debug("Removing obsolete device entry %s", device.name)
device_reg.async_update_device( device_reg.async_update_device(
device.id, remove_config_entry_id=config_entry.entry_id device.id, remove_config_entry_id=config_entry.entry_id