From 863b63d75e5c312cd3eb6744776f835b8401062c Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 7 Sep 2020 04:08:50 +0200 Subject: [PATCH] Fix handling of device registry defaults (#39688) --- homeassistant/helpers/device_registry.py | 13 +++-- tests/helpers/test_device_registry.py | 61 +++++++++++++++++++++++- 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index 7c990aa397d..1dff2ef9483 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -239,15 +239,14 @@ class DeviceRegistry: device = deleted_device.to_device_entry() self._add_device(device) - else: - if default_manufacturer and not device.manufacturer: - manufacturer = default_manufacturer + if default_manufacturer is not _UNDEF and device.manufacturer is None: + manufacturer = default_manufacturer - if default_model and not device.model: - model = default_model + if default_model is not _UNDEF and device.model is None: + model = default_model - if default_name and not device.name: - name = default_name + if default_name is not _UNDEF and device.name is None: + name = default_name if via_device is not None: via = self.async_get_device({via_device}, set()) diff --git a/tests/helpers/test_device_registry.py b/tests/helpers/test_device_registry.py index 5b671dce134..7c9e8a6e262 100644 --- a/tests/helpers/test_device_registry.py +++ b/tests/helpers/test_device_registry.py @@ -841,8 +841,8 @@ async def test_restore_simple_device(hass, registry, update_events): assert update_events[3]["device_id"] == entry3.id -async def test_get_or_create_sets_default_values(hass, registry): - """Make sure we do not duplicate entries.""" +async def test_get_or_create_empty_then_set_default_values(hass, registry): + """Test creating an entry, then setting default name, model, manufacturer.""" entry = registry.async_get_or_create( identifiers={("bridgeid", "0123")}, config_entry_id="1234" ) @@ -871,3 +871,60 @@ async def test_get_or_create_sets_default_values(hass, registry): assert entry.name == "default name 1" assert entry.model == "default model 1" assert entry.manufacturer == "default manufacturer 1" + + +async def test_get_or_create_empty_then_update(hass, registry): + """Test creating an entry, then setting name, model, manufacturer.""" + entry = registry.async_get_or_create( + identifiers={("bridgeid", "0123")}, config_entry_id="1234" + ) + assert entry.name is None + assert entry.model is None + assert entry.manufacturer is None + + entry = registry.async_get_or_create( + config_entry_id="1234", + identifiers={("bridgeid", "0123")}, + name="name 1", + model="model 1", + manufacturer="manufacturer 1", + ) + assert entry.name == "name 1" + assert entry.model == "model 1" + assert entry.manufacturer == "manufacturer 1" + + entry = registry.async_get_or_create( + config_entry_id="1234", + identifiers={("bridgeid", "0123")}, + default_name="default name 1", + default_model="default model 1", + default_manufacturer="default manufacturer 1", + ) + assert entry.name == "name 1" + assert entry.model == "model 1" + assert entry.manufacturer == "manufacturer 1" + + +async def test_get_or_create_sets_default_values(hass, registry): + """Test creating an entry, then setting default name, model, manufacturer.""" + entry = registry.async_get_or_create( + config_entry_id="1234", + identifiers={("bridgeid", "0123")}, + default_name="default name 1", + default_model="default model 1", + default_manufacturer="default manufacturer 1", + ) + assert entry.name == "default name 1" + assert entry.model == "default model 1" + assert entry.manufacturer == "default manufacturer 1" + + entry = registry.async_get_or_create( + config_entry_id="1234", + identifiers={("bridgeid", "0123")}, + default_name="default name 2", + default_model="default model 2", + default_manufacturer="default manufacturer 2", + ) + assert entry.name == "default name 1" + assert entry.model == "default model 1" + assert entry.manufacturer == "default manufacturer 1"