Derive entity registry key from _cluster_match#813
Conversation
`@register_entity(cluster_id)` took an explicit cluster ID that was used only as a key into `ENTITY_REGISTRY` to speed up discovery lookups — the actual matching is done entirely by the class's `_cluster_match`. The ID was therefore redundant: in every one of the 304 registrations it was one of the match's mandatory clusters (`server_clusters`/`client_clusters`), and nothing enforced that it was. Make `register_entity` a bare decorator that derives the key from the match. Since every mandatory cluster must be present for a match to succeed, indexing under any single one is a sufficient discovery key, so each class stays in exactly one bucket (no double-evaluation during discovery). The resulting `ENTITY_REGISTRY` is byte-for-byte identical to before — same buckets, same ordering — so discovery behavior is unchanged. Also drop the now-unused `OnOff as OnOffCluster` alias in cover.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev #813 +/- ##
==========================================
- Coverage 97.29% 97.27% -0.02%
==========================================
Files 55 55
Lines 10934 10937 +3
==========================================
+ Hits 10638 10639 +1
- Misses 296 298 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR refactors entity discovery registration by making @register_entity derive its registry bucket key from each entity’s _cluster_match, removing the previously redundant explicit cluster-id argument while keeping discovery behavior the same.
Changes:
- Convert
register_entityfrom a decorator factory (@register_entity(cluster_id)) into a bare decorator (@register_entity) that computes the registry key from_cluster_match. - Update all platform entity registrations to use the new bare decorator form.
- Remove an unused import alias in the cover platform (
OnOff as OnOffCluster).
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| zha/application/platforms/init.py | Reworks register_entity to derive the ENTITY_REGISTRY key from _cluster_match and adds validation. |
| zha/application/platforms/virtual.py | Updates all virtual entities to use @register_entity without an explicit cluster id. |
| zha/application/platforms/update.py | Updates OTA update entities to use the bare @register_entity decorator. |
| zha/application/platforms/switch.py | Updates switch entities to use the bare @register_entity decorator. |
| zha/application/platforms/siren.py | Updates siren entities to use the bare @register_entity decorator. |
| zha/application/platforms/sensor/init.py | Updates sensor entities to use the bare @register_entity decorator. |
| zha/application/platforms/select.py | Updates select entities to use the bare @register_entity decorator. |
| zha/application/platforms/number/init.py | Updates number entities to use the bare @register_entity decorator. |
| zha/application/platforms/lock/init.py | Updates lock entity to use the bare @register_entity decorator. |
| zha/application/platforms/light/init.py | Updates light entities to use the bare @register_entity decorator. |
| zha/application/platforms/fan/init.py | Updates fan entities to use the bare @register_entity decorator. |
| zha/application/platforms/device_tracker.py | Updates device tracker entity to use the bare @register_entity decorator. |
| zha/application/platforms/cover/init.py | Updates cover entities to use the bare @register_entity decorator and removes unused OnOffCluster alias import. |
| zha/application/platforms/climate/init.py | Updates climate entities to use the bare @register_entity decorator. |
| zha/application/platforms/button/init.py | Updates button entities to use the bare @register_entity decorator. |
| zha/application/platforms/binary_sensor/init.py | Updates binary sensor entities to use the bare @register_entity decorator. |
| zha/application/platforms/alarm_control_panel/init.py | Updates alarm control panel entity to use the bare @register_entity decorator. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
DRAFT.
@register_entity(cluster_id)took an explicit cluster ID that was used only as a key intoENTITY_REGISTRYto speed up discovery lookups — the actual matching is done entirely by the class's_cluster_match. The ID was therefore redundant: in every one of the 304 registrations it was one of the match's mandatory clusters (server_clusters/client_clusters), and nothing enforced that it was.Make
register_entitya bare decorator that derives the key from the match. Since every mandatory cluster must be present for a match to succeed, indexing under any single one is a sufficient discovery key, so each class stays in exactly one bucket (no double-evaluation during discovery). The resultingENTITY_REGISTRYis byte-for-byte identical to before — same buckets, same ordering — so discovery behavior is unchanged.Also drop the now-unused
OnOff as OnOffClusteralias in cover.