giampaolo / psutil

Cross-platform lib for process and system monitoring in Python
BSD 3-Clause "New" or "Revised" License
10.18k stars 1.38k forks source link

[Linux] sensors_battery() reports "percent" value > 100 #1785

Open linusg opened 4 years ago

linusg commented 4 years ago

Platform

Bug description

psutil.sensors_battery() reports a percent value above 100 when the battery is almost fully charged:

Python 3.8.3 (default, May 29 2020, 00:00:00) 
[GCC 10.1.1 20200507 (Red Hat 10.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.sensors_battery()
sbattery(percent=107.43449116392443, secsleft=<BatteryTime.POWER_TIME_UNLIMITED: -2>, power_plugged=True)
>>> psutil.sensors_battery()
sbattery(percent=108.53138330286411, secsleft=<BatteryTime.POWER_TIME_UNLIMITED: -2>, power_plugged=True)
>>> psutil.sensors_battery()
sbattery(percent=112.06581352833638, secsleft=<BatteryTime.POWER_TIME_UNLIMITED: -2>, power_plugged=True)
>>>
sbattery(percent=114.38147471054235, secsleft=<BatteryTime.POWER_TIME_UNLIMITED: -2>, power_plugged=True)
>>> psutil.sensors_battery()
sbattery(percent=115.11273613650214, secsleft=<BatteryTime.POWER_TIME_UNLIMITED: -2>, power_plugged=True)
>>> 

Where psutil reports 114%, GNOME reports 97% (battery icon top left corner).

Internally the laptop seems to have two batteries, yet I would expect the percentage to never exceed 100 - is this to be expected?

image

linusg commented 4 years ago

Actually the weird percentage comes from the sysfs:

$ cat /sys/class/power_supply/BAT0/capacity
117
$ cat /sys/class/power_supply/BAT1/capacity
99

I'll investigate further. Feel free to close if this is out of scope for psutil to handle.