From 34e27dfbb53d021ebcbad3f074c3a981ab3c2420 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 1 Nov 2026 16:55:03 +0100 Subject: [PATCH] Keep UPower's battery rate when sysfs has no live reading Firmware that doesn't know the present rate leaves current_now in place but fails every read of it, so the readable-file check passed, the read came back empty, and the draw showed as 0W. Only take the sysfs reading when it is a number, and otherwise keep UPower's rate. Co-Authored-By: Claude Opus 5.5 --- bin/omarchy-battery-status | 24 ++++++++++++++++-------- test/shell.d/battery-status-test.sh | 9 +++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/bin/omarchy-battery-status b/bin/omarchy-battery-status index 49d194af239d..00a067346229 100755 --- a/bin/omarchy-battery-status +++ b/bin/omarchy-battery-status @@ -61,15 +61,23 @@ 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" +# A sysfs reading as a number, or nothing. Firmware that doesn't know the +# present rate leaves current_now in place but fails every read of it (ENODEV), +# so a readable file is no promise of a value. +read_sysfs_number() { + local value + { value=$(<"$1"); } 2>/dev/null || return 1 + [[ $value =~ ^-?[0-9]+$ ]] || return 1 + echo "$value" +} + # 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 }') +# the instantaneous sysfs reading when there is one so the open panel stays +# live; without one, UPower's rate, worked out from the falling charge, stands. +if microwatts=$(read_sysfs_number "$battery_path/power_now"); then + power_rate_raw=$(awk -v microwatts="$microwatts" 'BEGIN { print microwatts / 1000000 }') +elif microamps=$(read_sysfs_number "$battery_path/current_now") && microvolts=$(read_sysfs_number "$battery_path/voltage_now"); then + power_rate_raw=$(awk -v microamps="$microamps" -v microvolts="$microvolts" 'BEGIN { print microamps * microvolts / 1000000000000 }') fi # Some kernels sign power_now by direction (negative while discharging on diff --git a/test/shell.d/battery-status-test.sh b/test/shell.d/battery-status-test.sh index a1f906176872..b97ae88f4d33 100644 --- a/test/shell.d/battery-status-test.sh +++ b/test/shell.d/battery-status-test.sh @@ -77,6 +77,15 @@ grep -Fx $'rate\t7.3W' <<<"$generic_output" >/dev/null || fail "battery status a grep -Fx $'time\t3h 53m' <<<"$generic_output" >/dev/null || fail "battery status time left follows the live draw" pass "battery status supports Apple Silicon and arbitrary native battery paths" +# Firmware that doesn't know the present rate leaves current_now in place but +# gives nothing on read; UPower's rate, from the falling charge, stands. +rm "$tmp_dir/power/CMB0/power_now" +: >"$tmp_dir/power/CMB0/current_now" +printf '12900000\n' >"$tmp_dir/power/CMB0/voltage_now" +unknown_rate_output=$(OMARCHY_TEST_NATIVE_PATH=CMB0 OMARCHY_POWER_SUPPLY_PATH="$tmp_dir/power" PATH="$tmp_dir/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell) +grep -Fx $'rate\t7.3W' <<<"$unknown_rate_output" >/dev/null || fail "an unreadable current_now leaves UPower's rate in place" "$unknown_rate_output" +pass "battery status keeps UPower's rate when the battery has no live reading" + # An 80% hold threshold doesn't trip while the battery is still below it. hold_dir=$(mktemp -d) trap 'rm -rf "$tmp_dir" "$hold_dir"' EXIT