mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Reduce some linear searches to cleanup the device registry (#112277)
Some of the data we had to search for was already available in a dict or underlying data structure. Make it available instead of having to build it every time. There are more places these can be used, but I only did the device registry cleanup for now
This commit is contained in:
@@ -90,6 +90,9 @@ def test_get_or_create_updates_data(entity_registry: er.EntityRegistry) -> None:
|
||||
unit_of_measurement="initial-unit_of_measurement",
|
||||
)
|
||||
|
||||
assert set(entity_registry.async_device_ids()) == {"mock-dev-id"}
|
||||
assert set(entity_registry.async_entity_ids()) == {"light.hue_5678"}
|
||||
|
||||
assert orig_entry == er.RegistryEntry(
|
||||
"light.hue_5678",
|
||||
"5678",
|
||||
@@ -159,6 +162,9 @@ def test_get_or_create_updates_data(entity_registry: er.EntityRegistry) -> None:
|
||||
unit_of_measurement="updated-unit_of_measurement",
|
||||
)
|
||||
|
||||
assert set(entity_registry.async_device_ids()) == {"new-mock-dev-id"}
|
||||
assert set(entity_registry.async_entity_ids()) == {"light.hue_5678"}
|
||||
|
||||
new_entry = entity_registry.async_get_or_create(
|
||||
"light",
|
||||
"hue",
|
||||
@@ -203,6 +209,9 @@ def test_get_or_create_updates_data(entity_registry: er.EntityRegistry) -> None:
|
||||
unit_of_measurement=None,
|
||||
)
|
||||
|
||||
assert set(entity_registry.async_device_ids()) == set()
|
||||
assert set(entity_registry.async_entity_ids()) == {"light.hue_5678"}
|
||||
|
||||
|
||||
def test_get_or_create_suggested_object_id_conflict_register(
|
||||
entity_registry: er.EntityRegistry,
|
||||
@@ -446,6 +455,8 @@ def test_async_get_entity_id(entity_registry: er.EntityRegistry) -> None:
|
||||
)
|
||||
assert entity_registry.async_get_entity_id("light", "hue", "123") is None
|
||||
|
||||
assert set(entity_registry.async_entity_ids()) == {"light.hue_1234"}
|
||||
|
||||
|
||||
async def test_updating_config_entry_id(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
@@ -1469,6 +1480,7 @@ def test_entity_registry_items() -> None:
|
||||
entities = er.EntityRegistryItems()
|
||||
assert entities.get_entity_id(("a", "b", "c")) is None
|
||||
assert entities.get_entry("abc") is None
|
||||
assert set(entities.get_entity_ids()) == set()
|
||||
|
||||
entry1 = er.RegistryEntry("test.entity1", "1234", "hue")
|
||||
entry2 = er.RegistryEntry("test.entity2", "2345", "hue")
|
||||
@@ -1482,6 +1494,7 @@ def test_entity_registry_items() -> None:
|
||||
assert entities.get_entry(entry1.id) is entry1
|
||||
assert entities.get_entity_id(("test", "hue", "2345")) is entry2.entity_id
|
||||
assert entities.get_entry(entry2.id) is entry2
|
||||
assert set(entities.get_entity_ids()) == {"test.entity2", "test.entity1"}
|
||||
|
||||
entities.pop("test.entity1")
|
||||
del entities["test.entity2"]
|
||||
@@ -1491,6 +1504,8 @@ def test_entity_registry_items() -> None:
|
||||
assert entities.get_entity_id(("test", "hue", "2345")) is None
|
||||
assert entities.get_entry(entry2.id) is None
|
||||
|
||||
assert set(entities.get_entity_ids()) == set()
|
||||
|
||||
|
||||
async def test_disabled_by_str_not_allowed(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
|
||||
Reference in New Issue
Block a user