Add core APIs to migrate device identifiers and entity unique_id (#23481)

* Add device identifiers migration

* Add entity unique_id migration

* Update per arch issue

* Move to existing update methods
This commit is contained in:
Andrew Sayre authored and Paulus Schoutsen committed 2019-04-30 10:04:37 -07:00
1 parent 41f0066e76
commit cfaaae661a
4 files changed
+61 -9

No files matched your search

+12 -4
View File
@@ -361,17 +361,25 @@ async def test_update(registry):
config_entry_id='1234',
connections={
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')
})
},
identifiers={('hue', '456'), ('bla', '123')})
new_identifiers = {
('hue', '654'),
('bla', '321')
}
assert not entry.area_id
assert not entry.name_by_user
updated_entry = registry.async_update_device(
entry.id, area_id='12345A', name_by_user='Test Friendly Name')
with patch.object(registry, 'async_schedule_save') as mock_save:
updated_entry = registry.async_update_device(
entry.id, area_id='12345A', name_by_user='Test Friendly Name',
new_identifiers=new_identifiers)
assert mock_save.call_count == 1
assert updated_entry != entry
assert updated_entry.area_id == '12345A'
assert updated_entry.name_by_user == 'Test Friendly Name'
assert updated_entry.identifiers == new_identifiers
async def test_loading_race_condition(hass):
+26
View File
@@ -271,3 +271,29 @@ async def test_loading_race_condition(hass):
mock_load.assert_called_once_with()
assert results[0] == results[1]
async def test_update_entity_unique_id(registry):
"""Test entity's unique_id is updated."""
entry = registry.async_get_or_create(
'light', 'hue', '5678', config_entry_id='mock-id-1')
new_unique_id = '1234'
with patch.object(registry, 'async_schedule_save') as mock_schedule_save:
updated_entry = registry.async_update_entity(
entry.entity_id, new_unique_id=new_unique_id)
assert updated_entry != entry
assert updated_entry.unique_id == new_unique_id
assert mock_schedule_save.call_count == 1
async def test_update_entity_unique_id_conflict(registry):
"""Test migration raises when unique_id already in use."""
entry = registry.async_get_or_create(
'light', 'hue', '5678', config_entry_id='mock-id-1')
entry2 = registry.async_get_or_create(
'light', 'hue', '1234', config_entry_id='mock-id-1')
with patch.object(registry, 'async_schedule_save') as mock_schedule_save, \
pytest.raises(ValueError):
registry.async_update_entity(
entry.entity_id, new_unique_id=entry2.unique_id)
assert mock_schedule_save.call_count == 0