Skip to content

Commit febe048

Browse files
authored
Merge pull request #10 from arraylabs/door_update
Changes to get_status
2 parents 4beef8e + 5451a19 commit febe048

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

pymyq/__init__.py

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import requests
22
import logging
3+
from time import sleep
34

45

56
class MyQAPI:
@@ -42,6 +43,7 @@ class MyQAPI:
4243
LOGIN_ENDPOINT = "api/v4/User/Validate"
4344
DEVICE_LIST_ENDPOINT = "api/v4/UserDeviceDetails/Get"
4445
DEVICE_SET_ENDPOINT = "api/v4/DeviceAttribute/PutDeviceAttribute"
46+
DEVICE_ATTRIBUTE_GET_ENDPOINT = "api/v4/DeviceAttribute/getDeviceAttribute"
4547
USERAGENT = "Chamberlain/3773 (iPhone; iOS 11.0.3; Scale/2.00)"
4648

4749
REQUEST_TIMEOUT = 3.0
@@ -56,6 +58,7 @@ class MyQAPI:
5658
'7': STATE_UNKNOWN,
5759
'8': STATE_TRANSITION,
5860
'9': STATE_OPEN,
61+
'0': STATE_UNKNOWN
5962
}
6063

6164
logger = logging.getLogger(__name__)
@@ -127,7 +130,7 @@ def get_devices(self):
127130
)
128131

129132
devices.raise_for_status()
130-
133+
131134
except requests.exceptions.HTTPError as ex:
132135
self.logger.error("MyQ - API Error[get_devices] %s", ex)
133136
return False
@@ -138,7 +141,7 @@ def get_devices(self):
138141
except KeyError:
139142
self.logger.error("MyQ - Login security token may have expired, will attempt relogin on next update")
140143
self._logged_in = False
141-
144+
142145

143146
def get_garage_doors(self):
144147
"""List only MyQ garage door devices."""
@@ -164,20 +167,45 @@ def get_garage_doors(self):
164167
return False;
165168

166169
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()
169174

170175
garage_state = False
171176

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
180204

205+
doorstate = doorstate.json()['AttributeValue']
206+
207+
garage_state = self.DOOR_STATE[doorstate]
208+
181209
return garage_state
182210

183211
def close_device(self, device_id):

0 commit comments

Comments
 (0)