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

Update helper tests to use device & entity registry fixtures (#103710)

This commit is contained in:
Franck Nijhof
2023-11-10 09:32:19 +01:00
committed by GitHub
parent 9f6eef7cca
commit 70ad5ab3e4
9 changed files with 209 additions and 185 deletions

View File

@@ -594,7 +594,9 @@ async def test_async_remove_with_platform_update_finishes(hass: HomeAssistant) -
async def test_not_adding_duplicate_entities_with_unique_id(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test for not adding duplicate entities.
@@ -627,9 +629,8 @@ async def test_not_adding_duplicate_entities_with_unique_id(
assert ent2.platform is None
assert len(hass.states.async_entity_ids()) == 1
registry = er.async_get(hass)
# test the entity name was not updated
entry = registry.async_get_or_create(DOMAIN, DOMAIN, "not_very_unique")
entry = entity_registry.async_get_or_create(DOMAIN, DOMAIN, "not_very_unique")
assert entry.original_name == "test1"
@@ -759,7 +760,9 @@ async def test_registry_respect_entity_disabled(hass: HomeAssistant) -> None:
async def test_unique_id_conflict_has_priority_over_disabled_entity(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that an entity that is not unique has priority over a disabled entity."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
@@ -777,9 +780,8 @@ async def test_unique_id_conflict_has_priority_over_disabled_entity(
assert "Platform test_domain does not generate unique IDs." in caplog.text
assert entity1.registry_entry is not None
assert entity2.registry_entry is None
registry = er.async_get(hass)
# test the entity name was not updated
entry = registry.async_get_or_create(DOMAIN, DOMAIN, "not_very_unique")
entry = entity_registry.async_get_or_create(DOMAIN, DOMAIN, "not_very_unique")
assert entry.original_name == "test1"
@@ -1074,12 +1076,13 @@ async def test_entity_registry_updates_invalid_entity_id(hass: HomeAssistant) ->
assert hass.states.get("diff_domain.world") is None
async def test_device_info_called(hass: HomeAssistant) -> None:
async def test_device_info_called(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test device info is forwarded correctly."""
registry = dr.async_get(hass)
config_entry = MockConfigEntry(entry_id="super-mock-id")
config_entry.add_to_hass(hass)
via = registry.async_get_or_create(
via = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections=set(),
identifiers={("hue", "via-id")},
@@ -1124,7 +1127,7 @@ async def test_device_info_called(hass: HomeAssistant) -> None:
assert len(hass.states.async_entity_ids()) == 2
device = registry.async_get_device(identifiers={("hue", "1234")})
device = device_registry.async_get_device(identifiers={("hue", "1234")})
assert device is not None
assert device.identifiers == {("hue", "1234")}
assert device.configuration_url == "http://192.168.0.100/config"
@@ -1139,12 +1142,13 @@ async def test_device_info_called(hass: HomeAssistant) -> None:
assert device.via_device_id == via.id
async def test_device_info_not_overrides(hass: HomeAssistant) -> None:
async def test_device_info_not_overrides(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
) -> None:
"""Test device info is forwarded correctly."""
registry = dr.async_get(hass)
config_entry = MockConfigEntry(entry_id="super-mock-id")
config_entry.add_to_hass(hass)
device = registry.async_get_or_create(
device = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "abcd")},
manufacturer="test-manufacturer",
@@ -1179,7 +1183,7 @@ async def test_device_info_not_overrides(hass: HomeAssistant) -> None:
assert await entity_platform.async_setup_entry(config_entry)
await hass.async_block_till_done()
device2 = registry.async_get_device(
device2 = device_registry.async_get_device(
connections={(dr.CONNECTION_NETWORK_MAC, "abcd")}
)
assert device2 is not None
@@ -1189,13 +1193,14 @@ async def test_device_info_not_overrides(hass: HomeAssistant) -> None:
async def test_device_info_homeassistant_url(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test device info with homeassistant URL."""
registry = dr.async_get(hass)
config_entry = MockConfigEntry(entry_id="super-mock-id")
config_entry.add_to_hass(hass)
registry.async_get_or_create(
device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections=set(),
identifiers={("mqtt", "via-id")},
@@ -1229,20 +1234,21 @@ async def test_device_info_homeassistant_url(
assert len(hass.states.async_entity_ids()) == 1
device = registry.async_get_device(identifiers={("mqtt", "1234")})
device = device_registry.async_get_device(identifiers={("mqtt", "1234")})
assert device is not None
assert device.identifiers == {("mqtt", "1234")}
assert device.configuration_url == "homeassistant://config/mqtt"
async def test_device_info_change_to_no_url(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test device info changes to no URL."""
registry = dr.async_get(hass)
config_entry = MockConfigEntry(entry_id="super-mock-id")
config_entry.add_to_hass(hass)
registry.async_get_or_create(
device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections=set(),
identifiers={("mqtt", "via-id")},
@@ -1277,13 +1283,15 @@ async def test_device_info_change_to_no_url(
assert len(hass.states.async_entity_ids()) == 1
device = registry.async_get_device(identifiers={("mqtt", "1234")})
device = device_registry.async_get_device(identifiers={("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: HomeAssistant) -> None:
async def test_entity_disabled_by_integration(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test entity disabled by integration."""
component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20))
await component.async_setup({})
@@ -1300,15 +1308,17 @@ async def test_entity_disabled_by_integration(hass: HomeAssistant) -> None:
assert entity_disabled.hass is None
assert entity_disabled.platform is None
registry = er.async_get(hass)
entry_default = registry.async_get_or_create(DOMAIN, DOMAIN, "default")
entry_default = entity_registry.async_get_or_create(DOMAIN, DOMAIN, "default")
assert entry_default.disabled_by is None
entry_disabled = registry.async_get_or_create(DOMAIN, DOMAIN, "disabled")
entry_disabled = entity_registry.async_get_or_create(DOMAIN, DOMAIN, "disabled")
assert entry_disabled.disabled_by is er.RegistryEntryDisabler.INTEGRATION
async def test_entity_disabled_by_device(hass: HomeAssistant) -> None:
async def test_entity_disabled_by_device(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test entity disabled by device."""
connections = {(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}
@@ -1328,7 +1338,6 @@ async def test_entity_disabled_by_device(hass: HomeAssistant) -> None:
hass, platform_name=config_entry.domain, platform=platform
)
device_registry = dr.async_get(hass)
device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections=connections,
@@ -1341,13 +1350,13 @@ async def test_entity_disabled_by_device(hass: HomeAssistant) -> None:
assert entity_disabled.hass is None
assert entity_disabled.platform is None
registry = er.async_get(hass)
entry_disabled = registry.async_get_or_create(DOMAIN, DOMAIN, "disabled")
entry_disabled = entity_registry.async_get_or_create(DOMAIN, DOMAIN, "disabled")
assert entry_disabled.disabled_by is er.RegistryEntryDisabler.DEVICE
async def test_entity_hidden_by_integration(hass: HomeAssistant) -> None:
async def test_entity_hidden_by_integration(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test entity hidden by integration."""
component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20))
await component.async_setup({})
@@ -1359,15 +1368,15 @@ async def test_entity_hidden_by_integration(hass: HomeAssistant) -> None:
await component.async_add_entities([entity_default, entity_hidden])
registry = er.async_get(hass)
entry_default = registry.async_get_or_create(DOMAIN, DOMAIN, "default")
entry_default = entity_registry.async_get_or_create(DOMAIN, DOMAIN, "default")
assert entry_default.hidden_by is None
entry_hidden = registry.async_get_or_create(DOMAIN, DOMAIN, "hidden")
entry_hidden = entity_registry.async_get_or_create(DOMAIN, DOMAIN, "hidden")
assert entry_hidden.hidden_by is er.RegistryEntryHider.INTEGRATION
async def test_entity_info_added_to_entity_registry(hass: HomeAssistant) -> None:
async def test_entity_info_added_to_entity_registry(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test entity info is written to entity registry."""
component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20))
await component.async_setup({})
@@ -1387,9 +1396,7 @@ async def test_entity_info_added_to_entity_registry(hass: HomeAssistant) -> None
await component.async_add_entities([entity_default])
registry = er.async_get(hass)
entry_default = registry.async_get_or_create(DOMAIN, DOMAIN, "default")
entry_default = entity_registry.async_get_or_create(DOMAIN, DOMAIN, "default")
assert entry_default == er.RegistryEntry(
"test_domain.best_name",
"default",
@@ -1729,12 +1736,12 @@ class SlowEntity(MockEntity):
)
async def test_entity_name_influences_entity_id(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
has_entity_name: bool,
entity_name: str | None,
expected_entity_id: str,
) -> None:
"""Test entity_id is influenced by entity name."""
registry = er.async_get(hass)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Mock setup entry method."""
@@ -1765,7 +1772,7 @@ async def test_entity_name_influences_entity_id(
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids()) == 1
assert registry.async_get(expected_entity_id) is not None
assert entity_registry.async_get(expected_entity_id) is not None
@pytest.mark.parametrize(
@@ -1780,6 +1787,7 @@ async def test_entity_name_influences_entity_id(
)
async def test_translated_entity_name_influences_entity_id(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
language: str,
has_entity_name: bool,
expected_entity_id: str,
@@ -1800,8 +1808,6 @@ async def test_translated_entity_name_influences_entity_id(
"""Initialize."""
self._attr_has_entity_name = has_entity_name
registry = er.async_get(hass)
translations = {
"en": {"component.test.entity.test_domain.test.name": "English name"},
"sv": {"component.test.entity.test_domain.test.name": "Swedish name"},
@@ -1839,7 +1845,7 @@ async def test_translated_entity_name_influences_entity_id(
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids()) == 1
assert registry.async_get(expected_entity_id) is not None
assert entity_registry.async_get(expected_entity_id) is not None
@pytest.mark.parametrize(
@@ -1860,6 +1866,7 @@ async def test_translated_entity_name_influences_entity_id(
)
async def test_translated_device_class_name_influences_entity_id(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
language: str,
has_entity_name: bool,
device_class: str | None,
@@ -1884,8 +1891,6 @@ async def test_translated_device_class_name_influences_entity_id(
"""Return True if an unnamed entity should be named by its device class."""
return self.device_class is not None
registry = er.async_get(hass)
translations = {
"en": {"component.test_domain.entity_component.test_class.name": "English cls"},
"sv": {"component.test_domain.entity_component.test_class.name": "Swedish cls"},
@@ -1923,7 +1928,7 @@ async def test_translated_device_class_name_influences_entity_id(
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids()) == 1
assert registry.async_get(expected_entity_id) is not None
assert entity_registry.async_get(expected_entity_id) is not None
@pytest.mark.parametrize(
@@ -1942,6 +1947,7 @@ async def test_translated_device_class_name_influences_entity_id(
)
async def test_device_name_defaulting_config_entry(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
config_entry_title: str,
entity_device_name: str,
entity_device_default_name: str,
@@ -1976,8 +1982,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(connections={(dr.CONNECTION_NETWORK_MAC, "1234")})
device = device_registry.async_get_device(
connections={(dr.CONNECTION_NETWORK_MAC, "1234")}
)
assert device is not None
assert device.name == expected_device_name
@@ -2002,6 +2009,8 @@ async def test_device_name_defaulting_config_entry(
)
async def test_device_type_error_checking(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
device_info: dict,
number_of_entities: int,
) -> None:
@@ -2027,8 +2036,6 @@ async def test_device_type_error_checking(
assert await entity_platform.async_setup_entry(config_entry)
dev_reg = dr.async_get(hass)
assert len(dev_reg.devices) == 0
ent_reg = er.async_get(hass)
assert len(ent_reg.entities) == number_of_entities
assert len(device_registry.devices) == 0
assert len(entity_registry.entities) == number_of_entities
assert len(hass.states.async_all()) == number_of_entities