Skip to content

fix(status): improve battery health percentage accuracy#991

Merged
tw93 merged 3 commits into
tw93:mainfrom
manuelguido:fix/status-battery-health-capacity
May 28, 2026
Merged

fix(status): improve battery health percentage accuracy#991
tw93 merged 3 commits into
tw93:mainfrom
manuelguido:fix/status-battery-health-capacity

Conversation

@manuelguido
Copy link
Copy Markdown
Contributor

@manuelguido manuelguido commented May 27, 2026

Summary

Fixes inaccurate battery health percentages in mo status by preferring the normalized AppleSmartBattery health capacity from ioreg when available.

On newer macOS versions, system_profiler can report a lower maximum capacity than the value shown in System Settings. Mole now uses AppleSmartBattery as the primary source for the health percentage, while keeping system_profiler as a fallback for health condition and older systems.

Fixes

Closes #ISSUE_NUMBER

Changes

  • Prefer normalized AppleSmartBattery MaxCapacity values for battery health percentage.
  • Keep cached system_profiler JSON/text parsing as fallback.
  • Add regression tests for the reported 97% vs 100% mismatch.
  • Ignore raw capacity values that are not valid health percentages.

Testing

  • git diff --check -- cmd/status/metrics_battery.go cmd/status/metrics_battery_test.go
  • VS Code diagnostics: no errors in touched files

Not run:

  • go test ./cmd/status, because the local shell did not have go or gofmt available.

@manuelguido manuelguido requested a review from tw93 as a code owner May 27, 2026 17:33
@manuelguido manuelguido changed the title fix(status): prefer AppleSmartBattery health capacity fix(status): improve battery health percentage accuracy May 27, 2026
Var block alignment was off, causing gofmt -l to flag the file.
Comment thread cmd/status/metrics_battery.go Fixed
CodeQL go/incorrect-integer-conversion flagged the float64-to-int
cast on line 237. The literal "value < 100000" guard wasn't enough
for the analyzer to prove the conversion was safe. Switching to
ioRegValueForKey + strconv.Atoi yields an int directly, no cast.
@tw93 tw93 merged commit 308af18 into tw93:main May 28, 2026
9 checks passed
@manuelguido manuelguido deleted the fix/status-battery-health-capacity branch May 28, 2026 14:57
@manuelguido manuelguido restored the fix/status-battery-health-capacity branch May 28, 2026 14:58
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.

3 participants