-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathmyq.py
156 lines (131 loc) · 5 KB
/
myq.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import requests
import os
import json
import time
class MyQ:
APP_ID = "Vj8pQggXLhLy0WHahglCD4N1nAkkXQtGYpq2HrHD7H1nvmbT55KqtN6RSF4ILB/i"
LOCALE = "en"
HOST_URI = "myqexternal.myqdevice.com"
LOGIN_ENDPOINT = "api/v4/User/Validate"
DEVICE_LIST_ENDPOINT = "api/v4/UserDeviceDetails/Get"
DEVICE_SET_ENDPOINT = "api/v4/DeviceAttribute/PutDeviceAttribute"
username = "<MYQ_LOGIN_USERNAME>"
password = "<MYQ_LOGIN_PASSWORD>"
myq_userid = ""
myq_security_token = ""
myq_cached_login_response = ""
myq_device_id = False
myq_lamp_device_id = False
def __init__(self, username, password):
self.username = username
self.password = password
def open(self):
self.change_door_state("open")
def close(self):
self.change_door_state("close")
def lamp_on(self):
self.change_lamp_state("on")
def lamp_off(self):
self.change_lamp_state("off")
def status(self):
state = self.check_door_state()
if state == "1":
return "open"
elif state == "2":
return "closed"
elif state == "3":
return "stopped"
elif state == "4":
return "opening"
elif state == "5":
return "closing"
elif state == "8":
return "moving"
elif state == "9":
return "open"
else:
return str(state) + ", an unknown state for the door."
def lamp_status(self):
state = self.check_lamp_state()
if state == "0":
return "off"
elif state == "1":
return "on"
else:
return "unknown"
def login(self):
params = {
'username': self.username,
'password': self.password
}
login = requests.post(
'https://{host_uri}/{login_endpoint}'.format(
host_uri=self.HOST_URI,
login_endpoint=self.LOGIN_ENDPOINT),
json=params,
headers={
'MyQApplicationId': self.APP_ID
}
)
auth = login.json()
self.myq_security_token = auth['SecurityToken']
return True
def change_device_state(self, payload):
device_action = requests.put(
'https://{host_uri}/{device_set_endpoint}'.format(
host_uri=self.HOST_URI,
device_set_endpoint=self.DEVICE_SET_ENDPOINT),
data=payload,
headers={
'MyQApplicationId': self.APP_ID,
'SecurityToken': self.myq_security_token
}
)
return device_action.status_code == 200
def change_lamp_state(self, command):
newstate = 1 if command.lower() == "on" else 0
payload = {
"attributeName": "desiredlightstate",
"myQDeviceId": self.myq_lamp_device_id,
"AttributeValue": newstate
}
return self.change_device_state(payload)
def change_door_state(self, command):
open_close_state = 1 if command.lower() == "open" else 0
payload = {
'attributeName': 'desireddoorstate',
'myQDeviceId': self.myq_device_id,
'AttributeValue': open_close_state,
}
return self.change_device_state(payload)
def get_devices(self):
devices = requests.get(
'https://{host_uri}/{device_list_endpoint}'.format(
host_uri=self.HOST_URI,
device_list_endpoint=self.DEVICE_LIST_ENDPOINT),
headers={
'MyQApplicationId': self.APP_ID,
'SecurityToken': self.myq_security_token
}
)
return devices.json()['Devices']
def get_device_id(self):
devices = self.get_devices()
for dev in devices:
print("Device => " + dev["MyQDeviceTypeName"])
if (not self.myq_device_id) and dev["MyQDeviceTypeName"] in ["VGDO", "GarageDoorOpener", "Garage Door Opener WGDO"]:
self.myq_device_id = str(dev["MyQDeviceId"])
elif (not self.myq_lamp_device_id) and dev["MyQDeviceTypeName"] == "LampModule":
self.myq_lamp_device_id = str(dev["MyQDeviceId"])
def check_door_state(self):
return self.check_device_state(self.myq_device_id, "doorstate")
def check_lamp_state(self):
return self.check_device_state(self.myq_lamp_device_id, "lightstate")
def check_device_state(self, device_id, state_name):
devices = self.get_devices()
for dev in devices:
if str(dev["MyQDeviceId"]) == str(device_id):
for attribute in dev["Attributes"]:
if attribute["AttributeDisplayName"] == state_name:
door_state = attribute['Value']
return door_state