[modules/battery] Fix ac and unknown display

If the computer runs on AC, display that instead of showing "100%" in
the status.

Also, if reading the charging status fails for some reason (except the
computer being on AC), go into critical state and display "n/a".

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-11 07:28:15 +01:00
parent 2cc2cf8282
commit d91294f010
3 changed files with 19 additions and 5 deletions

View file

@ -19,11 +19,15 @@ class Module(bumblebee.engine.Module):
super(Module, self).__init__(engine, config, super(Module, self).__init__(engine, config,
bumblebee.output.Widget(full_text=self.capacity) bumblebee.output.Widget(full_text=self.capacity)
) )
battery = self.parameter("device", "BAT1") battery = self.parameter("device", "BAT0")
self._path = "/sys/class/power_supply/{}".format(battery) self._path = "/sys/class/power_supply/{}".format(battery)
self._capacity = 100 self._capacity = 100
def capacity(self): def capacity(self):
if self._ac:
return "ac"
if self._capacity == -1:
return "n/a"
return "{:03d}%".format(self._capacity) return "{:03d}%".format(self._capacity)
def update(self, widgets): def update(self, widgets):
@ -31,16 +35,22 @@ class Module(bumblebee.engine.Module):
self._ac = False self._ac = False
if not os.path.exists(self._path): if not os.path.exists(self._path):
self._ac = True self._ac = True
self._capacity = 100
return
try: try:
with open(self._path + "/capacity") as f: with open(self._path + "/capacity") as f:
self._capacity = int(f.read()) self._capacity = int(f.read())
except IOError: except IOError:
self._capacity = 100 self._capacity = -1
self._capacity = self._capacity if self._capacity < 100 else 100 self._capacity = self._capacity if self._capacity < 100 else 100
def state(self, widget): def state(self, widget):
state = [] state = []
if self._capacity < 0:
return ["critical", "unknown"]
if self._capacity < int(self.parameter("critical", 10)): if self._capacity < int(self.parameter("critical", 10)):
state.append("critical") state.append("critical")
elif self._capacity < int(self.parameter("warning", 20)): elif self._capacity < int(self.parameter("warning", 20)):

View file

@ -121,8 +121,9 @@ class Theme(object):
state_themes = [] state_themes = []
# avoid infinite recursion # avoid infinite recursion
if name not in widget.state(): states = widget.state()
for state in widget.state(): if name not in states:
for state in states:
state_themes.append(self._get(widget, state, {})) state_themes.append(self._get(widget, state, {}))
value = self._defaults.get(name, default) value = self._defaults.get(name, default)

View file

@ -1,5 +1,8 @@
{ {
"defaults": { "separator": "", "padding": " " }, "defaults": {
"separator": "", "padding": " ",
"unknown": { "prefix": "" }
},
"date": { "prefix": "" }, "date": { "prefix": "" },
"time": { "prefix": "" }, "time": { "prefix": "" },
"memory": { "prefix": "" }, "memory": { "prefix": "" },