Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions homeassistant/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ def available(self) -> bool:


async def cleanup_device_registry(hass, device_id):
"""Remove device registry entry if there are no entities or triggers."""
"""Remove device registry entry if there are no remaining entities or triggers."""
# Local import to avoid circular dependencies
from . import device_trigger

Expand Down Expand Up @@ -1196,15 +1196,21 @@ async def async_added_to_hass(self) -> None:
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."""
async def _async_remove_state_and_registry_entry(self) -> None:
"""Remove entity's state and entity registry entry.

Remove entity from entity registry if it is registered, this also removes the state.
If the entity is not in the entity registry, just remove the state.
"""
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)
else:
await self.async_remove()

@callback
async def discovery_callback(payload):
Expand All @@ -1216,9 +1222,8 @@ async def discovery_callback(payload):
if not payload:
# Empty payload: Remove component
_LOGGER.info("Removing component: %s", self.entity_id)
self._cleanup_on_remove()
await async_remove_from_registry(self)
await self.async_remove()
self._cleanup_discovery_on_remove()
await _async_remove_state_and_registry_entry(self)
elif self._discovery_update:
# Non-empty payload: Notify component
_LOGGER.info("Updating component: %s", self.entity_id)
Expand Down Expand Up @@ -1246,9 +1251,9 @@ async def async_removed_from_registry(self) -> None:

async def async_will_remove_from_hass(self) -> None:
"""Stop listening to signal and cleanup discovery data.."""
self._cleanup_on_remove()
self._cleanup_discovery_on_remove()

def _cleanup_on_remove(self) -> None:
def _cleanup_discovery_on_remove(self) -> None:
"""Stop listening to signal and cleanup discovery data."""
if self._discovery_data and not self._removed_from_hass:
debug_info.remove_entity_data(self.hass, self.entity_id)
Expand Down
4 changes: 0 additions & 4 deletions tests/ignore_uncaught_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@
"tests.components.mqtt.test_init",
"test_setup_with_tls_config_of_v1_under_python36_only_uses_v1",
),
("tests.components.mqtt.test_light", "test_entity_device_info_remove"),
("tests.components.mqtt.test_light_json", "test_entity_device_info_remove"),
("tests.components.mqtt.test_light_template", "test_entity_device_info_remove"),
("tests.components.mqtt.test_switch", "test_entity_device_info_remove"),
("tests.components.qwikswitch.test_init", "test_binary_sensor_device"),
("tests.components.qwikswitch.test_init", "test_sensor_device"),
("tests.components.rflink.test_init", "test_send_command_invalid_arguments"),
Expand Down