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 pr
This commit is contained in:
mkmer 2023-12-19 01:58:35 -05:00 committed by GitHub
parent 09a0ace671
commit 061c144fe8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 28 deletions

View file

@ -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.