From 7d90815b8aa8072e9404d8895de82608274bb8c6 Mon Sep 17 00:00:00 2001 From: Peter Cakounes Date: Tue, 13 Oct 2026 20:44:28 -0400 Subject: [PATCH] Keep UPower's battery rate when sysfs power can't be read Some ACPI batteries list power_now and current_now with read permission but fail every read with ENODEV. The empty read became 0W and replaced UPower's correct energy-rate. Go by what the read returns instead. Fixes #15373 Co-Authored-By: Claude Opus 5.5 --- bin/omarchy-battery-status | 20 +++++++++++++++----- test/shell.d/battery-status-test.sh | 11 +++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/bin/omarchy-battery-status b/bin/omarchy-battery-status index 88331e79d757..67ed2c2233fa 100755 --- a/bin/omarchy-battery-status +++ b/bin/omarchy-battery-status @@ -46,12 +46,22 @@ 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 +# Some ACPI batteries (HP Spectre x360) list power_now and current_now but fail +# every read with ENODEV, so go by what a read returns, not the permission bit, +# and keep UPower's rate when nothing comes back. +power_now="" +current_now="" +voltage_now="" +[[ ! -r $battery_path/power_now ]] || power_now=$(cat "$battery_path/power_now" 2>/dev/null) +[[ ! -r $battery_path/current_now ]] || current_now=$(cat "$battery_path/current_now" 2>/dev/null) +[[ ! -r $battery_path/voltage_now ]] || voltage_now=$(cat "$battery_path/voltage_now" 2>/dev/null) + +if [[ -n $power_now ]]; then + power_rate_raw=$(awk -v microwatts="$power_now" 'BEGIN { print microwatts / 1000000 }') +elif [[ -n $current_now && -n $voltage_now ]]; then power_rate_raw=$(awk \ - -v microamps="$(<"$battery_path/current_now")" \ - -v microvolts="$(<"$battery_path/voltage_now")" \ + -v microamps="$current_now" \ + -v microvolts="$voltage_now" \ 'BEGIN { print microamps * microvolts / 1000000000000 }') fi diff --git a/test/shell.d/battery-status-test.sh b/test/shell.d/battery-status-test.sh index 011a5f53d7a6..0015110a3b00 100644 --- a/test/shell.d/battery-status-test.sh +++ b/test/shell.d/battery-status-test.sh @@ -150,6 +150,17 @@ unlimited_output=$(OMARCHY_POWER_SUPPLY_PATH="$hold_dir/power" PATH="$hold_dir/b grep -Fx $'time\t1h 30m' <<<"$unlimited_output" >/dev/null || fail "time to full ignores another battery's charge limit" "$unlimited_output" pass "battery status times charging to its own battery's limit" +# A battery that lists power_now and current_now but fails every read keeps +# UPower's rate. A directory stands in for the ENODEV attribute: it passes -r +# yet cannot be read as a file. +rm "$hold_dir/power/BAT0/power_now" +mkdir "$hold_dir/power/BAT0/power_now" "$hold_dir/power/BAT0/current_now" +printf '17556000\n' >"$hold_dir/power/BAT0/voltage_now" +sed -i 's/energy-rate: 0.1 W/energy-rate: 21.5336 W/' "$hold_dir/bin/upower" +unreadable_output=$(OMARCHY_POWER_SUPPLY_PATH="$hold_dir/power" PATH="$hold_dir/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell 2>&1) +grep -Fx $'rate\t21.5W' <<<"$unreadable_output" >/dev/null || fail "an unreadable power_now falls back to UPower's rate" "$unreadable_output" +pass "battery status keeps UPower's rate when sysfs power cannot be read" + if matches=$(rg -n 'omarchy-battery-(capacity|remaining|remaining-time)' "$ROOT/bin" "$ROOT/test" "$ROOT/shell" "$ROOT/docs"); then fail "battery status owns capacity and remaining calculations" "$matches" fi