[modules/battery] Autodetect battery devices

The module now creates a widget for each battery device it detects and
shows the status for each of them (I don't know of anyone with more than
a single battery, but if I'm overhauling the module anyhow, might as
well do it comprehensively).

fixes #117
This commit is contained in:
Tobias Witek 2017-06-18 11:35:06 +02:00
parent 24a7c5c926
commit d69f13f0b4
2 changed files with 70 additions and 51 deletions

View file

@ -3,12 +3,14 @@
"""Displays battery status, remaining percentage and charging information. """Displays battery status, remaining percentage and charging information.
Parameters: Parameters:
* battery.device : The device to read information from (defaults to BAT0) * battery.device : Comma-separated list of battery devices to read information from (defaults to auto for auto-detection)
* battery.warning : Warning threshold in % of remaining charge (defaults to 20) * battery.warning : Warning threshold in % of remaining charge (defaults to 20)
* battery.critical: Critical threshold in % of remaining charge (defaults to 10) * battery.critical : Critical threshold in % of remaining charge (defaults to 10)
* battery.showdevice : If set to "true", add the device name to the widget
""" """
import os import os
import glob
import bumblebee.input import bumblebee.input
import bumblebee.output import bumblebee.output
@ -16,56 +18,73 @@ import bumblebee.engine
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
def __init__(self, engine, config): def __init__(self, engine, config):
widget = bumblebee.output.Widget(full_text=self.capacity) widgets = []
widget.set("theme.minwidth", "100%") super(Module, self).__init__(engine, config, widgets)
super(Module, self).__init__(engine, config, widget) self._batteries = self.parameter("device", "auto").split(",")
battery = self.parameter("device", "BAT0") if self._batteries[0] == "auto":
self._path = "/sys/class/power_supply/{}".format(battery) self._batteries = glob.glob("/sys/class/power_supply/BAT*")
self._capacity = 100 else:
self._ac = False self._batteries = [ "/sys/class/power_supply/{}".format(b) for b in self._batteries ]
if len(self._batteries) == 0:
def capacity(self, widget): self._batteries = [ "/sys/class/power_supply/BAT0" ]
if self._ac: self.update(widgets)
return "ac"
if self._capacity == -1:
return "n/a"
return "{}%".format(self._capacity)
def update(self, widgets): def update(self, widgets):
self._ac = False new_widgets = []
if not os.path.exists(self._path): for path in self._batteries:
self._ac = True widget = self.widget(path)
self._capacity = 100 if not widget:
return widget = bumblebee.output.Widget(full_text=self.capacity, name=path)
new_widgets.append(widget)
self.capacity(widget)
while len(widgets) > 0: del widgets[0]
for widget in new_widgets:
widgets.append(widget)
self._widgets = widgets
def capacity(self, widget):
widget.set("capacity", -1)
widget.set("ac", False)
if not os.path.exists(widget.name):
widget.set("capacity", 100)
widget.set("ac", True)
return "ac"
capacity = 100
try: try:
with open(self._path + "/capacity") as f: with open("{}/capacity".format(widget.name)) as f:
self._capacity = int(f.read()) capacity = int(f.read())
except IOError: except IOError:
self._capacity = -1 return "n/a"
self._capacity = self._capacity if self._capacity < 100 else 100 capacity = capacity if capacity < 100 else 100
widget.set("capacity", capacity)
if self.parameter("showdevice") == "true":
widget.set("theme.minwidth", "100% ({})".format(os.path.basename(widget.name)))
return "{}% ({})".format(capacity, os.path.basename(widget.name))
widget.set("theme.minwidth", "100%")
return "{}%".format(capacity)
def state(self, widget): def state(self, widget):
state = [] state = []
capacity = widget.get("capacity")
if self._capacity < 0: if capacity < 0:
return ["critical", "unknown"] return ["critical", "unknown"]
if self._capacity < int(self.parameter("critical", 10)): if capacity < int(self.parameter("critical", 10)):
state.append("critical") state.append("critical")
elif self._capacity < int(self.parameter("warning", 20)): elif capacity < int(self.parameter("warning", 20)):
state.append("warning") state.append("warning")
if self._ac: if widget.get("ac"):
state.append("AC") state.append("AC")
else: else:
charge = "" charge = ""
with open(self._path + "/status") as f: with open("{}/status".format(widget.name)) as f:
charge = f.read().strip() charge = f.read().strip()
if charge == "Discharging": if charge == "Discharging":
state.append("discharging-{}".format(min([10, 25, 50, 80, 100] , key=lambda i:abs(i-self._capacity)))) state.append("discharging-{}".format(min([10, 25, 50, 80, 100] , key=lambda i:abs(i-capacity))))
else: else:
if self._capacity > 95: if capacity > 95:
state.append("charged") state.append("charged")
else: else:
state.append("charging") state.append("charging")

View file

