3
3
"""
4
4
5
5
import asyncio
6
- import configparser
7
6
import hashlib
8
7
import json
9
8
import logging
10
- import os
11
9
import threading
12
10
import time
13
11
from datetime import datetime , timedelta
14
12
15
13
import httpx
16
14
import websocket
15
+ import ssl
17
16
18
17
from .vacmap import VacMap
19
18
38
37
ACK_TIMEOUT = 5
39
38
HTTP_TIMEOUT = 5
40
39
41
- # ROOT DIR
42
- CREDS_FILE = "wb_creds"
43
- COMPONENT_DIR = os .path .dirname (os .path .abspath (__file__ ))
44
-
45
40
46
41
class WebackApi :
47
42
"""
@@ -92,16 +87,12 @@ async def login(self) -> bool:
92
87
},
93
88
}
94
89
95
- # Checking if there is cached token and is still valid
96
- if self .verify_cached_creds ():
97
- return True
98
-
99
90
# Checking if the region is China to use the Chinese Auth URL
100
91
if self .region == "86" :
101
92
auth_url_selected = AUTH_URL_CHINA
102
93
else :
103
94
auth_url_selected = AUTH_URL
104
-
95
+
105
96
resp = await self .send_http (auth_url_selected , ** params )
106
97
107
98
if resp is None :
@@ -125,7 +116,6 @@ async def login(self) -> bool:
125
116
self .token_exp = now_date + timedelta (seconds = self .token_duration )
126
117
_LOGGER .debug ("WebackApi login successful" )
127
118
128
- self .save_token_file ()
129
119
return True
130
120
if result_msg == SERVICE_ERROR :
131
121
# Wrong APP
@@ -147,67 +137,6 @@ async def login(self) -> bool:
147
137
_LOGGER .error ("WebackApi can't login (reason is: %s)" , result_msg )
148
138
return False
149
139
150
- def verify_cached_creds (self ):
151
- """
152
- Check if cached creds are not outdated
153
- """
154
- creds_data = self .get_token_file ()
155
- if "weback_token" in creds_data :
156
- weback_token = creds_data ["weback_token" ]
157
- if self .check_token_is_valid (
158
- weback_token .get ("token_exp" ),
159
- ) and self .user == weback_token .get ("user" ):
160
- # Valid creds to use, loading it
161
- self .jwt_token = weback_token .get ("jwt_token" )
162
- self .region_name = weback_token .get ("region_name" )
163
- self .wss_url = weback_token .get ("wss_url" )
164
- self .api_url = weback_token .get ("api_url" )
165
- self .token_exp = weback_token .get ("token_exp" )
166
- _LOGGER .debug ("WebackApi use cached creds." )
167
- return True
168
- _LOGGER .debug ("WebackApi has no or invalid cached creds, renew it..." )
169
- return False
170
-
171
- @staticmethod
172
- def get_token_file () -> dict :
173
- """
174
- Open token file and get all data.
175
- """
176
- creds_data = {}
177
- try :
178
- config = configparser .ConfigParser ()
179
- config .read (os .path .join (COMPONENT_DIR , CREDS_FILE ))
180
- creds_data = config ._sections
181
- except Exception as get_err :
182
- _LOGGER .debug (
183
- "WebackApi not found or invalid weback creds file error=%s" ,
184
- get_err ,
185
- )
186
- return creds_data
187
-
188
- def save_token_file (self ):
189
- """
190
- Save token file with all information
191
- """
192
- try :
193
- config = configparser .ConfigParser ()
194
- config .add_section ("weback_token" )
195
- config .set ("weback_token" , "user" , str (self .user ))
196
- config .set ("weback_token" , "jwt_token" , str (self .jwt_token ))
197
- config .set ("weback_token" , "token_exp" , str (self .token_exp ))
198
- config .set ("weback_token" , "api_url" , str (self .api_url ))
199
- config .set ("weback_token" , "wss_url" , str (self .wss_url ))
200
- config .set ("weback_token" , "region_name" , str (self .region_name ))
201
- with open (
202
- os .path .join (COMPONENT_DIR , CREDS_FILE ),
203
- "w" ,
204
- encoding = "utf-8" ,
205
- ) as configfile :
206
- config .write (configfile )
207
- _LOGGER .debug ("WebackApi saved new creds" )
208
- except Exception as excpt_msg :
209
- _LOGGER .debug ("WebackApi failed to saved new creds details=%s" , excpt_msg )
210
-
211
140
@staticmethod
212
141
def check_token_is_valid (token ) -> bool :
213
142
"""
@@ -277,9 +206,20 @@ async def send_http(url, **params):
277
206
"""
278
207
_LOGGER .debug ("Send HTTP request Url=%s Params=%s" , url , params )
279
208
timeout = httpx .Timeout (HTTP_TIMEOUT , connect = 15.0 )
209
+
210
+ # Create an SSL context that uses asyncio
211
+ ssl_context = ssl .SSLContext (ssl .PROTOCOL_TLS_CLIENT )
212
+
213
+ # Load default certs in a non-blocking way
214
+ loop = asyncio .get_running_loop ()
215
+ await loop .run_in_executor (None , ssl_context .load_default_certs )
216
+
280
217
for attempt in range (N_RETRY ):
281
218
try :
282
- async with httpx .AsyncClient (timeout = timeout ) as client :
219
+ async with httpx .AsyncClient (
220
+ timeout = timeout ,
221
+ verify = ssl_context ,
222
+ ) as client :
283
223
req = await client .post (url , ** params )
284
224
if req .status_code == 200 :
285
225
# Server status OK
@@ -288,7 +228,8 @@ async def send_http(url, **params):
288
228
return req .json ()
289
229
# Server status NOK
290
230
_LOGGER .warning (
291
- "WebackApi : Bad server response (status code=%s) retry... (%s/%s)" ,
231
+ "WebackApi : Bad server response (status code=%s) "
232
+ "retry... (%s/%s)" ,
292
233
req .status_code ,
293
234
attempt ,
294
235
N_RETRY ,
@@ -483,9 +424,6 @@ def __init__(self, user, password, region, country, app, client_id, api_version)
483
424
self ._last_refresh = 0
484
425
self .sent_counter = 0
485
426
486
- # Reloading cached creds
487
- self .verify_cached_creds ()
488
-
489
427
async def check_credentials (self ):
490
428
"""
491
429
Check if credentials for WSS link are OK
@@ -540,12 +478,13 @@ async def open_wss_thread(self):
540
478
if self .wst .is_alive ():
541
479
_LOGGER .debug ("WebackApi (WSS) Thread was init" )
542
480
return True
543
- _LOGGER .error ("WebackApi (WSS) Thread connection init has FAILED" )
544
- return False
481
+ else :
482
+ _LOGGER .error ("WebackApi (WSS) Thread connection init has FAILED" )
483
+ return False
545
484
546
485
except Exception as e :
547
486
self .socket_state = SOCK_ERROR
548
- _LOGGER .debug ("WebackApi (WSS) Error while opening socket" , e )
487
+ _LOGGER .debug ("WebackApi (WSS) Error while opening socket %s " , e )
549
488
return False
550
489
551
490
async def connect_wss (self ):
@@ -616,7 +555,6 @@ def on_message(self, ws, message):
616
555
self .map = VacMap (wss_data ["map_data" ])
617
556
else :
618
557
self .map .wss_update (wss_data ["map_data" ])
619
- # self.render_map()
620
558
except Exception as msg_excpt :
621
559
_LOGGER .error (
622
560
"WebackApi (WSS) Error during on_message (map_data) (details=%s)" ,
@@ -631,7 +569,8 @@ def on_message(self, ws, message):
631
569
wss_data ,
632
570
)
633
571
634
- # Close WSS link if we don't need it anymore or it will get closed by remote side
572
+ # Close WSS link if we don't need it anymore
573
+ # or it will get closed by remote side
635
574
if self ._refresh_time == 120 :
636
575
_LOGGER .debug ("WebackApi (WSS) Closing WSS..." )
637
576
self .ws .close ()
@@ -654,7 +593,7 @@ async def publish_wss(self, dict_message):
654
593
self .ws .close ()
655
594
self .socket_state = SOCK_CLOSE
656
595
657
- for attempt in range (N_RETRY ):
596
+ for _ in range (N_RETRY ):
658
597
if self .socket_state == SOCK_CONNECTED :
659
598
try :
660
599
self .ws .send (json_message )
@@ -669,7 +608,8 @@ async def publish_wss(self, dict_message):
669
608
)
670
609
else :
671
610
_LOGGER .debug (
672
- "WebackApi (WSS) Can't publish message socket_state=%s, reconnecting..." ,
611
+ "WebackApi (WSS) Can't publish message socket_state=%s"
612
+ ", reconnecting..." ,
673
613
self .socket_state ,
674
614
)
675
615
await self .connect_wss ()
@@ -702,7 +642,7 @@ async def send_command(self, thing_name, sub_type, working_payload):
702
642
async def force_cmd_refresh (self , thing_name , sub_type ):
703
643
"""Force refresh"""
704
644
_LOGGER .debug ("WebackApi (WSS) force refresh after sending cmd..." )
705
- for i in range (4 ):
645
+ for _ in range (4 ):
706
646
await asyncio .sleep (0.6 )
707
647
await self .update_status (thing_name , sub_type )
708
648
0 commit comments