diff --git a/i3pystatus/battery.py b/i3pystatus/battery.py index d1cfe6b..90f6fbe 100644 --- a/i3pystatus/battery.py +++ b/i3pystatus/battery.py @@ -3,6 +3,7 @@ from . import IntervalModule from .core.util import PrefixedKeyDict +from .core.desktop import display_notification class Battery: @staticmethod @@ -47,11 +48,30 @@ class BatteryChecker(IntervalModule): """ This class uses the /sys/class/power_supply/…/uevent interface to check for the 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" 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): self.base_path = "/sys/class/power_supply/{0}/uevent".format(self.battery_ident) @@ -61,18 +81,22 @@ class BatteryChecker(IntervalModule): color = "#ffffff" battery = Battery(self.base_path) - fdict = dict.fromkeys(("remaining_str", "remaining_hm"), "") status = battery.STATUS energy_now = battery.ENERGY_NOW energy_full = battery.ENERGY_FULL power_now = battery.POWER_NOW - fdict["percentage"] = (energy_now / energy_full) * 100 - fdict["percentage_design"] = (energy_now / battery.ENERGY_FULL_DESIGN) * 100 - fdict["consumption"] = power_now / 1000000 + fdict = { + "battery_ident": self.battery_ident, + "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" elif power_now: if status == "Discharging": @@ -86,6 +110,14 @@ class BatteryChecker(IntervalModule): remaining = RemainingCalculator(energy_full-energy_now, power_now) 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 = { "full_text": self.format.format(**fdict).strip(), "instance": self.battery_ident,