Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Keep a dead pack's advertised capacity out of the headline figures
UPower's composite DisplayDevice sums every present battery's
energy-full, so a dead pack that still advertises the capacity it once
had drags the percentage toward half and stretches time-to-full toward
ten hours while the live pack sits near full (#7916). Split packs by
electrical liveness — voltage, stored energy, or current flow, since a
dead pack keeps advertising energy_full — and rebuild percentage,
capacity, and remaining time from the live packs' own energy readings
whenever a dead pack was excluded.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9xdoAKqs6u53PAahM88u
  • Loading branch information
geirolafs and claude committed Aug 25, 2026
commit cbe070522d17f1314a23a3a8f72b853ee5b4bfe8
71 changes: 69 additions & 2 deletions bin/omarchy-battery-status
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,43 @@ power_rate_raw=$(awk '/energy-rate/ { print $2; exit }' <<<"$battery_info")

# Per-pack readings (live wattage, cycle counts) cover every system battery.
# Peripheral batteries (mice, keyboards) also report type Battery, but carry
# scope Device and are not part of the machine's power budget.
# scope Device and are not part of the machine's power budget. A pack showing
# no voltage, stored energy, or current flow is electrically dead — a healthy
# empty cell still reports voltage. energy_full is no sign of life: a dead
# pack keeps advertising the capacity it once had (#7916).
batteries=()
dead_packs=()
for supply in "$power_supply_path"/*; do
[[ -r $supply/type && $(<"$supply/type") == "Battery" ]] || continue
[[ -r $supply/scope && $(<"$supply/scope") == "Device" ]] && continue
if [[ -r $supply/present ]]; then
[[ $(<"$supply/present") == "1" ]] || continue
fi
batteries+=("$supply")

live=false
for attr in voltage_now energy_now charge_now current_now power_now; do
value=$(cat "$supply/$attr" 2>/dev/null) || continue
[[ $value =~ ^-?[0-9]+$ ]] || continue
if (( value != 0 )); then
live=true
break
fi
done

if [[ $live == "true" ]]; then
batteries+=("$supply")
else
dead_packs+=("$supply")
fi
done

# No telemetry at all is not proof of death: if every pack reads dead, keep
# them all rather than reporting nothing.
if (( ${#batteries[@]} == 0 )); then
batteries=("${dead_packs[@]}")
dead_packs=()
fi

# UPower's energy-rate can lag the kernel telemetry by tens of seconds. Sum
# the instantaneous sysfs readings so the open panel stays live and a
# multi-battery machine shows its combined draw — but only when every pack
Expand Down Expand Up @@ -100,6 +126,47 @@ power_rate=$(awk -v rate="${power_rate_raw:-0}" 'BEGIN {
}')
state=$(awk '/state/ { print $2; exit }' <<<"$battery_info")

# UPower's composite DisplayDevice still counts a dead pack's advertised
# energy-full, sagging the percentage and stretching time-to-full toward a
# pack that holds nothing (#7916). When a dead pack was excluded, rebuild the
# headline figures from the live packs' own energy readings; firmware that
# only exposes charge counters keeps the aggregate values.
if (( ${#dead_packs[@]} > 0 )); then
live_energy=0
live_energy_full=0
for supply in "${batteries[@]}"; do
energy=$(cat "$supply/energy_now" 2>/dev/null)
full=$(cat "$supply/energy_full" 2>/dev/null)
if [[ ! $energy =~ ^[0-9]+$ || ! $full =~ ^[0-9]+$ ]]; then
live_energy_full=0
break
fi
live_energy=$((live_energy + energy))
live_energy_full=$((live_energy_full + full))
done

if (( live_energy_full > 0 )); then
percentage=$((100 * live_energy / live_energy_full))
capacity=$((live_energy_full / 1000000))
time_remaining=$(awk -v energy="$live_energy" -v full="$live_energy_full" \
-v rate="${power_rate_raw:-0}" -v state="$state" 'BEGIN {
if (rate <= 0) exit
if (state == "charging") hours = (full - energy) / 1000000 / rate
else if (state == "discharging") hours = energy / 1000000 / rate
else exit
whole = int(hours)
minutes = int((hours - whole) * 60)
if (whole == 0) {
printf "%dm", minutes
} else if (minutes > 0) {
printf "%dh %dm", whole, minutes
} else {
printf "%dh", whole
}
}')
fi
fi

# Charge thresholds are per-device values the aggregate does not carry, so
# they still come from the first battery's own UPower info.
threshold_info=$(upower -i "$battery")
Expand Down
67 changes: 66 additions & 1 deletion test/shell.d/battery-status-dual-battery-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ 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 '12600000\n' >"$tmp_dir/power2/BAT1/voltage_now"
printf '5\n' >"$tmp_dir/power2/BAT1/cycle_count"
printf '18500000\n' >"$tmp_dir/power2/BAT1/power_now"

Expand Down Expand Up @@ -130,10 +131,74 @@ pass "an empty power_now read falls back to UPower's aggregate rate"

rm "$tmp_dir/power2/BAT1/power_now"
: >"$tmp_dir/power2/BAT1/current_now"
printf '12600000\n' >"$tmp_dir/power2/BAT1/voltage_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 empty current_now read falls back to the aggregate rate instead of 0W" "$shell_output"

pass "an empty current_now read falls back to UPower's aggregate rate"

# UPower's composite DisplayDevice keeps a dead pack's advertised energy-full
# in the percentage denominator: a 20.65Wh corpse next to a 20.94Wh live pack
# near full reads as 49% with a ten-hour charge (#7916). The headline figures
# must follow the packs that actually hold a charge — and a dead pack still
# advertising its old energy_full must not count as alive.
mkdir -p "$tmp_dir/power3/BAT0" "$tmp_dir/power3/BAT1" "$tmp_dir/bin3"

printf 'Battery\n' >"$tmp_dir/power3/BAT0/type"
printf '1\n' >"$tmp_dir/power3/BAT0/present"
printf '0\n' >"$tmp_dir/power3/BAT0/voltage_now"
printf '0\n' >"$tmp_dir/power3/BAT0/energy_now"
printf '20650000\n' >"$tmp_dir/power3/BAT0/energy_full"
printf '0\n' >"$tmp_dir/power3/BAT0/power_now"

printf 'Battery\n' >"$tmp_dir/power3/BAT1/type"
printf '1\n' >"$tmp_dir/power3/BAT1/present"
printf '12300000\n' >"$tmp_dir/power3/BAT1/voltage_now"
printf '20400000\n' >"$tmp_dir/power3/BAT1/energy_now"
printf '20940000\n' >"$tmp_dir/power3/BAT1/energy_full"
printf '2200000\n' >"$tmp_dir/power3/BAT1/power_now"

cat >"$tmp_dir/bin3/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: 20.4 Wh
energy-full: 41.59 Wh
energy-rate: 2.2 W
time to full: 9.6 hours
percentage: 49%
INFO
exit 0
fi

if [[ $1 == "-i" ]]; then
cat <<'INFO'
native-path: BAT0
state: pending-charge
percentage: 0%
INFO
exit 0
fi

exit 1
STUB
chmod +x "$tmp_dir/bin3/upower"

shell_output=$(OMARCHY_POWER_SUPPLY_PATH="$tmp_dir/power3" PATH="$tmp_dir/bin3:$PATH" "$ROOT/bin/omarchy-battery-status" --shell)

grep -Fx $'percentage\t97%' <<<"$shell_output" >/dev/null || fail "a dead pack's energy-full stays out of the percentage denominator" "$shell_output"
grep -Fx $'state\tcharging' <<<"$shell_output" >/dev/null || fail "the live pack's charging state survives a dead sibling" "$shell_output"
grep -Fx $'rate\t2.2W' <<<"$shell_output" >/dev/null || fail "the rate reflects the live pack's draw" "$shell_output"
grep -Fx $'size\t20Wh' <<<"$shell_output" >/dev/null || fail "capacity counts only packs that hold a charge" "$shell_output"
grep -Fx $'time\t14m' <<<"$shell_output" >/dev/null || fail "time to full is not stretched by a dead pack's capacity" "$shell_output"

pass "a dead pack's advertised capacity does not drag down the headline figures"