mirror of
https://github.com/home-assistant/core.git
synced 2025-04-28 03:07:50 +00:00
Fix device_registry cleanup behavior (#35977)
* Fix: Only decives which are not referenced by an entity or a config_entry are removed * Adapted test for async_cleanup * Changed variable names
This commit is contained in:
parent
f1b91b050c
commit
919f3243de
@ -399,17 +399,25 @@ def async_cleanup(
|
|||||||
ent_reg: "entity_registry.EntityRegistry",
|
ent_reg: "entity_registry.EntityRegistry",
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Clean up device registry."""
|
"""Clean up device registry."""
|
||||||
# Find all devices that are no longer referenced in the entity registry.
|
# Find all devices that are referenced by a config_entry.
|
||||||
referenced = {entry.device_id for entry in ent_reg.entities.values()}
|
config_entry_ids = {entry.entry_id for entry in hass.config_entries.async_entries()}
|
||||||
orphan = set(dev_reg.devices) - referenced
|
references_config_entries = {
|
||||||
|
device.id
|
||||||
|
for device in dev_reg.devices.values()
|
||||||
|
for config_entry_id in device.config_entries
|
||||||
|
if config_entry_id in config_entry_ids
|
||||||
|
}
|
||||||
|
|
||||||
|
# Find all devices that are referenced in the entity registry.
|
||||||
|
references_entities = {entry.device_id for entry in ent_reg.entities.values()}
|
||||||
|
|
||||||
|
orphan = set(dev_reg.devices) - references_entities - references_config_entries
|
||||||
|
|
||||||
for dev_id in orphan:
|
for dev_id in orphan:
|
||||||
dev_reg.async_remove_device(dev_id)
|
dev_reg.async_remove_device(dev_id)
|
||||||
|
|
||||||
# Find all referenced config entries that no longer exist
|
# Find all referenced config entries that no longer exist
|
||||||
# This shouldn't happen but have not been able to track down the bug :(
|
# This shouldn't happen but have not been able to track down the bug :(
|
||||||
config_entry_ids = {entry.entry_id for entry in hass.config_entries.async_entries()}
|
|
||||||
|
|
||||||
for device in list(dev_reg.devices.values()):
|
for device in list(dev_reg.devices.values()):
|
||||||
for config_entry_id in device.config_entries:
|
for config_entry_id in device.config_entries:
|
||||||
if config_entry_id not in config_entry_ids:
|
if config_entry_id not in config_entry_ids:
|
||||||
|
@ -539,7 +539,7 @@ async def test_cleanup_device_registry(hass, registry):
|
|||||||
device_registry.async_cleanup(hass, registry, ent_reg)
|
device_registry.async_cleanup(hass, registry, ent_reg)
|
||||||
|
|
||||||
assert registry.async_get_device({("hue", "d1")}, set()) is not None
|
assert registry.async_get_device({("hue", "d1")}, set()) is not None
|
||||||
assert registry.async_get_device({("hue", "d2")}, set()) is None
|
assert registry.async_get_device({("hue", "d2")}, set()) is not None
|
||||||
assert registry.async_get_device({("hue", "d3")}, set()) is not None
|
assert registry.async_get_device({("hue", "d3")}, set()) is not None
|
||||||
assert registry.async_get_device({("something", "d4")}, set()) is None
|
assert registry.async_get_device({("something", "d4")}, set()) is None
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user