[module/battery] Add remaining time, if available
Add remaining time directly to widget, if available, and if not on charge. see #146
This commit is contained in:
parent
6a0578d2c3
commit
115f03cb0f
3 changed files with 32 additions and 27 deletions
|
@ -33,7 +33,6 @@ 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")
|
||||||
|
@ -50,24 +49,22 @@ 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))
|
def remaining(self):
|
||||||
self._widgets[-1].set("type", "remaining")
|
estimate = 0.0
|
||||||
try:
|
try:
|
||||||
type = power.PowerManagement().get_providing_power_source_type()
|
power_type = power.PowerManagement().get_providing_power_source_type()
|
||||||
|
|
||||||
|
# do not show remaining if on AC
|
||||||
|
if power.PowerManagement().get_providing_power_source_type() == power.POWER_TYPE_AC:
|
||||||
|
return None
|
||||||
|
|
||||||
estimate = power.PowerManagement().get_time_remaining_estimate()
|
estimate = power.PowerManagement().get_time_remaining_estimate()
|
||||||
|
if estimate == -1.0:
|
||||||
if type == power.POWER_TYPE_AC and estimate == -2.0:
|
return "n/a"
|
||||||
self._estimate = "Unlimited"
|
except Exception:
|
||||||
elif estimate == -1.0:
|
return "n/a"
|
||||||
self._estimate = "Unknown"
|
return bumblebee.util.durationfmt(estimate*60, shorten=True, suffix=True) # estimate is in minutes
|
||||||
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)
|
||||||
|
@ -88,16 +85,15 @@ class Module(bumblebee.engine.Module):
|
||||||
widget.set("theme.minwidth", "100% ({})".format(os.path.basename(widget.name)))
|
widget.set("theme.minwidth", "100% ({})".format(os.path.basename(widget.name)))
|
||||||
return "{}% ({})".format(capacity, os.path.basename(widget.name))
|
return "{}% ({})".format(capacity, os.path.basename(widget.name))
|
||||||
widget.set("theme.minwidth", "100%")
|
widget.set("theme.minwidth", "100%")
|
||||||
return "{}%".format(capacity)
|
|
||||||
|
remaining = None
|
||||||
|
if bumblebee.util.asbool(self.parameter("showremaining", True)):
|
||||||
|
remaining = self.remaining()
|
||||||
|
|
||||||
|
return "{}%{}".format(capacity, "" if not remaining else " ({})".format(remaining))
|
||||||
|
|
||||||
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:
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import shlex
|
import shlex
|
||||||
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
@ -45,12 +46,19 @@ def bytefmt(num):
|
||||||
num /= 1024.0
|
num /= 1024.0
|
||||||
return "{:.2f}GiB".format(num*1024.0)
|
return "{:.2f}GiB".format(num*1024.0)
|
||||||
|
|
||||||
def durationfmt(duration):
|
def durationfmt(duration, shorten=False, suffix=False):
|
||||||
|
duration = int(duration)
|
||||||
minutes, seconds = divmod(duration, 60)
|
minutes, seconds = divmod(duration, 60)
|
||||||
hours, minutes = divmod(minutes, 60)
|
hours, minutes = divmod(minutes, 60)
|
||||||
|
suf = "m"
|
||||||
res = "{:02d}:{:02d}".format(minutes, seconds)
|
res = "{:02d}:{:02d}".format(minutes, seconds)
|
||||||
if hours > 0: res = "{:02d}:{}".format(hours, res)
|
if hours > 0:
|
||||||
|
if shorten:
|
||||||
|
res = "{:02d}:{:02d}".format(hours, minutes)
|
||||||
|
else:
|
||||||
|
res = "{:02d}:{}".format(hours, res)
|
||||||
|
suf = "h"
|
||||||
|
|
||||||
return res
|
return "{}{}".format(res, suf if suffix else "")
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -32,6 +32,7 @@ class TestBatteryModule(unittest.TestCase):
|
||||||
self.exists.return_value = True
|
self.exists.return_value = True
|
||||||
self.engine = mock.Mock()
|
self.engine = mock.Mock()
|
||||||
self.config = Config()
|
self.config = Config()
|
||||||
|
self.config.set("battery.showremaining", "false")
|
||||||
self.module = Module(engine=self.engine, config={"config":self.config})
|
self.module = Module(engine=self.engine, config={"config":self.config})
|
||||||
|
|
||||||
self.config.set("battery.critical", "20")
|
self.config.set("battery.critical", "20")
|
||||||
|
|
Loading…
Reference in a new issue