async def test_loading_saving_data(hass, registry): """Test that we load/save data correctly.""" orig_via = registry.async_get_or_create( config_entry_id="123", connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, identifiers={("hue", "0123")}, manufacturer="manufacturer", model="via", ) orig_light = registry.async_get_or_create( config_entry_id="456", connections=set(), identifiers={("hue", "456")}, manufacturer="manufacturer", model="light", via_device=("hue", "0123"), ) assert len(registry.devices) == 2 # Now load written data in new registry registry2 = device_registry.DeviceRegistry(hass) await flush_store(registry._store) await registry2.async_load() # Ensure same order assert list(registry.devices) == list(registry2.devices) new_via = registry2.async_get_device({("hue", "0123")}, set()) new_light = registry2.async_get_device({("hue", "456")}, set()) assert orig_via == new_via assert orig_light == new_light
def mock_device_registry(hass, mock_entries=None): """Mock the Device Registry.""" registry = device_registry.DeviceRegistry(hass) registry.devices = mock_entries or OrderedDict() hass.data[device_registry.DATA_REGISTRY] = registry return registry
async def test_loading_saving_data(hass, registry): """Test that we load/save data correctly.""" orig_hub = registry.async_get_or_create( config_entry_id='123', connections={(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')}, identifiers={('hue', '0123')}, manufacturer='manufacturer', model='hub') orig_light = registry.async_get_or_create(config_entry_id='456', connections=set(), identifiers={('hue', '456')}, manufacturer='manufacturer', model='light', via_hub=('hue', '0123')) assert len(registry.devices) == 2 # Now load written data in new registry registry2 = device_registry.DeviceRegistry(hass) await flush_store(registry._store) await registry2.async_load() # Ensure same order assert list(registry.devices) == list(registry2.devices) new_hub = registry2.async_get_device({('hue', '0123')}, set()) new_light = registry2.async_get_device({('hue', '456')}, set()) assert orig_hub == new_hub assert orig_light == new_light
async def test_loading_saving_data(hass, registry): """Test that we load/save data correctly.""" orig_via = registry.async_get_or_create( config_entry_id="123", connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, identifiers={("hue", "0123")}, manufacturer="manufacturer", model="via", name="Original Name", sw_version="Orig SW 1", entry_type="device", ) orig_light = registry.async_get_or_create( config_entry_id="456", connections=set(), identifiers={("hue", "456")}, manufacturer="manufacturer", model="light", via_device=("hue", "0123"), ) orig_light2 = registry.async_get_or_create( config_entry_id="456", connections=set(), identifiers={("hue", "789")}, manufacturer="manufacturer", model="light", via_device=("hue", "0123"), ) registry.async_remove_device(orig_light2.id) assert len(registry.devices) == 2 assert len(registry.deleted_devices) == 1 orig_via = registry.async_update_device(orig_via.id, area_id="mock-area-id", name_by_user="******") # Now load written data in new registry registry2 = device_registry.DeviceRegistry(hass) await flush_store(registry._store) await registry2.async_load() # Ensure same order assert list(registry.devices) == list(registry2.devices) assert list(registry.deleted_devices) == list(registry2.deleted_devices) new_via = registry2.async_get_device({("hue", "0123")}, set()) new_light = registry2.async_get_device({("hue", "456")}, set()) assert orig_via == new_via assert orig_light == new_light
def mock_device_registry(hass, mock_entries=None): """Mock the Device Registry.""" registry = device_registry.DeviceRegistry(hass) registry.devices = mock_entries or OrderedDict() async def _get_reg(): return registry hass.data[device_registry.DATA_REGISTRY] = \ hass.loop.create_task(_get_reg()) return registry
async def test_loading_saving_data(hass, registry, area_registry): """Test that we load/save data correctly.""" orig_via = registry.async_get_or_create( config_entry_id="123", connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, identifiers={("hue", "0123")}, manufacturer="manufacturer", model="via", name="Original Name", sw_version="Orig SW 1", entry_type="device", ) orig_light = registry.async_get_or_create( config_entry_id="456", connections=set(), identifiers={("hue", "456")}, manufacturer="manufacturer", model="light", via_device=("hue", "0123"), disabled_by="user", ) orig_light2 = registry.async_get_or_create( config_entry_id="456", connections=set(), identifiers={("hue", "789")}, manufacturer="manufacturer", model="light", via_device=("hue", "0123"), ) registry.async_remove_device(orig_light2.id) orig_light3 = registry.async_get_or_create( config_entry_id="789", connections={(device_registry.CONNECTION_NETWORK_MAC, "34:56:AB:CD:EF:12")}, identifiers={("hue", "abc")}, manufacturer="manufacturer", model="light", ) registry.async_get_or_create( config_entry_id="abc", connections={(device_registry.CONNECTION_NETWORK_MAC, "34:56:AB:CD:EF:12")}, identifiers={("abc", "123")}, manufacturer="manufacturer", model="light", ) registry.async_remove_device(orig_light3.id) orig_light4 = registry.async_get_or_create( config_entry_id="789", connections={(device_registry.CONNECTION_NETWORK_MAC, "34:56:AB:CD:EF:12")}, identifiers={("hue", "abc")}, manufacturer="manufacturer", model="light", ) assert orig_light4.id == orig_light3.id orig_kitchen_light = registry.async_get_or_create( config_entry_id="999", connections=set(), identifiers={("hue", "999")}, manufacturer="manufacturer", model="light", via_device=("hue", "0123"), disabled_by="user", suggested_area="Kitchen", ) assert len(registry.devices) == 4 assert len(registry.deleted_devices) == 1 orig_via = registry.async_update_device(orig_via.id, area_id="mock-area-id", name_by_user="******") # Now load written data in new registry registry2 = device_registry.DeviceRegistry(hass) await flush_store(registry._store) await registry2.async_load() # Ensure same order assert list(registry.devices) == list(registry2.devices) assert list(registry.deleted_devices) == list(registry2.deleted_devices) new_via = registry2.async_get_device({("hue", "0123")}) new_light = registry2.async_get_device({("hue", "456")}) new_light4 = registry2.async_get_device({("hue", "abc")}) assert orig_via == new_via assert orig_light == new_light assert orig_light4 == new_light4 # Ensure a save/load cycle does not keep suggested area new_kitchen_light = registry2.async_get_device({("hue", "999")}) assert orig_kitchen_light.suggested_area == "Kitchen" orig_kitchen_light_witout_suggested_area = registry.async_update_device( orig_kitchen_light.id, suggested_area=None) assert orig_kitchen_light_witout_suggested_area.suggested_area is None assert orig_kitchen_light_witout_suggested_area == new_kitchen_light