mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-11-24 18:28:21 +00:00
Disable multi requests on json decode error during multi-request (#1025)
Issue affecting some P100 devices
This commit is contained in:
@@ -47,6 +47,9 @@ class SmartProtocol(BaseProtocol):
|
||||
self._terminal_uuid: str = base64.b64encode(md5(uuid.uuid4().bytes)).decode()
|
||||
self._request_id_generator = SnowflakeId(1, 1)
|
||||
self._query_lock = asyncio.Lock()
|
||||
self._multi_request_batch_size = (
|
||||
self._transport._config.batch_size or self.DEFAULT_MULTI_REQUEST_BATCH_SIZE
|
||||
)
|
||||
|
||||
def get_smart_request(self, method, params=None) -> str:
|
||||
"""Get a request message as a string."""
|
||||
@@ -117,9 +120,16 @@ class SmartProtocol(BaseProtocol):
|
||||
|
||||
end = len(multi_requests)
|
||||
# Break the requests down as there can be a size limit
|
||||
step = (
|
||||
self._transport._config.batch_size or self.DEFAULT_MULTI_REQUEST_BATCH_SIZE
|
||||
)
|
||||
step = self._multi_request_batch_size
|
||||
if step == 1:
|
||||
# If step is 1 do not send request batches
|
||||
for request in multi_requests:
|
||||
method = request["method"]
|
||||
req = self.get_smart_request(method, request["params"])
|
||||
resp = await self._transport.send(req)
|
||||
self._handle_response_error_code(resp, method, raise_on_error=False)
|
||||
multi_result[method] = resp["result"]
|
||||
return multi_result
|
||||
for i in range(0, end, step):
|
||||
requests_step = multi_requests[i : i + step]
|
||||
|
||||
@@ -141,7 +151,21 @@ class SmartProtocol(BaseProtocol):
|
||||
batch_name,
|
||||
pf(response_step),
|
||||
)
|
||||
self._handle_response_error_code(response_step, batch_name)
|
||||
try:
|
||||
self._handle_response_error_code(response_step, batch_name)
|
||||
except DeviceError as ex:
|
||||
# P100 sometimes raises JSON_DECODE_FAIL_ERROR on batched request so
|
||||
# disable batching
|
||||
if (
|
||||
ex.error_code is SmartErrorCode.JSON_DECODE_FAIL_ERROR
|
||||
and self._multi_request_batch_size != 1
|
||||
):
|
||||
self._multi_request_batch_size = 1
|
||||
raise _RetryableError(
|
||||
"JSON Decode failure, multi requests disabled"
|
||||
) from ex
|
||||
raise ex
|
||||
|
||||
responses = response_step["result"]["responses"]
|
||||
for response in responses:
|
||||
method = response["method"]
|
||||
|
||||
Reference in New Issue
Block a user