Skip to content

Scope quirk attribute update state to the cluster instance - #1896

Open
zigpy-review-bot wants to merge 3 commits into
devfrom
zigpy-bot/instance-scoped-attribute-update-suppression
Open

zigpy-review-bot wants to merge 3 commits into
devfrom
zigpy-bot/instance-scoped-attribute-update-suppression

Conversation

@zigpy-review-bot

Copy link
Copy Markdown
Collaborator

Proposed change

While a reported or read attribute is passed through a quirk's _update_attribute, zigpy suppresses the AttributeUpdatedEvent for that attribute (#1754) and remembers the manufacturer code to look it up with (#1760). Both were context variables, and neither was tied to the cluster instance the attribute belongs to. That made them apply in places they were never meant for:

  • Tasks and timers started from _update_attribute inherit context variables. If such a task or timer updates the same attribute later, the value only ends up in the attribute cache: no AttributeUpdatedEvent is emitted, so the update isn't persisted and ZHA isn't notified. This is the "Separate issue" described in Fix IKEA battery percentage being doubled twice after firmware read zha-device-handlers#5397: the IKEA battery quirk reads the firmware version in a background task and then corrects the battery percentage, and HA keeps showing the uncorrected value until something else refreshes the entity or the next battery report arrives.
  • The suppression was keyed by cluster ID and attribute ID. A quirk that forwards the attribute to the same cluster on another endpoint (or another device) had that update's event suppressed as well, and nothing emitted it afterwards.
  • The manufacturer code was matched on the attribute ID alone. A quirk that forwards a manufacturer-specific report to an attribute with the same ID on another cluster had that attribute looked up with the manufacturer code. For a standard attribute the lookup fails, so the value was stored as an unknown "legacy" attribute and the event had no attribute name.

This PR replaces both context variables with a single dict on the cluster instance, mapping the attribute IDs currently being passed through the quirk to their manufacturer codes. It's only filled for the duration of the synchronous _update_attribute call, so it can't apply to anything running later, and it only applies to the cluster instance that received the attribute. _update_attribute calls for that attribute ID on that cluster behave as before.

Additional information

  • This should not be released before Fix IKEA battery percentage being doubled twice after firmware read zha-device-handlers#5397. Without that fix, the IKEA quirk's background task writes the battery percentage doubled twice (a report of 50 becomes 200). Today that wrong value stays hidden in the cache because its event is suppressed. With this PR it is emitted, so old-firmware IKEA devices would show twice the real percentage (more than 100% above half charge) until the next report. With both changes, the task emits the correct value (verified with a battery report sent through the quirk at the head of that PR: AttributeReportedEvent 50, then AttributeUpdatedEvent 100).
  • Behavior change for quirks: an update of the same attribute from a task or timer started in _update_attribute is now treated like any other update the quirk does on its own. It emits an AttributeUpdatedEvent, and the attribute is looked up without the manufacturer code. If the cluster defines several attributes with that ID, a deferred update by integer ID is therefore ambiguous and is stored as a legacy value, where it previously updated the manufacturer-specific attribute by accident. I found no quirk in zha-device-handlers that does this.
  • Tests: test_quirk_deferred_attribute_update_emits_event (task and timer, report and read), test_quirk_forwards_attribute_update_to_other_endpoint (report and read) and test_quirk_manufacturer_code_context_other_cluster (known and unknown attribute) cover the three cases above and fail on dev without the fix. test_quirk_attribute_update_nested_and_failing covers nesting and cleanup after an exception.
  • The zha and zha-device-handlers test suites pass against this branch.

@codecov

codecov Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.49%. Comparing base (fb63170) to head (28b5bb6).
⚠️ Report is 2 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1896      +/-   ##
==========================================
- Coverage   99.51%   99.49%   -0.02%     
==========================================
  Files          61       61              
  Lines       12733    13129     +396     
==========================================
+ Hits        12671    13063     +392     
- Misses         62       66       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The implementation correctly scopes transient state and has comprehensive regression coverage; only a minor documentation wording issue remains.

Review effort: Balanced
Findings: 1 Low severity

Open (1)
What changed in this PR

Scopes quirk attribute-update state to individual cluster instances, preventing deferred or forwarded updates from being incorrectly suppressed.

Changes:

  • Replaces global context variables with per-cluster tracking.
  • Adds coverage for deferred, forwarded, manufacturer-specific, nested, and failing updates.
File Description
zigpy/​zcl/​__init__.py Implements cluster-scoped quirk update tracking.
tests/​test_zcl.py Tests isolation, cleanup, and deferred behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread zigpy/zcl/__init__.py Outdated
@zigpy-review-bot zigpy-review-bot added the bugfix This PR fixes a bug label Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix This PR fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants