[modules/battery] Re-enable battery module

This commit is contained in:
Tobias Witek 2020-03-06 20:57:32 +01:00
parent 9b96c142d5
commit b841ba3c93
4 changed files with 40 additions and 24 deletions

View file

@ -19,8 +19,9 @@ def load(module_name, config=None):
mod = importlib.import_module('modules.{}.{}'.format(namespace, module_name))
return getattr(mod, 'Module')(config)
except ModuleNotFoundError as e:
pass
log.fatal('failed to import {}: {}'.format(module_name, e))
except ImportError as e:
log.fatal('failed to import {}: {}'.format(module_name, e))
error = str(e)
if not error:
error = 'No such module'

View file

@ -20,6 +20,7 @@
- WAL support / colorscheme support
- tkinter / popups
- scrolling decorator (incl. minwidth, alignment)
- theme.exclude??
## Improvements
- pango output (improve - maybe autodetect? see #531)

View file

@ -13,7 +13,11 @@ Parameters:
import os
import glob
import power
import logging
try:
import power
except ImportError:
pass
import core.module
import core.widget
@ -21,6 +25,8 @@ import core.input
import util.format
log = logging.getLogger(__name__)
class Module(core.module.Module):
def __init__(self, config=None):
widgets = []
@ -33,24 +39,16 @@ class Module(core.module.Module):
self._batteries = ['/sys/class/power_supply/{}'.format(b) for b in self._batteries]
if len(self._batteries) == 0:
raise Exceptions('no batteries configured/found')
self.update()
core.input.register(self, button=core.input.LEFT_MOUSE,
cmd='gnome-power-statistics')
def update(self, widgets):
new_widgets = []
for path in self._batteries:
widget = self.widget(path)
if not widget:
widget = bumblebee.output.Widget(full_text=self.capacity, name=path)
new_widgets.append(widget)
self.capacity(widget)
while len(widgets) > 0: del widgets[0]
for widget in new_widgets:
if bumblebee.util.asbool(self.parameter('decorate', True)) == False:
widget.set('theme.exclude', 'suffix')
log.debug('adding new widget for {}'.format(path))
widget = core.widget.Widget(full_text=self.capacity, name=path, module=self)
widgets.append(widget)
self._widgets = widgets
self.capacity(widget)
if util.format.asbool(self.parameter('decorate', True)) == False:
widget.set('theme.exclude', 'suffix')
def remaining(self):
estimate = 0.0
@ -63,18 +61,18 @@ class Module(core.module.Module):
return ''
except Exception:
return ''
return bumblebee.util.durationfmt(estimate*60, shorten=True, suffix=True) # estimate is in minutes
return util.format.duration(estimate*60, shorten=True, suffix=True) # estimate is in minutes
def capacity(self, widget):
widget.set('capacity', -1)
widget.set('ac', False)
if not os.path.exists(widget.name):
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:
with open('{}/capacity'.format(widget.name())) as f:
capacity = int(f.read())
except IOError:
return 'n/a'
@ -83,18 +81,18 @@ class Module(core.module.Module):
widget.set('capacity', capacity)
# Read power conumption
if bumblebee.util.asbool(self.parameter('showpowerconsumption', False)):
r=open(widget.name + '/power_now', 'r')
if util.format.asbool(self.parameter('showpowerconsumption', False)):
r=open(widget.name() + '/power_now', 'r')
output = '{}% ({})'.format(capacity,str(int(r.read())/1000000) + 'W')
else:
output = '{}%'.format(capacity)
widget.set('theme.minwidth', '100%')
if bumblebee.util.asbool(self.parameter('showremaining', True))\
if util.format.asbool(self.parameter('showremaining', True))\
and self.getCharge(widget) == 'Discharging':
output = '{} {}'.format(output, self.remaining())
if bumblebee.util.asbool(self.parameter('showdevice', False)):
if util.format.asbool(self.parameter('showdevice', False)):
output = '{} ({})'.format(output, os.path.basename(widget.name))
return output
@ -104,6 +102,7 @@ class Module(core.module.Module):
capacity = widget.get('capacity')
if capacity < 0:
log.debug('battery state: {}'.format(state))
return ['critical', 'unknown']
if capacity < int(self.parameter('critical', 10)):
@ -124,15 +123,15 @@ class Module(core.module.Module):
state.append('charged')
else:
state.append('charging')
return state
def getCharge(self, widget):
charge = ''
try:
with open('{}/status'.format(widget.name)) as f:
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

View file

@ -30,4 +30,19 @@ def byte(val, fmt='{:.2f}'):
val /= 1024.0
return '{}GiB'.format(fmt).format(val*1024.0)
def duration(duration, shorten=False, suffix=False):
duration = int(duration)
minutes, seconds = divmod(duration, 60)
hours, minutes = divmod(minutes, 60)
suf = 'm'
res = '{:02d}:{:02d}'.format(minutes, seconds)
if hours > 0:
if shorten:
res = '{:02d}:{:02d}'.format(hours, minutes)
else:
res = '{:02d}:{}'.format(hours, res)
suf = 'h'
return '{}{}'.format(res, suf if suffix else '')
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4