Correct logic in honeywell for cleaning up stale devices (#106018)
* code quality fixes * remove unnecessary code * Remove comment * change config entry configuration order * update based on aladdin connect prpull/106035/head
parent
09a0ace671
commit
061c144fe8
|
@ -118,23 +118,17 @@ def remove_stale_devices(
|
|||
device_entries = dr.async_entries_for_config_entry(
|
||||
device_registry, config_entry.entry_id
|
||||
)
|
||||
all_device_ids: set = set()
|
||||
for device in devices.values():
|
||||
all_device_ids.add(device.deviceid)
|
||||
all_device_ids = {device.deviceid for device in devices.values()}
|
||||
|
||||
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
|
||||
if identifier[0] == DOMAIN:
|
||||
device_id = identifier[1]
|
||||
break
|
||||
|
||||
device_id = identifier[1]
|
||||
break
|
||||
|
||||
if remove and (device_id is None or device_id not in all_device_ids):
|
||||
if 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.
|
||||
|
|
|
@ -132,28 +132,51 @@ async def test_remove_stale_device(
|
|||
"""Test that the stale device is removed."""
|
||||
location.devices_by_id[another_device.deviceid] = another_device
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
config_entry_other = MockConfigEntry(
|
||||
domain="OtherDomain",
|
||||
data={},
|
||||
unique_id="unique_id",
|
||||
)
|
||||
config_entry_other.add_to_hass(hass)
|
||||
device_entry_other = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry_other.entry_id,
|
||||
identifiers={("OtherDomain", 7654321)},
|
||||
)
|
||||
|
||||
device_registry.async_update_device(
|
||||
device_entry_other.id,
|
||||
add_config_entry_id=config_entry.entry_id,
|
||||
merge_identifiers={(DOMAIN, 7654321)},
|
||||
)
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
assert (
|
||||
hass.states.async_entity_ids_count() == 6
|
||||
) # 2 climate entities; 4 sensor entities
|
||||
assert hass.states.async_entity_ids_count() == 6
|
||||
|
||||
device_entry = dr.async_entries_for_config_entry(
|
||||
device_entries = dr.async_entries_for_config_entry(
|
||||
device_registry, config_entry.entry_id
|
||||
)
|
||||
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)
|
||||
|
||||
device_entries_other = dr.async_entries_for_config_entry(
|
||||
device_registry, config_entry_other.entry_id
|
||||
)
|
||||
|
||||
assert len(device_entries) == 2
|
||||
assert any((DOMAIN, 1234567) in device.identifiers for device in device_entries)
|
||||
assert any((DOMAIN, 7654321) in device.identifiers for device in device_entries)
|
||||
assert any(
|
||||
("OtherDomain", 7654321) in device.identifiers for device in device_entry
|
||||
("OtherDomain", 7654321) in device.identifiers for device in device_entries
|
||||
)
|
||||
assert len(device_entries_other) == 1
|
||||
assert any(
|
||||
("OtherDomain", 7654321) in device.identifiers
|
||||
for device in device_entries_other
|
||||
)
|
||||
assert any(
|
||||
(DOMAIN, 7654321) in device.identifiers for device in device_entries_other
|
||||
)
|
||||
|
||||
assert await config_entry.async_unload(hass)
|
||||
|
@ -169,11 +192,21 @@ async def test_remove_stale_device(
|
|||
hass.states.async_entity_ids_count() == 3
|
||||
) # 1 climate entities; 2 sensor entities
|
||||
|
||||
device_entry = dr.async_entries_for_config_entry(
|
||||
device_entries = dr.async_entries_for_config_entry(
|
||||
device_registry, config_entry.entry_id
|
||||
)
|
||||
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
|
||||
assert len(device_entries) == 1
|
||||
assert any((DOMAIN, 1234567) in device.identifiers for device in device_entries)
|
||||
assert not any((DOMAIN, 7654321) in device.identifiers for device in device_entries)
|
||||
assert not any(
|
||||
("OtherDomain", 7654321) in device.identifiers for device in device_entries
|
||||
)
|
||||
|
||||
device_entries_other = dr.async_entries_for_config_entry(
|
||||
device_registry, config_entry_other.entry_id
|
||||
)
|
||||
assert len(device_entries_other) == 1
|
||||
assert any(
|
||||
("OtherDomain", 7654321) in device.identifiers
|
||||
for device in device_entries_other
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue