1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Differentiate between device info types (#95641)

* Differentiate between device info types

* Update allowed fields

* Update homeassistant/helpers/entity_platform.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Split up message in 2 lines

* Use dict for device info types

* Extract device info function and test error checking

* Simplify parsing device info

* move checks around

* Simplify more

* Move error checking around

* Fix order

* fallback config entry title to domain

* Remove fallback for name to config entry domain

* Ensure mocked configuration URLs are strings

* one more test case

* Apply suggestions from code review

Co-authored-by: Erik Montnemery <erik@montnemery.com>

---------

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Paulus Schoutsen
2023-07-10 09:56:06 -04:00
committed by GitHub
parent af22a90b3a
commit eee8566694
5 changed files with 165 additions and 133 deletions

View File

@@ -1169,57 +1169,6 @@ async def test_device_info_not_overrides(hass: HomeAssistant) -> None:
assert device2.model == "test-model"
async def test_device_info_invalid_url(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test device info is forwarded correctly."""
registry = dr.async_get(hass)
registry.async_get_or_create(
config_entry_id="123",
connections=set(),
identifiers={("hue", "via-id")},
manufacturer="manufacturer",
model="via",
)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Mock setup entry method."""
async_add_entities(
[
# Valid device info, but invalid url
MockEntity(
unique_id="qwer",
device_info={
"identifiers": {("hue", "1234")},
"configuration_url": "foo://192.168.0.100/config",
},
),
]
)
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({("hue", "1234")})
assert device is not None
assert device.identifiers == {("hue", "1234")}
assert device.configuration_url is None
assert (
"Ignoring invalid device configuration_url 'foo://192.168.0.100/config'"
in caplog.text
)
async def test_device_info_homeassistant_url(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
@@ -1838,28 +1787,85 @@ async def test_translated_device_class_name_influences_entity_id(
@pytest.mark.parametrize(
("entity_device_name", "entity_device_default_name", "expected_device_name"),
(
"config_entry_title",
"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"),
("Mock Config Entry Title", None, None, "Mock Config Entry Title"),
("Mock Config Entry Title", "", None, "Mock Config Entry Title"),
("Mock Config Entry Title", None, "Hello", "Hello"),
("Mock Config Entry Title", "Mock Device Name", None, "Mock Device Name"),
],
)
async def test_device_name_defaulting_config_entry(
hass: HomeAssistant,
config_entry_title: str,
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,
"connections": {(dr.CONNECTION_NETWORK_MAC, "1234")},
}
if entity_device_default_name:
device_info["default_name"] = entity_device_default_name
else:
device_info["name"] = entity_device_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=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(set(), {(dr.CONNECTION_NETWORK_MAC, "1234")})
assert device is not None
assert device.name == expected_device_name
@pytest.mark.parametrize(
("device_info"),
[
# No identifiers
{},
{"name": "bla"},
{"default_name": "bla"},
# Match multiple types
{
"identifiers": {("hue", "1234")},
"name": "bla",
"default_name": "yo",
},
# Invalid configuration URL
{
"identifiers": {("hue", "1234")},
"configuration_url": "foo://192.168.0.100/config",
},
],
)
async def test_device_type_error_checking(
hass: HomeAssistant,
device_info: dict,
) -> None:
"""Test catching invalid device info."""
class DeviceNameEntity(Entity):
_attr_unique_id = "qwer"
@@ -1879,9 +1885,9 @@ async def test_device_name_defaulting_config_entry(
)
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
assert len(dev_reg.devices) == 0
# Entity should still be registered
ent_reg = er.async_get(hass)
assert ent_reg.async_get("test_domain.test_qwer") is not None