Handle current entity id matches the automatic entity id

This commit is contained in:
Erik 2025-05-13 09:09:59 +02:00
parent 3f1d3bfc4a
commit 3e19b5f6d5
2 changed files with 17 additions and 4 deletions

View File

@ -9,13 +9,13 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components import websocket_api
from homeassistant.components.websocket_api import ERR_NOT_FOUND, require_admin
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant, callback, split_entity_id
from homeassistant.helpers import (
config_validation as cv,
device_registry as dr,
entity_component,
entity_registry as er,
)
from homeassistant.helpers.entity_component import async_get_entity_suggested_object_id
from homeassistant.helpers.json import json_dumps
@ -344,10 +344,15 @@ def websocket_get_automatic_entity_ids(
if not (entry := registry.entities.get(entity_id)):
automatic_entity_ids[entity_id] = None
continue
if (
suggested := async_get_entity_suggested_object_id(hass, entity_id)
) == split_entity_id(entry.entity_id)[1]:
# No need to generate a new entity ID
automatic_entity_ids[entity_id] = None
continue
automatic_entity_ids[entity_id] = registry.async_generate_entity_id(
entry.domain,
entity_component.async_get_entity_suggested_object_id(hass, entity_id)
or f"{entry.platform}_{entry.unique_id}",
suggested or f"{entry.platform}_{entry.unique_id}",
)
connection.send_message(

View File

@ -1361,6 +1361,12 @@ async def test_get_automatic_entity_ids(
unique_id="uniq9",
platform="test_domain",
),
"test_domain.test_10": RegistryEntryWithDefaults(
entity_id="test_domain.test_10",
unique_id="uniq10",
platform="test_domain",
suggested_object_id="test_10",
),
"test_domain.collision": RegistryEntryWithDefaults(
entity_id="test_domain.collision",
unique_id="uniq_collision",
@ -1390,6 +1396,7 @@ async def test_get_automatic_entity_ids(
"test_domain.test_7",
"test_domain.test_8",
"test_domain.test_9",
"test_domain.test_10",
"test_domain.unknown",
],
}
@ -1408,5 +1415,6 @@ async def test_get_automatic_entity_ids(
"test_domain.test_7": "test_domain.entity_name_7", # entity name property
"test_domain.test_8": "test_domain.suggested_8", # suggested_object_id
"test_domain.test_9": "test_domain.name_by_user_9", # name by user in registry
"test_domain.test_10": None, # automatic entity id matches current entity id
"test_domain.unknown": None, # no test_domain.unknown in registry
}