battery
This commit is contained in:
parent
a892a09581
commit
a89b421ebb
@ -1,7 +1,5 @@
|
||||
from i3pystatus.file import File
|
||||
|
||||
__imported__ = [File]
|
||||
|
||||
class Backlight(File):
|
||||
"""
|
||||
Screen backlight info
|
||||
@ -22,7 +20,7 @@ class Backlight(File):
|
||||
backlight="acpi_video0"
|
||||
format="{brightness}/{max_brightness}"
|
||||
|
||||
interval=1
|
||||
interval=5
|
||||
base_path = "/sys/class/backlight/{backlight}/"
|
||||
components={
|
||||
"brightness": (int, "brightness"),
|
||||
|
@ -2,6 +2,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from i3pystatus import IntervalModule
|
||||
from .core.util import PrefixedKeyDict
|
||||
|
||||
class Battery:
|
||||
"""
|
||||
@ -36,6 +37,21 @@ class Battery:
|
||||
|
||||
setattr(self, self.lchop(key), self.convert(value))
|
||||
|
||||
class RemainingCalculator:
|
||||
def __init__(self, energy, power):
|
||||
self.remaining_time = (energy / power) * 60
|
||||
self.hours, self.minutes = map(int, divmod(self.remaining_time, 60))
|
||||
|
||||
def get_dict(self, prefix):
|
||||
d = PrefixedKeyDict(prefix)
|
||||
d.update({
|
||||
"str": "{}:{:02}".format(self.hours, self.minutes),
|
||||
"hm": "{}h:{:02}m".format(self.hours, self.minutes),
|
||||
"hours": self.hours,
|
||||
"mins": self.minutes,
|
||||
})
|
||||
return d
|
||||
|
||||
class BatteryChecker(IntervalModule):
|
||||
"""
|
||||
This class uses the /proc/acpi/battery interface to check for the
|
||||
@ -54,7 +70,7 @@ class BatteryChecker(IntervalModule):
|
||||
color = "#ffffff"
|
||||
|
||||
battery = Battery(self.base_path)
|
||||
fdict = dict.fromkeys(("remaining", "remaining_hm"), "")
|
||||
fdict = dict.fromkeys(("remaining_str", "remaining_hm"), "")
|
||||
|
||||
status = battery.STATUS
|
||||
energy_now = battery.ENERGY_NOW
|
||||
@ -65,23 +81,23 @@ class BatteryChecker(IntervalModule):
|
||||
fdict["percentage_design"] = (energy_now / battery.ENERGY_FULL_DESIGN) * 100
|
||||
fdict["consumption"] = power_now / 1000000
|
||||
|
||||
if not power_now:
|
||||
return
|
||||
|
||||
if status == "Full":
|
||||
fdict["status"] = "FULL"
|
||||
elif status == "Discharging":
|
||||
else:
|
||||
if status == "Discharging":
|
||||
fdict["status"] = "DIS"
|
||||
remaining_time = (energy_now / power_now) * 60
|
||||
hours, minutes = map(int, divmod(remaining_time, 60))
|
||||
remaining = RemainingCalculator(energy_now, power_now)
|
||||
|
||||
fdict["remaining"] = "{}:{:02}".format(hours, minutes)
|
||||
fdict["remaining_hm"] = "{}h {:02}m".format(hours, minutes)
|
||||
fdict["remaining_hours"] = hours
|
||||
fdict["remaining_mins"] = minutes
|
||||
|
||||
if remaining_time < 15:
|
||||
if remaining.remaining_time < 15:
|
||||
urgent = True
|
||||
color = "#ff0000"
|
||||
else: # Charging, Unknown etc. (My thinkpad says Unknown if close to fully charged)
|
||||
fdict["status"] = "CHR"
|
||||
remaining = RemainingCalculator(energy_full-energy_now, power_now)
|
||||
fdict.update(remaining.get_dict("remaining_"))
|
||||
|
||||
self.output = {
|
||||
"full_text": self.format.format(**fdict).strip(),
|
||||
|
@ -9,6 +9,7 @@ __all__ = [
|
||||
"SettingsBase",
|
||||
"ClassFinder",
|
||||
"ModuleList",
|
||||
"KeyConstraintDict", "PrefixedKeyDict",
|
||||
]
|
||||
|
||||
class ModuleList(collections.UserList):
|
||||
@ -22,6 +23,15 @@ class ModuleList(collections.UserList):
|
||||
module.registered(self.status_handler)
|
||||
super().append(module)
|
||||
|
||||
class PrefixedKeyDict(collections.UserDict):
|
||||
def __init__(self, prefix):
|
||||
super().__init__()
|
||||
|
||||
self.prefix = prefix
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
super().__setitem__(self.prefix + key, value)
|
||||
|
||||
class KeyConstraintDict(collections.UserDict):
|
||||
class MissingKeys(Exception):
|
||||
def __init__(self, keys):
|
||||
@ -41,15 +51,15 @@ class KeyConstraintDict(collections.UserDict):
|
||||
else:
|
||||
raise KeyError(key)
|
||||
|
||||
def missing(self):
|
||||
return self.required_keys - (self.seen_keys & self.required_keys)
|
||||
|
||||
def __iter__(self):
|
||||
if self.missing():
|
||||
raise self.MissingKeys(self.missing())
|
||||
|
||||
return self.data.__iter__()
|
||||
|
||||
def missing(self):
|
||||
return self.required_keys - (self.seen_keys & self.required_keys)
|
||||
|
||||
class SettingsBase:
|
||||
"""
|
||||
Support class for providing a nice and flexible settings interface
|
||||
@ -125,7 +135,6 @@ class ClassFinder:
|
||||
return predicate
|
||||
|
||||
def search_module(self, module):
|
||||
# Neat trick: [(x,y),(u,v)] becomes [(x,u),(y,v)]
|
||||
return list(zip(*inspect.getmembers(module, self.predicate_factory(module))))[1]
|
||||
|
||||
def get_class(self, module):
|
||||
|
Loading…
Reference in New Issue
Block a user