Repository: ultrabug/py3status

Issue:
Commit 9cf96a91 Broke battery_level module
Hello,

From my investigation commit 9cf96a91 broke the `battery_level` module. 

I'm running on Arch Linux and the latest update to py3status seemed to have pulled this in. 

I pulled the source from this github repo and installed py3status on my machine, with 9cf96a91 reverted, and the `battery_level` module seems to be working fine again.

Can we revert this commit until a working solution is refined?


Discussion:
I have the same experience:
`battery_level: unsupported operand type(s) for /: 'NoneType' and 'NoneType'`

I just tested reverting the code change and it works again.
ping @dkm 
@armandg @naclander can any of you reproduce this and get a larger backtrace to understand where this comes from?

I can't reproduce this here :/ 
Maybe it's a python version thing ? I'll have a look later today. Can you please provide your python version so I can fix this quickly ? Sorry !
@dkm 
 ~ $ python3 --version
`Python 3.8.2`


Full traceback:
```
 /usr/local/lib/python3.8/dist-packages/py3status/modules $ python3 battery_level.py
Module `test_module`: selected measurement_mode `sys`
unsupported operand type(s) for /: 'NoneType' and 'NoneType' (TypeError) module.py line 940.
Traceback
TypeError: unsupported operand type(s) for /: 'NoneType' and 'NoneType'
  File "/usr/local/lib/python3.8/dist-packages/py3status/module.py", line 940, in run
    response = method()
  File "battery_level.py", line 176, in battery_level
    battery_list = self.get_battery_info()
  File "battery_level.py", line 320, in _extract_battery_info_from_sys
    math.floor(remaining_energy / capacity * 100)
```

It seems that running through the `/sys/class/power_supply/BAT0` path works fine, because the error happens when the script is getting the info for path `/sys/class/power_supply/hidpp_battery_0`. That last path is for my wireless mouse.
A thought:

What kind of batteries will _not_ be checked if we leave out the `"*bat*"` from the bglobs variable in line 298?
Because this was the smallest change I had to do in commit 9cf96a9 for it to work.
@armandg : The kind that motivated the original patch (GPD Pocket v1) `max170xx_battery`. I was thinking of better filtering by checking the subdir contains the correct files before proceeding. I'll push some patch later tonight (after kids are asleep :D)
Could you provide me with a listing of files within your `/sys/class/power_supply/hidpp_battery_0` dir ? To be sure that filtering on the existence of the files used in `battery_level` is enough.
Below is a listing of all files under `/sys/class/power_supply/` :

```
tree /sys/class/power_supply

/sys/class/power_supply/
├── AC -> ../../devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0003:00/power_supply/AC/
├── BAT0 -> ../../devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0A:00/power_supply/BAT0/
├── ucsi-source-psy-USBC000:001 -> ../../devices/platform/USBC000:00/power_supply/ucsi-source-psy-USBC000:001/
├── ucsi-source-psy-USBC000:002 -> ../../devices/platform/USBC000:00/power_supply/ucsi-source-psy-USBC000:002/
└── wacom_battery_0 -> ../../devices/pci0000:00/0000:00:15.0/i2c_designware.0/i2c-1/i2c-WCOM4840:00/0018:056A:4840.0001/power_supply/wacom_battery_0/

5 directories, 0 files
```
Thanks. I only see now that the merge of the PR did not include my original commit message : https://github.com/dkm/py3status/commit/5300b79dc5925e85ee9ab2a616437154d6f7d4d6

```
Previous code only matches with "BAT*" names in the /sys/class/power_supply.
There does not seem to be any naming convention in the documentation:
https://www.kernel.org/doc/html/latest/power/power_supply_class.html
For example, the battery on the GPD Pocket is exposed as "max170xx_battery".

This patch allows for easier addition of glob patterns to use to match for
battery nodes and adds "*bat*" in the list.
```

That would have answered your previous question.

Resolving patch:
diff --git a/py3status/modules/battery_level.py b/py3status/modules/battery_level.py
--- a/py3status/modules/battery_level.py
+++ b/py3status/modules/battery_level.py
@@ -302,6 +302,9 @@ def _parse_battery_info(sys_path):
         for path in path_its:
             r = _parse_battery_info(path)
 
+            if not r:
+                continue
+
             capacity = r.get(
                 "POWER_SUPPLY_ENERGY_FULL", r.get("POWER_SUPPLY_CHARGE_FULL")
             )
@@ -315,6 +318,16 @@ def _parse_battery_info(sys_path):
             current_now = r.get("POWER_SUPPLY_CURRENT_NOW", 0)
             voltage_now = r.get("POWER_SUPPLY_VOLTAGE_NOW", 0)
 
+            # missing values may indicate this is not a battery and should be skipped
+            if not (
+                capacity
+                and present_rate
+                and remaining_energy
+                and current_now
+                and voltage_now
+            ):
+                continue
+
             battery = {}
             battery["capacity"] = capacity
             battery["charging"] = "Charging" in r["POWER_SUPPLY_STATUS"]


Test patch: