Repository navigation
fix: compare battery_low_binary_state as the bool it is, not a string - #5081
Merged
andrew-codechimp merged 1 commit intoAug 31, 2026
Conversation
_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.
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. |
6 tasks done
andrew-codechimp
pushed a commit
that referenced
this pull request
Aug 31, 2026
…#5081) Co-authored-by: Oscar Pacheco <[email protected]>
proscar87
deleted the
fix/binary-battery-low-state-on-coordinator-refresh
branch
October 3, 2026 01:50
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.
Problem
BatteryNotesBatteryBinaryLowSensor._handle_coordinator_update():battery_low_binary_stateis a bool — set inasync_state_changed_listeneraswrapped_battery_low_state.state == "on". Comparing it against the string"on"again is alwaysFalse, 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: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_lowwasn't being wired up on the coordinator for this sensor type, andasync_added_to_hass()wasn't callingsuper(). 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 onmain. With #5080's coordinator change now reliably settingwrapped_battery_low,_handle_coordinator_updatereaches 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_updatetouches):True/Falseasync_state_changed_listenerwould have set for the same underlying stateConfirmed the two
True-expecting tests fail against the pre-fix line (git stashon justbinary_sensor.py) with the exact reported symptom (Falseinstead ofTrue), 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.