[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
This commit is contained in:
Tobias Witek 2017-07-23 18:45:07 +02:00
parent a312269240
commit 6a0578d2c3
2 changed files with 33 additions and 1 deletions

View file

@ -17,6 +17,11 @@ import bumblebee.output
import bumblebee.engine import bumblebee.engine
import bumblebee.util import bumblebee.util
try:
import power
except ImportError:
pass
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
def __init__(self, engine, config): def __init__(self, engine, config):
widgets = [] widgets = []
@ -28,6 +33,7 @@ class Module(bumblebee.engine.Module):
self._batteries = [ "/sys/class/power_supply/{}".format(b) for b in self._batteries ] self._batteries = [ "/sys/class/power_supply/{}".format(b) for b in self._batteries ]
if len(self._batteries) == 0: if len(self._batteries) == 0:
self._batteries = [ "/sys/class/power_supply/BAT0" ] self._batteries = [ "/sys/class/power_supply/BAT0" ]
self._estimate = "n/a"
self.update(widgets) self.update(widgets)
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
cmd="gnome-power-statistics") cmd="gnome-power-statistics")
@ -44,6 +50,24 @@ class Module(bumblebee.engine.Module):
for widget in new_widgets: for widget in new_widgets:
widgets.append(widget) widgets.append(widget)
self._widgets = widgets 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): def capacity(self, widget):
widget.set("capacity", -1) widget.set("capacity", -1)
@ -68,6 +92,12 @@ class Module(bumblebee.engine.Module):
def state(self, widget): def state(self, widget):
state = [] state = []
if widget.get("type", "battery") == "remaining":
if self._estimate == "Unlimited":
return "unlimited"
return "estimate"
capacity = widget.get("capacity") capacity = widget.get("capacity")
if capacity < 0: if capacity < 0:

View file

@ -75,7 +75,9 @@
"discharging-25": { "prefix": "", "suffix": "" }, "discharging-25": { "prefix": "", "suffix": "" },
"discharging-50": { "prefix": "", "suffix": "" }, "discharging-50": { "prefix": "", "suffix": "" },
"discharging-80": { "prefix": "", "suffix": "" }, "discharging-80": { "prefix": "", "suffix": "" },
"discharging-100": { "prefix": "", "suffix": "" } "discharging-100": { "prefix": "", "suffix": "" },
"unlimited": { "prefix": "", "suffix": "" },
"estimate": { "prefix": "" }
}, },
"caffeine": { "caffeine": {
"activated": {"prefix": " " }, "deactivated": { "prefix": " " } "activated": {"prefix": " " }, "deactivated": { "prefix": " " }