Skip to content

fix: compare battery_low_binary_state as the bool it is, not a string - #5081

Merged
andrew-codechimp merged 1 commit into
andrew-codechimp:mainfrom
proscar87:fix/binary-battery-low-state-on-coordinator-refresh
Aug 31, 2026
Merged

andrew-codechimp merged 1 commit into
andrew-codechimp:mainfrom
proscar87:fix/binary-battery-low-state-on-coordinator-refresh

Conversation

@proscar87

Copy link
Copy Markdown
Contributor

Problem

BatteryNotesBatteryBinaryLowSensor._handle_coordinator_update():

self._attr_is_on = self.coordinator.battery_low_binary_state == "on"

battery_low_binary_state is a bool — set in async_state_changed_listener as wrapped_battery_low_state.state == "on". Comparing it against the string "on" again is always False, regardless of the actual value. So every coordinator refresh (HA startup, integration reload, async_refresh()) resets the exposed entity to "off" no matter what the source sensor says.

The sibling code path, async_state_changed_listener (fires on a live state-change event, e.g. toggling the source sensor in Developer Tools), does it correctly a few lines above:

self._attr_is_on = self.coordinator.battery_low_binary_state

This fully explains #5078's reported symptom and reproduction steps: correct after a manual toggle, wrong after add/restart/reload.

Relation to #5080

#5080 (merged just before I opened this) fixed a related-but-different bug in the same area: wrapped_battery_low wasn't being wired up on the coordinator for this sensor type, and async_added_to_hass() wasn't calling super(). That's likely why some configurations stopped showing the symptom after #5080 — but it didn't touch this comparison, which is still present and still wrong on main. With #5080's coordinator change now reliably setting wrapped_battery_low, _handle_coordinator_update reaches this line on every refresh going forward, so it's worth fixing directly rather than relying on the earlier guard rarely being hit.

Fix

Assign the bool directly, mirroring async_state_changed_listener's already-correct pattern.

Testing

Added tests/test_binary_sensor.py (4 tests, constructing the sensor via __new__ with a mock coordinator/hass since this needs no more than _handle_coordinator_update touches):

  • reflects the stored bool for both True/False
  • matches what async_state_changed_listener would have set for the same underlying state
  • unavailable/unknown source still correctly clears state regardless of the stored bool

Confirmed the two True-expecting tests fail against the pre-fix line (git stash on just binary_sensor.py) with the exact reported symptom (False instead of True), and pass with the fix. Full suite: 14/14 (10 pre-existing + 4 new). ruff check, ruff format --check, mypy --check-untyped-defs: all clean.

_handle_coordinator_update() compared coordinator.battery_low_binary_state
(a bool, set by async_state_changed_listener as
wrapped_battery_low_state.state == "on") against the string "on" again --
always False regardless of the real value. A coordinator refresh (e.g. HA
startup/reload) always reset the exposed entity to "off"; only a live
state-change event (which assigns _attr_is_on directly from the bool, no
string comparison) ever showed the correct state.

andrew-codechimp#5080 fixed a related bug in the same area (wrapped_battery_low wasn't
being wired up for this sensor type, and async_added_to_hass() wasn't
calling super()), which is likely why issue andrew-codechimp#5078's reporter stopped
seeing the symptom in some configurations. But that fix didn't touch this
comparison, and it's still live and still wrong: with andrew-codechimp#5080's coordinator
change now reliably setting wrapped_battery_low, this line runs on every
refresh going forward, so it's worth fixing outright rather than relying
on the guard rarely being reached.

Fix mirrors the already-correct pattern in async_state_changed_listener:
assign the bool directly.
@andrew-codechimp
andrew-codechimp merged commit 15a0eb2 into andrew-codechimp:main Aug 31, 2026
4 checks passed
@andrew-codechimp

Copy link
Copy Markdown
Owner

Thanks for this, thought I had it solved with the other changes I made but this bit I missed.

I've not implemented full tests yet but it's something I want to work towards so great to have these as well.

@proscar87
proscar87 deleted the fix/binary-battery-low-state-on-coordinator-refresh branch October 3, 2026 01:50
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.

[Bug]: Source binary battery sensor initial 'on' state does not trigger battery_low_binary_state setter on setup/restart

2 participants