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