From 3e19b5f6d53320756ab0f33e82e73969bddb18da Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 13 May 2025 09:09:59 +0200 Subject: [PATCH] Handle current entity id matches the automatic entity id --- homeassistant/components/config/entity_registry.py | 13 +++++++++---- tests/components/config/test_entity_registry.py | 8 ++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/config/entity_registry.py b/homeassistant/components/config/entity_registry.py index 02486bc33a0..b011b476187 100644 --- a/homeassistant/components/config/entity_registry.py +++ b/homeassistant/components/config/entity_registry.py @@ -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( diff --git a/tests/components/config/test_entity_registry.py b/tests/components/config/test_entity_registry.py index 694c931f1b8..9e0c1f584b5 100644 --- a/tests/components/config/test_entity_registry.py +++ b/tests/components/config/test_entity_registry.py @@ -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 }