From 8bfe40d191be355c8956fb133077ec9c2b8da523 Mon Sep 17 00:00:00 2001 From: matan h <56131718+matan-h@users.noreply.github.com> Date: Sat, 4 May 2024 22:20:53 +0300 Subject: [PATCH] fix json decode quirk for xiaomi e10 (#1922) 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 --- miio/protocol.py | 2 ++ miio/tests/test_protocol.py | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/miio/protocol.py b/miio/protocol.py index 49d6e5846..8d66e02a6 100644 --- a/miio/protocol.py +++ b/miio/protocol.py @@ -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): diff --git a/miio/tests/test_protocol.py b/miio/tests/test_protocol.py index be8f8556f..184ac4103 100644 --- a/miio/tests/test_protocol.py +++ b/miio/tests/test_protocol.py @@ -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}