@ -26,6 +26,7 @@ class TestBatteryModule(unittest.TestCase):
self.file = mock.Mock() self.file = mock.Mock()
self.file.__enter__ = lambda x: self.file self.file.__enter__ = lambda x: self.file
self.file.__exit__ = lambda x, a, b, c: "" self.file.__exit__ = lambda x, a, b, c: ""
self.file.read.return_value = "120"
self.open.return_value = self.file self.open.return_value = self.file
self.exists.return_value = True self.exists.return_value = True
@ -40,9 +41,7 @@ class TestBatteryModule(unittest.TestCase):
self.normalValue = "26" self.normalValue = "26"
self.chargedValue = "96" self.chargedValue = "96"
for widget in self.module.widgets(): self.module.widgets()[0]
widget.link_module(self.module)
self.anyWidget = widget
def tearDown(self): def tearDown(self):
self._stdout.stop() self._stdout.stop()
@ -56,46 +55,47 @@ class TestBatteryModule(unittest.TestCase):
def test_critical(self): def test_critical(self):
self.file.read.return_value = self.criticalValue self.file.read.return_value = self.criticalValue
self.module.update_all() self.module.update_all()
self.assertTrue("critical" in self.module.state(self.anyWidget)) self.assertTrue("critical" in self.module.state(self.module.widgets()[0]))
def test_warning(self): def test_warning(self):
self.file.read.return_value = self.warningValue self.file.read.return_value = self.warningValue
self.module.update_all() self.module.update_all()
self.assertTrue("warning" in self.module.state(self.anyWidget)) self.assertTrue("warning" in self.module.state(self.module.widgets()[0]))
def test_normal(self): def test_normal(self):
self.file.read.return_value = self.normalValue self.file.read.return_value = self.normalValue
self.module.update_all() self.module.update_all()
self.assertTrue(not "warning" in self.module.state(self.anyWidget)) self.assertTrue(not "warning" in self.module.state(self.module.widgets()[0]))
self.assertTrue(not "critical" in self.module.state(self.anyWidget)) self.assertTrue(not "critical" in self.module.state(self.module.widgets()[0]))
def test_overload(self): def test_overload(self):
self.file.read.return_value = "120" self.file.read.return_value = "120"
self.module.update_all() self.module.update_all()
self.assertTrue(not "warning" in self.module.state(self.anyWidget)) self.assertTrue(not "warning" in self.module.state(self.module.widgets()[0]))
self.assertTrue(not "critical" in self.module.state(self.anyWidget)) self.assertTrue(not "critical" in self.module.state(self.module.widgets()[0]))
self.assertEquals(self.module.capacity(self.anyWidget), "100%") self.assertEquals(self.module.capacity(self.module.widgets()[0]), "100%")
def test_ac(self): def test_ac(self):
self.exists.return_value = False self.exists.return_value = False
self.file.read.return_value = "120"
self.module.update_all() self.module.update_all()
self.assertEquals(self.module.capacity(self.anyWidget), "ac") self.assertEquals(self.module.capacity(self.module.widgets()[0]), "ac")
self.assertTrue("AC" in self.module.state(self.anyWidget)) self.assertTrue("AC" in self.module.state(self.module.widgets()[0]))
def test_error(self): def test_error(self):
self.file.read.side_effect = IOError("failed to read") self.file.read.side_effect = IOError("failed to read")
self.module.update_all() self.module.update_all()
self.assertEquals(self.module.capacity(self.anyWidget), "n/a") self.assertEquals(self.module.capacity(self.module.widgets()[0]), "n/a")
self.assertTrue("critical" in self.module.state(self.anyWidget)) self.assertTrue("critical" in self.module.state(self.module.widgets()[0]))
self.assertTrue("unknown" in self.module.state(self.anyWidget)) self.assertTrue("unknown" in self.module.state(self.module.widgets()[0]))
def test_charging(self): def test_charging(self):
self.file.read.return_value = self.chargedValue self.file.read.return_value = self.chargedValue
self.module.update_all() self.module.update_all()
self.assertTrue("charged" in self.module.state(self.anyWidget)) self.assertTrue("charged" in self.module.state(self.module.widgets()[0]))
self.file.read.return_value = self.normalValue self.file.read.return_value = self.normalValue
self.module.update_all() self.module.update_all()
self.assertTrue("charging" in self.module.state(self.anyWidget)) self.assertTrue("charging" in self.module.state(self.module.widgets()[0]))
def test_discharging(self): def test_discharging(self):
for limit in [ 10, 25, 50, 80, 100 ]: for limit in [ 10, 25, 50, 80, 100 ]:
@ -103,6 +103,6 @@ class TestBatteryModule(unittest.TestCase):
self.file.read.return_value = str(value) self.file.read.return_value = str(value)
self.module.update_all() self.module.update_all()
self.file.read.return_value = "Discharging" self.file.read.return_value = "Discharging"
self.assertTrue("discharging-{}".format(limit) in self.module.state(self.anyWidget)) self.assertTrue("discharging-{}".format(limit) in self.module.state(self.module.widgets()[0]))
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4