2016-10-30 18:10:25 +01:00
|
|
|
import datetime
|
|
|
|
import bumblebee.module
|
|
|
|
|
2016-10-31 15:49:15 +01:00
|
|
|
def usage():
|
2016-10-31 16:08:03 +01:00
|
|
|
return "battery or battery::<battery ID, defaults to BAT0>"
|
2016-10-31 15:49:15 +01:00
|
|
|
|
|
|
|
def notes():
|
2016-10-31 16:08:03 +01:00
|
|
|
return "Reads /sys/class/power_supply/<ID>/[capacity|status]. Warning is at 20% remaining charge, Critical at 10%."
|
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-10-30 18:10:25 +01:00
|
|
|
class Module(bumblebee.module.Module):
|
2016-11-01 08:09:10 +01:00
|
|
|
def __init__(self, output, args):
|
2016-10-31 10:45:15 +01:00
|
|
|
super(Module, self).__init__(args)
|
2016-10-30 18:10:25 +01:00
|
|
|
self._battery = "BAT0" if not args else args[0]
|
|
|
|
self._capacity = 0
|
|
|
|
self._status = "Unknown"
|
|
|
|
|
|
|
|
def data(self):
|
2016-10-31 07:46:21 +01:00
|
|
|
with open("/sys/class/power_supply/{}/capacity".format(self._battery)) 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-10-31 07:46:21 +01:00
|
|
|
return "{:02d}%".format(self._capacity)
|
2016-10-30 18:10:25 +01:00
|
|
|
|
2016-10-31 12:01:21 +01:00
|
|
|
def warning(self):
|
|
|
|
return self._capacity < 20
|
|
|
|
|
|
|
|
def critical(self):
|
|
|
|
return self._capacity < 10
|
|
|
|
|
2016-10-31 07:34:43 +01:00
|
|
|
def state(self):
|
2016-10-31 07:46:21 +01:00
|
|
|
with open("/sys/class/power_supply/{}/status".format(self._battery)) as f:
|
2016-10-31 07:34:43 +01:00
|
|
|
self._status = f.read().strip()
|
|
|
|
if self._status == "Discharging":
|
2016-10-31 11:35:12 +01:00
|
|
|
if self._capacity < 10:
|
|
|
|
return "discharging_critical"
|
|
|
|
if self._capacity < 25:
|
|
|
|
return "discharging_low"
|
|
|
|
if self._capacity < 50:
|
|
|
|
return "discharging_medium"
|
|
|
|
if self._capacity < 75:
|
|
|
|
return "discharging_high"
|
|
|
|
return "discharging_full"
|
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-10-30 18:10:25 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|