made battery module much more flexible

So I can have my old representation back... indeed you can
do pretty much anything now with it:

status.register(battery,
    format="{consumption:.2f}W {percentage:.2f}% [{percentage_design:.2f}%] {remaining_hm}"
)
This commit is contained in:
enkore 2013-02-24 01:36:56 +01:00
parent ed003c123f
commit bde56dfdec

View File

@ -42,8 +42,9 @@ class BatteryChecker(IntervalModule):
battery status battery status
""" """
settings = ("battery_ident",) settings = ("battery_ident", "format")
battery_ident = "BAT0" battery_ident = "BAT0"
format = "{status} {remaining}"
def init(self): def init(self):
self.base_path = "/sys/class/power_supply/{0}/uevent".format(self.battery_ident) self.base_path = "/sys/class/power_supply/{0}/uevent".format(self.battery_ident)
@ -53,29 +54,39 @@ class BatteryChecker(IntervalModule):
color = "#ffffff" color = "#ffffff"
battery = Battery(self.base_path) battery = Battery(self.base_path)
fdict = dict.fromkeys(("remaining", "remaining_hms"), "")
status = battery.STATUS status = battery.STATUS
energy_now = battery.ENERGY_NOW energy_now = battery.ENERGY_NOW
energy_full = battery.ENERGY_FULL
power_now = battery.POWER_NOW
fdict["percentage"] = (energy_now / energy_full) * 100
fdict["percentage_design"] = (energy_now / battery.ENERGY_FULL_DESIGN) * 100
fdict["consumption"] = power_now / 1000000
if status == "Full": if status == "Full":
full_text = "fully charged" fdict["status"] = "FULL"
elif status == "Discharging": elif status == "Discharging":
power_now = battery.POWER_NOW fdict["status"] = "DIS"
remaining_time_secs = (energy_now / power_now) * 3600 remaining_time = (energy_now / power_now) * 60
hours, remainder = divmod(remaining_time_secs, 3600) hours, minutes = map(int, divmod(remaining_time, 60))
minutes, seconds = divmod(remainder, 60)
full_text = "%ih %im %is remaining" % (hours, minutes, seconds) fdict["remaining"] = "{}:{:02}".format(hours, minutes)
if remaining_time_secs < (15*60): fdict["remaining_hm"] = "{}h {:02}m".format(hours, minutes)
fdict["remaining_hours"] = hours
fdict["remaining_mins"] = minutes
if remaining_time < 15:
urgent = True urgent = True
color = "#ff0000" color = "#ff0000"
else: # Charging, Unknown etc. (My thinkpad says Unknown if close to fully charged) else: # Charging, Unknown etc. (My thinkpad says Unknown if close to fully charged)
energy_full = battery.ENERGY_FULL fdict["status"] = "CHR"
percentage = (energy_now / energy_full) * 100
full_text = "%.2f%% charged" % percentage
self.output = { self.output = {
"full_text": full_text, "full_text": self.format.format(**fdict).strip(),
"instance": self.battery_ident, "instance": self.battery_ident,
"urgent": urgent, "urgent": urgent,
"color": color "color": color
} }