Fix battery time-remaining on macOS 27 (IOPowerSources returns -1)#7
Open
heath-toby wants to merge 1 commit into
Open
Fix battery time-remaining on macOS 27 (IOPowerSources returns -1)#7heath-toby wants to merge 1 commit into
heath-toby wants to merge 1 commit into
Conversation
On macOS 27 the high-level IOPowerSources API -- which hs.battery.timeRemaining()
and timeToFullCharge() (and pmset / Activity Monitor) rely on -- returns -1
("no estimate"), so the battery readout only ever said "Calculating time until
empty/full" and never gave a time.
The battery firmware's own estimate is still published in the raw
AppleSmartBattery IORegistry node, so fall back to that when the API returns -1,
and to an independent RemainingCapacity / Amperage calculation if even that is
missing. The standard API is still tried first, so behaviour is unchanged on
macOS <= 26 (and if Apple later fixes the API).
Also distinguishes actively "Charging." from "Plugged in, not charging." (e.g.
sitting at an 80% charge limit).
Co-Authored-By: Claude Opus 4.8 <[email protected]>
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.
Summary
On macOS 27 beta the battery hotkey only ever announces "Calculating time until empty/full" — the time estimate never appears.
Cause
hs.battery.timeRemaining()/timeToFullCharge()read the high-level IOPowerSources API, which macOS 27 broke: it returns-1("no estimate").pmset -g battand Activity Monitor show the same "no estimate", so it is an OS-level regression, not a Hammerspoon one.Fix
Layered, best source first — a no-op on macOS ≤ 26:
hs.batteryAPI. On macOS ≤ 26 (and if Apple ever fixes 27) it returns a valid estimate and is used unchanged.-1, read the battery firmware's own estimate from the rawAppleSmartBatteryIORegistry node (AvgTimeToEmpty/AvgTimeToFull), which is still populated on 27.RemainingCapacity ÷ Amperage.Also distinguishes actively
Charging.fromPlugged in, not charging.(e.g. sitting at an 80% charge limit).Testing
Tested on macOS 27 beta (Apple Silicon): accurate times again (e.g. "9 hours, 7 minutes until empty"). Cross-checked the firmware estimate against the independent calc — within ~20 min of each other.
readSmartBatteryshells out toioregonly when the API is actually broken.🤖 Generated with Claude Code