[modules/battery] Re-enable battery module
Extend theme to be able to accept lists of values and cycle through them. Use this to "animate" the charging symbol for the battery. see #23
This commit is contained in:
parent
72e375ac8b
commit
4d4a7bf29d
2 changed files with 60 additions and 0 deletions
54
bumblebee/modules/battery.py
Normal file
54
bumblebee/modules/battery.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
# pylint: disable=C0111,R0903
|
||||
|
||||
import os
|
||||
|
||||
import bumblebee.input
|
||||
import bumblebee.output
|
||||
import bumblebee.engine
|
||||
|
||||
class Module(bumblebee.engine.Module):
|
||||
def __init__(self, engine, config):
|
||||
super(Module, self).__init__(engine, config,
|
||||
bumblebee.output.Widget(full_text=self.capacity)
|
||||
)
|
||||
battery = self.parameter("device", "BAT0")
|
||||
self._path = "/sys/class/power_supply/{}".format(battery)
|
||||
self._capacity = 100
|
||||
|
||||
def capacity(self):
|
||||
return "{:02d}%".format(self._capacity)
|
||||
|
||||
def update(self, widgets):
|
||||
widget = widgets[0]
|
||||
self._ac = False
|
||||
if not os.path.exists(self._path):
|
||||
self._ac = True
|
||||
|
||||
with open(self._path + "/capacity") as f:
|
||||
self._capacity = int(f.read())
|
||||
self._capacity = self._capacity if self._capacity < 100 else 100
|
||||
|
||||
def state(self, widget):
|
||||
state = []
|
||||
if self._capacity < self.parameter("critical", 10):
|
||||
state.append("critical")
|
||||
elif self._capacity < self.parameter("warning", 20):
|
||||
state.append("warning")
|
||||
|
||||
if self._ac:
|
||||
state.append("AC")
|
||||
else:
|
||||
charge = ""
|
||||
with open(self._path + "/status") as f:
|
||||
charge = f.read().strip()
|
||||
if charge == "Discharging":
|
||||
state.append("discharging-{}".format(min([10, 25, 50, 80, 100] , key=lambda i:abs(i-self._capacity))))
|
||||
else:
|
||||
if self._capacity > 95:
|
||||
state.append("charged")
|
||||
else:
|
||||
state.append("charging")
|
||||
|
||||
return state
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
|
@ -132,6 +132,12 @@ class Theme(object):
|
|||
for theme in state_themes:
|
||||
value = theme.get(name, value)
|
||||
|
||||
if isinstance(value, list):
|
||||
key = "{}-idx".format(name)
|
||||
idx = widget.get(key, 0)
|
||||
widget.set(key, (idx + 1) % len(value))
|
||||
value = value[idx]
|
||||
|
||||
return value
|
||||
|
||||
# algorithm copied from
|
||||
|
|
Loading…
Reference in a new issue