From c265d3f3ccf464c32154219369453974f13b5cd3 Mon Sep 17 00:00:00 2001 From: mkmer Date: Thu, 14 Sep 2023 00:22:28 -0400 Subject: [PATCH] Late review for honeywell (#100299) * Late review for honeywell * Actually test same id different domain * Update homeassistant/components/honeywell/climate.py Co-authored-by: Martin Hjelmare * Update climate.py * Refactor dont_remove --------- Co-authored-by: Martin Hjelmare --- homeassistant/components/honeywell/climate.py | 11 ++++++++--- tests/components/honeywell/test_init.py | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/honeywell/climate.py b/homeassistant/components/honeywell/climate.py index c285ab83bd1..63d05135d5d 100644 --- a/homeassistant/components/honeywell/climate.py +++ b/homeassistant/components/honeywell/climate.py @@ -111,18 +111,23 @@ def remove_stale_devices( device_entries = dr.async_entries_for_config_entry( device_registry, config_entry.entry_id ) - all_device_ids: list = [] + all_device_ids: set = set() for device in devices.values(): - all_device_ids.append(device.deviceid) + all_device_ids.add(device.deviceid) for device_entry in device_entries: device_id: str | None = None + remove = True for identifier in device_entry.identifiers: + if identifier[0] != DOMAIN: + remove = False + continue + device_id = identifier[1] break - if device_id is None or device_id not in all_device_ids: + if remove and (device_id is None or device_id not in all_device_ids): # If device_id is None an invalid device entry was found for this config entry. # If the device_id is not in existing device ids it's a stale device entry. # Remove config entry from this device entry in either case. diff --git a/tests/components/honeywell/test_init.py b/tests/components/honeywell/test_init.py index e5afe311295..73dda8ed223 100644 --- a/tests/components/honeywell/test_init.py +++ b/tests/components/honeywell/test_init.py @@ -130,7 +130,15 @@ async def test_remove_stale_device( ) -> None: """Test that the stale device is removed.""" location.devices_by_id[another_device.deviceid] = another_device + config_entry.add_to_hass(hass) + + device_registry = dr.async_get(hass) + device_registry.async_get_or_create( + config_entry_id=config_entry.entry_id, + identifiers={("OtherDomain", 7654321)}, + ) + await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() assert config_entry.state is ConfigEntryState.LOADED @@ -142,9 +150,12 @@ async def test_remove_stale_device( device_entry = dr.async_entries_for_config_entry( device_registry, config_entry.entry_id ) - assert len(device_entry) == 2 + assert len(device_entry) == 3 assert any((DOMAIN, 1234567) in device.identifiers for device in device_entry) assert any((DOMAIN, 7654321) in device.identifiers for device in device_entry) + assert any( + ("OtherDomain", 7654321) in device.identifiers for device in device_entry + ) assert await config_entry.async_unload(hass) await hass.async_block_till_done() @@ -162,5 +173,8 @@ async def test_remove_stale_device( device_entry = dr.async_entries_for_config_entry( device_registry, config_entry.entry_id ) - assert len(device_entry) == 1 + assert len(device_entry) == 2 assert any((DOMAIN, 1234567) in device.identifiers for device in device_entry) + assert any( + ("OtherDomain", 7654321) in device.identifiers for device in device_entry + )