Allow configuration_url to be removed/nullified from device registry (#58564)
* Allow configuration_url to be removed from device registry * Add test * Check for None before stringifying and url parsing * Add type to dict to remove mypy error on assigning Nonepull/58977/head
parent
928b8bcdbd
commit
07c9d77414
|
@ -458,7 +458,9 @@ class EntityPlatform:
|
|||
device_id = None
|
||||
|
||||
if config_entry_id is not None and device_info is not None:
|
||||
processed_dev_info = {"config_entry_id": config_entry_id}
|
||||
processed_dev_info: dict[str, str | None] = {
|
||||
"config_entry_id": config_entry_id
|
||||
}
|
||||
for key in (
|
||||
"connections",
|
||||
"default_manufacturer",
|
||||
|
@ -477,18 +479,21 @@ class EntityPlatform:
|
|||
processed_dev_info[key] = device_info[key] # type: ignore[misc]
|
||||
|
||||
if "configuration_url" in device_info:
|
||||
configuration_url = str(device_info["configuration_url"])
|
||||
if urlparse(configuration_url).scheme in [
|
||||
"http",
|
||||
"https",
|
||||
"homeassistant",
|
||||
]:
|
||||
processed_dev_info["configuration_url"] = configuration_url
|
||||
if device_info["configuration_url"] is None:
|
||||
processed_dev_info["configuration_url"] = None
|
||||
else:
|
||||
_LOGGER.warning(
|
||||
"Ignoring invalid device configuration_url '%s'",
|
||||
configuration_url,
|
||||
)
|
||||
configuration_url = str(device_info["configuration_url"])
|
||||
if urlparse(configuration_url).scheme in [
|
||||
"http",
|
||||
"https",
|
||||
"homeassistant",
|
||||
]:
|
||||
processed_dev_info["configuration_url"] = configuration_url
|
||||
else:
|
||||
_LOGGER.warning(
|
||||
"Ignoring invalid device configuration_url '%s'",
|
||||
configuration_url,
|
||||
)
|
||||
|
||||
try:
|
||||
device = device_registry.async_get_or_create(**processed_dev_info) # type: ignore[arg-type]
|
||||
|
|
|
@ -1011,6 +1011,51 @@ async def test_device_info_homeassistant_url(hass, caplog):
|
|||
assert device.configuration_url == "homeassistant://config/mqtt"
|
||||
|
||||
|
||||
async def test_device_info_change_to_no_url(hass, caplog):
|
||||
"""Test device info changes to no URL."""
|
||||
registry = dr.async_get(hass)
|
||||
registry.async_get_or_create(
|
||||
config_entry_id="123",
|
||||
connections=set(),
|
||||
identifiers={("mqtt", "via-id")},
|
||||
manufacturer="manufacturer",
|
||||
model="via",
|
||||
configuration_url="homeassistant://config/mqtt",
|
||||
)
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Mock setup entry method."""
|
||||
async_add_entities(
|
||||
[
|
||||
# Valid device info, with homeassistant url
|
||||
MockEntity(
|
||||
unique_id="qwer",
|
||||
device_info={
|
||||
"identifiers": {("mqtt", "1234")},
|
||||
"configuration_url": None,
|
||||
},
|
||||
),
|
||||
]
|
||||
)
|
||||
return True
|
||||
|
||||
platform = MockPlatform(async_setup_entry=async_setup_entry)
|
||||
config_entry = MockConfigEntry(entry_id="super-mock-id")
|
||||
entity_platform = MockEntityPlatform(
|
||||
hass, platform_name=config_entry.domain, platform=platform
|
||||
)
|
||||
|
||||
assert await entity_platform.async_setup_entry(config_entry)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_entity_ids()) == 1
|
||||
|
||||
device = registry.async_get_device({("mqtt", "1234")})
|
||||
assert device is not None
|
||||
assert device.identifiers == {("mqtt", "1234")}
|
||||
assert device.configuration_url is None
|
||||
|
||||
|
||||
async def test_entity_disabled_by_integration(hass):
|
||||
"""Test entity disabled by integration."""
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20))
|
||||
|
|
Loading…
Reference in New Issue