Skip to content

Fix battery status reading the wrong device on multi-battery hardware - #8864

Closed
zmitton wants to merge 2 commits into
omacom:quattrofrom
zmitton:fix/battery-status-multi-battery
Closed

zmitton wants to merge 2 commits into
omacom:quattrofrom
zmitton:fix/battery-status-multi-battery

Conversation

@zmitton

@zmitton zmitton commented Aug 29, 2026

Copy link
Copy Markdown

omarchy-battery-status picks the first BAT* device from upower -e and reads percentage, state, capacity, rate, time, and cycles from it directly. On hardware with more than one battery (e.g. a ThinkPad with a Power Bridge bay), the first-enumerated device can be an idle/empty one — so the panel shows a correct icon and meter (both driven by UPower's DisplayDevice elsewhere in the shell) alongside a completely wrong detail row: in the reported case, 0% with 0W/0Wh and no time remaining while the battery was actually near full.

Fix

  • Read UPower's DisplayDevice instead — the same aggregate source the bar icon and panel meter already use — so the detail row matches what's on screen.
  • Sum the live sysfs wattage refinement across every BAT* device (DisplayDevice has no native-path of its own); an idle battery reading 0 doesn't affect the total, so this is a no-op on single-battery hardware.
  • Take the max cycle count across devices rather than the first, so an idle battery reporting 0 doesn't hide the real count.
  • Charge thresholds still come from an individual device: they're per-ACPI-device policy, and UPower reports the same value on every battery object.

Tests

Added test/shell.d/battery-status-multi-battery-test.sh, which stubs a dual-battery machine (idle BAT0 with all-zero fields, real BAT1) and verifies the status output reports the aggregate reading rather than BAT0's zeroes. Both battery test files pass; the full shell suite shows no new failures versus quattro.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Guuwe3XxYQGY1A9rJizAim

omarchy-battery-status picked the first BAT* device from `upower -e`
and read percentage, state, capacity, rate, time, and cycles from it
directly. On hardware with more than one battery (e.g. a ThinkPad with
a Power Bridge bay), the first-enumerated device can be an idle/empty
one, so the panel shows a correct icon and meter (both driven by
UPower's DisplayDevice elsewhere in the shell) alongside a completely
wrong detail row — in the reported case, 0% with 0W/0Wh and no time
remaining, while the battery was actually near full.

Read UPower's DisplayDevice instead, the same aggregate source the bar
icon and panel meter already use, so the detail row matches what's on
screen. Charge threshold is genuinely per-ACPI-device policy rather
than a per-battery reading (UPower reports the same value on every
battery object), so that lookup still queries an individual device.
The live sysfs wattage refinement and cycle count now sum/max across
every BAT* device instead of reading a single native-path, since an
idle battery reporting 0 doesn't affect either.
Copilot AI balanced review requested due to automatic review settings August 29, 2026 03:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Uses UPower’s aggregate DisplayDevice to report accurate multi-battery status.

Changes:

  • Aggregates battery details and live wattage.
  • Reports the highest cycle count.
  • Adds multi-battery regression coverage.

Tip

If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
bin/omarchy-battery-status Uses aggregate battery data and multi-device telemetry.
test/shell.d/battery-status-test.sh Updates the single-battery UPower fixture.
test/shell.d/battery-status-multi-battery-test.sh Adds a dual-battery regression test.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +83 to +84
grep -Fx $'rate\t10.8W' <<<"$shell_output" >/dev/null || fail "multi-battery status sums live sysfs wattage across every battery" "$shell_output"
grep -Fx $'threshold\t75-80%' <<<"$shell_output" >/dev/null || fail "multi-battery status still reports the charge threshold" "$shell_output"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — fixed in e60f7f8. Both fixture batteries now carry distinguishable nonzero telemetry (BAT0: 4.2W / 12 cycles, BAT1: 10.8W / 347 cycles), and the test asserts the summed rate (15W) and the max cycle count (347), so a regression to picking a single device or the first cycle_count now fails.

…ttery test

The dual-battery fixture had BAT0 contribute 0W and no cycle_count file,
so the rate assertion couldn't tell summing every device apart from just
picking BAT1, and the max-cycle-count behavior wasn't exercised at all.
Give both batteries distinguishable nonzero values (4.2W/12 cycles and
10.8W/347 cycles) and assert the sum (15W) and the maximum (347).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Guuwe3XxYQGY1A9rJizAim
@zmitton

zmitton commented Aug 29, 2026

Copy link
Copy Markdown
Author

Here is a screenshot of the bug. It shows 0% battery but also a nearly-full battery bar.
image

@SurreptitiousSR

Copy link
Copy Markdown

Tested this PR's omarchy-battery-status at e60f7f8 on real dual-battery hardware — a ThinkPad T480 with the internal pack plus the bottom hot-swap pack, i.e. the machine from #6885. Omarchy 4.0.3-1, kernel 7.2.3-arch1-3, upower 1.91.3. Both packs healthy and non-zero, so this exercises the normal case rather than the dead-BAT0 case in #7916.

Hardware state at test time, straight from sysfs:

Device Energy Full Percent power_now Status Cycles
BAT0 (SMP 01AV421, internal) 13.98 Wh 17.58 Wh 80% 0 W Not charging 128
BAT1 (LGC 01AV490, bottom) 16.9 Wh 19.06 Wh 89% 2.3 W Charging 250
DisplayDevice 30.9 Wh 36.64 Wh 84% 2.3 W charging —

The aggregate half of the fix works. Same machine, same moment:

Field main This PR Truth
percentage 80% 84% 84%
size 17Wh 36Wh 36.6 Wh
rate 0W 2.3W 2.3 W
time (empty) 2h 23m ~2.2 h

The empty time on main is worth calling out as a distinct symptom of the same bug, because it's guaranteed rather than incidental on this hardware: the ThinkPad EC charges one pack at a time, so the idle pack is always Not charging / pending-charge and therefore always has no time-to-full. On main the panel's "Time to full" row reads — for the entire charge cycle. This PR fixes that.

Two things the PR leaves behind, both visible on this machine:

1. cycles collapses two real packs into one number. sort -rn | head -1 reports 250, hiding BAT0's 128. The max-not-first reasoning is right for a dead pack reporting 0, but here both counts are real and independently meaningful — these packs wear at different rates and get replaced separately. 128 / 250 reads better than a max that silently attributes the worse pack's wear to both.

2. The bogus 75-80% charge limit survives — this PR still reads it from upower -i on the first BAT device, and the comment above that block ("UPower reports the same values on every battery device") is accurate but doesn't rescue the value, because it's fabricated on every device. On this machine the kernel attributes are unrestricted:

$ cat /sys/class/power_supply/BAT{0,1}/charge_control_{start,end}_threshold
0
100
0
100
$ cat /sys/class/power_supply/BAT0/charge_behaviour
[auto] inhibit-charge force-discharge

no TLP installed and nothing else setting a limit, yet the PR still emits threshold 75-80%. The source is a catch-all in /usr/lib/udev/hwdb.d/60-upower-battery.hwdb:

battery:*:*:dmi:*
 CHARGE_LIMIT=75,80

which matches every machine (systemd-hwdb query confirms CHARGE_LIMIT=75,80 here). Those are the values UPower offers for a limit, not one in force — ChargeThresholdEnabled on the D-Bus device is false:

$ busctl get-property org.freedesktop.UPower \
    /org/freedesktop/UPower/devices/battery_BAT0 \
    org.freedesktop.UPower.Device ChargeThresholdEnabled
b false

That's #9586 / #7374 rather than this PR's remit, so entirely fair to leave it — but dual-battery hardware makes it markedly easier to hit, which is why it shows up here. Model.chargeThresholdActive() treats PendingCharge as proof a limit is being enforced, and serialized charging means one pack sits in pending-charge for most of every charge cycle. Once BAT1 tops out and before BAT0 takes over, DisplayDevice itself reports pending-charge, and the panel then captions an unrestricted machine "Charge limit 75-80% / Holding". Gating on ChargeThresholdEnabled (or on the sysfs attributes being non-default) would settle it.

Neither of those blocks the fix this PR is making. The aggregate numbers are correct on this hardware where main's are not.

Testing and write-up done with Claude Code.

@bjarneo

bjarneo commented Sep 20, 2026

Copy link
Copy Markdown
Member

Automated duplication check: this pull request looks similar to #10392, which covers the same battery-status multi-battery aggregation. I keep that one open and close this one to consolidate review.

If you feel this is the wrong decision, please open the PR again with a note on the difference.

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.

4 participants