Validate device info string fields in the registry (#170021)

This commit is contained in:
Artur Pragacz
2026-05-12 11:01:04 +02:00
committed by GitHub
parent 99b1e7c229
commit 36cc629faf
8 changed files with 153 additions and 52 deletions
+1 -1
View File
@@ -262,7 +262,7 @@ async def test_device_setup_registry(
assert device_entry.name == device.name
assert device_entry.model == device.model
assert device_entry.manufacturer == device.manufacturer
assert device_entry.sw_version == device.fwversion
assert device_entry.sw_version == str(device.fwversion)
for entry in er.async_entries_for_device(entity_registry, device_entry.id):
assert (
@@ -13,7 +13,7 @@
}),
'disabled_by': None,
'entry_type': None,
'hw_version': 1,
'hw_version': '1',
'id': <ANY>,
'identifiers': set({
}),
@@ -26,7 +26,7 @@
'name_by_user': None,
'primary_config_entry': <ANY>,
'serial_number': '1234567890ab',
'sw_version': 2,
'sw_version': '2',
'via_device_id': None,
})
# ---
@@ -9,7 +9,7 @@
}),
'disabled_by': None,
'entry_type': None,
'hw_version': 111,
'hw_version': '111',
'id': <ANY>,
'identifiers': set({
tuple(
@@ -26,7 +26,7 @@
'name_by_user': None,
'primary_config_entry': <ANY>,
'serial_number': None,
'sw_version': 127,
'sw_version': '127',
'via_device_id': None,
})
# ---
+55
View File
@@ -4914,6 +4914,61 @@ async def test_device_info_configuration_url_validation(
)
@pytest.mark.parametrize(
"field",
[
"hw_version",
"manufacturer",
"model",
"model_id",
"serial_number",
"sw_version",
],
)
@pytest.mark.parametrize(
("value", "stored_value", "expected_log"),
[
(1.0, "1.0", "passes a non-string value of type float as {field}"),
((1, 2), "(1, 2)", "passes a non-string value of type tuple as {field}"),
("hw-1", "hw-1", ""),
(None, None, ""),
],
)
async def test_device_info_string_field_validation(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
caplog: pytest.LogCaptureFixture,
field: str,
value: Any,
stored_value: str | None,
expected_log: str,
) -> None:
"""Test string device info fields are validated and coerced."""
config_entry_1 = MockConfigEntry()
config_entry_1.add_to_hass(hass)
config_entry_2 = MockConfigEntry()
config_entry_2.add_to_hass(hass)
entry = device_registry.async_get_or_create(
config_entry_id=config_entry_1.entry_id,
identifiers={("something", "1234")},
name="name",
**{field: value},
)
assert getattr(entry, field) == stored_value
update_device = device_registry.async_get_or_create(
config_entry_id=config_entry_2.entry_id,
identifiers={("something", "5678")},
name="name",
)
updated = device_registry.async_update_device(update_device.id, **{field: value})
assert updated is not None
assert getattr(updated, field) == stored_value
assert expected_log.format(field=field) in caplog.text
@pytest.mark.parametrize("load_registries", [False])
async def test_loading_invalid_configuration_url_from_storage(
hass: HomeAssistant,