Skip to content

Instantly share code, notes, and snippets.

@chanux
Last active December 24, 2015 06:09
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 chanux/6754845 to your computer and use it in GitHub Desktop.
Save chanux/6754845 to your computer and use it in GitHub Desktop.
Laptop battery status notifier inspired by http://www.wired.com/gadgetlab/2013/09/laptop-battery. Images are Ubuntu specific.
#!/usr/bin/env python
# Inspired by the article http://www.wired.com/gadgetlab/2013/09/laptop-battery
#
# Info about battery life vary depending on who you ask from. So I'm not sure
# what to believe anymore. Anyhow wrote this because the last time I tried at
# writing a battery status notifier, I failed.
#
# I couldn't live with that memory!
import pynotify
import time
F_BAT_CAPACITY = '/sys/class/power_supply/BAT0/capacity'
F_BAT_STATUS = '/sys/class/power_supply/BAT0/status'
UNPLUG_IMG = '/usr/share/icons/Humanity/status/48/gpm-primary-080-charging.svg'
PLUG_IMG = '/usr/share/icons/Humanity/status/48/gpm-primary-040.svg'
POLL_INTERVAL = 300 #5 minutes
def notify(title, message, image):
pynotify.init('BATTERY-HEALTH')
notice = pynotify.Notification(title, message, image)
notice.show()
return
def battery_check():
notified_to_plug = False
notified_to_unplug = False
while True:
with open(F_BAT_CAPACITY, 'r') as fcharge:
charge = int(fcharge.readline())
with open(F_BAT_STATUS, 'r') as fstatus:
if fstatus.readline() == 'Discharging':
plugged = False
else:
plugged = True
if charge >= 80 and (plugged and not notified_to_unplug):
notify("Unplug me from power",
"battery level is %s" % charge,
UNPLUG_IMG)
notified_to_unplug = True
notified_to_plug = False
elif charge <= 40 and (not plugged and not notified_to_plug):
notify("Plug me back on power",
"battery level is %d" % charge,
PLUG_IMG)
notified_to_plug = True
notified_to_unplug = False
time.sleep(POLL_INTERVAL)
if __name__ == "__main__":
battery_check()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment