@@ -27,6 +27,10 @@ def __init__(self, username, password):
2727 self .username = username
2828 self .password = password
2929
30+
31+ self ._devices = None
32+
33+
3034 self .__roles = []
3135
3236 async def login (self ):
@@ -43,60 +47,58 @@ async def login(self):
4347
4448 # 401
4549 # {'errorCode': 'AUTHENTICATION_ERROR', 'error': 'Bad credentials'}
50+ # {'errorCode': 'AUTHENTICATION_ERROR', 'error': 'Your setup cannot be accessed through this application'}
51+ if response .status == 401 :
52+ if result ['errorCode' ] == 'AUTHENTICATION_ERROR' :
53+
54+ if 'Too many requests' in result ['error' ]:
55+ print (result ['error' ])
56+
57+ if 'Your setup cannot be accessed through this application' in result ['error' ]:
58+ print (result ['error' ])
59+
60+ if 'Bad credentials' in result ['error' ]:
61+ print (result ['error' ])
62+
63+ print (result ['error' ])
4664
65+ return False # todo throw error
66+
4767 # 401
4868 # {'errorCode': 'AUTHENTICATION_ERROR', 'error': 'Too many requests, try again later : login with xxx@xxx.tld'}
4969 # TODO Add retry logic on too many requests + for debug, log requests + timespans
50-
70+
5171 # 200
5272 # {'success': True, 'roles': [{'name': 'ENDUSER'}]}
53- if ( response .status is 200 ) :
73+ if response .status == 200 :
5474 if result ['success' ] == True :
5575 self .__roles = result ['roles' ]
5676 self .__cookies = response .cookies
5777
5878 return True
5979
80+ # Temp fallbacks
6081 print (response .status )
6182 print (result )
6283
63- async def get_devices (self ):
64-
65- cookies = self .__cookies
66-
67- async with aiohttp .ClientSession () as session :
68- async with session .get (API_URL + 'setup/devices' , cookies = cookies ) as response :
69-
70- print (response .status )
71- print (response )
72-
73- result = await response .json ()
74-
75- print (result )
76- # 401
77- # {'errorCode': 'AUTHENTICATION_ERROR', 'error': 'Bad credentials'}
78-
79- # {'success': True, 'roles': [{'name': 'ENDUSER'}]}
80- if (response .status is 200 ):
81- if result ["success" ] == True :
82- print (result )
83-
84- # TODO Save cookies
84+ async def get_devices (self , refresh = False ):
8585
86- async def get_states (self ):
87-
88- cookies = self .__cookies
86+ if self ._devices is None or refresh == True :
8987
90- async with aiohttp .ClientSession () as session :
91- async with session .get (API_URL + 'setup/devices/states' , cookies = cookies ) as response :
88+ cookies = self .__cookies
9289
93- print (response .status )
94- result = await response .json ()
95-
96- print (result )
97-
90+ async with aiohttp .ClientSession () as session :
91+ async with session .get (API_URL + 'setup/devices' , cookies = cookies ) as response :
9892
93+ result = await response .json ()
9994
100-
95+ if (response .status is 200 ):
96+ self ._devices = result
10197
98+ return result
99+
100+ # TODO add retry logic for unauthorized?
102101
102+ else :
103+ return []
104+ # TODO Save cookies
0 commit comments