Last active
November 13, 2020 07:51
-
-
Save mingjunlu/fae8b889cd15444f6602826142e6ac66 to your computer and use it in GitHub Desktop.
This file contains 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
# Python 3.6.6 on macOS Mojave 10.14.1 | |
import pync | |
import psutil | |
from time import sleep | |
def check_battery(low, high): | |
# 獲得電池資訊 | |
battery = psutil.sensors_battery() | |
charging = battery.power_plugged | |
percent = battery.percent | |
message = '目前電池電量為 ' + str(percent) + '%' | |
# 判斷是否需要連接或移除電源線 | |
if percent <= low and not charging: | |
return {'title': '電量不足', 'message': message} | |
elif percent >= high and charging: | |
return {'title': '電量充足', 'message': message} | |
while True: | |
alert = check_battery(40, 80) | |
if alert: | |
pync.notify(alert['message'], title=alert['title'], sound='default') | |
sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment