Cleanup entity and device registry on MQTT discovery removal (#32693)
* Cleanup entity and device registry on MQTT discovery removal. * Review comments
This commit is contained in:
1 parent
5f5cb8bea8
commit
8bc542776b
17 files changed
+466
-17
No files matched your search
@@ -37,7 +37,6 @@ from homeassistant.exceptions import (
|
||||
Unauthorized,
|
||||
)
|
||||
from homeassistant.helpers import config_validation as cv, event, template
|
||||
from homeassistant.helpers.device_registry import async_get_registry as get_dev_reg
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.typing import ConfigType, HomeAssistantType, ServiceDataType
|
||||
@@ -1157,6 +1156,23 @@ class MqttAvailability(Entity):
|
||||
return availability_topic is None or self._available
|
||||
|
||||
|
||||
async def cleanup_device_registry(hass, device_id):
|
||||
"""Remove device registry entry if there are no entities or triggers."""
|
||||
# Local import to avoid circular dependencies
|
||||
from . import device_trigger
|
||||
|
||||
device_registry = await hass.helpers.device_registry.async_get_registry()
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
if (
|
||||
device_id
|
||||
and not hass.helpers.entity_registry.async_entries_for_device(
|
||||
entity_registry, device_id
|
||||
)
|
||||
and not await device_trigger.async_get_triggers(hass, device_id)
|
||||
):
|
||||
device_registry.async_remove_device(device_id)
|
||||
|
||||
|
||||
class MqttDiscoveryUpdate(Entity):
|
||||
"""Mixin used to handle updated discovery message."""
|
||||
|
||||
@@ -1173,8 +1189,18 @@ class MqttDiscoveryUpdate(Entity):
|
||||
self._discovery_data[ATTR_DISCOVERY_HASH] if self._discovery_data else None
|
||||
)
|
||||
|
||||
async def async_remove_from_registry(self) -> None:
|
||||
"""Remove entity from entity registry."""
|
||||
entity_registry = (
|
||||
await self.hass.helpers.entity_registry.async_get_registry()
|
||||
)
|
||||
if entity_registry.async_is_registered(self.entity_id):
|
||||
entity_entry = entity_registry.async_get(self.entity_id)
|
||||
entity_registry.async_remove(self.entity_id)
|
||||
await cleanup_device_registry(self.hass, entity_entry.device_id)
|
||||
|
||||
@callback
|
||||
def discovery_callback(payload):
|
||||
async def discovery_callback(payload):
|
||||
"""Handle discovery update."""
|
||||
_LOGGER.info(
|
||||
"Got update for entity with hash: %s '%s'", discovery_hash, payload,
|
||||
@@ -1182,13 +1208,13 @@ class MqttDiscoveryUpdate(Entity):
|
||||
if not payload:
|
||||
# Empty payload: Remove component
|
||||
_LOGGER.info("Removing component: %s", self.entity_id)
|
||||
self.hass.async_create_task(self.async_remove())
|
||||
clear_discovery_hash(self.hass, discovery_hash)
|
||||
self._remove_signal()
|
||||
self._cleanup_on_remove()
|
||||
await async_remove_from_registry(self)
|
||||
await self.async_remove()
|
||||
elif self._discovery_update:
|
||||
# Non-empty payload: Notify component
|
||||
_LOGGER.info("Updating component: %s", self.entity_id)
|
||||
self.hass.async_create_task(self._discovery_update(payload))
|
||||
await self._discovery_update(payload)
|
||||
|
||||
if discovery_hash:
|
||||
self._remove_signal = async_dispatcher_connect(
|
||||
@@ -1199,15 +1225,25 @@ class MqttDiscoveryUpdate(Entity):
|
||||
|
||||
async def async_removed_from_registry(self) -> None:
|
||||
"""Clear retained discovery topic in broker."""
|
||||
discovery_topic = self._discovery_data[ATTR_DISCOVERY_TOPIC]
|
||||
publish(
|
||||
self.hass, discovery_topic, "", retain=True,
|
||||
)
|
||||
if self._discovery_data:
|
||||
discovery_topic = self._discovery_data[ATTR_DISCOVERY_TOPIC]
|
||||
publish(
|
||||
self.hass, discovery_topic, "", retain=True,
|
||||
)
|
||||
|
||||
async def async_will_remove_from_hass(self) -> None:
|
||||
"""Stop listening to signal."""
|
||||
"""Stop listening to signal and cleanup discovery data.."""
|
||||
self._cleanup_on_remove()
|
||||
|
||||
def _cleanup_on_remove(self) -> None:
|
||||
"""Stop listening to signal and cleanup discovery data."""
|
||||
if self._discovery_data:
|
||||
clear_discovery_hash(self.hass, self._discovery_data[ATTR_DISCOVERY_HASH])
|
||||
self._discovery_data = None
|
||||
|
||||
if self._remove_signal:
|
||||
self._remove_signal()
|
||||
self._remove_signal = None
|
||||
|
||||
|
||||
def device_info_from_config(config):
|
||||
@@ -1270,7 +1306,7 @@ class MqttEntityDeviceInfo(Entity):
|
||||
async def websocket_remove_device(hass, connection, msg):
|
||||
"""Delete device."""
|
||||
device_id = msg["device_id"]
|
||||
dev_registry = await get_dev_reg(hass)
|
||||
dev_registry = await hass.helpers.device_registry.async_get_registry()
|
||||
|
||||
device = dev_registry.async_get(device_id)
|
||||
if not device:
|
||||
|
||||
@@ -25,6 +25,7 @@ from . import (
|
||||
CONF_PAYLOAD,
|
||||
CONF_QOS,
|
||||
DOMAIN,
|
||||
cleanup_device_registry,
|
||||
)
|
||||
from .discovery import MQTT_DISCOVERY_UPDATED, clear_discovery_hash
|
||||
|
||||
@@ -187,6 +188,7 @@ async def async_setup_trigger(hass, config, config_entry, discovery_data):
|
||||
device_trigger.detach_trigger()
|
||||
clear_discovery_hash(hass, discovery_hash)
|
||||
remove_signal()
|
||||
await cleanup_device_registry(hass, device.id)
|
||||
else:
|
||||
# Non-empty payload: Update trigger
|
||||
_LOGGER.info("Updating trigger: %s", discovery_hash)
|
||||
|
||||
Reference in new issue
Block a user