-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathget_connection_status.py
executable file
·57 lines (48 loc) · 1.39 KB
/
get_connection_status.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
54
55
56
57
#!/usr/bin/env python
import os
import re
import json
import sys
mac = os.getenv('AIRPODS_MAC')
if mac == None or mac == "":
menu = {
"items": [
{
"uid": 0,
"type": "default",
"title": "Your setup isn't completet yet.",
"subtitle": "Configure your AirPods device to get started.",
"arg": "unconfigured",
"icon": {
"path": "unconfigured.png"
}
}
]
}
print(json.dumps(menu))
sys.exit()
devices = os.popen('/bin/bash -c "system_profiler SPBluetoothDataType"').read()
connectedRegex = r"(Address: " + mac.upper() + "(.*\n)*?.*Connected: )(Yes|No)"
statusMatch = re.finditer(connectedRegex, devices, re.MULTILINE)
status = ""
for matchNum, match in enumerate(statusMatch):
if match.group() != '':
if match.group(3) == "Yes":
status = "connected"
else:
status = "disconnected"
menu = {
"items": [
{
"uid": 0,
"type": "default",
"title": "Disconnect AirPods" if status == "connected" else "Connect AirPods",
"subtitle": "Status: " + status,
"arg": status,
"icon": {
"path": "icon.png" if status == "disconnected" else "disconnect.png"
}
}
]
}
print(json.dumps(menu))