Skip to content

Keep UPower's rate when a sysfs power attribute cannot be read - #7182

Closed
hegge25 wants to merge 1 commit into
omacom:quattrofrom
hegge25:fix-unreadable-power-now
Closed

hegge25 wants to merge 1 commit into
omacom:quattrofrom
hegge25:fix-unreadable-power-now

Conversation

@hegge25

@hegge25 hegge25 commented Aug 16, 2026

Copy link
Copy Markdown

On an HP Spectre x360 14-ea0 the power panel sits at 0W while the battery visibly drains. omarchy-battery-status reports rate 0W at the same moment UPower has the right answer:

$ omarchy-battery-status --shell | grep rate
rate	0W
$ upower -i $(upower -e | grep BAT) | grep energy-rate
energy-rate:         11.1458 W

The cause is the sysfs override that keeps the open panel live:

if [[ -r $battery_path/power_now ]]; then
  power_rate_raw=$(awk -v microwatts="$(<"$battery_path/power_now")" 'BEGIN { print microwatts / 1000000 }')

[[ -r ]] proves the attribute exists with read permission — not that reading it works. This firmware advertises power_now and then fails the read() itself:

$ ls -l /sys/class/power_supply/BAT0/power_now
-r--r--r-- 1 root root 4096 Aug 16 22:04 /sys/class/power_supply/BAT0/power_now
$ cat /sys/class/power_supply/BAT0/power_now
cat: /sys/class/power_supply/BAT0/power_now: No such device

So the guard passes, $(<...) hands back an empty string, awk computes "" / 1000000 = 0, and the correct energy-rate read out of UPower ten lines earlier is overwritten with zero. The current_now/voltage_now fallback has the same hole — a failed read there multiplies out to 0W just as quietly.

The fix:

  • read_power_attribute requires the read to succeed and the contents to parse as an integer. When it doesn't, power_rate_raw keeps the UPower value it already held, which is the correct figure on this hardware.
  • Both branches go through it, so the current_now/voltage_now path is covered too.
  • Where the kernel does answer, its instantaneous reading still wins over UPower's laggier one — that is the reason the override exists, and the new test pins it so this fix cannot quietly become "always use UPower".

Notes for review:

Verified:

rate
packaged omarchy-battery-status 0W
this branch 11.1W
upower -i energy-rate 11.1458 W

The new test fails on quattro (expected: 7.3W / actual: 0W) and passes with the fix. ./test/shell runs 178 of 181 files green; the three failures are pre-existing on a clean checkout and unrelated (config, snapper, unowned-system-paths all want an omarchy-pkgs checkout that isn't present here). ./test/cli passes.

🤖 Generated with Claude Code

Some ACPI firmware advertises power_now and then fails the read itself
with ENODEV. `[[ -r ]]` only proves the file exists with read permission,
so the empty string that comes back divides down to 0 and overwrites the
energy-rate UPower had already computed correctly, leaving the power
panel reading 0W while the battery drains.

Read the attribute through a helper that requires the read to succeed and
the contents to parse as a number, and fall through to UPower otherwise.
The current_now/voltage_now fallback had the same hole and gets the same
guard. Where the kernel does answer, its instantaneous reading still wins,
which is why the sysfs override exists in the first place.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@sorena100

Copy link
Copy Markdown

Confirming the same power_now/ENODEV failure on another HP Spectre generation:

  • Model: HP Spectre x360 2-in-1 Laptop 14-ef2xxx
  • Board: 8BA2
  • Kernel: 7.1.8-arch1-3
  • Battery: BAT1

At the same moment:

$ omarchy-battery-status --shell
percentage      90%
state           discharging
rate            0W
size            55Wh
time            4h 5m
cycles          0

UPower has the correct rate:

$ upower -i "$battery" | grep -E 'state|energy-rate|time to|native-path'
native-path:          BAT1
state:                discharging
energy-rate:          11.9872 W
time to empty:        4.1 hours

But the sysfs attribute exists and fails when read:

$ cat /sys/class/power_supply/BAT1/power_now
cat: /sys/class/power_supply/BAT1/power_now: No such device

current_now is unavailable as well, while voltage_now is readable.

@geirolafs

Copy link
Copy Markdown

Heads up: #6845 now covers this failure too — commit 3c40745 there discards a power_now/current_now read that passes [[ -r ]] but comes back empty (ENODEV), falling back to UPower's aggregate rate instead of reporting 0W, with tests for both the power_now and current_now paths. Might save the maintainers merging both.

@hegge25

hegge25 commented Aug 26, 2026

Copy link
Copy Markdown
Author

@geirolafs this is my first pr, so I'm not sure what you want me to do? Should I take it down?

@hegge25

hegge25 commented Aug 26, 2026

Copy link
Copy Markdown
Author

@geirolafs Thanks for the heads up — you're right, and no action needed from you. I pulled your branch and ran it on the machine from this PR (HP Spectre x360 14-ea0, BAT0, power_now present but ENODEV, no current_now): 0W before, 8.3W on cbe0705, against UPower's 8.27621 W. 3c40745 plus the [[ $microwatts =~ ^-?[0-9]+$ ]] || continue in the metering loop covers this cleanly, and your dual-battery test already locks both empty-read paths.

So closing this in favour of #6845 rather than leaving two overlapping changes to the same block for the maintainers. I've posted the hardware verification over there.

@sorena100 thanks for the third confirmation — that detail is on #6845 now too, and #6845 is the one to watch for the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Power panel shows 0W discharging when power_now read returns ENODEV

3 participants