Detect duplicate entities in discovery and log unique ID collisions
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
- Domain
- backend, testing-qa
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
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_entityraises 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 nounique_id_suffixboth 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
QuirkV2Deviceand 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_listis the other implicit deduplication:IdentifyButtonuses it to keep one identify button per device. puddly's fork branchpuddly/drop-is-supported-in-list(no PR yet) removesis_supported_in_listand 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_listitem 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from zigpy/zha
-
Device override creates the generic entity instead of the vendor-specific one (Light instead of HueLight, ForceOnLight, MinTransitionLight)Possibly taken @zigpy-review-bot claimed this today. Openbot: code audit
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Each time a device becomes available again, ZHA adds another firmware update listener that is never releasedPossibly taken @zigpy-review-bot claimed this 1 day ago. Openbot: code audit
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
Maintainers usually reply within 1 day
-
Devices that left the network are re-initialized on every availability check, reading attributes from a device that is gonePossibly taken @zigpy-review-bot claimed this 1 day ago. Openbot: code audit
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
device specific quirks
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
pimutils/vdirsyncer#1242 ·
-
priority:P2
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
QuantConnect/lean-cli#792 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
open-webui/open-webui#35360 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Suggest link: Destiny Verify (MIT zero-dependency pre-commit security, citation & link hygiene CLI)Open
Difficulty 1/5 Under an hour Newbie friendliness 68/100