Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
76 changes: 60 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,45 @@ 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.
batteries=()
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")
done

# 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
microwatts=$(awk \
-v microamps="$(<"$supply/current_now")" \
-v microvolts="$(<"$supply/voltage_now")" \
'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 +94,12 @@ 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")

# 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 +143,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
117 changes: 117 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,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"
Comment thread
geirolafs marked this conversation as resolved.
Resolved
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"
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