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