Skip to content

Instantly share code, notes, and snippets.

@uetchy
Last active August 20, 2018 05:49
Show Gist options
  • Save uetchy/419dfd2b75af162cd51db1f9d0a189f5 to your computer and use it in GitHub Desktop.
Save uetchy/419dfd2b75af162cd51db1f9d0a189f5 to your computer and use it in GitHub Desktop.
Inspect your MacBook's battery.
#!/usr/bin/env python
from __future__ import print_function
import subprocess
import re
def getIOReg():
cmd = "ioreg -l | grep Capacity"
response = subprocess.check_output(cmd, shell=True)
return response.decode('utf-8')
def parseIOReg(rawResult):
maxCapacity = int(re.search(r'"MaxCapacity" = (\d+)\n', rawResult).group(1))
designCapacity = int(
re.search(r'"DesignCapacity" = (\d+)\n', rawResult).group(1))
cycleCount = int(re.search(r'"Cycle Count"=(\d+)\}', rawResult).group(1))
return [maxCapacity, designCapacity, cycleCount]
if __name__ == '__main__':
maxCapacity, designCapacity, cycleCount = parseIOReg(getIOReg())
print('Max Capacity:', maxCapacity, 'mAh')
print('Design Capacity:', designCapacity, 'mAh')
print('Cycle Count:', cycleCount, 'cycles')
print('Cycle Count Remaining: {} cycles - {}% reached'.format(
1000 - cycleCount, round(float(cycleCount) / 1000 * 100)))
availability = float(maxCapacity) / designCapacity
print('Battery Availability: {}%'.format(round(availability * 100, 1)))
if (availability < 0.8):
print(
'===> Your battery is eligible for free battery replacement service in AppleCare+. Repair it now!'
)
@uetchy
Copy link
Author

uetchy commented Aug 7, 2018

You'll get a result like this:

➜ python mac-battery-inspector.py
Max Capacity: 4032 mAh
Design Capacity: 5297 mAh
Cycle Count: 602 cycles
Cycle Count Remaining: 398 cycles - 60% reached
Battery Availability: 76.1%
===> Your battery is eligible for free battery replacement service in AppleCare+. Repair it now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment