linux: Fix < 0.01 W energy-rate readings from power_now sysfs property

Currently, if a power supplies' power_now sysfs file reports discharge
rates < 0.01 W, the code will try to calculate the discharge rate from
the legacy sysfs files. On new kernels where those don't exist, this
produces wrong results.

For example, on a dual-battery Thinkpad T450s, while the external
battery is discharging, the internal battery reports power_now = 0,
but the corresponding upower energy-rate field incorrectly reads
about 2.3 W.

This patch fixes the issue by falling back to the legacy code only if
the legacy current_now sysfs file exists.

Closes: #7, #44
This commit is contained in:
Philipp Zabel 2021-06-17 18:14:53 +02:00
parent 0f2837acde
commit 2949136b15

View file

@ -682,8 +682,9 @@ up_device_supply_refresh_battery (UpDeviceSupply *supply,
state = up_device_supply_get_state (native);
/* this is the new value in uW */
energy_rate = fabs (g_udev_device_get_sysfs_attr_as_double_uncached (native, "power_now") / 1000000.0);
if (energy_rate < 0.01) {
if (g_udev_device_has_sysfs_attr (native, "power_now")) {
energy_rate = fabs (g_udev_device_get_sysfs_attr_as_double_uncached (native, "power_now") / 1000000.0);
} else {
gdouble charge_full;
/* convert charge to energy */