1
0
mirror of https://github.com/home-assistant/core synced 2024-07-08 20:17:01 +00:00

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
This commit is contained in:
Erik Montnemery 2024-06-27 09:26:31 +02:00 committed by GitHub
parent c9c573dbce
commit 3e9b57cc07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 0 deletions

View File

@ -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"

View File

@ -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()
)