-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpybattery.py
executable file
·53 lines (43 loc) · 1.47 KB
/
pybattery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python
#ref: https://gist.github.com/pudquick/134acb5f7423312effcc98ec56679136
import objc
from Foundation import NSBundle
IOKit = NSBundle.bundleWithIdentifier_('com.apple.framework.IOKit')
functions = [("IOServiceGetMatchingService", b"II@"),
("IOServiceMatching", b"@*"),
("IORegistryEntryCreateCFProperties", b"IIo^@@I"),
("IOPSCopyPowerSourcesByType", b"@I"),
("IOPSCopyPowerSourcesInfo", b"@"),
]
objc.loadBundleFunctions(IOKit, globals(), functions)
# matches information pulled by: pmset -g rawbatt
def raw_battery_dict():
battery = IOServiceGetMatchingService(0, IOServiceMatching("AppleSmartBattery"))
if (battery != 0):
# we have a battery
err, props = IORegistryEntryCreateCFProperties(battery, None, None, 0)
return props
# matches information pulled by: pmset -g batt
def adjusted_battery_dict():
try:
battery = list(IOPSCopyPowerSourcesByType(0))[0]
except:
battery = 0
if (battery != 0):
# we have a battery
return battery
def raw_battery_percent():
d = raw_battery_dict()
if d:
curc = d['CurrentCapacity']
maxc = d['MaxCapacity']
perc = 100.*curc/maxc
return perc
def adjusted_battery_percent():
d = adjusted_battery_dict()
if d:
return d["Current Capacity"]
print raw_battery_dict()
print adjusted_battery_dict()
print raw_battery_percent()
print adjusted_battery_percent()