Warn instead of crashing on multiple explicitly primary entities - #877
Draft
TheJulianJES wants to merge 1 commit into
Draft
Warn instead of crashing on multiple explicitly primary entities#877TheJulianJES wants to merge 1 commit into
TheJulianJES wants to merge 1 commit into
Conversation
The quirks v2 builder validates against multiple primary entities per device, but a hand-written quirk overriding `discover_entities()` can still produce them. Log a warning and elect no primary entity instead of failing the assert (which crashed device initialization). All explicitly primary entities still report `primary` themselves, as the explicit marking takes precedence over the election.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #877 +/- ##
=======================================
Coverage 97.19% 97.19%
=======================================
Files 57 57
Lines 10543 10545 +2
=======================================
+ Hits 10247 10249 +2
Misses 296 296 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
Prevents device initialization crashes when quirks define multiple explicit primary entities.
Changes:
- Replaces the assertion with a warning and no elected primary.
- Adds regression coverage for election behavior and logging.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
zha/zigbee/device.py |
Handles multiple explicit primaries safely. |
tests/test_device.py |
Tests the new fallback behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Follow-up to a TODO in #861.
The quirks v2 builder validates against multiple primary entities per device, but a hand-written quirk overriding
discover_entities()can still produce them. The election previously hitassert not explicitly_primary— crashing device initialization, which at gateway startup runs in a bareasyncio.gather, so one buggy custom quirk could break startup.The election now logs a warning and elects no primary entity instead. Note that all explicitly primary entities still report
primarythemselves (the explicit marking takes precedence over the election), so a device in this state shows multiple primary entities — the warning is the signal that the quirk is buggy.Tests
test_multiple_explicit_primary_entities: two entities marked explicitly primary (like a buggy hand-written quirk would) no longer crash the election; no primary entity is elected and a warning is logged. Fails ondevwith theAssertionError.