diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index 31da40134a5..504448b948d 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -2,6 +2,8 @@ import logging import uuid +from collections import OrderedDict + import attr from homeassistant.core import callback @@ -45,7 +47,7 @@ class DeviceRegistry: @callback def async_get_device(self, identifiers: str, connections: tuple): """Check if device is registered.""" - for device in self.devices: + for device in self.devices.values(): if any(iden in device.identifiers for iden in identifiers) or \ any(conn in device.connections for conn in connections): return device @@ -75,7 +77,7 @@ class DeviceRegistry: name=name, sw_version=sw_version ) - self.devices.append(device) + self.devices[device.id] = device self.async_schedule_save() @@ -86,10 +88,10 @@ class DeviceRegistry: devices = await self._store.async_load() if devices is None: - self.devices = [] + self.devices = OrderedDict() return - self.devices = [DeviceEntry( + self.devices = {device['id']: DeviceEntry( config_entries=device['config_entries'], connections={tuple(conn) for conn in device['connections']}, identifiers={tuple(iden) for iden in device['identifiers']}, @@ -98,7 +100,7 @@ class DeviceRegistry: name=device['name'], sw_version=device['sw_version'], id=device['id'], - ) for device in devices['devices']] + ) for device in devices['devices']} @callback def async_schedule_save(self): @@ -120,7 +122,7 @@ class DeviceRegistry: 'name': entry.name, 'sw_version': entry.sw_version, 'id': entry.id, - } for entry in self.devices + } for entry in self.devices.values() ] return data diff --git a/tests/helpers/test_device_registry.py b/tests/helpers/test_device_registry.py index b2e73071823..84ad54f7b82 100644 --- a/tests/helpers/test_device_registry.py +++ b/tests/helpers/test_device_registry.py @@ -1,13 +1,15 @@ """Tests for the Device Registry.""" import pytest +from collections import OrderedDict + from homeassistant.helpers import device_registry def mock_registry(hass, mock_entries=None): """Mock the Device Registry.""" registry = device_registry.DeviceRegistry(hass) - registry.devices = mock_entries or [] + registry.devices = mock_entries or OrderedDict() async def _get_reg(): return registry