Skip to content
Open
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
124 changes: 119 additions & 5 deletions bin/omarchy-battery-status
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,27 @@ case "${1:-}" in
;;
esac

battery=$(upower -e 2>/dev/null | grep BAT | head -n 1)
[[ -z $battery ]] && exit 0
upower_devices=$(upower -e 2>/dev/null)
first_battery=$(grep BAT <<<"$upower_devices" | head -n 1)
[[ -z $first_battery ]] && exit 0

# DisplayDevice is the combined view, same source the bar icon reads. Use it
# only when more than one pack exists; a single pack stays on BAT* so the
# sysfs power_now path still applies. (Overlaps #10392 — kept here so this
# draft is reviewable standalone; rebase to drop the duplicate once #10392
# lands.)
pack_count=$(grep -c BAT <<<"$upower_devices" || true)
if (( pack_count > 1 )); then
battery=$(grep DisplayDevice <<<"$upower_devices" | head -n 1)
else
battery=""
fi
if [[ -z $battery ]]; then
battery="$first_battery"
fi

battery_info=$(upower -i "$battery")
[[ -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 @@ -44,6 +61,46 @@ time_remaining=$(awk '/time to (empty|full)/ {
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"
# DisplayDevice's native-path is not a sysfs battery node.
[[ -d $battery_path && -r $battery_path/type ]] || battery_path=""

# Per-pack liveness: a dead pack keeps advertising its old energy_full, so
# energy_full is no sign of life (#7916). A pack showing no voltage, stored
# energy, or current flow is electrically dead — a healthy empty cell still
# reports voltage. Peripheral batteries (mice, keyboards) report type Battery
# but carry scope Device and are not part of the machine's power budget.
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. Use
# the instantaneous sysfs reading when available so the open panel stays live.
Expand All @@ -62,8 +119,57 @@ 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
# energy_now and energy_full are separate reads, so a capacity update in
# between can leave a pack holding more than its full; cap it so the
# headline stays at or under 100% and time to full never goes negative.
(( energy > full )) && energy=$full
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
Comment thread
ujo4eva marked this conversation as resolved.
Resolved
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 "$first_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 +213,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 live 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
181 changes: 181 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,181 @@
#!/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

# Dual-battery ThinkPad whose internal pack (BAT0) is dead: no voltage, no
# stored energy, enumerated first by UPower. The removable pack (BAT1) powers
# the machine. DisplayDevice itself is correct here (dead pack advertises 0).
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/voltage_now"
printf '0\n' >"$tmp_dir/power/BAT0/energy_now"
printf '0\n' >"$tmp_dir/power/BAT0/energy_full"
printf '%s\n' -1 >"$tmp_dir/power/BAT0/cycle_count"

printf 'Battery\n' >"$tmp_dir/power/BAT1/type"
printf '1\n' >"$tmp_dir/power/BAT1/present"
printf '12300000\n' >"$tmp_dir/power/BAT1/voltage_now"
printf '27300000\n' >"$tmp_dir/power/BAT1/energy_now"
printf '46500000\n' >"$tmp_dir/power/BAT1/energy_full"
printf '5\n' >"$tmp_dir/power/BAT1/cycle_count"

# A wireless mouse also reports type Battery, but its scope marks it as a
# peripheral: it must not leak into 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 '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"
echo "/org/freedesktop/UPower/devices/DisplayDevice"
exit 0
fi

if [[ $1 == "-i" && $2 == "/org/freedesktop/UPower/devices/DisplayDevice" ]]; then
cat <<'INFO'
native-path: DisplayDevice
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" ]]; 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 $'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: 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 '12000000\n' >"$tmp_dir/power2/BAT0/voltage_now"
printf '7\n' >"$tmp_dir/power2/BAT0/cycle_count"

printf 'Battery\n' >"$tmp_dir/power2/BAT1/type"
printf '1\n' >"$tmp_dir/power2/BAT1/present"
printf '12600000\n' >"$tmp_dir/power2/BAT1/voltage_now"
printf '5\n' >"$tmp_dir/power2/BAT1/cycle_count"

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

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 lists both cycle counts"

# 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). 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 '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"

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"
echo "/org/freedesktop/UPower/devices/DisplayDevice"
exit 0
fi

if [[ $1 == "-i" && $2 == "/org/freedesktop/UPower/devices/DisplayDevice" ]]; then
cat <<'INFO'
native-path: DisplayDevice
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 $'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"

# energy_now and energy_full are read separately, so a capacity update in
# between can leave the live pack reporting more than its full. The headline
# caps at 100% and time to full does not go negative.
printf '21100000\n' >"$tmp_dir/power3/BAT1/energy_now"

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

grep -Fx $'percentage\t100%' <<<"$shell_output" >/dev/null || fail "a pack reading above its full caps the percentage at 100%" "$shell_output"
grep -Fx $'time\t0m' <<<"$shell_output" >/dev/null || fail "a pack reading above its full gives no negative time to full" "$shell_output"

pass "a pack reading above its full stays within bounds"
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