Repository navigation
Fix battery status on multi-battery machines #6845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5431bf8
Fix battery status on multi-battery machines
geirolafs d5d25d5
Cover two live packs in the dual-battery test
geirolafs 3aba5ea
Treat every system battery as one pool instead of splitting live from…
dhh f56bd1a
Fall back to the aggregate rate when a pack has no power telemetry
dhh 3c40745
Discard failed telemetry reads instead of coercing them to 0W
geirolafs cbe0705
Keep a dead pack's advertised capacity out of the headline figures
geirolafs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" | ||
|
|
||
| tmp_dir=$(mktemp -d) | ||
| trap 'rm -rf "$tmp_dir"' EXIT | ||
|
|
||
| # A dual-battery ThinkPad whose internal pack (BAT0) is dead: zero full-charge | ||
| # capacity, no cycle count, enumerated first by UPower. The removable pack | ||
| # (BAT1) is the one actually powering the machine. | ||
| mkdir -p "$tmp_dir/bin" | ||
| mkdir -p "$tmp_dir/power/BAT0" "$tmp_dir/power/BAT1" | ||
|
|
||
| printf 'Battery\n' >"$tmp_dir/power/BAT0/type" | ||
| printf '1\n' >"$tmp_dir/power/BAT0/present" | ||
| printf '0\n' >"$tmp_dir/power/BAT0/energy_full" | ||
| printf '%s\n' -1 >"$tmp_dir/power/BAT0/cycle_count" | ||
| printf '0\n' >"$tmp_dir/power/BAT0/power_now" | ||
|
|
||
| printf 'Battery\n' >"$tmp_dir/power/BAT1/type" | ||
| printf '1\n' >"$tmp_dir/power/BAT1/present" | ||
| printf '46530000\n' >"$tmp_dir/power/BAT1/energy_full" | ||
| printf '5\n' >"$tmp_dir/power/BAT1/cycle_count" | ||
| printf '30500000\n' >"$tmp_dir/power/BAT1/power_now" | ||
|
|
||
| # A wireless mouse also reports type Battery, but its scope marks it as a | ||
| # peripheral: it must not leak into the machine's wattage or cycle counts. | ||
| mkdir -p "$tmp_dir/power/hidpp_battery_0" | ||
| printf 'Battery\n' >"$tmp_dir/power/hidpp_battery_0/type" | ||
| printf 'Device\n' >"$tmp_dir/power/hidpp_battery_0/scope" | ||
| printf '1000000\n' >"$tmp_dir/power/hidpp_battery_0/power_now" | ||
| printf '99\n' >"$tmp_dir/power/hidpp_battery_0/cycle_count" | ||
|
|
||
| cat >"$tmp_dir/bin/upower" <<'STUB' | ||
| #!/bin/bash | ||
|
|
||
| if [[ $1 == "-e" ]]; then | ||
| echo "/org/freedesktop/UPower/devices/battery_BAT0" | ||
| echo "/org/freedesktop/UPower/devices/battery_BAT1" | ||
| exit 0 | ||
| fi | ||
|
|
||
| if [[ $1 == "-i" && $2 == "/org/freedesktop/UPower/devices/DisplayDevice" ]]; then | ||
| cat <<'INFO' | ||
| state: charging | ||
| energy: 27.3 Wh | ||
| energy-full: 46.5 Wh | ||
| energy-rate: 30.9 W | ||
| time to full: 1.2 hours | ||
| percentage: 58% | ||
| INFO | ||
| exit 0 | ||
| fi | ||
|
|
||
| if [[ $1 == "-i" && $2 == "/org/freedesktop/UPower/devices/battery_BAT0" ]]; then | ||
| cat <<'INFO' | ||
| native-path: BAT0 | ||
| state: pending-charge | ||
| energy: 0 Wh | ||
| energy-full: 0 Wh | ||
| energy-rate: 0 W | ||
| percentage: 0% | ||
| charge-start-threshold: 75% | ||
| charge-end-threshold: 80% | ||
| INFO | ||
| exit 0 | ||
| fi | ||
|
|
||
| exit 1 | ||
| STUB | ||
| chmod +x "$tmp_dir/bin/upower" | ||
|
|
||
| shell_output=$(OMARCHY_POWER_SUPPLY_PATH="$tmp_dir/power" PATH="$tmp_dir/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell) | ||
|
|
||
| grep -Fx $'percentage\t58%' <<<"$shell_output" >/dev/null || fail "dual battery reports the aggregate percentage, not the dead pack's 0%" "$shell_output" | ||
| grep -Fx $'state\tcharging' <<<"$shell_output" >/dev/null || fail "dual battery reports the aggregate state, not the dead pack's pending-charge" "$shell_output" | ||
| grep -Fx $'rate\t30.5W' <<<"$shell_output" >/dev/null || fail "dual battery sums live sysfs wattage over packs that hold a charge" "$shell_output" | ||
| grep -Fx $'size\t46Wh' <<<"$shell_output" >/dev/null || fail "dual battery reports the aggregate capacity, not the dead pack's 0Wh" "$shell_output" | ||
| grep -Fx $'cycles\t5' <<<"$shell_output" >/dev/null || fail "dual battery reports the live pack's cycle count, not the dead pack's" "$shell_output" | ||
| grep -Fx $'threshold\t75-80%' <<<"$shell_output" >/dev/null || fail "threshold still comes from the first battery's UPower info" "$shell_output" | ||
|
|
||
| pass "dual-battery machine with a dead pack reports the working battery" | ||
|
|
||
| # Both packs alive: wattage is the combined draw and each pack contributes | ||
| # its own cycle count, in sysfs order. | ||
| mkdir -p "$tmp_dir/power2/BAT0" "$tmp_dir/power2/BAT1" | ||
|
|
||
| printf 'Battery\n' >"$tmp_dir/power2/BAT0/type" | ||
| printf '1\n' >"$tmp_dir/power2/BAT0/present" | ||
| printf '23800000\n' >"$tmp_dir/power2/BAT0/energy_full" | ||
| printf '7\n' >"$tmp_dir/power2/BAT0/cycle_count" | ||
| printf '12000000\n' >"$tmp_dir/power2/BAT0/power_now" | ||
|
|
||
| printf 'Battery\n' >"$tmp_dir/power2/BAT1/type" | ||
| printf '1\n' >"$tmp_dir/power2/BAT1/present" | ||
| printf '46530000\n' >"$tmp_dir/power2/BAT1/energy_full" | ||
| printf '5\n' >"$tmp_dir/power2/BAT1/cycle_count" | ||
| printf '18500000\n' >"$tmp_dir/power2/BAT1/power_now" | ||
|
|
||
| shell_output=$(OMARCHY_POWER_SUPPLY_PATH="$tmp_dir/power2" PATH="$tmp_dir/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell) | ||
|
|
||
| grep -Fx $'rate\t30.5W' <<<"$shell_output" >/dev/null || fail "two live packs report their combined sysfs wattage" "$shell_output" | ||
| grep -Fx $'cycles\t7 / 5' <<<"$shell_output" >/dev/null || fail "two live packs report both cycle counts in sysfs order" "$shell_output" | ||
|
|
||
| pass "dual-battery machine with two live packs sums wattage and lists both cycle counts" | ||
|
|
||
| # A pack without sysfs power telemetry would make the sum a partial total, so | ||
| # the rate falls back to UPower's aggregate instead. | ||
| rm "$tmp_dir/power2/BAT1/power_now" | ||
|
|
||
| shell_output=$(OMARCHY_POWER_SUPPLY_PATH="$tmp_dir/power2" PATH="$tmp_dir/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell) | ||
|
|
||
| grep -Fx $'rate\t30.9W' <<<"$shell_output" >/dev/null || fail "an unmetered pack falls back to the aggregate rate instead of a partial sum" "$shell_output" | ||
|
|
||
| pass "a pack without power telemetry falls back to UPower's aggregate rate" | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.