Psutil: [Linux] Battery not detected by psutil

Created on 16 Sep 2020  路  4Comments  路  Source: giampaolo/psutil

Platform
Linux Mint 19.3 (Ubuntu 18.04) 32-bit
32-bit Pentium 4 laptop
psutil version: 5.7.2
python 3.6.9

Bug description
Psutil thinks there is no battery in this laptop.

Output of python3 -c "import psutil; print(psutil.sensors_battery())" is:

None

./battery.py

no battery is installed

./sensors.py

acpitz
    Temperatures:
        acpitz               47.8掳C (high=84.8掳C, critical=84.8掳C)

As a result, this machine doesn't show any battery info in BpyTOP 1.0.32 or later, which uses psutil to get the battery info. But the battery is detected just fine by the XFCE power plugin sitting in the system tray.

power_plugin_battery

cat /sys/class/power_supply/BAT0/model_name

BAT0

cat /sys/class/power_supply/BAT0/type

Battery

cat /sys/class/power_supply/BAT0/status

Full (or Disharging, or Charging, as usual)

cat /sys/class/power_supply/BAT0/charge_now

5814000

cat /sys/class/power_supply/BAT0/hwmon2/name

BAT0

ll /sys/class/power_supply/BAT0/

-rw-r--r-- 1 root root 4096 Sep 16 12:01 alarm
-r--r--r-- 1 root root 4096 Sep 16 11:54 capacity
-r--r--r-- 1 root root 4096 Sep 16 12:01 capacity_level
-r--r--r-- 1 root root 4096 Sep 16 11:56 charge_full
-r--r--r-- 1 root root 4096 Sep 16 12:01 charge_full_design
-r--r--r-- 1 root root 4096 Sep 16 11:56 charge_now
-r--r--r-- 1 root root 4096 Sep 16 11:56 current_now
-r--r--r-- 1 root root 4096 Sep 16 12:01 cycle_count
lrwxrwxrwx 1 root root    0 Sep 16 12:01 device -> ../../../PNP0C0A:00/
drwxr-xr-x 3 root root    0 Sep 16 12:01 hwmon2/
-r--r--r-- 1 root root 4096 Sep 16 12:01 manufacturer
-r--r--r-- 1 root root 4096 Sep 16 12:01 model_name
drwxr-xr-x 2 root root    0 Sep 16 12:01 power/
-r--r--r-- 1 root root 4096 Sep 16 11:56 present
-r--r--r-- 1 root root 4096 Sep 16 12:01 serial_number
-r--r--r-- 1 root root 4096 Sep 16 11:56 status
lrwxrwxrwx 1 root root    0 Sep 16 12:01 subsystem -> ../../../../../../class/power_supply/
-r--r--r-- 1 root root 4096 Sep 16 12:01 technology
-r--r--r-- 1 root root 4096 Sep 16 12:01 type
-rw-r--r-- 1 root root 4096 Sep 16 12:01 uevent
-r--r--r-- 1 root root 4096 Sep 16 11:56 voltage_min_design
-r--r--r-- 1 root root 4096 Sep 16 11:56 voltage_now

All the information about the battery is available. But there is no /sys/class/power_supply/BAT0/name file, instead it's under hwmon2/name. Could that be part of the problem?

Test results
{ output of python -c psutil.tests (failures only, not full result) }

I don't know how to run this test command correctly. I get this error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'psutil' is not defined
bug linux

Most helpful comment

PR #1838 should fix this then.

All 4 comments

Checked the documentation to figure out how to run the tests:

python3 -m psutil.tests

This was at the end of the run:

======================================================================
ERROR: psutil.tests.test_misc.TestMisc.test_setup_script
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/kris/.local/lib/python3.6/site-packages/psutil/tests/test_misc.py", line 358, in test_setup_script
    module = import_module_by_path(setup_py)
  File "/home/kris/.local/lib/python3.6/site-packages/psutil/tests/__init__.py", line 1537, in import_module_by_path
    spec.loader.exec_module(mod)
  File "<frozen importlib._bootstrap_external>", line 674, in exec_module
  File "<frozen importlib._bootstrap_external>", line 780, in get_code
  File "<frozen importlib._bootstrap_external>", line 832, in get_data
FileNotFoundError: [Errno 2] No such file or directory: '/home/kris/.local/lib/python3.6/site-packages/setup.py'

======================================================================
FAIL: psutil.tests.test_process.TestProcess.test_terminal
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/kris/.local/lib/python3.6/site-packages/psutil/tests/test_process.py", line 305, in test_terminal
    self.assertIsNone(terminal)
AssertionError: '/dev/pts/1' is not None

----------------------------------------------------------------------
Ran 542 tests in 61.062s

FAILED (failures=1, errors=1, skipped=165)
FAILED

Error lines after filtering any line with " ok" or " only":

psutil.tests.test_system.TestDiskAPIs.test_disk_usage_unicode ... skipped 'not an ASCII fs'
psutil.tests.test_system.TestSensorsAPIs.test_sensors_battery ... skipped 'no battery'
psutil.tests.test_process.TestProcess.test_ionice_win ... skipped 'not supported on this win version'
psutil.tests.test_process.TestProcess.test_terminal ... FAIL
psutil.tests.test_linux.TestSensorsBattery.test_emulate_energy_full_0 ... skipped 'no battery'
psutil.tests.test_linux.TestSensorsBattery.test_emulate_energy_full_not_avail ... skipped 'no battery'
psutil.tests.test_linux.TestSensorsBattery.test_emulate_no_base_files ... skipped 'no battery'
psutil.tests.test_linux.TestSensorsBattery.test_emulate_no_power ... skipped 'no battery'
psutil.tests.test_linux.TestSensorsBattery.test_emulate_power_not_plugged ... skipped 'no battery'
psutil.tests.test_linux.TestSensorsBattery.test_emulate_power_not_plugged_2 ... skipped 'no battery'
psutil.tests.test_linux.TestSensorsBattery.test_emulate_power_plugged ... skipped 'no battery'
psutil.tests.test_linux.TestSensorsBattery.test_emulate_power_plugged_2 ... skipped 'no battery'
psutil.tests.test_linux.TestSensorsBattery.test_emulate_power_undetermined ... skipped 'no battery'
psutil.tests.test_linux.TestSensorsBattery.test_percent ... skipped 'no battery'
psutil.tests.test_linux.TestSensorsBattery.test_power_plugged ... skipped 'no battery'
psutil.tests.test_testutils.TestMemLeakClass.test_leak_mem ... Run #1: extra-mem=1.0M, per-call=20.7K, calls=50
psutil.tests.test_misc.TestMisc.test_setup_script ... ERROR
psutil.tests.test_misc.TestScripts.test_battery ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_coverage ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_cpu_distribution ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_disk_usage ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_executable ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_fans ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_free ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_ifconfig ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_iotop ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_killall ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_meminfo ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_netstat ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_nettop ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_pidof ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_pmap ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_procinfo ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_procsmem ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_ps ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_pstree ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_sensors ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_temperatures ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_top ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_who ... skipped "can't locate scripts directory"
psutil.tests.test_misc.TestScripts.test_winservices ... skipped "can't locate scripts directory"

I tried running this inside a cloned psutil folder to bypass the "cant locate scripts directory" errors but that didn't seem to work.

The full test run captured with python3 -m psutil.tests 2>&1 | tee test_output.txt is here:

test_output.txt

From https://github.com/aristocratos/bpytop/issues/110

I can see the issue now, to get a reading you need one the files: "energy_now" or "charge_now" (present) and one of the files "power_now" or "current_now" (present), your "current_now" file is however empty, so it doesn't return a value, which results in the function returning None.

My patch added support for a "time_to_empty_now" file that can be used if either of the previous values fail, but your battery doesn't provide a "time_to_empty_now" file, so no help there.

It essentially fails because it can't get a value to report as "seconds to empty", but it does get a valid percentage value from "capacity" file and the current charge state from "status" file, so you could maybe argue that psutil should return the "seconds to empty" as unknown instead if it can get the other values.

@giampaolo
So the proposal would be to not return the function as "None" if "secsleft" or "power_plugged" can't be determined but return those values as "psutil.POWER_TIME_UNKNOWN" and "None".
And then only if "percent" can't be determined return "None" instead of the named tuple.

If you think this makes sense I can make a PR with the changes.

Hello. Yes, if we can get at least the percent value then I would say it makes sense to at least return that one.
I just checked the doc and it already states that both secsleft and power_plugged can be undetermined, so your change does not even seem to alter the current behavior / promise.
Please go for it. ;-)

PR #1838 should fix this then.

Was this page helpful?
0 / 5 - 0 ratings