diff --git a/bin/omarchy-battery-status b/bin/omarchy-battery-status index 7f642e620137..337a5888b9b1 100755 --- a/bin/omarchy-battery-status +++ b/bin/omarchy-battery-status @@ -63,12 +63,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 a86f5cea45b1..1822d965401c 100644 --- a/test/shell.d/battery-status-test.sh +++ b/test/shell.d/battery-status-test.sh @@ -160,6 +160,18 @@ danish_time=$(OMARCHY_LANGUAGE_FILE="$tmp_dir/language" OMARCHY_POWER_SUPPLY_PAT grep -Fx $'time\t1 t 30 min' <<<"$danish_time" >/dev/null || fail "battery status durations follow the Omarchy language" "$danish_time" pass "battery status reads in the Omarchy language" +# 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" +rmdir "$hold_dir/power/BAT0/power_now" "$hold_dir/power/BAT0/current_now" + # A full battery says so, with no time to count and no 0W draw. cat >"$hold_dir/bin/upower" <<'STUB' #!/bin/bash