Ensure load the device registry if it contains invalid configuration URLs (#97589)
This commit is contained in:
parent
97e28acfc9
commit
80e0bcfaea
2 changed files with 47 additions and 3 deletions
|
@ -198,9 +198,7 @@ class DeviceEntry:
|
|||
|
||||
area_id: str | None = attr.ib(default=None)
|
||||
config_entries: set[str] = attr.ib(converter=set, factory=set)
|
||||
configuration_url: str | URL | None = attr.ib(
|
||||
converter=_validate_configuration_url, default=None
|
||||
)
|
||||
configuration_url: str | None = attr.ib(default=None)
|
||||
connections: set[tuple[str, str]] = attr.ib(converter=set, factory=set)
|
||||
disabled_by: DeviceEntryDisabler | None = attr.ib(default=None)
|
||||
entry_type: DeviceEntryType | None = attr.ib(default=None)
|
||||
|
@ -482,6 +480,8 @@ class DeviceRegistry:
|
|||
via_device: tuple[str, str] | None | UndefinedType = UNDEFINED,
|
||||
) -> DeviceEntry:
|
||||
"""Get device. Create if it doesn't exist."""
|
||||
if configuration_url is not UNDEFINED:
|
||||
configuration_url = _validate_configuration_url(configuration_url)
|
||||
|
||||
# Reconstruct a DeviceInfo dict from the arguments.
|
||||
# When we upgrade to Python 3.12, we can change this method to instead
|
||||
|
@ -681,6 +681,9 @@ class DeviceRegistry:
|
|||
new_values["identifiers"] = new_identifiers
|
||||
old_values["identifiers"] = old.identifiers
|
||||
|
||||
if configuration_url is not UNDEFINED:
|
||||
configuration_url = _validate_configuration_url(configuration_url)
|
||||
|
||||
for attr_name, value in (
|
||||
("area_id", area_id),
|
||||
("configuration_url", configuration_url),
|
||||
|
|
|
@ -1730,3 +1730,44 @@ async def test_device_info_configuration_url_validation(
|
|||
device_registry.async_update_device(
|
||||
update_device.id, configuration_url=configuration_url
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("load_registries", [False])
|
||||
async def test_loading_invalid_configuration_url_from_storage(
|
||||
hass: HomeAssistant, hass_storage: dict[str, Any]
|
||||
) -> None:
|
||||
"""Test loading stored devices with an invalid URL."""
|
||||
hass_storage[dr.STORAGE_KEY] = {
|
||||
"version": dr.STORAGE_VERSION_MAJOR,
|
||||
"minor_version": dr.STORAGE_VERSION_MINOR,
|
||||
"data": {
|
||||
"devices": [
|
||||
{
|
||||
"area_id": None,
|
||||
"config_entries": ["1234"],
|
||||
"configuration_url": "invalid",
|
||||
"connections": [],
|
||||
"disabled_by": None,
|
||||
"entry_type": dr.DeviceEntryType.SERVICE,
|
||||
"hw_version": None,
|
||||
"id": "abcdefghijklm",
|
||||
"identifiers": [["serial", "12:34:56:AB:CD:EF"]],
|
||||
"manufacturer": None,
|
||||
"model": None,
|
||||
"name_by_user": None,
|
||||
"name": None,
|
||||
"sw_version": None,
|
||||
"via_device_id": None,
|
||||
}
|
||||
],
|
||||
"deleted_devices": [],
|
||||
},
|
||||
}
|
||||
|
||||
await dr.async_load(hass)
|
||||
registry = dr.async_get(hass)
|
||||
assert len(registry.devices) == 1
|
||||
entry = registry.async_get_or_create(
|
||||
config_entry_id="1234", identifiers={("serial", "12:34:56:AB:CD:EF")}
|
||||
)
|
||||
assert entry.configuration_url == "invalid"
|
||||
|
|
Loading…
Add table
Reference in a new issue