Skip to content

Instantly share code, notes, and snippets.

@Videonauth
Forked from SergKolo/battery_monitor.py
Last active July 23, 2016 18:26
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 Videonauth/64396c2706fa8c4b51a359263bb7a9d2 to your computer and use it in GitHub Desktop.
Save Videonauth/64396c2706fa8c4b51a359263bb7a9d2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from sys import argv
from time import sleep, time
from gi import require_version
import subprocess
import dbus
require_version('Notify', '0.7')
from gi.repository import Notify
def send_notification(title, text):
try:
if Notify.init(argv[0]):
n = Notify.Notification.new("Notify")
n.update(title, text)
n.set_urgency(2)
if not n.show():
raise SyntaxError("sending notification failed!")
else:
raise SyntaxError("can't initialize notification!")
except SyntaxError as error_message:
print(error_message)
if error_message == "sending notification failed!":
Notify.uninit()
print("exiting ...")
exit()
else:
Notify.uninit()
def run_command(command_list):
try:
stdout = subprocess.check_output(command_list)
except subprocess.CalledProcessError:
pass
else:
if stdout:
return stdout
def run_dbus_method(bus_type, obj, path, interface, method, arg):
bus = ""
if bus_type == "session":
bus = dbus.SessionBus()
if bus_type == "system":
bus = dbus.SystemBus()
proxy = bus.get_object(obj, path)
method = proxy.get_dbus_method(method, interface)
if arg:
return method(arg)
else:
return method()
def suspend_system():
run_dbus_method('session',
'com.canonical.Unity',
'/com/canonical/Unity/Session',
'com.canonical.Unity.Session',
'Suspend', 'None')
def refresh_battery():
battery_path = ""
for line in run_command(['upower', '-e']).decode().split('\n'):
if 'battery_BAT' in line:
battery_path = line
break
run_dbus_method('system',
'org.freedesktop.UPower',
battery_path,
'org.freedesktop.UPower.Device',
'Refresh', 'None')
def get_battery_percentage():
output = run_command(['upower', '--dump']).decode().split('\n')
found_battery = False
for line in output:
if 'BAT' in line:
found_battery = True
if found_battery and 'percentage' in line:
return line.split()[1].split('%')[0]
if __name__ == "__main__":
notification_timer = time()
while True:
notified = False
while subprocess.call(['on_ac_power']) == 0:
sleep(0.25)
refresh_battery()
battery_percentage = int(get_battery_percentage())
if battery_percentage == int(argv[2]) and not notified:
subprocess.call(['zenity', '--info', '--text', 'Battery reached' + argv[2] + '%'])
notified = True
while subprocess.call(['on_ac_power']) == 1:
sleep(0.25)
refresh_battery()
battery_percentage = int(get_battery_percentage())
if battery_percentage <= int(argv[1]):
if battery_percentage <= 10:
send_notification('Low Battery', 'Will suspend in 60 seconds')
sleep(60)
suspend_system()
continue
if notification_timer < time():
notification_timer = time() + 600
send_notification('Low Battery', 'Plug in your charger')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment