[modules/battery_all] Better error handling if battery not found

When there are errors identifying the battery, make sure that the module
returns "n/a" instead of just throwing an error.

fixes #455
This commit is contained in:
Tobias Witek 2019-10-20 09:58:12 +02:00
parent e6d36ffd96
commit cc58817978

View file

@ -25,9 +25,12 @@ except ImportError:
class Module(bumblebee.engine.Module):
def __init__(self, engine, config):
self._batteries = []
try:
for battery in os.listdir('/sys/class/power_supply/'):
if not any(i in battery for i in ['AC', 'hidpp']):
self._batteries.append("/sys/class/power_supply/" + battery)
except:
pass
super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.capacity))
@ -67,11 +70,7 @@ class Module(bumblebee.engine.Module):
errors += 1
if errors == len(self._batteries):
# if all batteries return errors, but we are still running
# assume we are on A/C
widget.set("ac", True)
widget.set("capacity", 100)
return "ac"
return "n/a"
capacity = int( float(self.energy_now) / float(self.energy_full) * 100.0)
capacity = capacity if capacity < 100 else 100