Remove old Keenetic NDMS2 entities when some interfaces are unselected (#47311)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Andrey Kupreychik 2021-05-26 15:26:23 +07:00 committed by GitHub
parent c6f108f7c3
commit 5ee0df29d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 3 deletions

View File

@ -1,9 +1,14 @@
"""The keenetic_ndms2 component."""
from __future__ import annotations
from homeassistant.components import binary_sensor, device_tracker
import logging
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER_DOMAIN
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_SCAN_INTERVAL
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry, entity_registry
from .const import (
CONF_CONSIDER_HOME,
@ -20,7 +25,8 @@ from .const import (
)
from .router import KeeneticRouter
PLATFORMS = [device_tracker.DOMAIN, binary_sensor.DOMAIN]
PLATFORMS = [BINARY_SENSOR_DOMAIN, DEVICE_TRACKER_DOMAIN]
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
@ -57,6 +63,37 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
hass.data[DOMAIN].pop(config_entry.entry_id)
new_tracked_interfaces: set[str] = set(config_entry.options[CONF_INTERFACES])
if router.tracked_interfaces - new_tracked_interfaces:
_LOGGER.debug(
"Cleaning device_tracker entities since some interfaces are now untracked:"
)
ent_reg = entity_registry.async_get(hass)
dev_reg = device_registry.async_get(hass)
# We keep devices currently connected to new_tracked_interfaces
keep_devices: set[str] = {
mac
for mac, device in router.last_devices.items()
if device.interface in new_tracked_interfaces
}
for entity_entry in list(ent_reg.entities.values()):
if (
entity_entry.config_entry_id == config_entry.entry_id
and entity_entry.domain == DEVICE_TRACKER_DOMAIN
):
mac = entity_entry.unique_id.partition("_")[0]
if mac not in keep_devices:
_LOGGER.debug("Removing entity %s", entity_entry.entity_id)
ent_reg.async_remove(entity_entry.entity_id)
dev_reg.async_update_device(
entity_entry.device_id,
remove_config_entry_id=config_entry.entry_id,
)
_LOGGER.debug("Finished cleaning device_tracker entities")
return unload_ok

View File

@ -48,6 +48,7 @@ class KeeneticRouter:
self._cancel_periodic_update: Callable | None = None
self._available = False
self._progress = None
self._tracked_interfaces = set(config_entry.options[CONF_INTERFACES])
@property
def client(self):
@ -105,6 +106,11 @@ class KeeneticRouter:
"""Config entry option defining number of seconds from last seen to away."""
return timedelta(seconds=self.config_entry.options[CONF_CONSIDER_HOME])
@property
def tracked_interfaces(self):
"""Tracked interfaces."""
return self._tracked_interfaces
@property
def signal_update(self):
"""Event specific per router entry to signal updates."""
@ -178,7 +184,7 @@ class KeeneticRouter:
self._last_devices = {
dev.mac: dev
for dev in _response
if dev.interface in self.config_entry.options[CONF_INTERFACES]
if dev.interface in self._tracked_interfaces
}
_LOGGER.debug("Successfully fetched data from router: %s", str(_response))
self._router_info = self._client.get_router_info()