Default device name to config entry title (#95547)

* Default device name to config entry title

* Only apply name default if device info provided

* Fix logic detecting type of device info
This commit is contained in:
Paulus Schoutsen 2023-06-30 13:54:20 -04:00 committed by GitHub
parent d4e40ed73f
commit 9280dc69ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 10 deletions

View File

@ -608,19 +608,12 @@ class EntityPlatform:
entity.add_to_platform_abort()
return
if self.config_entry is not None:
config_entry_id: str | None = self.config_entry.entry_id
else:
config_entry_id = None
device_info = entity.device_info
device_id = None
device = None
if config_entry_id is not None and device_info is not None:
processed_dev_info: dict[str, str | None] = {
"config_entry_id": config_entry_id
}
if self.config_entry and device_info is not None:
processed_dev_info: dict[str, str | None] = {}
for key in (
"connections",
"default_manufacturer",
@ -641,6 +634,17 @@ class EntityPlatform:
key # type: ignore[literal-required]
]
if (
# device info that is purely meant for linking doesn't need default name
any(
key not in {"identifiers", "connections"}
for key in (processed_dev_info)
)
and "default_name" not in processed_dev_info
and not processed_dev_info.get("name")
):
processed_dev_info["name"] = self.config_entry.title
if "configuration_url" in device_info:
if device_info["configuration_url"] is None:
processed_dev_info["configuration_url"] = None
@ -660,7 +664,8 @@ class EntityPlatform:
try:
device = device_registry.async_get_or_create(
**processed_dev_info # type: ignore[arg-type]
config_entry_id=self.config_entry.entry_id,
**processed_dev_info, # type: ignore[arg-type]
)
device_id = device.id
except RequiredParameterMissing:

View File

@ -1827,3 +1827,53 @@ async def test_translated_device_class_name_influences_entity_id(
assert len(hass.states.async_entity_ids()) == 1
assert registry.async_get(expected_entity_id) is not None
@pytest.mark.parametrize(
("entity_device_name", "entity_device_default_name", "expected_device_name"),
[
(None, None, "Mock Config Entry Title"),
("", None, "Mock Config Entry Title"),
(None, "Hello", "Hello"),
("Mock Device Name", None, "Mock Device Name"),
],
)
async def test_device_name_defaulting_config_entry(
hass: HomeAssistant,
entity_device_name: str,
entity_device_default_name: str,
expected_device_name: str,
) -> None:
"""Test setting the device name based on input info."""
device_info = {
"identifiers": {("hue", "1234")},
"name": entity_device_name,
}
if entity_device_default_name:
device_info["default_name"] = entity_device_default_name
class DeviceNameEntity(Entity):
_attr_unique_id = "qwer"
_attr_device_info = device_info
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Mock setup entry method."""
async_add_entities([DeviceNameEntity()])
return True
platform = MockPlatform(async_setup_entry=async_setup_entry)
config_entry = MockConfigEntry(
title="Mock Config Entry Title", 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()
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_device({("hue", "1234")})
assert device is not None
assert device.name == expected_device_name