Skip to content

fix(battery): aggregate all packs in battery status - #12445

Closed
ansonboby wants to merge 1 commit into
omacom:quattrofrom
ansonboby:fix/multi-battery-status
Closed

ansonboby wants to merge 1 commit into
omacom:quattrofrom
ansonboby:fix/multi-battery-status

Conversation

@ansonboby

Copy link
Copy Markdown

Summary

omarchy-battery-status read only the first UPower battery (grep BAT | head -n 1), so multi-battery machines (ThinkPads, Surface Book) showed one pack's charge as the whole machine.

Now aggregates across every UPower battery: percentage energy-weighted like UPower's DisplayDevice (with mean fallback when energy fields are absent), capacity and draw summed (live sysfs power_now summed across BAT* so the panel stays live), remaining time follows the most urgent pack, state prefers charging > discharging. Thresholds/cycles keep first-available. No consumer changes — --shell keys and the human line are byte-identical in shape, so the power panel and low-battery notification inherit the fix.

Test plan

  • Extended test/shell.d/battery-status-test.sh: single-battery assertions kept (fixture made self-consistent for weighted math), plus a dual-battery section (40% + 60% packs) asserting 50% combined, 100Wh, 10W summed draw, 2h urgent-pack time. All pass; battery-test.sh and bin-style-test.sh unaffected.

Closes #7214, closes #7897, closes #8303, closes #8504, closes #8521, closes #9026, closes #9776, closes #10244, closes #11618, closes #11990.

Single-battery UPower read reported only BAT0 on multi-battery
machines. Percentage is now energy-weighted across packs, capacity
and draw are summed, and remaining time follows the most urgent
pack. No consumer changes: output keys and lines are unchanged.

Closes omacom#7214, closes omacom#7897, closes omacom#8303, closes omacom#8504, closes omacom#8521, closes omacom#9026, closes omacom#9776, closes omacom#10244, closes omacom#11618, closes omacom#11990.
@sebastien-debros

Copy link
Copy Markdown

Tested this PR on real dual-battery hardware, and it fixes the bug.

Hardware: ThinkPad T470s (internal BAT0 + a second pack in BAT1), Omarchy 4.0.4-1, tested at a6aee39.

Raw sysfs:

BAT0: Not charging  energy_now=5.60 Wh  energy_full=19.73 Wh  power_now=0 W
BAT1: Charging      energy_now=7.54 Wh  energy_full=23.37 Wh  power_now=17.0 W
UPower DisplayDevice: 43.1 Wh full, 13.11 Wh now, 30.4% charging, 17.2 W

Before (installed 4.0.4-1, reads only BAT0):

Battery 28%  ·  Holding at 75-80%  ·  0W / 19Wh

After (this PR):

Battery 30%  ·  1h 15m to full  ·   17W / 43Wh
percentage 30%  |  state charging  |  rate 17W  |  size 43Wh  |  time 1h 15m

@ansonboby

Copy link
Copy Markdown
Author

Thanks for testing this on real dual-battery hardware — that's exactly the machine class this targets. Your numbers line up with the intended aggregation: 30% energy-weighted across packs (matching UPower's own DisplayDevice), 43Wh summed capacity, 17W summed live draw, and time-to-full from the charging pack. The BAT0-only 28% / 0W read before is the bug in a nutshell. The detailed before/after makes review easy — appreciate it.

@ansonboby

Copy link
Copy Markdown
Author

Status check — no formal review on this one yet, though sebastien-debros already verified it on real dual-battery hardware: "Tested this PR on real dual-battery hardware, and it fixes the bug." Their test was on a ThinkPad T470s (internal BAT0 plus a second pack in BAT1) at a6aee39, which is the head the PR is on now.

Re-verified against current quattro just now — battery-status-test.sh 6/6, PR MERGEABLE/CLEAN, no failing checks:

  • capacity and remaining-time calculations are owned by the helper rather than left to the caller;
  • percentage is weighted across packs, and capacity and power draw are summed;
  • the reported time is the most urgent pack, and the state is combined.

Happy to take another look at anything, or to add coverage if there's a case the aggregation should handle.

@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed against quattro at a6aee39, alongside the other open pull requests fixing the same bug: #10392, #12024, #12789 (draft, stacked on #10392) and #13292 (which fixes a phantom first pack rather than aggregation, so it is not a competitor). The bug is real: quattro reads only the first BAT* device. But the fix brought forward for the maintainer is #10392, which switches to UPower's DisplayDevice when more than one pack exists and leaves single-battery machines on the code they run today. This one changes the parsing every laptop goes through, and that change has a defect.

Charging time and thresholds are misread, on single-battery machines too. bin/omarchy-battery-status serialises each battery's fields with spaces (the awk END printf) and splits them with a default-IFS read -r. Whitespace splitting collapses empty fields, and UPower never prints both time to empty and time to full, so every field after the missing one shifts left:

  • A charging battery with no thresholds: its time to full lands in empty_mins, full_mins is empty, and the status shows no time (Battery 60% · to full · ...).
  • A battery that reports charge-start-threshold: 75% / charge-end-threshold: 80%: full_mins becomes 75, so a charging machine shows 1h 15m to full whatever the real time is, and the threshold reads as 80% instead of 75-80%. The 1h 15m to full in the dual-battery test above is 75 minutes on a machine whose earlier output shows a 75-80% threshold.

I confirmed the shift by running the awk program and read from this diff on those inputs; the discharging case without thresholds is unaffected, which is the only case the tests cover. A non-whitespace separator (IFS='|' read) would fix the parsing.

Remaining time follows the wrong pack. On ThinkPads the packs drain one after the other, so the earliest pack's time to empty is the time until handover, not until the machine dies: one pack at 10Wh draining at 10W beside an idle one holding 30Wh shows 1h left for about four hours of battery. DisplayDevice computes time from the combined energy and rate. Separately, the summed sysfs rate replaces UPower's total as soon as any one pack has a readable power_now, so a pack without one drops out of the wattage.

Nothing was run on a worker: with #10392 already carried forward for this bug, verifying this one would not change what the maintainer decides. The second opinion, Codex Medium, also ranked #10392 first and found the same parsing shift; it can read this review's reasoning, so that agreement is not guaranteed independent. It added two findings of its own: the partial sysfs sum above, and that single-battery percentage is now recalculated from energy rather than taken from UPower, which changes what single-battery machines display (the original test fixture reports 51% and this would show 49%, which is why that fixture had to change).

This now waits on the maintainer, who decides between this and #10392.

@omarchybot

Copy link
Copy Markdown
Collaborator

Thank you for this, @ansonboby. #10392 fixes the same problem in a way the review judged better, so this is closed in favour of it. If it does not cover your case, please say so there.

Closed at the maintainer's request. The review comment above has the details; it was done by Claude Opus 5.5 with Codex Medium as a second opinion, whose agreement is not independent.

@omarchybot omarchybot closed this Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment