Simplify unifi cleanup logic (#65345)

This commit is contained in:
Paulus Schoutsen 2022-01-31 22:22:12 -08:00 committed by GitHub
parent 88ed2f3b3e
commit b05b4c4b38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 69 deletions

View file

@ -3,7 +3,7 @@ import logging
from typing import Any
from homeassistant.core import callback
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
@ -78,34 +78,14 @@ class UniFiBase(Entity):
raise NotImplementedError
async def remove_item(self, keys: set) -> None:
"""Remove entity if key is part of set.
Remove entity if no entry in entity registry exist.
Remove entity registry entry if no entry in device registry exist.
Remove device registry entry if there is only one linked entity (this entity).
Remove config entry reference from device registry entry if there is more than one config entry.
Remove entity registry entry if there are more than one entity linked to the device registry entry.
"""
"""Remove entity if key is part of set."""
if self.key not in keys:
return
entity_registry = er.async_get(self.hass)
entity_entry = entity_registry.async_get(self.entity_id)
if not entity_entry:
if self.registry_entry:
er.async_get(self.hass).async_remove(self.entity_id)
else:
await self.async_remove(force_remove=True)
return
device_registry = dr.async_get(self.hass)
device_entry = device_registry.async_get(entity_entry.device_id)
if not device_entry:
entity_registry.async_remove(self.entity_id)
return
device_registry.async_update_device(
entity_entry.device_id,
remove_config_entry_id=self.controller.config_entry.entry_id,
)
entity_registry.async_remove(self.entity_id)
@property
def should_poll(self) -> bool: