[modules/battery] Rewrite for bigger reuse
This commit is contained in:
parent
3e79b95df4
commit
fae06da446
3 changed files with 63 additions and 50 deletions
|
@ -27,74 +27,92 @@ import core.input
|
||||||
|
|
||||||
import util.format
|
import util.format
|
||||||
|
|
||||||
|
class BatteryManager(object):
|
||||||
|
def remaining(self):
|
||||||
|
try:
|
||||||
|
estimate = power.PowerManagement().get_time_remaining_estimate()
|
||||||
|
# do not show remaining if on AC
|
||||||
|
if estimate == power.common.TIME_REMAINING_UNLIMITED:
|
||||||
|
return None
|
||||||
|
return estimate*60 # return value in seconds
|
||||||
|
except Exception as e:
|
||||||
|
return -1
|
||||||
|
return -1
|
||||||
|
|
||||||
|
def read(self, battery, component, default=None):
|
||||||
|
path = '/sys/class/power_supply/{}'.format(battery)
|
||||||
|
if not os.path.exists(path):
|
||||||
|
return default
|
||||||
|
try:
|
||||||
|
with open('{}/{}'.format(path, component)) as f:
|
||||||
|
return f.read().strip()
|
||||||
|
except IOError:
|
||||||
|
return 'n/a'
|
||||||
|
return default
|
||||||
|
|
||||||
|
|
||||||
|
def capacity(self, battery):
|
||||||
|
capacity = self.read(battery, 'capacity', 100)
|
||||||
|
if capacity != 'n/a':
|
||||||
|
capacity = int(capacity)
|
||||||
|
|
||||||
|
return capacity if capacity < 100 else 100
|
||||||
|
|
||||||
|
def isac(self, battery):
|
||||||
|
path = '/sys/class/power_supply/{}'.format(battery)
|
||||||
|
return not os.path.exists(path)
|
||||||
|
|
||||||
|
def consumption(self, battery):
|
||||||
|
consumption = self.read(battery, 'power_now', 'n/a')
|
||||||
|
if consumption == 'n/a':
|
||||||
|
return 'n/a'
|
||||||
|
return '{}W'.format(int(consumption)/1000000)
|
||||||
|
|
||||||
|
def charge(self, battery):
|
||||||
|
return self.read(battery, 'status', 'n/a')
|
||||||
|
|
||||||
class Module(core.module.Module):
|
class Module(core.module.Module):
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
widgets = []
|
widgets = []
|
||||||
super().__init__(config, widgets)
|
super().__init__(config, widgets)
|
||||||
|
|
||||||
|
self.__manager = BatteryManager()
|
||||||
|
|
||||||
self._batteries = util.format.aslist(self.parameter('device', 'auto'))
|
self._batteries = util.format.aslist(self.parameter('device', 'auto'))
|
||||||
if self._batteries[0] == 'auto':
|
if self._batteries[0] == 'auto':
|
||||||
self._batteries = glob.glob('/sys/class/power_supply/BAT*')
|
self._batteries = [ os.path.basename(battery) for battery in glob.glob('/sys/class/power_supply/BAT*') ]
|
||||||
else:
|
|
||||||
self._batteries = ['/sys/class/power_supply/{}'.format(b) for b in self._batteries]
|
|
||||||
if len(self._batteries) == 0:
|
if len(self._batteries) == 0:
|
||||||
raise Exceptions('no batteries configured/found')
|
raise Exceptions('no batteries configured/found')
|
||||||
core.input.register(self, button=core.input.LEFT_MOUSE,
|
core.input.register(self, button=core.input.LEFT_MOUSE,
|
||||||
cmd='gnome-power-statistics')
|
cmd='gnome-power-statistics')
|
||||||
|
|
||||||
for path in self._batteries:
|
for battery in self._batteries:
|
||||||
log.debug('adding new widget for {}'.format(path))
|
log.debug('adding new widget for {}'.format(battery))
|
||||||
widget = core.widget.Widget(full_text=self.capacity, name=path, module=self)
|
widget = core.widget.Widget(full_text=self.capacity, name=battery, module=self)
|
||||||
widgets.append(widget)
|
widgets.append(widget)
|
||||||
self.capacity(widget)
|
|
||||||
if util.format.asbool(self.parameter('decorate', True)) == False:
|
if util.format.asbool(self.parameter('decorate', True)) == False:
|
||||||
widget.set('theme.exclude', 'suffix')
|
widget.set('theme.exclude', 'suffix')
|
||||||
|
|
||||||
def remaining(self):
|
|
||||||
estimate = 0.0
|
|
||||||
try:
|
|
||||||
estimate = power.PowerManagement().get_time_remaining_estimate()
|
|
||||||
# do not show remaining if on AC
|
|
||||||
if estimate == power.common.TIME_REMAINING_UNLIMITED:
|
|
||||||
return None
|
|
||||||
if estimate == power.common.TIME_REMAINING_UNKNOWN:
|
|
||||||
return ''
|
|
||||||
except Exception:
|
|
||||||
return ''
|
|
||||||
return util.format.duration(estimate*60, compact=True, unit=True) # estimate is in minutes
|
|
||||||
|
|
||||||
def capacity(self, widget):
|
def capacity(self, widget):
|
||||||
widget.set('capacity', -1)
|
capacity = self.__manager.capacity(widget.name())
|
||||||
widget.set('ac', False)
|
|
||||||
if not os.path.exists(widget.name()):
|
|
||||||
widget.set('capacity', 100)
|
|
||||||
widget.set('ac', True)
|
|
||||||
return 'ac'
|
|
||||||
capacity = 100
|
|
||||||
try:
|
|
||||||
with open('{}/capacity'.format(widget.name())) as f:
|
|
||||||
capacity = int(f.read())
|
|
||||||
except IOError:
|
|
||||||
return 'n/a'
|
|
||||||
|
|
||||||
capacity = capacity if capacity < 100 else 100
|
|
||||||
widget.set('capacity', capacity)
|
widget.set('capacity', capacity)
|
||||||
|
widget.set('ac', self.__manager.isac(widget.name()))
|
||||||
|
widget.set('theme.minwidth', '100%')
|
||||||
|
|
||||||
# Read power conumption
|
# Read power conumption
|
||||||
if util.format.asbool(self.parameter('showpowerconsumption', False)):
|
if util.format.asbool(self.parameter('showpowerconsumption', False)):
|
||||||
r=open(widget.name() + '/power_now', 'r')
|
output = '{}% ({})'.format(capacity, self.__manager.consumption(widget.name()))
|
||||||
output = '{}% ({})'.format(capacity,str(int(r.read())/1000000) + 'W')
|
|
||||||
else:
|
else:
|
||||||
output = '{}%'.format(capacity)
|
output = '{}%'.format(capacity)
|
||||||
|
|
||||||
widget.set('theme.minwidth', '100%')
|
|
||||||
if util.format.asbool(self.parameter('showremaining', True))\
|
if util.format.asbool(self.parameter('showremaining', True))\
|
||||||
and self.getCharge(widget) == 'Discharging':
|
and self.__manager.charge(widget.name()) == 'Discharging':
|
||||||
output = '{} {}'.format(output, self.remaining())
|
remaining = self.__manager.remaining()
|
||||||
|
if remaining >= 0:
|
||||||
|
output = '{} {}'.format(output, util.format.duration(remaining, compact=True, unit=True))
|
||||||
|
|
||||||
if util.format.asbool(self.parameter('showdevice', False)):
|
if util.format.asbool(self.parameter('showdevice', False)):
|
||||||
output = '{} ({})'.format(output, os.path.basename(widget.name))
|
output = '{} ({})'.format(output, widget.name())
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
@ -114,7 +132,7 @@ class Module(core.module.Module):
|
||||||
if widget.get('ac'):
|
if widget.get('ac'):
|
||||||
state.append('AC')
|
state.append('AC')
|
||||||
else:
|
else:
|
||||||
charge = self.getCharge(widget)
|
charge = self.__manager.charge(widget.name())
|
||||||
if charge == 'Discharging':
|
if charge == 'Discharging':
|
||||||
state.append('discharging-{}'.format(min([10, 25, 50, 80, 100], key=lambda i: abs(i-capacity))))
|
state.append('discharging-{}'.format(min([10, 25, 50, 80, 100], key=lambda i: abs(i-capacity))))
|
||||||
elif charge == 'Unknown':
|
elif charge == 'Unknown':
|
||||||
|
@ -126,13 +144,4 @@ class Module(core.module.Module):
|
||||||
state.append('charging')
|
state.append('charging')
|
||||||
return state
|
return state
|
||||||
|
|
||||||
def getCharge(self, widget):
|
|
||||||
charge = ''
|
|
||||||
try:
|
|
||||||
with open('{}/status'.format(widget.name())) as f:
|
|
||||||
charge = f.read().strip()
|
|
||||||
except IOError:
|
|
||||||
pass
|
|
||||||
return charge
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -76,6 +76,8 @@ class format(unittest.TestCase):
|
||||||
self.assertEqual('00:20m', duration(20, unit=True))
|
self.assertEqual('00:20m', duration(20, unit=True))
|
||||||
self.assertEqual('00:20m', duration(20, compact=True, unit=True))
|
self.assertEqual('00:20m', duration(20, compact=True, unit=True))
|
||||||
|
|
||||||
|
self.assertEqual('n/a', duration(-1))
|
||||||
|
|
||||||
def test_seconds(self):
|
def test_seconds(self):
|
||||||
self.assertEqual(10, seconds(10))
|
self.assertEqual(10, seconds(10))
|
||||||
self.assertEqual(10, seconds('10'))
|
self.assertEqual(10, seconds('10'))
|
||||||
|
|
|
@ -55,6 +55,8 @@ def seconds(duration):
|
||||||
|
|
||||||
def duration(duration, compact=False, unit=False):
|
def duration(duration, compact=False, unit=False):
|
||||||
duration = int(duration)
|
duration = int(duration)
|
||||||
|
if duration < 0:
|
||||||
|
return 'n/a'
|
||||||
minutes, seconds = divmod(duration, 60)
|
minutes, seconds = divmod(duration, 60)
|
||||||
hours, minutes = divmod(minutes, 60)
|
hours, minutes = divmod(minutes, 60)
|
||||||
suf = 'm'
|
suf = 'm'
|
||||||
|
|
Loading…
Reference in a new issue