From 6a0578d2c3e0b434a55acc3c21c4dc931791b846 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sun, 23 Jul 2017 18:45:07 +0200 Subject: [PATCH] [modules/battery] Incorporate @yvesh's estimated time remaining Move changes proposed by @yvesh to show the remaining battery time into the battery module. see #146 --- bumblebee/modules/battery.py | 30 ++++++++++++++++++++++++++++++ themes/icons/awesome-fonts.json | 4 +++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/bumblebee/modules/battery.py b/bumblebee/modules/battery.py index d6bd1d2..d3c2383 100644 --- a/bumblebee/modules/battery.py +++ b/bumblebee/modules/battery.py @@ -17,6 +17,11 @@ import bumblebee.output import bumblebee.engine import bumblebee.util +try: + import power +except ImportError: + pass + class Module(bumblebee.engine.Module): def __init__(self, engine, config): widgets = [] @@ -28,6 +33,7 @@ class Module(bumblebee.engine.Module): self._batteries = [ "/sys/class/power_supply/{}".format(b) for b in self._batteries ] if len(self._batteries) == 0: self._batteries = [ "/sys/class/power_supply/BAT0" ] + self._estimate = "n/a" self.update(widgets) engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="gnome-power-statistics") @@ -44,6 +50,24 @@ class Module(bumblebee.engine.Module): for widget in new_widgets: widgets.append(widget) self._widgets = widgets + if bumblebee.util.asbool(self.parameter("showremaining", False)): + self._widgets.append(bumblebee.output.Widget(full_text=self.remaining)) + self._widgets[-1].set("type", "remaining") + try: + type = power.PowerManagement().get_providing_power_source_type() + estimate = power.PowerManagement().get_time_remaining_estimate() + + if type == power.POWER_TYPE_AC and estimate == -2.0: + self._estimate = "Unlimited" + elif estimate == -1.0: + self._estimate = "Unknown" + else: + self._estimate = str(round(estimate / 60, 1)) + ' h' + except Exception as e: + self._estimate = "n/a" + + def remaining(self, widget): + return str(self._estimate) def capacity(self, widget): widget.set("capacity", -1) @@ -68,6 +92,12 @@ class Module(bumblebee.engine.Module): def state(self, widget): state = [] + + if widget.get("type", "battery") == "remaining": + if self._estimate == "Unlimited": + return "unlimited" + return "estimate" + capacity = widget.get("capacity") if capacity < 0: diff --git a/themes/icons/awesome-fonts.json b/themes/icons/awesome-fonts.json index a50c962..9917d97 100644 --- a/themes/icons/awesome-fonts.json +++ b/themes/icons/awesome-fonts.json @@ -75,7 +75,9 @@ "discharging-25": { "prefix": "", "suffix": "" }, "discharging-50": { "prefix": "", "suffix": "" }, "discharging-80": { "prefix": "", "suffix": "" }, - "discharging-100": { "prefix": "", "suffix": "" } + "discharging-100": { "prefix": "", "suffix": "" }, + "unlimited": { "prefix": "", "suffix": "" }, + "estimate": { "prefix": "" } }, "caffeine": { "activated": {"prefix": " " }, "deactivated": { "prefix": " " }