Fix discovery crash from quirk removing ZCL attributes#788
Draft
TheJulianJES wants to merge 6 commits into
Draft
Fix discovery crash from quirk removing ZCL attributes#788TheJulianJES wants to merge 6 commits into
TheJulianJES wants to merge 6 commits into
Conversation
Custom v1 quirks that fully override a standard cluster's `attributes` dict (e.g. `attributes = LocalDataCluster.attributes.copy()` on an `OnOff` cluster) produce clusters without standard attribute definitions. `Cluster.is_attribute_unsupported()` raises `KeyError` for unknown attribute names, which propagated out of `Switch._is_supported()` and failed the whole gateway initialization. Check `attributes_by_name` first, like all other `_is_supported` implementations already do. Also fix the check order in `WindowCoveringInversionSwitch._is_supported`, where the existing guard ran after `is_attribute_unsupported()`.
`configure_cluster_configs` aggregates configs from discovered entities before they are filtered by `is_supported()`, so a quirk-replaced cluster missing standard attribute definitions made `find_attribute()` raise `KeyError` during device configuration. Skip such attributes with a debug log, matching how the attribute read path already tolerates them.
A sibling entity's `_server_cluster_config` can list attributes beyond its own `_attribute_name`, which may not exist on a quirk-replaced cluster, making `is_attribute_unsupported()` raise `KeyError` during polling. Check `attributes_by_name` first.
Reproduces the `KeyError: 'on_off'` from home-assistant/core#173265
Avoids registering a v1 quirk in the global `DEVICE_REGISTRY`.
An `AttributeDefs` class inheriting `BaseAttributeDefs` instead of `OnOff.AttributeDefs` replaces the standard attribute definitions the same way the legacy `attributes` dict override does.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #788 +/- ##
=======================================
Coverage 97.41% 97.41%
=======================================
Files 50 50
Lines 10419 10423 +4
=======================================
+ Hits 10150 10154 +4
Misses 269 269 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DRAFT.
Proposed change
This fixes an issue where a (custom) quirk can remove standard ZCL attributes. Most entity platforms already have guards checking if the attribute even exists, but some do not. This adds them.
Additional information
I'm not sure if this is something we should add – quirks shouldn't misbehave like this. Or are there valid use-cases for deleting ZCL attributes...? But currently, ZHA startup breaks completely when using these custom quirks.
Should address:
This "regression" was introduced with:
AI summary
Issue and fix summary (CLICK TO EXPAND)
Issue
Some custom v1 quirks fully replace a standard cluster's attribute definitions, e.g. the widely used
ts0601_trv_moes.pyfrom jacekk015/zha_quirks does:The resulting
OnOffcluster has noon_off/start_up_on_offattribute definitions.Cluster.is_attribute_unsupported()(andfind_attribute()) raiseKeyErrorfor attribute names without a definition.Since #657,
Switch._is_supported()callscluster.is_attribute_unsupported("on_off")during entity discovery. TheKeyErrorpropagated throughDevice._add_pending_entities()→Gateway.load_devices()→ HA'sasync_setup_entry, so one broken custom quirk prevented the entire ZHA integration from starting (ConfigEntryNotReadyretry loop). Previously, the cluster-handler-based code tolerated these clusters.Affected code paths
Switch._is_supported()was the only_is_supportedimplementation missing theattributes_by_nameguard that all other platforms already had (the crash from the linked issue).WindowCoveringInversionSwitch._is_supported()had the guard, but evaluatedis_attribute_unsupported()first.configure_cluster_configs()calledfind_attribute()unguarded on reporting attributes aggregated from entities not yet filtered byis_supported(), failing on device join/reconfigure.AggregatedClusterPoller.async_update()calledis_attribute_unsupported()unguarded on sibling entities' cluster-config attributes during polling.Fix
Check
attr_name in cluster.attributes_by_namebefore callingis_attribute_unsupported()/find_attribute()at the four sites above, treating attributes without definitions as unsupported (entity not created / reporting skipped with a debug log).A regression test joins a device with a quirks v2 quirk (local registry) that replaces the
OnOffcluster with one whoseAttributeDefsdoes not inherit the standard definitions, reproducing the exactKeyError: 'on_off'from the issue on unfixed code, and asserts device initialization succeeds with no switch entity created.