Detect duplicate entities in discovery and log unique ID collisions

Open
#979 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python

Research direction

Start with default entity discovery and Device._add_pending_entities in zha/zigbee/device.py; trace how input and output clusters produce entities with the same (platform, unique_id) and how the pending path handles them. Add coverage for duplicate discovery and for logging when a distinct entity is dropped, then verify discovery yields one entity and the collision log names both. The issue does not identify the relevant test files, so locate existing discovery and device entity tests first.

Written by the indexing model from the issue text.

Description

entities
Summary

Follow-up from zigpy/zha-device-handlers#5410, where duplicate entity detection and unique ID collisions were left for ZHA to fix. ZHA's default discovery can yield the same entity more than once. _add_pending_entities then keeps the first entity for each (platform, unique_id) and drops the rest without logging anything. Discovery should not yield duplicates, and a real collision should be logged.

Problem
  • A cluster that is both an input and an output cluster on an endpoint makes default discovery yield two entity objects with the same platform and unique ID. Seen on the NodOn SIN-4-RS-20 cover and the TS0041A switch snapshots.
  • Device._add_pending_entities (zha/zigbee/device.py, "Ignore entities that already exist") drops a later entity whose key is already taken, with no log. _add_entity raises on the same condition, but the pending path never reaches it.
  • The same silent drop hides quirk mistakes. Two .adds_entity() calls with the same ZCL class on one endpoint and no unique_id_suffix both get {ieee}-{endpoint}. An added ZCL entity without a suffix can also collide with a default light, switch or cover, which use the same {ieee}-{endpoint} form. In both cases only the first entity is created, with nothing in the log.
Effect on zha-quirks

QuirkV2Device._replace_entities (zhaquirks/builder/device.py) works around the duplicates: it matches on distinct (PLATFORM, unique_id) and replaces only the first copy. The second copy keeps the original class and still contributes its cluster configuration. If the replacement then fails its support check, ZHA adds that leftover copy, so the original entity comes back, while a single matching entity would produce no entity at all (the open review threads on device.py in zigpy/zha-device-handlers#5410). A replacement class on a different platform keeps the unique ID but not the (platform, unique_id) key, so the leftover copy is added next to the replacement. Once discovery no longer yields duplicates, all of this goes away without a quirks change.

Proposed fix
  • Don't yield an entity twice from default discovery, so QuirkV2Device and other consumers see each entity once.
  • In _add_pending_entities, log when a different entity is dropped because its (platform, unique_id) is already taken, naming both entities.
  • Optional, on the quirks side: reject colliding .adds_entity() calls when the quirk is built. In zigpy/zha-device-handlers#5410 puddly called that nice to have, not needed for a first pass.
Related
  • is_supported_in_list is the other implicit deduplication: IdentifyButton uses it to keep one identify button per device. puddly's fork branch puddly/drop-is-supported-in-list (no PR yet) removes is_supported_in_list and makes the identify button a single device-level entity. It does not touch the input/output cluster duplicates.
  • zigpy/zha#725 (entity discovery issues) has an is_supported_in_list item that this branch would also settle.
Dominant language
Python
Stars
96
Forks
84
Avg merge
2d 4h
Merged PRs (30d)
30

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from zigpy/zha

All issues in zigpy/zha

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.