Fix logic and add additional tests

This commit is contained in:
Erik
2025-05-09 17:47:54 +02:00
parent 0924740cb4
commit 31f8ea523f
2 changed files with 73 additions and 3 deletions

View File

@@ -84,6 +84,9 @@ def async_get_entity_suggested_object_id(
if entity_entry.suggested_object_id: if entity_entry.suggested_object_id:
return entity_entry.suggested_object_id return entity_entry.suggested_object_id
if entity_entry.name:
return entity_entry.name
entity_comp = hass.data.get(DATA_INSTANCES, {}).get(domain) entity_comp = hass.data.get(DATA_INSTANCES, {}).get(domain)
entity_obj = entity_comp.get_entity(entity_id) if entity_comp else None entity_obj = entity_comp.get_entity(entity_id) if entity_comp else None
if entity_obj: if entity_obj:
@@ -92,7 +95,7 @@ def async_get_entity_suggested_object_id(
device = dr.async_get(hass).async_get(device_id) device = dr.async_get(hass).async_get(device_id)
return async_calculate_suggested_object_id(entity_obj, device) return async_calculate_suggested_object_id(entity_obj, device)
return entity_entry.suggested_object_id return entity_entry.calculated_object_id
class EntityComponent[_EntityT: entity.Entity = entity.Entity]: class EntityComponent[_EntityT: entity.Entity = entity.Entity]:

View File

@@ -1,6 +1,7 @@
"""Test entity_registry API.""" """Test entity_registry API."""
from datetime import datetime from datetime import datetime
import logging
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
@@ -11,6 +12,7 @@ from homeassistant.const import ATTR_ICON, EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.device_registry import DeviceEntryDisabler from homeassistant.helpers.device_registry import DeviceEntryDisabler
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.entity_registry import ( from homeassistant.helpers.entity_registry import (
RegistryEntryDisabler, RegistryEntryDisabler,
RegistryEntryHider, RegistryEntryHider,
@@ -1290,6 +1292,10 @@ async def test_remove_non_existing_entity(
assert not msg["success"] assert not msg["success"]
_LOGGER = logging.getLogger(__name__)
DOMAIN = "test_domain"
async def test_get_automatic_entity_ids( async def test_get_automatic_entity_ids(
hass: HomeAssistant, client: MockHAClientWebSocket hass: HomeAssistant, client: MockHAClientWebSocket
) -> None: ) -> None:
@@ -1306,7 +1312,7 @@ async def test_get_automatic_entity_ids(
entity_id="test_domain.test_2", entity_id="test_domain.test_2",
unique_id="uniq2", unique_id="uniq2",
platform="test_platform", platform="test_platform",
suggested_object_id="blabla", suggested_object_id="suggested_2",
), ),
"test_domain.test_3": RegistryEntryWithDefaults( "test_domain.test_3": RegistryEntryWithDefaults(
entity_id="test_domain.test_3", entity_id="test_domain.test_3",
@@ -1314,6 +1320,47 @@ async def test_get_automatic_entity_ids(
platform="test_platform", platform="test_platform",
suggested_object_id="collision", suggested_object_id="collision",
), ),
"test_domain.test_4": RegistryEntryWithDefaults(
calculated_object_id="calculated_4",
entity_id="test_domain.test_4",
unique_id="uniq4",
platform="test_platform",
suggested_object_id="suggested_4",
),
"test_domain.test_5": RegistryEntryWithDefaults(
calculated_object_id="calculated_5",
entity_id="test_domain.test_5",
unique_id="uniq5",
platform="test_platform",
),
"test_domain.test_6": RegistryEntryWithDefaults(
calculated_object_id="calculated_6",
entity_id="test_domain.test_6",
unique_id="uniq6",
platform="test_domain",
suggested_object_id="suggested_6",
),
"test_domain.test_7": RegistryEntryWithDefaults(
calculated_object_id="calculated_7",
entity_id="test_domain.test_7",
unique_id="uniq7",
platform="test_domain",
),
"test_domain.test_8": RegistryEntryWithDefaults(
calculated_object_id="calculated_8",
entity_id="test_domain.test_8",
name="name_by_user_8",
unique_id="uniq8",
platform="test_domain",
suggested_object_id="suggested_8",
),
"test_domain.test_9": RegistryEntryWithDefaults(
calculated_object_id="calculated_9",
entity_id="test_domain.test_9",
name="name_by_user_9",
unique_id="uniq9",
platform="test_domain",
),
"test_domain.collision": RegistryEntryWithDefaults( "test_domain.collision": RegistryEntryWithDefaults(
entity_id="test_domain.collision", entity_id="test_domain.collision",
unique_id="uniq_collision", unique_id="uniq_collision",
@@ -1322,6 +1369,14 @@ async def test_get_automatic_entity_ids(
}, },
) )
component = EntityComponent(_LOGGER, DOMAIN, hass)
await component.async_setup({})
entity6 = MockEntity(unique_id="uniq6", name="entity_name_6")
entity7 = MockEntity(unique_id="uniq7", name="entity_name_7")
entity8 = MockEntity(unique_id="uniq8", name="entity_name_8")
entity9 = MockEntity(unique_id="uniq9", name="entity_name_9")
await component.async_add_entities([entity6, entity7, entity8, entity9])
await client.send_json_auto_id( await client.send_json_auto_id(
{ {
"type": "config/entity_registry/get_automatic_entity_ids", "type": "config/entity_registry/get_automatic_entity_ids",
@@ -1329,6 +1384,12 @@ async def test_get_automatic_entity_ids(
"test_domain.test_1", "test_domain.test_1",
"test_domain.test_2", "test_domain.test_2",
"test_domain.test_3", "test_domain.test_3",
"test_domain.test_4",
"test_domain.test_5",
"test_domain.test_6",
"test_domain.test_7",
"test_domain.test_8",
"test_domain.test_9",
"test_domain.unknown", "test_domain.unknown",
], ],
} }
@@ -1339,7 +1400,13 @@ async def test_get_automatic_entity_ids(
assert msg["success"] assert msg["success"]
assert msg["result"] == { assert msg["result"] == {
"test_domain.test_1": "test_domain.test_platform_uniq1", # platform + unique_id "test_domain.test_1": "test_domain.test_platform_uniq1", # platform + unique_id
"test_domain.test_2": "test_domain.blabla", # suggested_object_id "test_domain.test_2": "test_domain.suggested_2", # suggested_object_id
"test_domain.test_3": "test_domain.collision_2", # suggested_object_id + _2 "test_domain.test_3": "test_domain.collision_2", # suggested_object_id + _2
"test_domain.test_4": "test_domain.suggested_4", # suggested_object_id
"test_domain.test_5": "test_domain.calculated_5", # calculated_object_id
"test_domain.test_6": "test_domain.suggested_6", # suggested_object_id
"test_domain.test_7": "test_domain.entity_name_7", # entity name property
"test_domain.test_8": "test_domain.suggested_6", # suggested_object_id
"test_domain.test_9": "test_domain.entity_name_7", # name by user in registry
"test_domain.unknown": None, # no test_domain.unknown in registry "test_domain.unknown": None, # no test_domain.unknown in registry
} }