[modules/battery] fold battery-all into battery
Make battery-all a submodule of battery by adding a special parameter ("compact-devices") that compacts all devices into a single widget.
This commit is contained in:
parent
fae06da446
commit
1dbe4f6fcc
1 changed files with 47 additions and 11 deletions
|
@ -9,6 +9,7 @@ Parameters:
|
|||
* battery.showdevice : If set to 'true', add the device name to the widget (defaults to False)
|
||||
* battery.decorate : If set to 'false', hides additional icons (charging, etc.) (defaults to True)
|
||||
* battery.showpowerconsumption: If set to 'true', show current power consumption (defaults to False)
|
||||
* battery.compact-devices : If set to 'true', compacts multiple batteries into a single entry (default to False)
|
||||
"""
|
||||
|
||||
import os
|
||||
|
@ -58,10 +59,28 @@ class BatteryManager(object):
|
|||
|
||||
return capacity if capacity < 100 else 100
|
||||
|
||||
def capacity_all(self, batteries):
|
||||
now = 0
|
||||
full = 0
|
||||
for battery in batteries:
|
||||
try:
|
||||
with open('/sys/class/power_supply/{}/energy_full'.format(battery)) as f:
|
||||
full += int(f.read())
|
||||
with open('/sys/class/power_supply/{}/energy_now'.format(battery)) as f:
|
||||
now += int(f.read())
|
||||
except IOError:
|
||||
return 'n/a'
|
||||
return int(float(now)/float(full)*100.0)
|
||||
|
||||
def isac(self, battery):
|
||||
path = '/sys/class/power_supply/{}'.format(battery)
|
||||
return not os.path.exists(path)
|
||||
|
||||
def isac_any(self, batteries):
|
||||
for battery in batteries:
|
||||
if self.isac(battery): return True
|
||||
return False
|
||||
|
||||
def consumption(self, battery):
|
||||
consumption = self.read(battery, 'power_now', 'n/a')
|
||||
if consumption == 'n/a':
|
||||
|
@ -71,6 +90,12 @@ class BatteryManager(object):
|
|||
def charge(self, battery):
|
||||
return self.read(battery, 'status', 'n/a')
|
||||
|
||||
def charge_any(self, batteries):
|
||||
for battery in batteries:
|
||||
if self.charge(battery) == 'Discharging':
|
||||
return 'Discharging'
|
||||
return 'Charged'
|
||||
|
||||
class Module(core.module.Module):
|
||||
def __init__(self, config):
|
||||
widgets = []
|
||||
|
@ -86,17 +111,25 @@ class Module(core.module.Module):
|
|||
core.input.register(self, button=core.input.LEFT_MOUSE,
|
||||
cmd='gnome-power-statistics')
|
||||
|
||||
if util.format.asbool(self.parameter('compact-devices', False)):
|
||||
widget = core.widget.Widget(full_text=self.capacity, name='all-batteries', module=self)
|
||||
widgets.append(widget)
|
||||
else:
|
||||
for battery in self._batteries:
|
||||
log.debug('adding new widget for {}'.format(battery))
|
||||
widget = core.widget.Widget(full_text=self.capacity, name=battery, module=self)
|
||||
widgets.append(widget)
|
||||
for w in self.widgets():
|
||||
if util.format.asbool(self.parameter('decorate', True)) == False:
|
||||
widget.set('theme.exclude', 'suffix')
|
||||
|
||||
def capacity(self, widget):
|
||||
if widget.name() == 'all-batteries':
|
||||
capacity = self.__manager.capacity_all(self._batteries)
|
||||
else:
|
||||
capacity = self.__manager.capacity(widget.name())
|
||||
widget.set('capacity', capacity)
|
||||
widget.set('ac', self.__manager.isac(widget.name()))
|
||||
widget.set('ac', self.__manager.isac_any(self._batteries))
|
||||
widget.set('theme.minwidth', '100%')
|
||||
|
||||
# Read power conumption
|
||||
|
@ -131,6 +164,9 @@ class Module(core.module.Module):
|
|||
|
||||
if widget.get('ac'):
|
||||
state.append('AC')
|
||||
else:
|
||||
if widget.name() == 'all-batteries':
|
||||
charge = self.__manager.charge_any(self._batteries)
|
||||
else:
|
||||
charge = self.__manager.charge(widget.name())
|
||||
if charge == 'Discharging':
|
||||
|
|
Loading…
Reference in a new issue