Skip to content

Commit 82d292c

Browse files
author
jalcaras
committed
fix: remove warn message, refactor: remove useless return, style
1 parent 1cb969e commit 82d292c

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

custom_components/weback_vacuum/webackapi.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Weback API class
33
"""
4+
45
import asyncio
56
import configparser
67
import hashlib
@@ -25,8 +26,8 @@
2526
# API Answer
2627
SUCCESS_OK = "success"
2728
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
3031

3132
# API
3233
AUTH_URL = "https://user.grit-cloud.com/prod/oauth"
@@ -76,7 +77,7 @@ async def login(self) -> bool:
7677
"payload": {
7778
"opt": "login",
7879
"pwd": hashlib.md5(
79-
self.password.encode()
80+
self.password.encode(),
8081
).hexdigest(), # nosec B324
8182
},
8283
"header": {
@@ -87,7 +88,7 @@ async def login(self) -> bool:
8788
"account": self.user,
8889
"client_id": self.client_id,
8990
},
90-
}
91+
},
9192
}
9293

9394
# Checking if there is cached token and is still valid
@@ -98,8 +99,7 @@ async def login(self) -> bool:
9899

99100
if resp is None:
100101
_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",
103103
)
104104
return False
105105

@@ -123,15 +123,13 @@ async def login(self) -> bool:
123123
if result_msg == SERVICE_ERROR:
124124
# Wrong APP
125125
_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",
128127
)
129128
return False
130129
if result_msg == USER_NOT_EXIST:
131130
# User NOK
132131
_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",
135133
)
136134
return False
137135
if result_msg == PASSWORD_NOK:
@@ -150,7 +148,7 @@ def verify_cached_creds(self):
150148
if "weback_token" in creds_data:
151149
weback_token = creds_data["weback_token"]
152150
if self.check_token_is_valid(
153-
weback_token.get("token_exp")
151+
weback_token.get("token_exp"),
154152
) and self.user == weback_token.get("user"):
155153
# Valid creds to use, loading it
156154
self.jwt_token = weback_token.get("jwt_token")
@@ -175,7 +173,8 @@ def get_token_file() -> dict:
175173
creds_data = config._sections
176174
except Exception as get_err:
177175
_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,
179178
)
180179
return creds_data
181180

@@ -193,7 +192,9 @@ def save_token_file(self):
193192
config.set("weback_token", "wss_url", str(self.wss_url))
194193
config.set("weback_token", "region_name", str(self.region_name))
195194
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",
197198
) as configfile:
198199
config.write(configfile)
199200
_LOGGER.debug("WebackApi saved new creds")
@@ -231,7 +232,8 @@ async def get_robot_list(self):
231232

232233
if resp["msg"] == SUCCESS_OK:
233234
_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"],
235237
)
236238
return resp["data"]["thing_list"]
237239
_LOGGER.error("WebackApi failed to get robot list (details : %s)", resp)
@@ -292,8 +294,7 @@ async def send_http(url, **params):
292294
N_RETRY,
293295
)
294296
_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",
297298
N_RETRY,
298299
)
299300
return {"msg": "error", "details": f"Failed after {N_RETRY} retry"}
@@ -572,7 +573,8 @@ def on_close(self, ws, close_status_code, close_msg):
572573

573574
if close_status_code or close_msg:
574575
_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),
576578
)
577579
_LOGGER.debug("WebackApi (WSS) Close Message: %s", str(close_msg))
578580
self.socket_state = SOCK_CLOSE
@@ -618,7 +620,8 @@ def on_message(self, ws, message):
618620
self._call_subscriber()
619621
else:
620622
_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,
622625
)
623626

624627
# 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):
638641
# Server do not answer (maybe other app are open ???) re-start WSS connection
639642
_LOGGER.warning(
640643
"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...)",
642645
)
643646
self.sent_counter = 0
644647
self.ws.close()
@@ -664,7 +667,8 @@ async def publish_wss(self, dict_message):
664667
)
665668
await self.connect_wss()
666669
_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,
668672
)
669673
return False
670674

@@ -673,7 +677,9 @@ async def send_command(self, thing_name, sub_type, working_payload):
673677
Pack command to send
674678
"""
675679
_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,
677683
)
678684
payload = {
679685
"topic_name": "$aws/things/" + thing_name + "/shadow/update",
@@ -685,7 +691,6 @@ async def send_command(self, thing_name, sub_type, working_payload):
685691
self._refresh_time = 5
686692
await self.publish_wss(payload)
687693
await self.force_cmd_refresh(thing_name, sub_type)
688-
return
689694

690695
async def force_cmd_refresh(self, thing_name, sub_type):
691696
"""Force refresh"""
@@ -714,11 +719,11 @@ async def update_status(self, thing_name, sub_type):
714719
def adapt_refresh_time(self, status):
715720
"""Adapt refreshing time depending on robot status"""
716721
_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+
722727
_LOGGER.debug("WebackApi (WSS) > Set refreshing to 120s")
723728
self._refresh_time = 120
724729

@@ -733,7 +738,7 @@ async def refresh_handler(self, thing_name, sub_type):
733738
_LOGGER.debug("WebackApi (WSS) Refreshing...")
734739
await self.update_status(thing_name, sub_type)
735740
except Exception as refresh_excpt:
736-
_LOGGER.error(
741+
_LOGGER.exception(
737742
"WebackApi (WSS) Error during refresh_handler (details=%s)",
738743
refresh_excpt,
739744
)

0 commit comments

Comments
 (0)