Skip to content

Commit

Permalink
fix json decode quirk for xiaomi e10 (#1922)
Browse files Browse the repository at this point in the history
Version:
Firmware version: `2.2.4_0050`
Hardware version: `esp32`

some commands (e.g. `genericmiot call map:rename-map '[0,"string"]'`)
generate json-like string like this:
```json
{"id":2,"result":,"exe_time":0}
```
and this produce the error:
```log
ERROR:miio.protocol:Unable to parse json 'b'{"id":2,"result":,"exe_time":0}'': Expecting value: line 1 column 18 (char 17)
ERROR:miio.click_common:Exception: Unable to parse message payload
```
I fix that by removing the nonsense `result":,`.

---------

Co-authored-by: Teemu Rytilahti <[email protected]>
  • Loading branch information
matan-h and rytilahti authored May 4, 2024
1 parent 5b67f64 commit 8bfe40d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions miio/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ def _decode(self, obj, context, path) -> Union[Dict, bytes]:
),
# fix double commas for xiaomi.vacuum.b112, fw: 2.2.4_0049
lambda decrypted_bytes: decrypted_bytes.replace(b",,", b","),
# fix "result":," no sense key for xiaomi.vacuum.b112, fw:2.2.4_0050
lambda decrypted_bytes: decrypted_bytes.replace(b'"result":,', b""),
]

for i, quirk in enumerate(decrypted_quirks):
Expand Down
11 changes: 11 additions & 0 deletions miio/tests/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ def test_decode_json_quirk_cloud(token):
assert parsed_msg.data.value["id"] == 123456


def test_decode_json_empty_result(token):
"""Test for quirk handling on empty result seen with xiaomi.vacuum.b112."""
ctx = {"token": token}
serialized_msg = build_msg(b'{"id":2,"result":,"exe_time":0}', token)
parsed_msg = Message.parse(serialized_msg, **ctx)

assert parsed_msg.data.value
assert isinstance(parsed_msg.data.value, dict)
assert parsed_msg.data.value["id"] == 2


def test_decode_json_raises_for_invalid_json(token):
ctx = {"token": token}

Expand Down

0 comments on commit 8bfe40d

Please sign in to comment.