Skip to content

Instantly share code, notes, and snippets.

@mingjunlu
Last active November 13, 2020 07:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mingjunlu/fae8b889cd15444f6602826142e6ac66 to your computer and use it in GitHub Desktop.
Save mingjunlu/fae8b889cd15444f6602826142e6ac66 to your computer and use it in GitHub Desktop.
# 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