Skip to content

Instantly share code, notes, and snippets.

@dece
Created May 21, 2017 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dece/e9ab796d17e896726676786416eae899 to your computer and use it in GitHub Desktop.
Save dece/e9ab796d17e896726676786416eae899 to your computer and use it in GitHub Desktop.
ObLobat - Send a DBus notification on low battery
#!/usr/bin/env python3
""" Send a DBus notification on low battery.
Install Dependencies:
$ sudo apt-get install libdbus-glib-1-dev
$ sudo pip3 install batinfo python-daemon dbus-python notify2
"""
import subprocess
import sys
import time
import batinfo
import daemon
import notify2
NOTIF_NAME = "ob-lobat"
NOTIF_TITLE = "ObLobat"
NOTIF_LABEL_FMT = "Low battery: {perc}%"
BATTERY_ALERT = 10 # Percentage limit, send a notification when equal or below
CHECK_RATE = 60 # In seconds
def main():
notify2.init(NOTIF_NAME)
if len(sys.argv) >= 2 and sys.argv[1] == "-d":
with daemon.DaemonContext():
start_monitoring()
else:
start_monitoring()
def start_monitoring():
while True:
battery_perc = get_battery_perc()
if battery_perc is not None and battery_perc <= BATTERY_ALERT:
send_notification(battery_perc)
time.sleep(CHECK_RATE)
def get_battery_perc():
batteries = batinfo.Batteries()
if not batteries or not batteries.stat:
return None
main_battery = batteries.stat[0]
return main_battery.capacity
def send_notification(battery_perc):
notification = notify2.Notification(
NOTIF_TITLE, NOTIF_LABEL_FMT.format(perc=battery_perc)
)
notification.show()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment