1
1
import requests
2
2
import logging
3
+ from time import sleep
3
4
4
5
5
6
class MyQAPI :
@@ -42,6 +43,7 @@ class MyQAPI:
42
43
LOGIN_ENDPOINT = "api/v4/User/Validate"
43
44
DEVICE_LIST_ENDPOINT = "api/v4/UserDeviceDetails/Get"
44
45
DEVICE_SET_ENDPOINT = "api/v4/DeviceAttribute/PutDeviceAttribute"
46
+ DEVICE_ATTRIBUTE_GET_ENDPOINT = "api/v4/DeviceAttribute/getDeviceAttribute"
45
47
USERAGENT = "Chamberlain/3773 (iPhone; iOS 11.0.3; Scale/2.00)"
46
48
47
49
REQUEST_TIMEOUT = 3.0
@@ -56,6 +58,7 @@ class MyQAPI:
56
58
'7' : STATE_UNKNOWN ,
57
59
'8' : STATE_TRANSITION ,
58
60
'9' : STATE_OPEN ,
61
+ '0' : STATE_UNKNOWN
59
62
}
60
63
61
64
logger = logging .getLogger (__name__ )
@@ -127,7 +130,7 @@ def get_devices(self):
127
130
)
128
131
129
132
devices .raise_for_status ()
130
-
133
+
131
134
except requests .exceptions .HTTPError as ex :
132
135
self .logger .error ("MyQ - API Error[get_devices] %s" , ex )
133
136
return False
@@ -138,7 +141,7 @@ def get_devices(self):
138
141
except KeyError :
139
142
self .logger .error ("MyQ - Login security token may have expired, will attempt relogin on next update" )
140
143
self ._logged_in = False
141
-
144
+
142
145
143
146
def get_garage_doors (self ):
144
147
"""List only MyQ garage door devices."""
@@ -164,20 +167,45 @@ def get_garage_doors(self):
164
167
return False ;
165
168
166
169
def get_status (self , device_id ):
167
- """List only MyQ garage door devices."""
168
- devices = self .get_devices ()
170
+ """Get only door states"""
171
+
172
+ if not self ._logged_in :
173
+ self ._logged_in = self .is_login_valid ()
169
174
170
175
garage_state = False
171
176
172
- if devices != False :
173
- for device in devices :
174
- if device ['MyQDeviceTypeName' ] in self .SUPPORTED_DEVICE_TYPE_NAMES and device ['MyQDeviceId' ] == device_id :
175
- dev = {}
176
- for attribute in device ['Attributes' ]:
177
- if attribute ['AttributeDisplayName' ] == 'doorstate' :
178
- myq_garage_state = attribute ['Value' ]
179
- garage_state = self .DOOR_STATE [myq_garage_state ]
177
+ get_status_attempt = 0
178
+ for get_status_attempt in range (0 , 2 ):
179
+ try :
180
+ doorstate = requests .get (
181
+ 'https://{host_uri}/{device_attribute_get_endpoint}' .format (
182
+ host_uri = self .HOST_URI ,
183
+ device_attribute_get_endpoint = self .DEVICE_ATTRIBUTE_GET_ENDPOINT ),
184
+ headers = {
185
+ 'MyQApplicationId' : self .BRAND_MAPPINGS [self .brand ][self .APP_ID ],
186
+ 'SecurityToken' : self .security_token
187
+ },
188
+ params = {
189
+ 'AttributeName' : 'doorstate' ,
190
+ 'MyQDeviceId' : device_id
191
+ }
192
+ )
193
+
194
+ doorstate .raise_for_status ()
195
+ break
196
+
197
+ except requests .exceptions .HTTPError as ex :
198
+ get_status_attempt = get_status_attempt + 1
199
+ sleep (5 )
200
+
201
+ else :
202
+ self .logger .error ("MyQ - API Error[get_status] - Failed to get return from API after 3 attempts." )
203
+ return False
180
204
205
+ doorstate = doorstate .json ()['AttributeValue' ]
206
+
207
+ garage_state = self .DOOR_STATE [doorstate ]
208
+
181
209
return garage_state
182
210
183
211
def close_device (self , device_id ):
0 commit comments