[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)) mod = importlib.import_module('modules.{}.{}'.format(namespace, module_name))
return getattr(mod, 'Module')(config) return getattr(mod, 'Module')(config)
except ModuleNotFoundError as e: except ModuleNotFoundError as e:
pass log.fatal('failed to import {}: {}'.format(module_name, e))
except ImportError as e: except ImportError as e:
log.fatal('failed to import {}: {}'.format(module_name, e))
error = str(e) error = str(e)
if not error: if not error:
error = 'No such module' error = 'No such module'

View file

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

View file

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

View file

@ -30,4 +30,19 @@ def byte(val, fmt='{:.2f}'):
val /= 1024.0 val /= 1024.0
return '{}GiB'.format(fmt).format(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 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4