1
1
"""
2
2
Weback API class
3
3
"""
4
+
4
5
import asyncio
5
6
import configparser
6
7
import hashlib
25
26
# API Answer
26
27
SUCCESS_OK = "success"
27
28
SERVICE_ERROR = "ServiceErrorException"
28
- USER_NOT_EXIST = "UserNotExist" # nosec B105
29
- PASSWORD_NOK = "PasswordInvalid" # nosec B105
29
+ USER_NOT_EXIST = "UserNotExist"
30
+ PASSWORD_NOK = "PasswordInvalid" # noqa: S105
30
31
31
32
# API
32
33
AUTH_URL = "https://user.grit-cloud.com/prod/oauth"
@@ -76,7 +77,7 @@ async def login(self) -> bool:
76
77
"payload" : {
77
78
"opt" : "login" ,
78
79
"pwd" : hashlib .md5 (
79
- self .password .encode ()
80
+ self .password .encode (),
80
81
).hexdigest (), # nosec B324
81
82
},
82
83
"header" : {
@@ -87,7 +88,7 @@ async def login(self) -> bool:
87
88
"account" : self .user ,
88
89
"client_id" : self .client_id ,
89
90
},
90
- }
91
+ },
91
92
}
92
93
93
94
# Checking if there is cached token and is still valid
@@ -98,8 +99,7 @@ async def login(self) -> bool:
98
99
99
100
if resp is None :
100
101
_LOGGER .error (
101
- "WebackApi login failed, server sent an empty answer, "
102
- "please check repo's README.md about WeBack's discontinuation service"
102
+ "WebackApi login failed, server sent an empty answer" ,
103
103
)
104
104
return False
105
105
@@ -123,15 +123,13 @@ async def login(self) -> bool:
123
123
if result_msg == SERVICE_ERROR :
124
124
# Wrong APP
125
125
_LOGGER .error (
126
- "WebackApi login failed, application is not recognized, "
127
- "please check repo's README.md about WeBack's discontinuation service"
126
+ "WebackApi login failed, application is not recognized" ,
128
127
)
129
128
return False
130
129
if result_msg == USER_NOT_EXIST :
131
130
# User NOK
132
131
_LOGGER .error (
133
- "WebackApi login failed, user does not exist "
134
- "please check repo's README.md about WeBack's discontinuation service"
132
+ "WebackApi login failed, user does not exist" ,
135
133
)
136
134
return False
137
135
if result_msg == PASSWORD_NOK :
@@ -150,7 +148,7 @@ def verify_cached_creds(self):
150
148
if "weback_token" in creds_data :
151
149
weback_token = creds_data ["weback_token" ]
152
150
if self .check_token_is_valid (
153
- weback_token .get ("token_exp" )
151
+ weback_token .get ("token_exp" ),
154
152
) and self .user == weback_token .get ("user" ):
155
153
# Valid creds to use, loading it
156
154
self .jwt_token = weback_token .get ("jwt_token" )
@@ -175,7 +173,8 @@ def get_token_file() -> dict:
175
173
creds_data = config ._sections
176
174
except Exception as get_err :
177
175
_LOGGER .debug (
178
- "WebackApi not found or invalid weback creds file error=%s" , get_err
176
+ "WebackApi not found or invalid weback creds file error=%s" ,
177
+ get_err ,
179
178
)
180
179
return creds_data
181
180
@@ -193,7 +192,9 @@ def save_token_file(self):
193
192
config .set ("weback_token" , "wss_url" , str (self .wss_url ))
194
193
config .set ("weback_token" , "region_name" , str (self .region_name ))
195
194
with open (
196
- os .path .join (COMPONENT_DIR , CREDS_FILE ), "w" , encoding = "utf-8"
195
+ os .path .join (COMPONENT_DIR , CREDS_FILE ),
196
+ "w" ,
197
+ encoding = "utf-8" ,
197
198
) as configfile :
198
199
config .write (configfile )
199
200
_LOGGER .debug ("WebackApi saved new creds" )
@@ -231,7 +232,8 @@ async def get_robot_list(self):
231
232
232
233
if resp ["msg" ] == SUCCESS_OK :
233
234
_LOGGER .debug (
234
- "WebackApi get robot list OK : %s" , resp ["data" ]["thing_list" ]
235
+ "WebackApi get robot list OK : %s" ,
236
+ resp ["data" ]["thing_list" ],
235
237
)
236
238
return resp ["data" ]["thing_list" ]
237
239
_LOGGER .error ("WebackApi failed to get robot list (details : %s)" , resp )
@@ -292,8 +294,7 @@ async def send_http(url, **params):
292
294
N_RETRY ,
293
295
)
294
296
_LOGGER .error (
295
- "WebackApi : HTTP error after %s retry please check repo's "
296
- "README.md about WeBack's discontinuation service" ,
297
+ "WebackApi : HTTP error after %s retry" ,
297
298
N_RETRY ,
298
299
)
299
300
return {"msg" : "error" , "details" : f"Failed after { N_RETRY } retry" }
@@ -572,7 +573,8 @@ def on_close(self, ws, close_status_code, close_msg):
572
573
573
574
if close_status_code or close_msg :
574
575
_LOGGER .debug (
575
- "WebackApi (WSS) Close Status_code: %s " , str (close_status_code )
576
+ "WebackApi (WSS) Close Status_code: %s " ,
577
+ str (close_status_code ),
576
578
)
577
579
_LOGGER .debug ("WebackApi (WSS) Close Message: %s" , str (close_msg ))
578
580
self .socket_state = SOCK_CLOSE
@@ -618,7 +620,8 @@ def on_message(self, ws, message):
618
620
self ._call_subscriber ()
619
621
else :
620
622
_LOGGER .error (
621
- "WebackApi (WSS) Received an unknown message from server : %s" , wss_data
623
+ "WebackApi (WSS) Received an unknown message from server : %s" ,
624
+ wss_data ,
622
625
)
623
626
624
627
# Close WSS link if we don't need it anymore or it will get closed by remote side
@@ -638,7 +641,7 @@ async def publish_wss(self, dict_message):
638
641
# Server do not answer (maybe other app are open ???) re-start WSS connection
639
642
_LOGGER .warning (
640
643
"WebackApi (WSS) Link is UP, but server has stopped answering request. "
641
- "Maybe other WeBack app are opened ? (re-open it...)"
644
+ "Maybe other WeBack app are opened ? (re-open it...)" ,
642
645
)
643
646
self .sent_counter = 0
644
647
self .ws .close ()
@@ -664,7 +667,8 @@ async def publish_wss(self, dict_message):
664
667
)
665
668
await self .connect_wss ()
666
669
_LOGGER .error (
667
- "WebackApi (WSS) Failed to puslish message after %s retry" , N_RETRY
670
+ "WebackApi (WSS) Failed to puslish message after %s retry" ,
671
+ N_RETRY ,
668
672
)
669
673
return False
670
674
@@ -673,7 +677,9 @@ async def send_command(self, thing_name, sub_type, working_payload):
673
677
Pack command to send
674
678
"""
675
679
_LOGGER .debug (
676
- "WebackApi (WSS) send_command=%s for robot=%s" , working_payload , thing_name
680
+ "WebackApi (WSS) send_command=%s for robot=%s" ,
681
+ working_payload ,
682
+ thing_name ,
677
683
)
678
684
payload = {
679
685
"topic_name" : "$aws/things/" + thing_name + "/shadow/update" ,
@@ -685,7 +691,6 @@ async def send_command(self, thing_name, sub_type, working_payload):
685
691
self ._refresh_time = 5
686
692
await self .publish_wss (payload )
687
693
await self .force_cmd_refresh (thing_name , sub_type )
688
- return
689
694
690
695
async def force_cmd_refresh (self , thing_name , sub_type ):
691
696
"""Force refresh"""
@@ -714,11 +719,11 @@ async def update_status(self, thing_name, sub_type):
714
719
def adapt_refresh_time (self , status ):
715
720
"""Adapt refreshing time depending on robot status"""
716
721
_LOGGER .debug ("WebackApi (WSS) adapt for : %s" , status )
717
- if "working_status" in status :
718
- if status [ "working_status" ] not in self . DOCKED_STATES :
719
- _LOGGER . debug ( "WebackApi (WSS) > Set refreshing to 5s" )
720
- self . _refresh_time = 5
721
- return
722
+ if status . get ( "working_status" , None ) not in self . DOCKED_STATES :
723
+ _LOGGER . debug ( "WebackApi (WSS) > Set refreshing to 5s" )
724
+ self . _refresh_time = 5
725
+ return
726
+
722
727
_LOGGER .debug ("WebackApi (WSS) > Set refreshing to 120s" )
723
728
self ._refresh_time = 120
724
729
@@ -733,7 +738,7 @@ async def refresh_handler(self, thing_name, sub_type):
733
738
_LOGGER .debug ("WebackApi (WSS) Refreshing..." )
734
739
await self .update_status (thing_name , sub_type )
735
740
except Exception as refresh_excpt :
736
- _LOGGER .error (
741
+ _LOGGER .exception (
737
742
"WebackApi (WSS) Error during refresh_handler (details=%s)" ,
738
743
refresh_excpt ,
739
744
)
0 commit comments