You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import datetime
  2. import dbus
  3. from i3pystatus import IntervalModule, formatp
  4. class UPower(IntervalModule):
  5. settings = (
  6. ("interval", "Refresh interval"),
  7. ("battery_ident", "The battery identifier (same as setting)"),
  8. ("alert_percent", "alert percent"),
  9. ("format_chr", "format when charging"),
  10. ("color_chr", "color when charging"),
  11. ("format_dis", "format when discharging"),
  12. ("color_dis", "color when discharging"),
  13. ("format_dis_alert", "format when discharging with alert"),
  14. ("color_dis_alert", "color when discharging with alert"),
  15. ("format_full", "format when full"),
  16. ("color_full", "color when full"),
  17. ("format_dpl", "format when disable"),
  18. ("color_dpl", "color when disable"),
  19. )
  20. interval = 3
  21. battery_ident = "BAT0"
  22. format_chr = "{state} {percentage} {time_to_full}"
  23. format_dis = "{state} {percentage} {time_to_empty}"
  24. format_full = "{state} {percentage}"
  25. format_dpl = "{state}"
  26. color_chr = "#00FF00"
  27. color_dis = "#FFFF00"
  28. color_dis_alert = "#FF0000"
  29. color_full = "#00FF00"
  30. color_dpl = "#FF0000"
  31. alert_percent = 20
  32. def init(self):
  33. sess = dbus.SystemBus()
  34. dev = sess.get_object(
  35. "org.freedesktop.UPower",
  36. "/org/freedesktop/UPower/devices/battery_%s" % self.battery_ident)
  37. self.iface = dbus.Interface(
  38. dev, dbus_interface="org.freedesktop.DBus.Properties")
  39. def propertie(self, name):
  40. return self.iface.Get("org.freedesktop.UPower.Device", name)
  41. def time_to(self, name):
  42. seconds = int(self.iface.Get("org.freedesktop.UPower.Device", name))
  43. return datetime.timedelta(seconds=seconds)
  44. def run(self):
  45. status = ["CHR", "DIS", "DPL", "FULL"]
  46. try:
  47. statep = self.propertie("State")
  48. percent = int(self.propertie("Percentage"))
  49. time_to_empty = self.time_to("TimeToEmpty")
  50. time_to_full = self.time_to("TimeToFull")
  51. except Exception:
  52. percent = 0
  53. statep = 3
  54. time_to_empty = datetime.timedelta(seconds=0)
  55. time_to_full = datetime.timedelta(seconds=0)
  56. if statep == 1 and not time_to_full:
  57. statep = 4
  58. state = status[statep - 1] if statep - 1 < len(status) else status[2]
  59. format = getattr(self, "format_%s" % state.lower())
  60. color = getattr(self, "color_%s" % state.lower())
  61. if statep == 2 and percent < self.alert_percent:
  62. color = self.color_dis_alert
  63. format = self.format_dis_alert
  64. fdict = {
  65. "battery_ident": self.battery_ident,
  66. "percentage": percent,
  67. "time_to_full": time_to_full,
  68. "time_to_empty": time_to_empty,
  69. "state": state
  70. }
  71. self.output = {
  72. "full_text": formatp(format, **fdict),
  73. "urgent": False,
  74. "color": color
  75. }