Fix device reg considered changed (#17764)

* Fix device reg considered changed

* Better syntax
This commit is contained in:
Paulus Schoutsen 2018-10-25 16:43:11 +02:00 committed by Pascal Vizeli
parent b6e8cafdea
commit b5284aa445
2 changed files with 24 additions and 3 deletions

View File

@ -87,8 +87,8 @@ class DeviceRegistry:
device.id,
add_config_entry_id=config_entry_id,
hub_device_id=hub_device_id,
merge_connections=connections,
merge_identifiers=identifiers,
merge_connections=connections or _UNDEF,
merge_identifiers=identifiers or _UNDEF,
manufacturer=manufacturer,
model=model,
name=name,
@ -128,7 +128,8 @@ class DeviceRegistry:
('identifiers', merge_identifiers),
):
old_value = getattr(old, attr_name)
if value is not _UNDEF and value != old_value:
# If not undefined, check if `value` contains new items.
if value is not _UNDEF and not value.issubset(old_value):
changes[attr_name] = old_value | value
for attr_name, value in (

View File

@ -1,4 +1,6 @@
"""Tests for the Device Registry."""
from unittest.mock import patch
import pytest
from homeassistant.helpers import device_registry
@ -239,3 +241,21 @@ async def test_loading_saving_data(hass, registry):
assert orig_hub == new_hub
assert orig_light == new_light
async def test_no_unnecessary_changes(registry):
"""Make sure we do not consider devices changes."""
entry = registry.async_get_or_create(
config_entry_id='1234',
connections={('ethernet', '12:34:56:78:90:AB:CD:EF')},
identifiers={('hue', '456'), ('bla', '123')},
)
with patch('homeassistant.helpers.device_registry'
'.DeviceRegistry.async_schedule_save') as mock_save:
entry2 = registry.async_get_or_create(
config_entry_id='1234',
identifiers={('hue', '456')},
)
assert entry.id == entry2.id
assert len(mock_save.mock_calls) == 0