From ea2bb2448452639488d7f2b16a69047794b6948d Mon Sep 17 00:00:00 2001 From: Phil Bruckner Date: Tue, 2 Apr 2024 15:23:59 -0500 Subject: [PATCH] Remove old device tracker device cleanup code & test (#114668) --- .../components/device_tracker/config_entry.py | 22 ----- .../device_tracker/test_config_entry.py | 82 ------------------- 2 files changed, 104 deletions(-) diff --git a/homeassistant/components/device_tracker/config_entry.py b/homeassistant/components/device_tracker/config_entry.py index a1c1961dc43..99c152cd449 100644 --- a/homeassistant/components/device_tracker/config_entry.py +++ b/homeassistant/components/device_tracker/config_entry.py @@ -51,28 +51,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ) 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) diff --git a/tests/components/device_tracker/test_config_entry.py b/tests/components/device_tracker/test_config_entry.py index d8236c697c3..6a1731d5a77 100644 --- a/tests/components/device_tracker/test_config_entry.py +++ b/tests/components/device_tracker/test_config_entry.py @@ -581,88 +581,6 @@ def test_base_tracker_entity() -> 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( ("mac_address", "unique_id"), [(TEST_MAC_ADDRESS, f"{TEST_MAC_ADDRESS}_yo1")] )