1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-30 06:33:51 +00:00
This commit is contained in:
J. Nick Koston
2025-06-24 22:18:29 +02:00
parent ac3598f12a
commit 48f2911434
3 changed files with 83 additions and 74 deletions

View File

@@ -523,8 +523,8 @@ class EsphomeCore:
# Key: platform name (e.g. "sensor", "binary_sensor"), Value: count
self.platform_counts: defaultdict[str, int] = defaultdict(int)
# Track entity unique IDs to handle duplicates
# Key: (device_id, platform, object_id), Value: count of duplicates
self.unique_ids: dict[tuple[int, str, str], int] = {}
# Set of (device_id, platform, object_id) tuples
self.unique_ids: set[tuple[int, str, str]] = set()
# Whether ESPHome was started in verbose mode
self.verbose = False
# Whether ESPHome was started in quiet mode
@@ -556,7 +556,7 @@ class EsphomeCore:
self.loaded_integrations = set()
self.component_ids = set()
self.platform_counts = defaultdict(int)
self.unique_ids = {}
self.unique_ids = set()
PIN_SCHEMA_REGISTRY.reset()
@property

View File

@@ -99,23 +99,21 @@ async def setup_entity(var: MockObj, config: ConfigType, platform: str) -> None:
"Entity has empty name, using '%s' as object_id base", base_object_id
)
# Handle duplicates
# Check for duplicates
unique_key: tuple[int, str, str] = (device_id, platform, base_object_id)
if unique_key in CORE.unique_ids:
# Found duplicate, add suffix
count = CORE.unique_ids[unique_key] + 1
CORE.unique_ids[unique_key] = count
object_id = f"{base_object_id}_{count}"
_LOGGER.info(
"Duplicate %s entity '%s' found. Renaming to '%s'",
platform,
config[CONF_NAME],
object_id,
# Found duplicate - fail validation
from esphome.config_validation import Invalid
entity_name = config[CONF_NAME] or base_object_id
device_prefix = f" on device '{device_name}'" if device_name else ""
raise 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."
)
else:
# First occurrence
CORE.unique_ids[unique_key] = 1
# First occurrence - register it
CORE.unique_ids.add(unique_key)
object_id = base_object_id
add(var.set_object_id(object_id))