Skip to content

Instantly share code, notes, and snippets.

@ihaveamac
Created October 14, 2018 19:48
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 ihaveamac/4706824cb79bcb260b9eb8890db2a8af to your computer and use it in GitHub Desktop.
Save ihaveamac/4706824cb79bcb260b9eb8890db2a8af to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from __future__ import print_function
from __future__ import division
import subprocess as sp
from os.path import exists, expanduser
from sys import argv
from datetime import datetime
from Foundation import NSUserNotification
from Foundation import NSUserNotificationCenter
from Foundation import NSUserNotificationDefaultSoundName
def notify(_title, _message, _sound = False):
notification = NSUserNotification.alloc().init()
notification.setTitle_(_title)
notification.setInformativeText_(_message)
if _sound == True:
notification.setSoundName_(NSUserNotificationDefaultSoundName)
center = NSUserNotificationCenter.defaultUserNotificationCenter()
center.deliverNotification_(notification)
battery_info = sp.check_output(['ioreg', '-rc', 'AppleSmartBattery'])
now = datetime.now()
charging = None
for l in battery_info.splitlines():
if '"ExternalChargeCapable"' in l:
charging = l.split()[-1] != 'No'
elif '"MaxCapacity"' in l:
maximum = int(l.split()[-1])
elif '"CurrentCapacity"' in l:
current = int(l.split()[-1])
print('DateTime:', str(now))
print('Charging:', charging)
print('Maximum:', maximum)
print('Current:', current)
needs_unplugging = current / maximum
if charging:
print('Currently charging.')
if needs_unplugging >= 0.95:
print('Needs unplugging. (%f)' % (needs_unplugging))
notify('Battery charged', 'Charger should now be disconnected.')
else:
print('Does not need unplugging. (%f)' % (needs_unplugging))
else:
print('Not charging. (%f)' % (needs_unplugging))
if 'log' in argv:
out = expanduser('~/Library/Logs/Battery Log.csv')
if not exists(out):
with open(out, 'w') as f:
f.write('DateTime,Charging,Maximum,Current\n')
with open(out, 'a') as f:
f.write('%s,%s,%i,%i\n' % (now, charging, maximum, current))
print('Wrote log to', out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment