From 3e9b57cc07c81bec24530aab4dfc3792ddc9d289 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 27 Jun 2024 09:26:31 +0200 Subject: [PATCH] Don't allow updating a device to have no connections or identifiers (#120603) * Don't allow updating a device to have no connections or identifiers * Move check to the top of the function --- homeassistant/helpers/device_registry.py | 5 +++++ tests/helpers/test_device_registry.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index cfafa63ec3a..4579739f0e1 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -869,6 +869,11 @@ class DeviceRegistry(BaseRegistry[dict[str, list[dict[str, Any]]]]): ) add_config_entry = config_entry + if not new_connections and not new_identifiers: + raise HomeAssistantError( + "A device must have at least one of identifiers or connections" + ) + if merge_connections is not UNDEFINED and new_connections is not UNDEFINED: raise HomeAssistantError( "Cannot define both merge_connections and new_connections" diff --git a/tests/helpers/test_device_registry.py b/tests/helpers/test_device_registry.py index fa57cc7557e..3a525f00870 100644 --- a/tests/helpers/test_device_registry.py +++ b/tests/helpers/test_device_registry.py @@ -3052,3 +3052,22 @@ async def test_primary_config_entry( model="model", ) assert device.primary_config_entry == mock_config_entry_1.entry_id + + +async def test_update_device_no_connections_or_identifiers( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, +) -> None: + """Test updating a device clearing connections and identifiers.""" + mock_config_entry = MockConfigEntry(domain="mqtt", title=None) + mock_config_entry.add_to_hass(hass) + + device = device_registry.async_get_or_create( + config_entry_id=mock_config_entry.entry_id, + connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, + identifiers={("bridgeid", "0123")}, + ) + with pytest.raises(HomeAssistantError): + device_registry.async_update_device( + device.id, new_connections=set(), new_identifiers=set() + )