mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Remove old device tracker device cleanup code & test (#114668)
This commit is contained in:
parent
2175cd6039
commit
ea2bb24484
@ -51,28 +51,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
)
|
)
|
||||||
component.register_shutdown()
|
component.register_shutdown()
|
||||||
|
|
||||||
# Clean up old devices created by device tracker entities in the past.
|
|
||||||
# Can be removed after 2022.6
|
|
||||||
ent_reg = er.async_get(hass)
|
|
||||||
dev_reg = dr.async_get(hass)
|
|
||||||
|
|
||||||
devices_with_trackers = set()
|
|
||||||
devices_with_non_trackers = set()
|
|
||||||
|
|
||||||
for entity in ent_reg.entities.values():
|
|
||||||
if entity.device_id is None:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if entity.domain == DOMAIN:
|
|
||||||
devices_with_trackers.add(entity.device_id)
|
|
||||||
else:
|
|
||||||
devices_with_non_trackers.add(entity.device_id)
|
|
||||||
|
|
||||||
for device_id in devices_with_trackers - devices_with_non_trackers:
|
|
||||||
for entity in er.async_entries_for_device(ent_reg, device_id, True):
|
|
||||||
ent_reg.async_update_entity(entity.entity_id, device_id=None)
|
|
||||||
dev_reg.async_remove_device(device_id)
|
|
||||||
|
|
||||||
return await component.async_setup_entry(entry)
|
return await component.async_setup_entry(entry)
|
||||||
|
|
||||||
|
|
||||||
|
@ -581,88 +581,6 @@ def test_base_tracker_entity() -> None:
|
|||||||
assert entity.state_attributes is None
|
assert entity.state_attributes is None
|
||||||
|
|
||||||
|
|
||||||
async def test_cleanup_legacy(
|
|
||||||
hass: HomeAssistant,
|
|
||||||
config_entry: MockConfigEntry,
|
|
||||||
entity_registry: er.EntityRegistry,
|
|
||||||
device_registry: dr.DeviceRegistry,
|
|
||||||
) -> None:
|
|
||||||
"""Test we clean up devices created by old device tracker."""
|
|
||||||
device_entry_1 = device_registry.async_get_or_create(
|
|
||||||
config_entry_id=config_entry.entry_id, identifiers={(DOMAIN, "device1")}
|
|
||||||
)
|
|
||||||
device_entry_2 = device_registry.async_get_or_create(
|
|
||||||
config_entry_id=config_entry.entry_id, identifiers={(DOMAIN, "device2")}
|
|
||||||
)
|
|
||||||
device_entry_3 = device_registry.async_get_or_create(
|
|
||||||
config_entry_id=config_entry.entry_id, identifiers={(DOMAIN, "device3")}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Device with light + device tracker entity
|
|
||||||
entity_entry_1a = entity_registry.async_get_or_create(
|
|
||||||
DOMAIN,
|
|
||||||
"test",
|
|
||||||
"entity1a-unique",
|
|
||||||
config_entry=config_entry,
|
|
||||||
device_id=device_entry_1.id,
|
|
||||||
)
|
|
||||||
entity_entry_1b = entity_registry.async_get_or_create(
|
|
||||||
"light",
|
|
||||||
"test",
|
|
||||||
"entity1b-unique",
|
|
||||||
config_entry=config_entry,
|
|
||||||
device_id=device_entry_1.id,
|
|
||||||
)
|
|
||||||
# Just device tracker entity
|
|
||||||
entity_entry_2a = entity_registry.async_get_or_create(
|
|
||||||
DOMAIN,
|
|
||||||
"test",
|
|
||||||
"entity2a-unique",
|
|
||||||
config_entry=config_entry,
|
|
||||||
device_id=device_entry_2.id,
|
|
||||||
)
|
|
||||||
# Device with no device tracker entities
|
|
||||||
entity_entry_3a = entity_registry.async_get_or_create(
|
|
||||||
"light",
|
|
||||||
"test",
|
|
||||||
"entity3a-unique",
|
|
||||||
config_entry=config_entry,
|
|
||||||
device_id=device_entry_3.id,
|
|
||||||
)
|
|
||||||
# Device tracker but no device
|
|
||||||
entity_entry_4a = entity_registry.async_get_or_create(
|
|
||||||
DOMAIN,
|
|
||||||
"test",
|
|
||||||
"entity4a-unique",
|
|
||||||
config_entry=config_entry,
|
|
||||||
)
|
|
||||||
# Completely different entity
|
|
||||||
entity_entry_5a = entity_registry.async_get_or_create(
|
|
||||||
"light",
|
|
||||||
"test",
|
|
||||||
"entity4a-unique",
|
|
||||||
config_entry=config_entry,
|
|
||||||
)
|
|
||||||
|
|
||||||
await create_mock_platform(hass, config_entry, [])
|
|
||||||
|
|
||||||
for entity_entry in (
|
|
||||||
entity_entry_1a,
|
|
||||||
entity_entry_1b,
|
|
||||||
entity_entry_3a,
|
|
||||||
entity_entry_4a,
|
|
||||||
entity_entry_5a,
|
|
||||||
):
|
|
||||||
assert entity_registry.async_get(entity_entry.entity_id) is not None
|
|
||||||
|
|
||||||
entity_entry = entity_registry.async_get(entity_entry_2a.entity_id)
|
|
||||||
assert entity_entry is not None
|
|
||||||
# We've removed device so device ID cleared
|
|
||||||
assert entity_entry.device_id is None
|
|
||||||
# Removed because only had device tracker entity
|
|
||||||
assert device_registry.async_get(device_entry_2.id) is None
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("mac_address", "unique_id"), [(TEST_MAC_ADDRESS, f"{TEST_MAC_ADDRESS}_yo1")]
|
("mac_address", "unique_id"), [(TEST_MAC_ADDRESS, f"{TEST_MAC_ADDRESS}_yo1")]
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user