1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-29 22:24:26 +00:00

Fix entity hash collisions by enforcing unique names across devices per platform (#9276)

This commit is contained in:
J. Nick Koston
2025-06-30 21:38:19 -05:00
committed by GitHub
parent db7a420e54
commit 9a0ba1657e
4 changed files with 175 additions and 104 deletions

View File

@@ -184,25 +184,27 @@ def entity_duplicate_validator(platform: str) -> Callable[[ConfigType], ConfigTy
# No name to validate
return config
# Get the entity name and device info
# Get the entity name
entity_name = config[CONF_NAME]
device_id = "" # Empty string for main device
# Get device name if entity is on a sub-device
device_name = None
if CONF_DEVICE_ID in config:
device_id_obj = config[CONF_DEVICE_ID]
# Use the device ID string directly for uniqueness
device_id = device_id_obj.id
device_name = device_id_obj.id
# For duplicate detection, just use the sanitized name
name_key = sanitize(snake_case(entity_name))
# Calculate what object_id will actually be used
# This handles empty names correctly by using device/friendly names
name_key = get_base_entity_object_id(
entity_name, CORE.friendly_name, device_name
)
# Check for duplicates
unique_key = (device_id, platform, name_key)
unique_key = (platform, name_key)
if unique_key in CORE.unique_ids:
device_prefix = f" on device '{device_id}'" if device_id else ""
raise cv.Invalid(
f"Duplicate {platform} entity with name '{entity_name}' found{device_prefix}. "
f"Each entity on a device must have a unique name within its platform."
f"Duplicate {platform} entity with name '{entity_name}' found. "
f"Each entity must have a unique name within its platform across all devices."
)
# Add to tracking set