Skip to content

Commit

Permalink
Retry on error code -9999 (#1363)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rytilahti authored Mar 19, 2022
1 parent 68ed95c commit 2760a6d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion miio/miioprotocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions miio/tests/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down

0 comments on commit 2760a6d

Please sign in to comment.