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 functionpull/120643/head
parent
c9c573dbce
commit
3e9b57cc07
|
@ -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"
|
||||
|
|
|
@ -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()
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue