2016-10-30 18:10:25 +01:00
|
|
|
import datetime
|
|
|
|
import bumblebee.module
|
2016-11-17 23:02:49 +01:00
|
|
|
import os.path
|
2016-10-30 18:10:25 +01:00
|
|
|
|
2016-10-31 15:49:15 +01:00
|
|
|
def description():
|
2016-10-31 16:08:03 +01:00
|
|
|
return "Displays battery status, percentage and whether it's charging or discharging."
|
2016-10-31 15:49:15 +01:00
|
|
|
|
2016-11-05 16:18:53 +01:00
|
|
|
def parameters():
|
|
|
|
return [ "battery.device: The device to read from (defaults to BAT0)" ]
|
|
|
|
|
2016-10-30 18:10:25 +01:00
|
|
|
class Module(bumblebee.module.Module):
|
2016-12-02 18:53:34 +01:00
|
|
|
def __init__(self, output, config):
|
|
|
|
super(Module, self).__init__(output, config)
|
2016-11-05 14:26:02 +01:00
|
|
|
self._battery = config.parameter("device", "BAT0")
|
2016-11-17 23:02:49 +01:00
|
|
|
self._capacity = 100
|
2016-10-30 18:10:25 +01:00
|
|
|
self._status = "Unknown"
|
|
|
|
|
2016-11-05 13:09:28 +01:00
|
|
|
def widgets(self):
|
2016-11-17 23:02:49 +01:00
|
|
|
self._AC = False;
|
|
|
|
self._path = "/sys/class/power_supply/{}".format(self._battery)
|
|
|
|
if not os.path.exists(self._path):
|
|
|
|
self._AC = True;
|
|
|
|
return bumblebee.output.Widget(self,"AC")
|
|
|
|
|
|
|
|
with open(self._path + "/capacity") as f:
|
2016-10-30 18:10:25 +01:00
|
|
|
self._capacity = int(f.read())
|
2016-11-03 19:43:30 +01:00
|
|
|
self._capacity = self._capacity if self._capacity < 100 else 100
|
2016-10-30 18:10:25 +01:00
|
|
|
|
2016-11-05 13:12:30 +01:00
|
|
|
return bumblebee.output.Widget(self,"{:02d}%".format(self._capacity))
|
2016-10-30 18:10:25 +01:00
|
|
|
|
2016-11-05 13:42:26 +01:00
|
|
|
def warning(self, widget):
|
2016-11-05 14:26:02 +01:00
|
|
|
return self._capacity < self._config.parameter("warning", 20)
|
2016-10-31 12:01:21 +01:00
|
|
|
|
2016-11-05 13:42:26 +01:00
|
|
|
def critical(self, widget):
|
2016-11-05 14:26:02 +01:00
|
|
|
return self._capacity < self._config.parameter("critical", 10)
|
2016-10-31 12:01:21 +01:00
|
|
|
|
2016-11-05 13:42:26 +01:00
|
|
|
def state(self, widget):
|
2016-11-17 23:02:49 +01:00
|
|
|
if self._AC:
|
|
|
|
return "AC"
|
|
|
|
|
|
|
|
with open(self._path + "/status") as f:
|
2016-10-31 07:34:43 +01:00
|
|
|
self._status = f.read().strip()
|
2016-11-05 13:09:28 +01:00
|
|
|
|
2016-10-31 07:34:43 +01:00
|
|
|
if self._status == "Discharging":
|
2016-11-05 13:09:28 +01:00
|
|
|
status = "discharging-{}".format(min([ 10, 25, 50, 80, 100] , key=lambda i:abs(i-self._capacity)))
|
|
|
|
return status
|
2016-10-31 07:34:43 +01:00
|
|
|
else:
|
2016-10-31 11:46:07 +01:00
|
|
|
if self._capacity > 95:
|
|
|
|
return "charged"
|
2016-10-31 07:34:43 +01:00
|
|
|
return "charging"
|
2016-11-05 13:09:28 +01:00
|
|
|
|
2016-10-30 18:10:25 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|