1
mirror of https://github.com/home-assistant/core synced 2024-07-21 14:24:50 +02: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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 209 additions and 185 deletions

View File

@ -293,7 +293,9 @@ async def test_attach_entity_component_collection(hass: HomeAssistant) -> None:
assert hass.states.get("test.mock_1") is None
async def test_entity_component_collection_abort(hass: HomeAssistant) -> None:
async def test_entity_component_collection_abort(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test aborted entity adding is handled."""
ent_comp = entity_component.EntityComponent(_LOGGER, "test", hass)
await ent_comp.async_setup({})
@ -318,7 +320,6 @@ async def test_entity_component_collection_abort(hass: HomeAssistant) -> None:
collection.sync_entity_lifecycle(
hass, "test", "test", ent_comp, coll, MockMockEntity
)
entity_registry = er.async_get(hass)
entity_registry.async_get_or_create(
"test",
"test",
@ -360,7 +361,9 @@ async def test_entity_component_collection_abort(hass: HomeAssistant) -> None:
assert len(async_remove_calls) == 0
async def test_entity_component_collection_entity_removed(hass: HomeAssistant) -> None:
async def test_entity_component_collection_entity_removed(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test entity removal is handled."""
ent_comp = entity_component.EntityComponent(_LOGGER, "test", hass)
await ent_comp.async_setup({})
@ -385,7 +388,6 @@ async def test_entity_component_collection_entity_removed(hass: HomeAssistant) -
collection.sync_entity_lifecycle(
hass, "test", "test", ent_comp, coll, MockMockEntity
)
entity_registry = er.async_get(hass)
entity_registry.async_get_or_create(
"test", "test", "mock_id", suggested_object_id="mock_1"
)

View File

@ -1373,10 +1373,11 @@ async def test_state_attribute_boolean(hass: HomeAssistant) -> None:
assert test(hass)
async def test_state_entity_registry_id(hass: HomeAssistant) -> None:
async def test_state_entity_registry_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test with entity specified by entity registry id."""
registry = er.async_get(hass)
entry = registry.async_get_or_create(
entry = entity_registry.async_get_or_create(
"switch", "hue", "1234", suggested_object_id="test"
)
assert entry.entity_id == "switch.test"
@ -1715,10 +1716,11 @@ async def test_numeric_state_attribute(hass: HomeAssistant) -> None:
assert not test(hass)
async def test_numeric_state_entity_registry_id(hass: HomeAssistant) -> None:
async def test_numeric_state_entity_registry_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test with entity specified by entity registry id."""
registry = er.async_get(hass)
entry = registry.async_get_or_create(
entry = entity_registry.async_get_or_create(
"sensor", "hue", "1234", suggested_object_id="test"
)
assert entry.entity_id == "sensor.test"

View File

@ -1326,7 +1326,9 @@ async def test_update_suggested_area(
async def test_cleanup_device_registry(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test cleanup works."""
config_entry = MockConfigEntry(domain="hue")
@ -1349,13 +1351,12 @@ async def test_cleanup_device_registry(
# Remove the config entry without triggering the normal cleanup
hass.config_entries._entries.pop(ghost_config_entry.entry_id)
ent_reg = er.async_get(hass)
ent_reg.async_get_or_create("light", "hue", "e1", device_id=d1.id)
ent_reg.async_get_or_create("light", "hue", "e2", device_id=d1.id)
ent_reg.async_get_or_create("light", "hue", "e3", device_id=d3.id)
entity_registry.async_get_or_create("light", "hue", "e1", device_id=d1.id)
entity_registry.async_get_or_create("light", "hue", "e2", device_id=d1.id)
entity_registry.async_get_or_create("light", "hue", "e3", device_id=d3.id)
# Manual cleanup should detect the orphaned config entry
dr.async_cleanup(hass, device_registry, ent_reg)
dr.async_cleanup(hass, device_registry, entity_registry)
assert device_registry.async_get_device(identifiers={("hue", "d1")}) is not None
assert device_registry.async_get_device(identifiers={("hue", "d2")}) is not None
@ -1364,7 +1365,9 @@ async def test_cleanup_device_registry(
async def test_cleanup_device_registry_removes_expired_orphaned_devices(
hass: HomeAssistant, device_registry: dr.DeviceRegistry
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test cleanup removes expired orphaned devices."""
config_entry = MockConfigEntry(domain="hue")
@ -1384,8 +1387,7 @@ async def test_cleanup_device_registry_removes_expired_orphaned_devices(
assert len(device_registry.devices) == 0
assert len(device_registry.deleted_devices) == 3
ent_reg = er.async_get(hass)
dr.async_cleanup(hass, device_registry, ent_reg)
dr.async_cleanup(hass, device_registry, entity_registry)
assert len(device_registry.devices) == 0
assert len(device_registry.deleted_devices) == 3
@ -1393,7 +1395,7 @@ async def test_cleanup_device_registry_removes_expired_orphaned_devices(
future_time = time.time() + dr.ORPHANED_DEVICE_KEEP_SECONDS + 1
with patch("time.time", return_value=future_time):
dr.async_cleanup(hass, device_registry, ent_reg)
dr.async_cleanup(hass, device_registry, entity_registry)
assert len(device_registry.devices) == 0
assert len(device_registry.deleted_devices) == 0

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

View File

@ -866,27 +866,27 @@ async def test_disabled_by_config_entry_pref(
assert entry2.disabled_by is er.RegistryEntryDisabler.USER
async def test_restore_states(hass: HomeAssistant) -> None:
async def test_restore_states(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test restoring states."""
hass.state = CoreState.not_running
registry = er.async_get(hass)
registry.async_get_or_create(
entity_registry.async_get_or_create(
"light",
"hue",
"1234",
suggested_object_id="simple",
)
# Should not be created
registry.async_get_or_create(
entity_registry.async_get_or_create(
"light",
"hue",
"5678",
suggested_object_id="disabled",
disabled_by=er.RegistryEntryDisabler.HASS,
)
registry.async_get_or_create(
entity_registry.async_get_or_create(
"light",
"hue",
"9012",
@ -921,9 +921,9 @@ async def test_restore_states(hass: HomeAssistant) -> None:
"icon": "hass:original-icon",
}
registry.async_remove("light.disabled")
registry.async_remove("light.simple")
registry.async_remove("light.all_info_set")
entity_registry.async_remove("light.disabled")
entity_registry.async_remove("light.simple")
entity_registry.async_remove("light.all_info_set")
await hass.async_block_till_done()
@ -932,58 +932,58 @@ async def test_restore_states(hass: HomeAssistant) -> None:
assert hass.states.get("light.all_info_set") is None
async def test_async_get_device_class_lookup(hass: HomeAssistant) -> None:
async def test_async_get_device_class_lookup(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test registry device class lookup."""
hass.state = CoreState.not_running
ent_reg = er.async_get(hass)
ent_reg.async_get_or_create(
entity_registry.async_get_or_create(
"binary_sensor",
"light",
"battery_charging",
device_id="light_device_entry_id",
original_device_class="battery_charging",
)
ent_reg.async_get_or_create(
entity_registry.async_get_or_create(
"sensor",
"light",
"battery",
device_id="light_device_entry_id",
original_device_class="battery",
)
ent_reg.async_get_or_create(
entity_registry.async_get_or_create(
"light", "light", "demo", device_id="light_device_entry_id"
)
ent_reg.async_get_or_create(
entity_registry.async_get_or_create(
"binary_sensor",
"vacuum",
"battery_charging",
device_id="vacuum_device_entry_id",
original_device_class="battery_charging",
)
ent_reg.async_get_or_create(
entity_registry.async_get_or_create(
"sensor",
"vacuum",
"battery",
device_id="vacuum_device_entry_id",
original_device_class="battery",
)
ent_reg.async_get_or_create(
entity_registry.async_get_or_create(
"vacuum", "vacuum", "demo", device_id="vacuum_device_entry_id"
)
ent_reg.async_get_or_create(
entity_registry.async_get_or_create(
"binary_sensor",
"remote",
"battery_charging",
device_id="remote_device_entry_id",
original_device_class="battery_charging",
)
ent_reg.async_get_or_create(
entity_registry.async_get_or_create(
"remote", "remote", "demo", device_id="remote_device_entry_id"
)
device_lookup = ent_reg.async_get_device_class_lookup(
device_lookup = entity_registry.async_get_device_class_lookup(
{("binary_sensor", "battery_charging"), ("sensor", "battery")}
)
@ -1476,50 +1476,52 @@ def test_entity_registry_items() -> None:
assert entities.get_entry(entry2.id) is None
async def test_disabled_by_str_not_allowed(hass: HomeAssistant) -> None:
async def test_disabled_by_str_not_allowed(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test we need to pass disabled by type."""
reg = er.async_get(hass)
with pytest.raises(ValueError):
reg.async_get_or_create(
entity_registry.async_get_or_create(
"light", "hue", "1234", disabled_by=er.RegistryEntryDisabler.USER.value
)
entity_id = reg.async_get_or_create("light", "hue", "1234").entity_id
entity_id = entity_registry.async_get_or_create("light", "hue", "1234").entity_id
with pytest.raises(ValueError):
reg.async_update_entity(
entity_registry.async_update_entity(
entity_id, disabled_by=er.RegistryEntryDisabler.USER.value
)
async def test_entity_category_str_not_allowed(hass: HomeAssistant) -> None:
async def test_entity_category_str_not_allowed(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test we need to pass entity category type."""
reg = er.async_get(hass)
with pytest.raises(ValueError):
reg.async_get_or_create(
entity_registry.async_get_or_create(
"light", "hue", "1234", entity_category=EntityCategory.DIAGNOSTIC.value
)
entity_id = reg.async_get_or_create("light", "hue", "1234").entity_id
entity_id = entity_registry.async_get_or_create("light", "hue", "1234").entity_id
with pytest.raises(ValueError):
reg.async_update_entity(
entity_registry.async_update_entity(
entity_id, entity_category=EntityCategory.DIAGNOSTIC.value
)
async def test_hidden_by_str_not_allowed(hass: HomeAssistant) -> None:
async def test_hidden_by_str_not_allowed(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test we need to pass hidden by type."""
reg = er.async_get(hass)
with pytest.raises(ValueError):
reg.async_get_or_create(
entity_registry.async_get_or_create(
"light", "hue", "1234", hidden_by=er.RegistryEntryHider.USER.value
)
entity_id = reg.async_get_or_create("light", "hue", "1234").entity_id
entity_id = entity_registry.async_get_or_create("light", "hue", "1234").entity_id
with pytest.raises(ValueError):
reg.async_update_entity(entity_id, hidden_by=er.RegistryEntryHider.USER.value)
entity_registry.async_update_entity(
entity_id, hidden_by=er.RegistryEntryHider.USER.value
)
def test_migrate_entity_to_new_platform(
@ -1595,34 +1597,35 @@ def test_migrate_entity_to_new_platform(
)
async def test_restore_entity(hass, update_events, freezer):
async def test_restore_entity(
hass: HomeAssistant, entity_registry: er.EntityRegistry, update_events, freezer
):
"""Make sure entity registry id is stable and entity_id is reused if possible."""
registry = er.async_get(hass) # We need the real entity registry for this test
config_entry = MockConfigEntry(domain="light")
entry1 = registry.async_get_or_create(
entry1 = entity_registry.async_get_or_create(
"light", "hue", "1234", config_entry=config_entry
)
entry2 = registry.async_get_or_create(
entry2 = entity_registry.async_get_or_create(
"light", "hue", "5678", config_entry=config_entry
)
entry1 = registry.async_update_entity(
entry1 = entity_registry.async_update_entity(
entry1.entity_id, new_entity_id="light.custom_1"
)
registry.async_remove(entry1.entity_id)
registry.async_remove(entry2.entity_id)
assert len(registry.entities) == 0
assert len(registry.deleted_entities) == 2
entity_registry.async_remove(entry1.entity_id)
entity_registry.async_remove(entry2.entity_id)
assert len(entity_registry.entities) == 0
assert len(entity_registry.deleted_entities) == 2
# Re-add entities
entry1_restored = registry.async_get_or_create(
entry1_restored = entity_registry.async_get_or_create(
"light", "hue", "1234", config_entry=config_entry
)
entry2_restored = registry.async_get_or_create("light", "hue", "5678")
entry2_restored = entity_registry.async_get_or_create("light", "hue", "5678")
assert len(registry.entities) == 2
assert len(registry.deleted_entities) == 0
assert len(entity_registry.entities) == 2
assert len(entity_registry.deleted_entities) == 0
assert entry1 != entry1_restored
# entity_id is not restored
assert attr.evolve(entry1, entity_id="light.hue_1234") == entry1_restored
@ -1631,39 +1634,39 @@ async def test_restore_entity(hass, update_events, freezer):
assert attr.evolve(entry2, config_entry_id=None) == entry2_restored
# Remove two of the entities again, then bump time
registry.async_remove(entry1_restored.entity_id)
registry.async_remove(entry2.entity_id)
assert len(registry.entities) == 0
assert len(registry.deleted_entities) == 2
entity_registry.async_remove(entry1_restored.entity_id)
entity_registry.async_remove(entry2.entity_id)
assert len(entity_registry.entities) == 0
assert len(entity_registry.deleted_entities) == 2
freezer.tick(timedelta(seconds=er.ORPHANED_ENTITY_KEEP_SECONDS + 1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
# Re-add two entities, expect to get a new id after the purge for entity w/o config entry
entry1_restored = registry.async_get_or_create(
entry1_restored = entity_registry.async_get_or_create(
"light", "hue", "1234", config_entry=config_entry
)
entry2_restored = registry.async_get_or_create("light", "hue", "5678")
assert len(registry.entities) == 2
assert len(registry.deleted_entities) == 0
entry2_restored = entity_registry.async_get_or_create("light", "hue", "5678")
assert len(entity_registry.entities) == 2
assert len(entity_registry.deleted_entities) == 0
assert entry1.id == entry1_restored.id
assert entry2.id != entry2_restored.id
# Remove the first entity, then its config entry, finally bump time
registry.async_remove(entry1_restored.entity_id)
assert len(registry.entities) == 1
assert len(registry.deleted_entities) == 1
registry.async_clear_config_entry(config_entry.entry_id)
entity_registry.async_remove(entry1_restored.entity_id)
assert len(entity_registry.entities) == 1
assert len(entity_registry.deleted_entities) == 1
entity_registry.async_clear_config_entry(config_entry.entry_id)
freezer.tick(timedelta(seconds=er.ORPHANED_ENTITY_KEEP_SECONDS + 1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
# Re-add the entity, expect to get a new id after the purge
entry1_restored = registry.async_get_or_create(
entry1_restored = entity_registry.async_get_or_create(
"light", "hue", "1234", config_entry=config_entry
)
assert len(registry.entities) == 2
assert len(registry.deleted_entities) == 0
assert len(entity_registry.entities) == 2
assert len(entity_registry.deleted_entities) == 0
assert entry1.id != entry1_restored.id
# Check the events
@ -1687,18 +1690,19 @@ async def test_restore_entity(hass, update_events, freezer):
assert update_events[12] == {"action": "create", "entity_id": "light.hue_1234"}
async def test_async_migrate_entry_delete_self(hass):
async def test_async_migrate_entry_delete_self(
hass: HomeAssistant, entity_registry: er.EntityRegistry
):
"""Test async_migrate_entry."""
registry = er.async_get(hass)
config_entry1 = MockConfigEntry(domain="test1")
config_entry2 = MockConfigEntry(domain="test2")
entry1 = registry.async_get_or_create(
entry1 = entity_registry.async_get_or_create(
"light", "hue", "1234", config_entry=config_entry1, original_name="Entry 1"
)
entry2 = registry.async_get_or_create(
entry2 = entity_registry.async_get_or_create(
"light", "hue", "5678", config_entry=config_entry1, original_name="Entry 2"
)
entry3 = registry.async_get_or_create(
entry3 = entity_registry.async_get_or_create(
"light", "hue", "90AB", config_entry=config_entry2, original_name="Entry 3"
)
@ -1706,7 +1710,7 @@ async def test_async_migrate_entry_delete_self(hass):
def _async_migrator(entity_entry: er.RegistryEntry) -> dict[str, Any] | None:
entries.add(entity_entry.entity_id)
if entity_entry == entry1:
registry.async_remove(entry1.entity_id)
entity_registry.async_remove(entry1.entity_id)
return None
if entity_entry == entry2:
return {"original_name": "Entry 2 renamed"}
@ -1715,24 +1719,25 @@ async def test_async_migrate_entry_delete_self(hass):
entries = set()
await er.async_migrate_entries(hass, config_entry1.entry_id, _async_migrator)
assert entries == {entry1.entity_id, entry2.entity_id}
assert not registry.async_is_registered(entry1.entity_id)
entry2 = registry.async_get(entry2.entity_id)
assert not entity_registry.async_is_registered(entry1.entity_id)
entry2 = entity_registry.async_get(entry2.entity_id)
assert entry2.original_name == "Entry 2 renamed"
assert registry.async_get(entry3.entity_id) is entry3
assert entity_registry.async_get(entry3.entity_id) is entry3
async def test_async_migrate_entry_delete_other(hass):
async def test_async_migrate_entry_delete_other(
hass: HomeAssistant, entity_registry: er.EntityRegistry
):
"""Test async_migrate_entry."""
registry = er.async_get(hass)
config_entry1 = MockConfigEntry(domain="test1")
config_entry2 = MockConfigEntry(domain="test2")
entry1 = registry.async_get_or_create(
entry1 = entity_registry.async_get_or_create(
"light", "hue", "1234", config_entry=config_entry1, original_name="Entry 1"
)
entry2 = registry.async_get_or_create(
entry2 = entity_registry.async_get_or_create(
"light", "hue", "5678", config_entry=config_entry1, original_name="Entry 2"
)
registry.async_get_or_create(
entity_registry.async_get_or_create(
"light", "hue", "90AB", config_entry=config_entry2, original_name="Entry 3"
)
@ -1740,7 +1745,7 @@ async def test_async_migrate_entry_delete_other(hass):
def _async_migrator(entity_entry: er.RegistryEntry) -> dict[str, Any] | None:
entries.add(entity_entry.entity_id)
if entity_entry == entry1:
registry.async_remove(entry2.entity_id)
entity_registry.async_remove(entry2.entity_id)
return None
if entity_entry == entry2:
# We should not get here
@ -1750,4 +1755,4 @@ async def test_async_migrate_entry_delete_other(hass):
entries = set()
await er.async_migrate_entries(hass, config_entry1.entry_id, _async_migrator)
assert entries == {entry1.entity_id}
assert not registry.async_is_registered(entry2.entity_id)
assert not entity_registry.async_is_registered(entry2.entity_id)

View File

@ -78,9 +78,8 @@ def manager_fixture():
return mgr
async def test_name(hass: HomeAssistant) -> None:
async def test_name(hass: HomeAssistant, entity_registry: er.EntityRegistry) -> None:
"""Test the config flow name is copied from registry entry, with fallback to state."""
registry = er.async_get(hass)
entity_id = "switch.ceiling"
# No entry or state, use Object ID
@ -92,7 +91,7 @@ async def test_name(hass: HomeAssistant) -> None:
# Entity registered, use original name from registry entry
hass.states.async_remove(entity_id)
entry = registry.async_get_or_create(
entry = entity_registry.async_get_or_create(
"switch",
"test",
"unique",
@ -105,7 +104,7 @@ async def test_name(hass: HomeAssistant) -> None:
assert wrapped_entity_config_entry_title(hass, entry.id) == "Original Name"
# Entity has customized name
registry.async_update_entity("switch.ceiling", name="Custom Name")
entity_registry.async_update_entity("switch.ceiling", name="Custom Name")
assert wrapped_entity_config_entry_title(hass, entity_id) == "Custom Name"
assert wrapped_entity_config_entry_title(hass, entry.id) == "Custom Name"

View File

@ -1788,11 +1788,12 @@ async def test_shorthand_template_condition(
async def test_condition_validation(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test if we can use conditions which validate late in a script."""
registry = er.async_get(hass)
entry = registry.async_get_or_create(
entry = entity_registry.async_get_or_create(
"test", "hue", "1234", suggested_object_id="entity"
)
assert entry.entity_id == "test.entity"
@ -2385,11 +2386,12 @@ async def test_repeat_conditional(
async def test_repeat_until_condition_validation(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test if we can use conditions in repeat until conditions which validate late."""
registry = er.async_get(hass)
entry = registry.async_get_or_create(
entry = entity_registry.async_get_or_create(
"test", "hue", "1234", suggested_object_id="entity"
)
assert entry.entity_id == "test.entity"
@ -2447,11 +2449,12 @@ async def test_repeat_until_condition_validation(
async def test_repeat_while_condition_validation(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test if we can use conditions in repeat while conditions which validate late."""
registry = er.async_get(hass)
entry = registry.async_get_or_create(
entry = entity_registry.async_get_or_create(
"test", "hue", "1234", suggested_object_id="entity"
)
assert entry.entity_id == "test.entity"
@ -2868,11 +2871,12 @@ async def test_choose(
async def test_choose_condition_validation(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test if we can use conditions in choose actions which validate late."""
registry = er.async_get(hass)
entry = registry.async_get_or_create(
entry = entity_registry.async_get_or_create(
"test", "hue", "1234", suggested_object_id="entity"
)
assert entry.entity_id == "test.entity"
@ -3112,11 +3116,12 @@ async def test_if_disabled(
async def test_if_condition_validation(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test if we can use conditions in if actions which validate late."""
registry = er.async_get(hass)
entry = registry.async_get_or_create(
entry = entity_registry.async_get_or_create(
"test", "hue", "1234", suggested_object_id="entity"
)
assert entry.entity_id == "test.entity"

View File

@ -4080,9 +4080,10 @@ def test_state_with_unit(hass: HomeAssistant) -> None:
assert tpl.async_render() == ""
def test_state_with_unit_and_rounding(hass: HomeAssistant) -> None:
def test_state_with_unit_and_rounding(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test formatting the state rounded and with unit."""
entity_registry = er.async_get(hass)
entry = entity_registry.async_get_or_create(
"sensor", "test", "very_unique", suggested_object_id="test"
)
@ -4153,6 +4154,7 @@ def test_state_with_unit_and_rounding(hass: HomeAssistant) -> None:
)
def test_state_with_unit_and_rounding_options(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
rounded: str,
with_unit: str,
output1_1,
@ -4161,7 +4163,6 @@ def test_state_with_unit_and_rounding_options(
output2_2,
) -> None:
"""Test formatting the state rounded and with unit."""
entity_registry = er.async_get(hass)
entry = entity_registry.async_get_or_create(
"sensor", "test", "very_unique", suggested_object_id="test"
)

View File

@ -269,7 +269,9 @@ async def test_call_async_migrate_entry_failure_not_supported(
async def test_remove_entry(
hass: HomeAssistant, manager: config_entries.ConfigEntries
hass: HomeAssistant,
manager: config_entries.ConfigEntries,
entity_registry: er.EntityRegistry,
) -> None:
"""Test that we can remove an entry."""
@ -335,9 +337,8 @@ async def test_remove_entry(
assert len(hass.states.async_all()) == 1
# Check entity got added to entity registry
ent_reg = er.async_get(hass)
assert len(ent_reg.entities) == 1
entity_entry = list(ent_reg.entities.values())[0]
assert len(entity_registry.entities) == 1
entity_entry = list(entity_registry.entities.values())[0]
assert entity_entry.config_entry_id == entry.entry_id
# Remove entry
@ -358,7 +359,7 @@ async def test_remove_entry(
assert len(hass.states.async_all()) == 0
# Check that entity registry entry has been removed
entity_entry_list = list(ent_reg.entities.values())
entity_entry_list = list(entity_registry.entities.values())
assert not entity_entry_list