From 2760a6d8070a4adce8085737622ddf6c7d2bb627 Mon Sep 17 00:00:00 2001 From: Teemu R Date: Sat, 19 Mar 2022 05:24:44 +0100 Subject: [PATCH] Retry on error code -9999 (#1363) Error code -9999 is a common occurence when sending invalid command to a device, but which also seems to be recoverable error on others. This PR changes the protocol behavior by retrying when -9999 response is received. --- miio/miioprotocol.py | 3 ++- miio/tests/test_protocol.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/miio/miioprotocol.py b/miio/miioprotocol.py index 4575a11d8..d187ad5dd 100644 --- a/miio/miioprotocol.py +++ b/miio/miioprotocol.py @@ -269,7 +269,8 @@ def raw_id(self): def _handle_error(self, error): """Raise exception based on the given error code.""" - if "code" in error and error["code"] == -30001: + RECOVERABLE_ERRORS = [-30001, -9999] + if "code" in error and error["code"] in RECOVERABLE_ERRORS: raise RecoverableError(error) raise DeviceError(error) diff --git a/miio/tests/test_protocol.py b/miio/tests/test_protocol.py index 095c2e9ab..be8f8556f 100644 --- a/miio/tests/test_protocol.py +++ b/miio/tests/test_protocol.py @@ -75,8 +75,8 @@ def test_request_extra_params(proto): assert req["sid"] == 1234 -def test_device_error_handling(proto: MiIOProtocol): - retry_error = -30001 +@pytest.mark.parametrize("retry_error", [-30001, -9999]) +def test_device_error_handling(proto: MiIOProtocol, retry_error): with pytest.raises(RecoverableError): proto._handle_error({"code": retry_error})