1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- import datetime
- import dbus
-
- from i3pystatus import IntervalModule, formatp
-
-
- class UPower(IntervalModule):
-
- settings = (
- ("interval", "Refresh interval"),
- ("battery_ident", "The battery identifier (same as setting)"),
- ("alert_percent", "alert percent"),
- ("format_chr", "format when charging"),
- ("color_chr", "color when charging"),
- ("format_dis", "format when discharging"),
- ("color_dis", "color when discharging"),
- ("format_dis_alert", "format when discharging with alert"),
- ("color_dis_alert", "color when discharging with alert"),
- ("format_full", "format when full"),
- ("color_full", "color when full"),
- ("format_dpl", "format when disable"),
- ("color_dpl", "color when disable"),
- )
-
- interval = 3
- battery_ident = "BAT0"
-
- format_chr = "{state} {percentage} {time_to_full}"
- format_dis = "{state} {percentage} {time_to_empty}"
- format_full = "{state} {percentage}"
- format_dpl = "{state}"
-
- color_chr = "#00FF00"
- color_dis = "#FFFF00"
- color_dis_alert = "#FF0000"
- color_full = "#00FF00"
- color_dpl = "#FF0000"
-
- alert_percent = 20
-
- def init(self):
- sess = dbus.SystemBus()
- dev = sess.get_object(
- "org.freedesktop.UPower",
- "/org/freedesktop/UPower/devices/battery_%s" % self.battery_ident)
- self.iface = dbus.Interface(
- dev, dbus_interface="org.freedesktop.DBus.Properties")
-
- def propertie(self, name):
- return self.iface.Get("org.freedesktop.UPower.Device", name)
-
- def time_to(self, name):
- seconds = int(self.iface.Get("org.freedesktop.UPower.Device", name))
- return datetime.timedelta(seconds=seconds)
-
- def run(self):
- status = ["CHR", "DIS", "DPL", "FULL"]
- try:
- statep = self.propertie("State")
-
- percent = int(self.propertie("Percentage"))
- time_to_empty = self.time_to("TimeToEmpty")
- time_to_full = self.time_to("TimeToFull")
- except Exception:
- percent = 0
- statep = 3
- time_to_empty = datetime.timedelta(seconds=0)
- time_to_full = datetime.timedelta(seconds=0)
-
- if statep == 1 and not time_to_full:
- statep = 4
-
- state = status[statep - 1] if statep - 1 < len(status) else status[2]
- format = getattr(self, "format_%s" % state.lower())
- color = getattr(self, "color_%s" % state.lower())
- if statep == 2 and percent < self.alert_percent:
- color = self.color_dis_alert
- format = self.format_dis_alert
-
- fdict = {
- "battery_ident": self.battery_ident,
- "percentage": percent,
- "time_to_full": time_to_full,
- "time_to_empty": time_to_empty,
- "state": state
- }
-
- self.output = {
- "full_text": formatp(format, **fdict),
- "urgent": False,
- "color": color
- }
|