Skip to content

Warn instead of crashing on multiple explicitly primary entities - #877

Draft
TheJulianJES wants to merge 1 commit into
zigpy:devfrom
TheJulianJES:tjj/warn-multiple-explicit-primary
Draft

Warn instead of crashing on multiple explicitly primary entities#877
TheJulianJES wants to merge 1 commit into
zigpy:devfrom
TheJulianJES:tjj/warn-multiple-explicit-primary

Conversation

@TheJulianJES

Copy link
Copy Markdown
Contributor

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 hit assert not explicitly_primary — crashing device initialization, which at gateway startup runs in a bare asyncio.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 primary themselves (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 on dev with the AssertionError.

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

codecov Bot commented Aug 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.19%. Comparing base (f6273ae) to head (ab1e087).

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.
📢 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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants