#5: Ability to scream out loud if battery is low :-)

This commit is contained in:
enkore 2013-03-05 17:26:49 +01:00
parent 6740f5b1cc
commit ed7f95dd34

View File

@ -3,6 +3,7 @@
from . import IntervalModule from . import IntervalModule
from .core.util import PrefixedKeyDict from .core.util import PrefixedKeyDict
from .core.desktop import display_notification
class Battery: class Battery:
@staticmethod @staticmethod
@ -47,12 +48,31 @@ class BatteryChecker(IntervalModule):
""" """
This class uses the /sys/class/power_supply//uevent interface to check for the This class uses the /sys/class/power_supply//uevent interface to check for the
battery status battery status
Available formatters for format and alert_format_\*:
* remaining_str
* remaining_hm
* percentage
* percentage_design
* consumption (Watts)
* status
* battery_ident
""" """
settings = ("battery_ident", "format") settings = (
"battery_ident", "format",
("alert", "Display a libnotify-notification on low battery"),
"alert_percentage", "alert_format_title", "alert_format_body", "alert_percentage",
)
battery_ident = "BAT0" battery_ident = "BAT0"
format = "{status} {remaining}" format = "{status} {remaining}"
alert = False
alert_percentage = 10
alert_format_title = "Low battery"
alert_format_body = "Battery {battery_ident} has only {percentage} % ({remaining_hm}) remaining!"
def init(self): def init(self):
self.base_path = "/sys/class/power_supply/{0}/uevent".format(self.battery_ident) self.base_path = "/sys/class/power_supply/{0}/uevent".format(self.battery_ident)
@ -61,18 +81,22 @@ class BatteryChecker(IntervalModule):
color = "#ffffff" color = "#ffffff"
battery = Battery(self.base_path) battery = Battery(self.base_path)
fdict = dict.fromkeys(("remaining_str", "remaining_hm"), "")
status = battery.STATUS status = battery.STATUS
energy_now = battery.ENERGY_NOW energy_now = battery.ENERGY_NOW
energy_full = battery.ENERGY_FULL energy_full = battery.ENERGY_FULL
power_now = battery.POWER_NOW power_now = battery.POWER_NOW
fdict["percentage"] = (energy_now / energy_full) * 100 fdict = {
fdict["percentage_design"] = (energy_now / battery.ENERGY_FULL_DESIGN) * 100 "battery_ident": self.battery_ident,
fdict["consumption"] = power_now / 1000000 "remaining_str": "",
"remaining_hm": "",
"percentage": (energy_now / energy_full) * 100,
"percentage_design": (energy_now / battery.ENERGY_FULL_DESIGN) * 100,
"consumption": power_now / 1000000,
}
if status == "Full": if status == "Full" or fdict["percentage"] > 99:
fdict["status"] = "FULL" fdict["status"] = "FULL"
elif power_now: elif power_now:
if status == "Discharging": if status == "Discharging":
@ -86,6 +110,14 @@ class BatteryChecker(IntervalModule):
remaining = RemainingCalculator(energy_full-energy_now, power_now) remaining = RemainingCalculator(energy_full-energy_now, power_now)
fdict.update(remaining.get_dict("remaining_")) fdict.update(remaining.get_dict("remaining_"))
if self.alert and fdict["percentage"] <= self.alert_percentage:
display_notification(
title=self.alert_format_title.format(**fdict),
body=self.alert_format_body.format(**fdict),
icon="battery-caution",
urgency=2,
)
self.output = { self.output = {
"full_text": self.format.format(**fdict).strip(), "full_text": self.format.format(**fdict).strip(),
"instance": self.battery_ident, "instance": self.battery_ident,