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:
|
|
|
|
* battery.device : The device to read information from (defaults to BAT0)
|
|
|
|
* battery.warning : Warning threshold in % of remaining charge (defaults to 20)
|
|
|
|
* battery.critical: Critical threshold in % of remaining charge (defaults to 10)
|
|
|
|
"""
|
|
|
|
|
2016-12-10 12:00:08 +01:00
|
|
|
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)
|
|
|
|
)
|
2016-12-11 07:28:15 +01:00
|
|
|
battery = self.parameter("device", "BAT0")
|
2016-12-10 12:00:08 +01:00
|
|
|
self._path = "/sys/class/power_supply/{}".format(battery)
|
|
|
|
self._capacity = 100
|
|
|
|
|
|
|
|
def capacity(self):
|
2016-12-11 07:28:15 +01:00
|
|
|
if self._ac:
|
|
|
|
return "ac"
|
|
|
|
if self._capacity == -1:
|
|
|
|
return "n/a"
|
2016-12-10 14:50:49 +01:00
|
|
|
return "{:03d}%".format(self._capacity)
|
2016-12-10 12:00:08 +01:00
|
|
|
|
|
|
|
def update(self, widgets):
|
|
|
|
widget = widgets[0]
|
|
|
|
self._ac = False
|
|
|
|
if not os.path.exists(self._path):
|
|
|
|
self._ac = True
|
2016-12-11 07:28:15 +01:00
|
|
|
self._capacity = 100
|
|
|
|
return
|
2016-12-10 12:00:08 +01:00
|
|
|
|
2016-12-10 19:54:31 +01:00
|
|
|
try:
|
|
|
|
with open(self._path + "/capacity") as f:
|
|
|
|
self._capacity = int(f.read())
|
|
|
|
except IOError:
|
2016-12-11 07:28:15 +01:00
|
|
|
self._capacity = -1
|
2016-12-10 12:00:08 +01:00
|
|
|
self._capacity = self._capacity if self._capacity < 100 else 100
|
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
state = []
|
2016-12-11 07:28:15 +01:00
|
|
|
|
|
|
|
if self._capacity < 0:
|
|
|
|
return ["critical", "unknown"]
|
|
|
|
|
2016-12-10 14:50:49 +01:00
|
|
|
if self._capacity < int(self.parameter("critical", 10)):
|
2016-12-10 12:00:08 +01:00
|
|
|
state.append("critical")
|
2016-12-10 14:50:49 +01:00
|
|
|
elif self._capacity < int(self.parameter("warning", 20)):
|
2016-12-10 12:00:08 +01:00
|
|
|
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
|