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)
|
2017-07-08 06:44:08 +02:00
|
|
|
* battery.showdevice : If set to "true", add the device name to the widget (defaults to False)
|
2019-06-26 20:36:04 +02:00
|
|
|
* battery.decorate : If set to "false", hides additional icons (charging, etc.) (defaults to True)
|
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
|
2017-07-08 06:44:08 +02:00
|
|
|
import bumblebee.util
|
2016-12-10 12:00:08 +01:00
|
|
|
|
2017-07-23 18:45:07 +02:00
|
|
|
try:
|
|
|
|
import power
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2016-12-10 12:00:08 +01:00
|
|
|
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:
|
2017-10-13 17:06:18 +02:00
|
|
|
self._batteries = ["/sys/class/power_supply/{}".format(b) for b in self._batteries]
|
2017-06-18 11:35:06 +02:00
|
|
|
if len(self._batteries) == 0:
|
2017-10-13 17:06:18 +02:00
|
|
|
self._batteries = ["/sys/class/power_supply/BAT0"]
|
2017-06-18 11:35:06 +02:00
|
|
|
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
|
2017-07-23 18:45:07 +02:00
|
|
|
|
2017-07-26 16:41:30 +02:00
|
|
|
def remaining(self):
|
|
|
|
estimate = 0.0
|
|
|
|
try:
|
2017-07-30 08:57:56 +02:00
|
|
|
estimate = power.PowerManagement().get_time_remaining_estimate()
|
2017-07-26 16:41:30 +02:00
|
|
|
# do not show remaining if on AC
|
2017-07-30 08:57:56 +02:00
|
|
|
if estimate == power.common.TIME_REMAINING_UNLIMITED:
|
2017-07-26 16:41:30 +02:00
|
|
|
return None
|
2017-07-30 08:57:56 +02:00
|
|
|
if estimate == power.common.TIME_REMAINING_UNKNOWN:
|
2017-09-09 19:56:39 +02:00
|
|
|
return ""
|
2017-07-26 16:41:30 +02:00
|
|
|
except Exception:
|
2017-09-09 19:56:39 +02:00
|
|
|
return ""
|
2017-07-26 16:41:30 +02:00
|
|
|
return bumblebee.util.durationfmt(estimate*60, shorten=True, suffix=True) # estimate is in minutes
|
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)
|
2018-11-03 17:52:02 +01:00
|
|
|
output = "{}%".format(capacity)
|
2017-06-18 11:35:06 +02:00
|
|
|
widget.set("theme.minwidth", "100%")
|
2018-11-03 17:52:02 +01:00
|
|
|
|
|
|
|
if bumblebee.util.asbool(self.parameter("showremaining", True))\
|
|
|
|
and self.getCharge(widget) == "Discharging":
|
|
|
|
output = "{} {}".format(output, self.remaining())
|
2016-12-10 12:00:08 +01:00
|
|
|
|
2018-11-03 18:27:19 +01:00
|
|
|
if bumblebee.util.asbool(self.parameter("showdevice", False)):
|
2018-11-03 17:52:02 +01:00
|
|
|
output = "{} ({})".format(output, os.path.basename(widget.name))
|
2017-07-23 18:45:07 +02:00
|
|
|
|
2018-11-03 17:52:02 +01:00
|
|
|
return output
|
|
|
|
|
2017-07-26 16:41:30 +02: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")
|
|
|
|
|
2019-06-26 20:36:04 +02:00
|
|
|
if bumblebee.util.asbool(self.parameter("decorate", True)) == False:
|
|
|
|
return state
|
|
|
|
|
2017-06-18 11:35:06 +02:00
|
|
|
if widget.get("ac"):
|
2016-12-10 12:00:08 +01:00
|
|
|
state.append("AC")
|
|
|
|
else:
|
2018-11-03 17:52:02 +01:00
|
|
|
charge = self.getCharge(widget)
|
2016-12-10 12:00:08 +01:00
|
|
|
if charge == "Discharging":
|
2017-10-13 17:06:18 +02:00
|
|
|
state.append("discharging-{}".format(min([10, 25, 50, 80, 100], key=lambda i: abs(i-capacity))))
|
2018-11-03 17:52:02 +01:00
|
|
|
elif charge == "Unknown":
|
2018-11-09 18:19:05 +01:00
|
|
|
state.append("unknown-{}".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
|
|
|
|
|
2018-11-03 17:52:02 +01:00
|
|
|
def getCharge(self, widget):
|
|
|
|
charge = ""
|
|
|
|
try:
|
|
|
|
with open("{}/status".format(widget.name)) as f:
|
|
|
|
charge = f.read().strip()
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
return charge
|
2016-12-10 12:00:08 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|