Improve cleanup service for Fritz (#61484)

This commit is contained in:
Simone Chemelli 2022-01-05 20:21:15 +01:00 committed by GitHub
parent da1d74feb8
commit c3a432620c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 9 deletions

View File

@ -426,8 +426,9 @@ class FritzBoxTools(update_coordinator.DataUpdateCoordinator):
) )
return return
device_hosts_list: list[dict] = []
if service_call.service == SERVICE_CLEANUP: if service_call.service == SERVICE_CLEANUP:
device_hosts_list: list = await self.hass.async_add_executor_job( device_hosts_list = await self.hass.async_add_executor_job(
self.fritz_hosts.get_hosts_info self.fritz_hosts.get_hosts_info
) )
@ -447,15 +448,30 @@ class FritzBoxTools(update_coordinator.DataUpdateCoordinator):
) )
entities_removed: bool = False entities_removed: bool = False
device_hosts_macs = {device["mac"] for device in device_hosts_list} device_hosts_macs = set()
device_hosts_names = set()
for device in device_hosts_list:
device_hosts_macs.add(device["mac"])
device_hosts_names.add(device["name"])
for entry in ha_entity_reg_list: for entry in ha_entity_reg_list:
if ( if entry.original_name is None:
not _cleanup_entity_filter(entry)
or entry.unique_id.split("_")[0] in device_hosts_macs
):
continue continue
_LOGGER.info("Removing entity: %s", entry.name or entry.original_name) entry_name = entry.name or entry.original_name
entry_host = entry_name.split(" ")[0]
entry_mac = entry.unique_id.split("_")[0]
if not _cleanup_entity_filter(entry) or (
entry_mac in device_hosts_macs and entry_host in device_hosts_names
):
_LOGGER.debug(
"Skipping entity %s [mac=%s, host=%s]",
entry_name,
entry_mac,
entry_host,
)
continue
_LOGGER.info("Removing entity: %s", entry_name)
entity_reg.async_remove(entry.entity_id) entity_reg.async_remove(entry.entity_id)
entities_removed = True entities_removed = True

View File

@ -10,6 +10,7 @@ from fritzconnection.core.exceptions import (
FritzActionError, FritzActionError,
FritzActionFailedError, FritzActionFailedError,
FritzConnectionException, FritzConnectionException,
FritzLookUpError,
FritzSecurityError, FritzSecurityError,
FritzServiceError, FritzServiceError,
) )
@ -90,10 +91,16 @@ def service_call_action(
exc_info=True, exc_info=True,
) )
return None return None
except (FritzActionError, FritzActionFailedError, FritzServiceError): except (
FritzActionError,
FritzActionFailedError,
FritzServiceError,
FritzLookUpError,
):
_LOGGER.error( _LOGGER.error(
"Service/Action Error: cannot execute service %s", "Service/Action Error: cannot execute service %s with action %s",
service_name, service_name,
action_name,
exc_info=True, exc_info=True,
) )
return None return None