[modules/battery] gracefully handle "no batteries" case

if no batteries are found, assume the machine is running on AC.

fixes #636
This commit is contained in:
tobi-wan-kenobi 2020-05-28 08:17:35 +02:00
parent be008c0a6f
commit caf2fea04b

View file

@ -114,22 +114,29 @@ class Module(core.module.Module):
os.path.basename(battery) os.path.basename(battery)
for battery in glob.glob("/sys/class/power_supply/BAT*") for battery in glob.glob("/sys/class/power_supply/BAT*")
] ]
if len(self._batteries) == 0:
raise Exception("no batteries configured/found")
core.input.register( core.input.register(
self, button=core.input.LEFT_MOUSE, cmd="gnome-power-statistics" self, button=core.input.LEFT_MOUSE, cmd="gnome-power-statistics"
) )
if util.format.asbool(self.parameter("compact-devices", False)): if len(self._batteries) == 0:
widget = self.add_widget(full_text=self.capacity, name="all-batteries") widget = self.add_widget(full_text=self.ac, name="ac")
widget.set("ac", True)
widget.set("capacity", 100)
else: else:
for battery in self._batteries: if util.format.asbool(self.parameter("compact-devices", False)):
log.debug("adding new widget for {}".format(battery)) widget = self.add_widget(full_text=self.capacity, name="all-batteries")
widget = self.add_widget(full_text=self.capacity, name=battery) else:
for battery in self._batteries:
log.debug("adding new widget for {}".format(battery))
widget = self.add_widget(full_text=self.capacity, name=battery)
for w in self.widgets(): for w in self.widgets():
if util.format.asbool(self.parameter("decorate", True)) == False: if util.format.asbool(self.parameter("decorate", True)) == False:
widget.set("theme.exclude", "suffix") widget.set("theme.exclude", "suffix")
def ac(self, widget):
return "ac"
def capacity(self, widget): def capacity(self, widget):
if widget.name == "all-batteries": if widget.name == "all-batteries":
capacity = self.__manager.capacity_all(self._batteries) capacity = self.__manager.capacity_all(self._batteries)