Created
July 11, 2021 06:06
-
-
Save xyzshantaram/92006c8a570c1659b61e6f83dff5253d to your computer and use it in GitHub Desktop.
Battery monitor script for Linux computers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Copyright © 2021 Siddharth Singh | |
The MIT License | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
* The above copyright notice and this permission notice | |
shall be included in all copies or substantial portions | |
of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
import re | |
import subprocess | |
import gi | |
import time | |
import os | |
gi.require_version("Notify", "0.7") | |
from gi.repository import Notify | |
from gi.repository import GdkPixbuf | |
Notify.init("BatteryMonitor") | |
def get_stdout(cmd): | |
args = cmd.split() | |
output = subprocess.run(args, capture_output=True) | |
return output.returncode, output.stdout.decode('UTF-8') | |
def check(): | |
_, output = get_stdout('acpi --battery') | |
percs = list(map(lambda x: int(x.replace('%', '')), | |
re.findall(r'\d+%', output))) | |
percs.sort(reverse=True) | |
if 'Discharging' in output: | |
if percs[0] < 7: | |
notification(f'Battery at {percs[0]}%.', 'Suspending', Notify.Urgency.CRITICAL, Notify.EXPIRES_NEVER) | |
time.sleep(1) | |
os.system('systemctl suspend') | |
elif percs[0] < 10: | |
notification(f'Battery at {percs[0]}%.', 'Battery below 10%. Plug in now.', Notify.Urgency.CRITICAL, Notify.EXPIRES_NEVER) | |
elif percs[0] < 20: | |
notification('Battery getting low.', f'Battery at {percs[0]}. Plug in now.', Notify.Urgency.NORMAL, Notify.EXPIRES_DEFAULT) | |
def notification(title, message, urgency, expiry): | |
notif = Notify.Notification.new(title, message, None) | |
notif.set_urgency(urgency) | |
notif.set_timeout(expiry) | |
notif.show() | |
if __name__ == "__main__": | |
notification('Battery monitor started.', '', Notify.Urgency.LOW, Notify.EXPIRES_DEFAULT) | |
while True: | |
try: | |
check() | |
time.sleep(30) | |
except: | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment