bumblebee-status/bumblebee/modules/battery.py
Tobias Witek 4e648cf009 [modules] Add module-specific configuration
Big oversight in my previous commits: Widgets need to be able to have
specific configurations (i.e. the path for different instances of the
"disk" module has to be different).

To account for that, it is now possible to assign an "alias" to a module
instance using ":" (for example: -m "disk:home"). This alias is then
used for the configuration parameter resolution automatically, for
example:

-m disk:home -p home.path=/home

As a consequence, parameter names in the module code are now relative to
the module, which means: shorter!
2016-11-05 14:26:02 +01:00

45 lines
1.6 KiB
Python

import datetime
import bumblebee.module
def usage():
return "battery or battery::<battery ID, defaults to BAT0>"
def notes():
return "Reads /sys/class/power_supply/<ID>/[capacity|status]. Warning is at 20% remaining charge, Critical at 10%."
def description():
return "Displays battery status, percentage and whether it's charging or discharging."
class Module(bumblebee.module.Module):
def __init__(self, output, config, alias):
super(Module, self).__init__(output, config, alias)
self._battery = config.parameter("device", "BAT0")
self._capacity = 0
self._status = "Unknown"
def widgets(self):
with open("/sys/class/power_supply/{}/capacity".format(self._battery)) as f:
self._capacity = int(f.read())
self._capacity = self._capacity if self._capacity < 100 else 100
return bumblebee.output.Widget(self,"{:02d}%".format(self._capacity))
def warning(self, widget):
return self._capacity < self._config.parameter("warning", 20)
def critical(self, widget):
return self._capacity < self._config.parameter("critical", 10)
def state(self, widget):
with open("/sys/class/power_supply/{}/status".format(self._battery)) as f:
self._status = f.read().strip()
if self._status == "Discharging":
status = "discharging-{}".format(min([ 10, 25, 50, 80, 100] , key=lambda i:abs(i-self._capacity)))
return status
else:
if self._capacity > 95:
return "charged"
return "charging"
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4