From 9861bd88b9b773ec314b97228f2bf08b410df170 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 2 May 2025 04:44:38 -0500 Subject: [PATCH] Avoid working out suggested id in entity_platform when already registered (#144079) If the entity is already registered, avoid trying to work out the suggested_entity_id and suggested_object_id as async_get_or_create will discard them anyways. --- homeassistant/helpers/entity_platform.py | 41 ++++++++++++++---------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/homeassistant/helpers/entity_platform.py b/homeassistant/helpers/entity_platform.py index d4fa567e929..f543891d3f3 100644 --- a/homeassistant/helpers/entity_platform.py +++ b/homeassistant/helpers/entity_platform.py @@ -843,24 +843,31 @@ class EntityPlatform: else: device = None - # An entity may suggest the entity_id by setting entity_id itself - suggested_entity_id: str | None = entity.entity_id - if suggested_entity_id is not None: - suggested_object_id = split_entity_id(entity.entity_id)[1] - else: - if device and entity.has_entity_name: - device_name = device.name_by_user or device.name - if entity.use_device_name: - suggested_object_id = device_name - else: - suggested_object_id = ( - f"{device_name} {entity.suggested_object_id}" - ) - if not suggested_object_id: - suggested_object_id = entity.suggested_object_id + if not registered_entity_id: + # Do not bother working out a suggested_object_id + # if the entity is already registered as it will + # be ignored. + # + # An entity may suggest the entity_id by setting entity_id itself + suggested_entity_id: str | None = entity.entity_id + if suggested_entity_id is not None: + suggested_object_id = split_entity_id(entity.entity_id)[1] + else: + if device and entity.has_entity_name: + device_name = device.name_by_user or device.name + if entity.use_device_name: + suggested_object_id = device_name + else: + suggested_object_id = ( + f"{device_name} {entity.suggested_object_id}" + ) + if not suggested_object_id: + suggested_object_id = entity.suggested_object_id - if self.entity_namespace is not None: - suggested_object_id = f"{self.entity_namespace} {suggested_object_id}" + if self.entity_namespace is not None: + suggested_object_id = ( + f"{self.entity_namespace} {suggested_object_id}" + ) disabled_by: RegistryEntryDisabler | None = None if not entity.entity_registry_enabled_default: