Skip to content

Instantly share code, notes, and snippets.

@jletourneau
Created October 10, 2012 17:24
Show Gist options
  • Save jletourneau/3867071 to your computer and use it in GitHub Desktop.
Save jletourneau/3867071 to your computer and use it in GitHub Desktop.
OS X battery info in Python
#!/usr/bin/env python
import subprocess, re
def battery_info():
ioreg = subprocess.check_output(
['ioreg', '-r', '-w0', '-cAppleSmartBattery']
).split("\n")
d = dict()
for line in ioreg:
match = re.match(r'^ *"(.+)" = ("?)(.+)\2$', line)
if match is None:
continue
(key, val) = (match.group(1), match.group(3))
try:
d[key] = int(val)
except ValueError:
if val == 'Yes':
d[key] = True
elif val == 'No':
d[key] = False
else:
d[key] = val
return d
for key, val in battery_info().iteritems():
print "%s: %s" % (key, val)
TimeRemaining: 302
AvgTimeToEmpty: 302
IsCharging: False
PostDischargeWaitSeconds: 120
PermanentFailureStatus: 0
ManufactureDate: 16111
ExternalChargeCapable: False
Location: 0
Temperature: 3000
DeviceName: bq20z451
IOGeneralInterest: IOCommand is not serializable
BatterySerialNumber: D86128506X0DKRNAD
LegacyBatteryInfo: {"Amperage"=18446744073709550485,"Flags"=4,"Capacity"=6224,"Current"=5691,"Voltage"=8078,"Cycle Count"=38}
BatteryInvalidWakeSeconds: 30
DesignCycleCount70: 65535
ManufacturerData: <000000000406000101620000034b3231033030320353414e001900>
AvgTimeToFull: 65535
MaxCapacity: 6224
Manufacturer: SMP
InstantTimeToEmpty: 288
PostChargeWaitSeconds: 120
FullyCharged: False
CurrentCapacity: 5691
PackReserve: 167
OperationStatus: 58435
ExternalConnected: False
Amperage: 18446744073709550485
DesignCapacity: 6700
BatteryInstalled: True
InstantAmperage: 18446744073709549909
CycleCount: 38
DesignCycleCount9C: 1000
FirmwareSerialNumber: 1
MaxErr: 1
Voltage: 8078
AdapterInfo: 0
CellVoltage: (4026,4024,0,0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment