nwg-panel icon indicating copy to clipboard operation
nwg-panel copied to clipboard

Add support for multiple batteries

Open victoria-n-w opened this issue 2 years ago • 7 comments

I'm using a laptop with two batteries. However, only the capacity of the first one is displayed in panel, making it impossible to judge for how long the battery will last.

It would be best if the get_battery function (tools.py:483) automatically checked for multiple batteries, and adjusted the total percentage accordingly.

Unfortunately, it seems that psutils.sensors_battery() doesn't support multiple batteries. In that case, a simple script to calculate the current capacity could be used.

In the following example, you can see that my total battery percent is significantly lower than indicated by psutil

alia3% python
Python 3.10.5 (main, Jun  6 2022, 18:49:26) [GCC 12.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.sensors_battery()
sbattery(percent=69.56521739130434, secsleft=5685, power_plugged=False)
>>> 
alia3% cat /sys/class/power_supply/BAT0/capacity
69
alia3% cat /sys/class/power_supply/BAT1/capacity
5

I've written a simple script to get total current percentage, something similar could be used as a temporary workaround, maybe?

#! /bin/bash

FULL_SUM=0
NOW_SUM=0

for bat in /sys/class/power_supply/BAT*; do
	FULL_SUM=$(( FULL_SUM + $(cat $bat/energy_full) ))
	NOW_SUM=$(( NOW_SUM + $(cat $bat/energy_now) ))
done

echo $(( 100 * NOW_SUM / FULL_SUM  ))

(Note that batteries might have different capacities, hence the need to calculate the sum manually)

victoria-n-w avatar Jul 29 '22 22:07 victoria-n-w

I think it would be good to show both batteries separately. The problem is that I have no hardware to give it a try. It would be better to write and test changes on a machine with multiple batteries...

nwg-piotr avatar Jul 29 '22 22:07 nwg-piotr

I could help with testing/developing those changes

The sensors_battery function isn't really that complicated - maybe something similar could be implemented in nwg-panel?

victoria-n-w avatar Jul 29 '22 22:07 victoria-n-w

The sensors_battery function

We already use it, but it returns a single value on my machine:

[piotr@msi ~]$ python
Python 3.10.5 (main, Jun  6 2022, 18:49:26) [GCC 12.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.sensors_battery()
sbattery(percent=91.56889495225103, secsleft=<BatteryTime.POWER_TIME_UNKNOWN: -1>, power_plugged=None)
>>> 

May I see what it looks like on your laptop?

nwg-piotr avatar Jul 29 '22 22:07 nwg-piotr

yeah, on my laptop it detects only the first battery - there's a comment there that it's on purpose

psutils shows only one battery

>>> import psutil
>>> psutil.sensors_battery()
sbattery(percent=69.56521739130434, secsleft=5685, power_plugged=False)

even though I have two:

alia3% cat /sys/class/power_supply/BAT0/capacity
69
alia3% cat /sys/class/power_supply/BAT1/capacity
5

victoria-n-w avatar Jul 31 '22 03:07 victoria-n-w

Do you have 2 batteries in the output of upower -e | grep devices/battery?

nwg-piotr avatar Jul 31 '22 09:07 nwg-piotr

alia3% upower -e | grep devices/battery
/org/freedesktop/UPower/devices/battery_BAT0
/org/freedesktop/UPower/devices/battery_BAT1

I think detecting only the first battery is expected behaviour in psutils: https://github.com/giampaolo/psutil/blob/57ed46de8a988e7ab26279c2a967fb15b05397a3/psutil/_pslinux.py#L1468

victoria-n-w avatar Aug 01 '22 15:08 victoria-n-w

Yes, but nwg-panel already uses upower as an alternative method. We would only need to add some settings to force use of it, and to make use of multiple results.

nwg-piotr avatar Aug 01 '22 20:08 nwg-piotr

You may use the DisplayDevice of upower which shows the combined battery stats:

Device: /org/freedesktop/UPower/devices/DisplayDevice power supply: yes updated: Di 19 Sep 2023 11:36:36 CEST (23 seconds ago) has history: no has statistics: no battery present: yes state: discharging warning-level: none energy: 31,34 Wh energy-full: 82,62 Wh energy-rate: 7,285 W charge-cycles: N/A time to empty: 4,3 hours percentage: 37,9327% icon-name: 'battery-good-symbolic'

I have two batteries too on my T440s and this is the only way I can see the correct percentage.

janhieber avatar Sep 19 '23 09:09 janhieber