Skip to content

Remove unused _state attribute from binary sensors#821

Merged
TheJulianJES merged 1 commit into
devfrom
zigpy-bot/remove-binary-sensor-dead-state
Jul 9, 2026
Merged

Remove unused _state attribute from binary sensors#821
TheJulianJES merged 1 commit into
devfrom
zigpy-bot/remove-binary-sensor-dead-state

Conversation

@TheJulianJES

Copy link
Copy Markdown
Contributor

Addresses comment in:

What

BinarySensor._state is only ever assigned, never read, so this drops the four dead write sites (__init__, is_on, handle_attribute_updated, async_update).

Why

The state of a binary sensor is always recomputed directly from the cluster:

  • the state property returns self.is_on,
  • is_on reads self._cluster.get(self._attribute_name), and
  • maybe_emit_state_changed_event compares self.state (→ is_on).

Nothing reads self._state — not the module, not the base PlatformEntity, not the tests. (Other platforms such as switch, light, lock and cover genuinely use their own _state; this is specific to binary_sensor.)

Removing the attribute also means __init__ no longer calls is_on, so the init-time guard added in #803 (if not self.is_supported(): self._state = False, to stop is_on raising while importing legacy diagnostics) is no longer needed — this can replace that hunk. See #803 (comment).

`BinarySensor._state` was only ever assigned, never read: the `state`
property and `is_on` recompute directly from the cluster, and
`maybe_emit_state_changed_event` compares `self.state`. Drop the dead
assignments in `__init__`, `is_on`, `handle_attribute_updated` and
`async_update`.
@codecov

codecov Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.27%. Comparing base (9179b0d) to head (b7f0844).
⚠️ Report is 1 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #821      +/-   ##
==========================================
- Coverage   97.29%   97.27%   -0.02%     
==========================================
  Files          55       55              
  Lines       10933    10930       -3     
==========================================
- Hits        10637    10632       -5     
- Misses        296      298       +2     

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

@puddly puddly 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.

Nice!

@TheJulianJES

TheJulianJES commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Apparently this (unintentionally) tested failing entity creation 😅: https://app.codecov.io/gh/zigpy/zha/pull/821/indirect-changes. Whatever entity that affects is now correctly filtered out using _is_supported instead.

But maybe we should still have a basic test for the broad except.

@TheJulianJES
TheJulianJES marked this pull request as ready for review July 9, 2026 21:35
Copilot AI review requested due to automatic review settings July 9, 2026 21:35
@TheJulianJES
TheJulianJES merged commit c38e03e into dev Jul 9, 2026
10 of 11 checks passed

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

This PR removes the unused BinarySensor._state attribute and its associated write sites, relying solely on the cluster attribute cache for binary sensor state computation.

Changes:

  • Drop _state initialization in BinarySensor.__init__.
  • Remove dead _state writes in is_on, handle_attribute_updated, and async_update.
  • Keep state-change emission based on recomputed state (via is_on) rather than a stored field.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 145 to +147
def is_on(self) -> bool:
"""Return True if the switch is on based on the state machine."""
self._state = raw_state = self._cluster.get(self._attribute_name)
raw_state = self._cluster.get(self._attribute_name)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well, that's unrelated to this PR but a good thing to fix.

zigpy-review-bot added a commit that referenced this pull request Jul 9, 2026
`discover_entities_for_endpoint` wraps each entity instantiation in a
broad `except` so one broken entity is skipped (and logged) rather than
aborting discovery for the whole endpoint. This path was previously only
exercised incidentally; #821 removed the `BinarySensor._state = self.is_on`
init call that used to trip it, leaving discovery.py:355-357 uncovered.

Add an explicit test: patch `Switch.__init__` to raise, join the device,
and assert the failure is logged and the switch entity skipped while the
device still joins.
@TheJulianJES
TheJulianJES deleted the zigpy-bot/remove-binary-sensor-dead-state branch July 17, 2026 01:41
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.

4 participants