Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
148 changes: 132 additions & 16 deletions bin/omarchy-battery-status
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ esac
battery=$(upower -e 2>/dev/null | grep BAT | head -n 1)
[[ -z $battery ]] && exit 0

battery_info=$(upower -i "$battery")
# Headline figures come from UPower's aggregate DisplayDevice rather than the
# first battery UPower enumerates: on a multi-battery machine (dual-battery
# ThinkPads and the like) the first device can be a dead or empty pack, which
# pins the panel at 0% forever while the other battery charges fine.
battery_info=$(upower -i /org/freedesktop/UPower/devices/DisplayDevice)
[[ -z $battery_info ]] && exit 0

percentage=$(awk '/percentage/ { print int($2); exit }' <<<"$battery_info")
capacity=$(awk '/energy-full:/ { printf "%d", $2; exit }' <<<"$battery_info")
Expand All @@ -42,18 +47,76 @@ time_remaining=$(awk '/time to (empty|full)/ {
exit
}' <<<"$battery_info")
power_rate_raw=$(awk '/energy-rate/ { print $2; exit }' <<<"$battery_info")
native_path=$(awk '/native-path/ { print $2; exit }' <<<"$battery_info")
battery_path="$power_supply_path/$native_path"

# UPower's energy-rate can lag the kernel telemetry by tens of seconds. Use
# the instantaneous sysfs reading when available so the open panel stays live.
if [[ -r $battery_path/power_now ]]; then
power_rate_raw=$(awk -v microwatts="$(<"$battery_path/power_now")" 'BEGIN { print microwatts / 1000000 }')
elif [[ -r $battery_path/current_now && -r $battery_path/voltage_now ]]; then
power_rate_raw=$(awk \
-v microamps="$(<"$battery_path/current_now")" \
-v microvolts="$(<"$battery_path/voltage_now")" \
'BEGIN { print microamps * microvolts / 1000000000000 }')

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

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
# reports, since a partial sum would understate the total.
total_microwatts=0
metered_packs=0
for supply in "${batteries[@]}"; do
if [[ -r $supply/power_now ]]; then
microwatts=$(<"$supply/power_now")
elif [[ -r $supply/current_now && -r $supply/voltage_now ]]; then
# A failed read() (ENODEV) can leave a readable attribute empty; validate
# before awk, which would silently coerce that to 0.
microamps=$(<"$supply/current_now")
microvolts=$(<"$supply/voltage_now")
[[ $microamps =~ ^-?[0-9]+$ && $microvolts =~ ^-?[0-9]+$ ]] || continue
microwatts=$(awk \
-v microamps="$microamps" \
-v microvolts="$microvolts" \
'BEGIN { printf "%d", microamps * microvolts / 1000000 }')
else
continue
fi

[[ $microwatts =~ ^-?[0-9]+$ ]] || continue
total_microwatts=$((total_microwatts + microwatts))
((metered_packs += 1))
done

if (( metered_packs > 0 && metered_packs == ${#batteries[@]} )); then
power_rate_raw=$(awk -v microwatts="$total_microwatts" 'BEGIN { print microwatts / 1000000 }')
fi

power_rate=$(awk -v rate="${power_rate_raw:-0}" 'BEGIN {
Expand All @@ -62,8 +125,53 @@ power_rate=$(awk -v rate="${power_rate_raw:-0}" 'BEGIN {
print rounded
}')
state=$(awk '/state/ { print $2; exit }' <<<"$battery_info")
threshold_start=$(awk '/charge-start-threshold:/ { gsub(/%/, "", $2); print int($2); exit }' <<<"$battery_info")
threshold_end=$(awk '/charge-end-threshold:/ { gsub(/%/, "", $2); print int($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")
threshold_start=$(awk '/charge-start-threshold:/ { gsub(/%/, "", $2); print int($2); exit }' <<<"$threshold_info")
threshold_end=$(awk '/charge-end-threshold:/ { gsub(/%/, "", $2); print int($2); exit }' <<<"$threshold_info")

[[ -z $threshold_end ]] && threshold_end=$(cat "$power_supply_path"/BAT*/charge_control_end_threshold 2>/dev/null | head -1)
[[ -z $threshold_start ]] && threshold_start=$(cat "$power_supply_path"/BAT*/charge_control_start_threshold 2>/dev/null | head -1)
Expand Down Expand Up @@ -107,7 +215,15 @@ if [[ $shell_output == "true" ]]; then
printf 'size\t%s\n' "${capacity}Wh"
printf 'time\t%s\n' "$time_remaining"

cycles=$(cat "$power_supply_path"/BAT*/cycle_count 2>/dev/null | head -1)
# One cycle count per pack, so a multi-battery machine reports both rather
# than silently showing whichever sorts first. A dead pack's -1 falls to
# the digit check.
cycles=""
for supply in "${batteries[@]}"; do
count=$(cat "$supply/cycle_count" 2>/dev/null) || continue
[[ $count =~ ^[0-9]+$ ]] || continue
cycles+="${cycles:+ / }$count"
done

[[ -n $cycles ]] && printf 'cycles\t%s\n' "$cycles"

Expand Down
204 changes: 204 additions & 0 deletions test/shell.d/battery-status-dual-battery-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
#!/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 '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"

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"

# Some ACPI firmware fails the read() on a present telemetry attribute with
# ENODEV, yielding an empty string rather than a missing file. Neither an
# empty power_now nor an empty current_now may be coerced into a metered 0W
# (basecamp/omarchy#6895).
: >"$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 empty power_now read falls back to the aggregate rate instead of 0W" "$shell_output"

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"

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"
1 change: 1 addition & 0 deletions test/shell.d/battery-status-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ trap 'rm -rf "$tmp_dir"' EXIT

mkdir -p "$tmp_dir/bin"
mkdir -p "$tmp_dir/power/BAT0"
printf 'Battery\n' >"$tmp_dir/power/BAT0/type"
printf '900000\n' >"$tmp_dir/power/BAT0/current_now"
printf '12000000\n' >"$tmp_dir/power/BAT0/voltage_now"
cat >"$tmp_dir/bin/upower" <<'STUB'
Expand Down