-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhomespeak.py
90 lines (72 loc) · 2.94 KB
/
homespeak.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Python3 - Homespeak v0.02
# Query an online Google Home (or Chromecast) device
# usage: python3 homespeak.py <ip> <all|info|bluetooth|networks etc>
# 0.01 - initial commit
# 0.02 - refactor to make adding new get/posts easier and ip formatting validation
# 0.03 - fixed minor bug that caused program to crash when json response was not successful
# Requirements:
# pip3 install requests (for the get requests)
# https://rithvikvibhu.github.io/GHLocalApi/
#post_header = "content-type: application/json"
def printDivider(div, num ):
for x in range(0, num):
print(div, end='')
print()
return
import requests, json, sys, argparse, ipaddress
parser = argparse.ArgumentParser()
parser.add_argument("ip", help="ip address of the home device")
parser.add_argument("options", help="options include info, bluetooth, networks, donotdisturb, accessibility, alarms, alarmvolume, wifiscan, appdeviceid, all")
args = parser.parse_args()
#print(args.echo)
# command name = url strings, post/get, print string
command_list = {
'networks': ["/setup/configured_networks", "get", "Get Configured Networks"],
'bluetooth': ["/setup/bluetooth/get_bonded", "get", "Get Bluetooth Bonded"],
'info': ["/setup/eureka_info?params=version,audio,name,build_info,detail,device_info,net,wifi,setup,settings,opt_in,opencast,multizone,proxy,night_mode_params,user_eq,room_equalizer&options=detail", "get", "Get Device Info"],
'donotdisturb': ["/setup/assistant/notifications", "post", "Get Do Not Disturb"],
'accessibility': ["/setup/assistant/notifications", "post", "Get Accessibility Settings"],
'alarms': ["/setup/assistant/alarms", "get", "Get the currently set alarms and timers."],
'alarmvolume': ["/setup/assistant/alarms/volume", "post", "Get Alarm Volume"],
'wifiscan': ["/setup/scan_results", "get", "Wifi Scan Results"],
'appdeviceid': ["/setup/get_app_device_id", "post", "Get App Device ID"],
}
# validate IP address
try:
network = ipaddress.IPv4Network(args.ip)
except ValueError:
print('address/netmask is invalid for IPv4:', args.ip)
exit(0)
device = "http://"+args.ip+":8008"
# put options in commands list
if args.options == "all":
commands = command_list.keys()
elif args.options in command_list.keys():
commands = [args.options]
else:
print("Unknown option: Exiting")
exit(0)
# for all the commands, run either get or post against the device URL
for c in commands:
if c in command_list.keys():
command = command_list[c][0]
request_method = command_list[c][1]
print (command_list[c][2])
else:
print("Error")
continue
# sends get/post request and converts json
if request_method == "get":
r = requests.get(device + command)
#print (r.status_code)
elif request_method == "post":
r = requests.post(device + command)
#print (r.status_code)
else:
print ("Error")
printDivider("=", 50)
continue
# pretties up the json output
if r.status_code == 200:
print(json.dumps(r.json(), sort_keys=True, indent=4))
printDivider("=", 50)