Skip to content

Read battery aggregate from UPower DisplayDevice - #10384

Closed
crwirth wants to merge 3 commits into
omacom:quattrofrom
crwirth:fix/battery-status-display-device
Closed

crwirth wants to merge 3 commits into
omacom:quattrofrom
crwirth:fix/battery-status-display-device

Conversation

@crwirth

@crwirth crwirth commented Sep 5, 2026

Copy link
Copy Markdown

Summary

Fixes #10244.

omarchy-battery-status selected the first BAT* device from upower -e. On dual-battery laptops that is usually a full, idle internal pack while the removable pack drains, so the power panel percentage disagreed with the bar icon (which already reads UPower's combined DisplayDevice).

The script now prefers DisplayDevice for percentage, state, remaining time, and capacity, sums live sysfs power across every BAT, and still reads charge thresholds / cycle count from the first physical battery. If DisplayDevice is missing it falls back to the first BAT.

Test plan

  • bash test/shell.d/battery-status-test.sh
    • single-battery DisplayDevice path (existing rate/time assertions)
    • dual-battery BAT0-full / BAT1-draining stub: percentage/state/size/time from DisplayDevice, rate sum across BATs, rejects BAT0-only 98%
    • no-DisplayDevice fallback to first BAT
  • Live single-battery machine: script percentage matches upower -i .../DisplayDevice
  • Pre-fix dual-battery stub reproduction: reported percentage 98% / fully-charged from BAT0

Notes

Charge-threshold detection still uses the primary BAT's UPower/sysfs threshold fields; those are not published on DisplayDevice.

@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed by Opus 5 in Claude Code with an independent second opinion from Codex at xhigh reasoning. Tests ran on a disposable Omarchy VM with a real compositor, never on a stub of one: battery-status-test.sh, battery-test.sh, power-test.sh and power-present-test.sh through the shell-test wrapper, plus ./test/cli. All green, 112 CLI assertions and 37 shell assertions.

The aggregate change is right and it does fix #10244. Replaying the exact T480 numbers from that issue against a stubbed UPower gives percentage 37% / state discharging / size 66Wh / time 3h 12m, where the current script gives 98% / fully-charged. Nothing double-counts: DisplayDevice's Percentage is already the combined value and the sysfs loop replaces the rate rather than adding to it. On a real machine with no battery — the VM these tests ran on — the script exits 0 with no output, exactly as it does today, so desktops are unaffected. Same when upower is not installed at all.

One real defect, found by Codex and then reproduced by running the script: the sysfs sum replaced UPower's rate as soon as any pack yielded a reading, so a pack whose native-path does not resolve under /sys/class/power_supply dropped silently out of the total. Some drivers report native-path as a full sysfs path rather than a bare BAT1, and a hot-swapped pack can be enumerated before its node is readable. Both leave the loop summing a subset. On a dual-pack machine charging at 45W with the internal pack idle at power_now=0, that produced:

percentage	85%
state	holding
rate	0W

0W is below the 0.2W idle threshold at line 113, which feeds charge_holding, so a machine that is actively charging gets labelled "Holding at 75-80%". Wrong wattage and a wrong state, on the same hardware this PR is trying to fix.

Pushed d0a067d to your branch: take the sum only when every enumerated pack contributed, otherwise keep DisplayDevice's complete figure. A lagging total is wrong by seconds; a subtotal is wrong about what the machine is doing. It comes with a regression test, and that test fails against the previous behaviour — reproducing state holding / rate 0W — rather than merely passing against the new one.

Two things left for the maintainer rather than for you.

There is a competing pull request, #10392, doing the same thing more conservatively: it switches to DisplayDevice only when more than one pack is enumerated, so single-battery machines are untouched. It is safer as submitted but gives up the live sysfs wattage on every dual-pack machine — with UPower's energy-rate stubbed stale at 5.0W against a live 12W, this PR reports 12W and #10392 reports 5W, which is the lag the comment above that block exists to avoid. Codex reached the same conclusion independently, that yours is the better structure to finish; its independence is not currently guaranteed, so read that as agreement rather than as confirmation. Which one lands is not our call.

Second, a composition hazard worth knowing about: #10377 adds design and health keys reading $battery_info, the variable this PR renames. Its hunk applies cleanly onto this branch and then reads an unset variable, so both keys silently vanish and the health section never renders. Verified by applying the patch and running the result. Whichever lands second needs those two lines pointed at primary_info, since DisplayDevice carries neither energy-full-design nor capacity. #9612 conflicts textually with the threshold lines here, which is the safer failure of the two.

Nothing else found. Note that this does not touch #10344 in either of its two parts — you did not claim it did, and the threshold precedence it describes is #9612's territory.

@crwirth crwirth left a comment

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.

Changes look solid

fresh3nough and others added 3 commits September 6, 2026 19:49
omarchy-battery-status picked the first BAT device, so dual-battery
laptops reported BAT0 while the bar icon used DisplayDevice. Prefer
DisplayDevice for percentage/state/time/size and sum live sysfs power
across every BAT. Fall back to the first BAT when DisplayDevice is
absent.

Fixes #10244

Signed-off-by: fresh3nough <anonwurcod@proton.me>
The sum replaced UPower's rate as soon as any one pack yielded a sysfs
reading, so a pack whose native-path does not resolve under
/sys/class/power_supply silently dropped out of the total. On a dual-pack
machine charging at 45W with the internal pack idle at power_now=0, that
reported 0W -- and 0W is below the 0.2W idle threshold, so the panel
labelled a charging machine "Holding at 75-80%".

Take the sum only when every enumerated pack contributed. A lagging total
is wrong by seconds; a subtotal is wrong about what the machine is doing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Codex XHigh <noreply@openai.com>
Some drivers report native-path as a full /sys/devices path. Joining that
string onto power_supply_path never finds the node, so the pack drops out
of the live wattage sum and the earlier all-or-nothing guard falls back to
a lagging DisplayDevice rate. Always take the basename so bare and absolute
forms both resolve under OMARCHY_POWER_SUPPLY_PATH.

Also keep battery_info as an alias of the primary pack. Concurrent work
that still reads that name for design/health keys otherwise composes cleanly
and then silently emits nothing.

Signed-off-by: fresh3nough <anonwurcod@proton.me>
@crwirth
crwirth force-pushed the fix/battery-status-display-device branch from d0a067d to 8cf7ac1 Compare September 6, 2026 23:52
@crwirth

crwirth commented Sep 6, 2026

Copy link
Copy Markdown
Author

Follow-up on the review notes in #10384 (comment).

Rebased onto current quattro and pushed 8cf7ac17.

What changed:

  1. Absolute native-path resolution. The bot fix correctly refuses a partial sysfs sum, but drivers that report native-path as a full /sys/devices/.../power_supply/BAT1 path still never resolve when joined onto power_supply_path. That forces the lagging DisplayDevice rate even when the class node is readable. battery_sysfs_path now always takes the basename, so bare and absolute forms both resolve under OMARCHY_POWER_SUPPLY_PATH.

  2. battery_info kept as an alias of the primary pack. The composition hazard with Show design capacity and battery health in the power panel #10377 is real: its hunk applies cleanly onto this branch and then reads an unset variable, so design/health vanish. This does not take that PR's feature; it only preserves the old variable name so a later compose does not silently no-op. Design/capacity still come from the physical pack, not DisplayDevice.

Tests:

  • bash test/shell.d/battery-status-test.sh green, including:
    • single-battery DisplayDevice path
    • dual-battery aggregate vs BAT0-only
    • no-DisplayDevice fallback
    • absolute native-path live sum (new)
    • unreadable pack keeps DisplayDevice rate (bot regression)
    • composed design/health keys via battery_info alias (new)

Commit is DCO-signed (Signed-off-by: fresh3nough <anonwurcod@proton.me>) and SSH-signed.

@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.

@bjarneo bjarneo closed this Sep 20, 2026
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.

omarchy-battery-status reports wrong percentage on dual-battery laptops (reads only BAT0)

3 participants