2016-12-10 12:00:08 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
2016-12-10 12:03:58 +01:00
|
|
|
"""Displays battery status, remaining percentage and charging information.
|
|
|
|
|
|
|
|
Parameters:
|
2017-06-18 11:35:06 +02:00
|
|
|
* 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.critical : Critical threshold in % of remaining charge (defaults to 10)
|
|
|
|
* battery.showdevice : If set to "true", add the device name to the widget
|
2016-12-10 12:03:58 +01:00
|
|
|
"""
|
|
|
|
|
2016-12-10 12:00:08 +01:00
|
|
|
import os
|
2017-06-18 11:35:06 +02:00
|
|
|
import glob
|
2016-12-10 12:00:08 +01:00
|
|
|
|
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
|
|
|
|
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
2017-06-18 11:35:06 +02:00
|
|
|
widgets = []
|
|
|
|
super(Module, self).__init__(engine, config, widgets)
|
|
|
|
self._batteries = self.parameter("device", "auto").split(",")
|
|
|
|
if self._batteries[0] == "auto":
|
|
|
|
self._batteries = glob.glob("/sys/class/power_supply/BAT*")
|
|
|
|
else:
|
|
|
|
self._batteries = [ "/sys/class/power_supply/{}".format(b) for b in self._batteries ]
|
|
|
|
if len(self._batteries) == 0:
|
|
|
|
self._batteries = [ "/sys/class/power_supply/BAT0" ]
|
|
|
|
self.update(widgets)
|
2017-07-08 07:07:00 +02:00
|
|
|
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
|
|
|
|
cmd="gnome-power-statistics")
|
2016-12-10 12:00:08 +01:00
|
|
|
|
|
|
|
def update(self, widgets):
|
2017-06-18 11:35:06 +02:00
|
|
|
new_widgets = []
|
|
|
|
for path in self._batteries:
|
|
|
|
widget = self.widget(path)
|
|
|
|
if not widget:
|
|
|
|
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
|
2016-12-10 12:00:08 +01:00
|
|
|
|
2017-06-18 11:35:06 +02:00
|
|
|
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
|
2016-12-10 19:54:31 +01:00
|
|
|
try:
|
2017-06-18 11:35:06 +02:00
|
|
|
with open("{}/capacity".format(widget.name)) as f:
|
|
|
|
capacity = int(f.read())
|
2016-12-10 19:54:31 +01:00
|
|
|
except IOError:
|
2017-06-18 11:35:06 +02:00
|
|
|
return "n/a"
|
|
|
|
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)
|
2016-12-10 12:00:08 +01:00
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
state = []
|
2017-06-18 11:35:06 +02:00
|
|
|
capacity = widget.get("capacity")
|
2016-12-11 07:28:15 +01:00
|
|
|
|
2017-06-18 11:35:06 +02:00
|
|
|
if capacity < 0:
|
2016-12-11 07:28:15 +01:00
|
|
|
return ["critical", "unknown"]
|
|
|
|
|
2017-06-18 11:35:06 +02:00
|
|
|
if capacity < int(self.parameter("critical", 10)):
|
2016-12-10 12:00:08 +01:00
|
|
|
state.append("critical")
|
2017-06-18 11:35:06 +02:00
|
|
|
elif capacity < int(self.parameter("warning", 20)):
|
2016-12-10 12:00:08 +01:00
|
|
|
state.append("warning")
|
|
|
|
|
2017-06-18 11:35:06 +02:00
|
|
|
if widget.get("ac"):
|
2016-12-10 12:00:08 +01:00
|
|
|
state.append("AC")
|
|
|
|
else:
|
|
|
|
charge = ""
|
2017-06-18 11:35:06 +02:00
|
|
|
with open("{}/status".format(widget.name)) as f:
|
2016-12-10 12:00:08 +01:00
|
|
|
charge = f.read().strip()
|
|
|
|
if charge == "Discharging":
|
2017-06-18 11:35:06 +02:00
|
|
|
state.append("discharging-{}".format(min([10, 25, 50, 80, 100] , key=lambda i:abs(i-capacity))))
|
2016-12-10 12:00:08 +01:00
|
|
|
else:
|
2017-06-18 11:35:06 +02:00
|
|
|
if capacity > 95:
|
2016-12-10 12:00:08 +01:00
|
|
|
state.append("charged")
|
|
|
|
else:
|
|
|
|
state.append("charging")
|
|
|
|
|
|
|
|
return state
|
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|