From d5775526fd49ef6db52486c0a2d33cace01bf434 Mon Sep 17 00:00:00 2001 From: infy1995 Date: Mon, 9 Nov 2026 22:58:39 +0100 Subject: [PATCH 1/4] fix: redact private data in device diagnostics Redact JSON documents stored as strings, such as the A01 product info data point, which exposed the serial number, Wi-Fi name, IP and MAC. Also redact ssid and iotName. Decode the A01 product info and voice pack data points, which the device reports as JSON strings and always decoded to None, and add the decoded A01 state to device diagnostics as traits. --- roborock/devices/device.py | 4 ++ roborock/devices/traits/a01/__init__.py | 28 +++++++++-- roborock/diagnostics.py | 29 +++++++++-- tests/devices/test_a01_traits.py | 39 +++++++++++++- tests/devices/traits/a01/test_init.py | 67 +++++++++++++++++++++++++ tests/test_diagnostics.py | 63 +++++++++++++++++++++++ 6 files changed, 222 insertions(+), 8 deletions(-) diff --git a/roborock/devices/device.py b/roborock/devices/device.py index 44c275f6c..d57b451b0 100644 --- a/roborock/devices/device.py +++ b/roborock/devices/device.py @@ -255,6 +255,10 @@ def diagnostic_data(self) -> dict[str, Any]: extra: dict[str, Any] = {} if self.v1_properties: extra["traits"] = self.v1_properties.as_dict() + elif self.dyad: + extra["traits"] = self.dyad.as_dict() + elif self.zeo: + extra["traits"] = self.zeo.as_dict() elif self.b01_q10_properties: extra["traits"] = self.b01_q10_properties.as_dict() return redact_device_data( diff --git a/roborock/devices/traits/a01/__init__.py b/roborock/devices/traits/a01/__init__.py index 7682bc7f8..152b76ece 100644 --- a/roborock/devices/traits/a01/__init__.py +++ b/roborock/devices/traits/a01/__init__.py @@ -31,7 +31,14 @@ from datetime import UTC, datetime, time from typing import Any, Generic, TypeVar -from roborock.data import DyadProductInfo, DyadScheduleTask, DyadSndState, HomeDataProduct, RoborockCategory +from roborock.data import ( + DyadProductInfo, + DyadScheduleTask, + DyadSndState, + HomeDataProduct, + RoborockBase, + RoborockCategory, +) from roborock.data.dyad.dyad_code_mappings import ( DyadBrushSpeed, DyadCleanAssistPower, @@ -144,8 +151,8 @@ def _decode_dyad_schedule(value: str) -> list[DyadScheduleTask]: RoborockDyadDataProtocol.SELF_CLEAN_CUR_TEMP: lambda val: DyadSelfCleanWaterTemp(val).name, RoborockDyadDataProtocol.DRY_CUR_MODE: lambda val: DyadDryingMode(val).name, RoborockDyadDataProtocol.SCHEDULE_TASK: _decode_dyad_schedule, - RoborockDyadDataProtocol.SND_STATE: lambda val: DyadSndState.from_dict(val), - RoborockDyadDataProtocol.PRODUCT_INFO: lambda val: DyadProductInfo.from_dict(val), + RoborockDyadDataProtocol.SND_STATE: lambda val: DyadSndState.from_dict(json.loads(val)), + RoborockDyadDataProtocol.PRODUCT_INFO: lambda val: DyadProductInfo.from_dict(json.loads(val)), } ZEO_PROTOCOL_ENTRIES: dict[RoborockZeoProtocol, Callable] = { @@ -191,6 +198,17 @@ def convert_zeo_value(protocol_value: RoborockZeoProtocol, value: Any) -> Any: return None +def _diagnostic_value(value: Any) -> Any: + """Convert a decoded value to plain data for diagnostics.""" + if isinstance(value, RoborockBase): + return value.as_dict() + if isinstance(value, list): + return [_diagnostic_value(item) for item in value] + if isinstance(value, time): + return value.isoformat() + return value + + _DYAD_PROTOCOL_VALUES = frozenset(protocol.value for protocol in RoborockDyadDataProtocol) _ZEO_PROTOCOL_VALUES = frozenset(protocol.value for protocol in RoborockZeoProtocol) @@ -225,6 +243,10 @@ def values(self) -> dict[_P, Any]: """ return dict(self._values) + def as_dict(self) -> dict[str, Any]: + """Return the latest known state as plain data for diagnostics.""" + return {protocol.name: _diagnostic_value(value) for protocol, value in self._values.items()} + @property def last_message_time(self) -> datetime | None: """Time the last message was received from the device. diff --git a/roborock/diagnostics.py b/roborock/diagnostics.py index 26d1e89c7..d81c0f1c0 100644 --- a/roborock/diagnostics.py +++ b/roborock/diagnostics.py @@ -9,6 +9,7 @@ DeviceManager. """ +import json import time from collections import Counter from collections.abc import Generator, Mapping @@ -106,7 +107,9 @@ def reset(self) -> None: "ipAddress", "ipAdress", "wifiName", + "ssid", "rawName", + "iotName", "lat", "long", } @@ -123,8 +126,28 @@ def reset(self) -> None: REDACTED = "**REDACTED**" +def _redact_json_string(value: str, path: str) -> str: + """Redact sensitive data in a JSON document stored as a string. + + Some devices report structured data this way, such as the A01 product info + data point. A string that looks like JSON but cannot be parsed is fully + redacted, since its contents cannot be checked. + """ + if not value.lstrip().startswith(("{", "[")): + return value + try: + decoded = json.loads(value) + except json.JSONDecodeError: + return REDACTED + redacted = redact_device_data(decoded, path) + return value if redacted == decoded else json.dumps(redacted) + + def redact_device_data(data: T, path: str = "") -> T | dict[str, Any]: - """Redact sensitive data in a dict.""" + """Redact sensitive data in a dict, including JSON documents stored as strings.""" + if isinstance(data, str): + return cast(T, _redact_json_string(data, path)) + if not isinstance(data, (Mapping, list)): return data @@ -141,10 +164,8 @@ def redact_device_data(data: T, path: str = "") -> T | dict[str, Any]: redacted[key] = REDACTED elif key == DEVICE_UID and isinstance(value, str): redacted[key] = redact_device_uid(value) - elif isinstance(value, dict): + else: redacted[key] = redact_device_data(value, curr_path) - elif isinstance(value, list): - redacted[key] = [redact_device_data(item, curr_path) for item in value] return redacted diff --git a/tests/devices/test_a01_traits.py b/tests/devices/test_a01_traits.py index 83a5b5cb3..b7bb0ca1b 100644 --- a/tests/devices/test_a01_traits.py +++ b/tests/devices/test_a01_traits.py @@ -1,9 +1,14 @@ +import json from unittest.mock import AsyncMock, Mock, patch import pytest -from roborock.devices.traits.a01 import DyadApi, ZeoApi +from roborock.data import HomeDataDevice +from roborock.devices.device import RoborockDevice +from roborock.devices.traits.a01 import DyadApi, ZeoApi, create from roborock.roborock_message import RoborockDyadDataProtocol, RoborockZeoProtocol +from roborock.testing.a01_simulator import DEFAULT_DYAD_PRODUCT +from tests import mock_data @pytest.fixture @@ -51,3 +56,35 @@ async def test_zeo_query_values(mock_channel): assert RoborockZeoProtocol.STATE in result assert result[RoborockZeoProtocol.STATE] == "spinning" assert result[RoborockZeoProtocol.COUNTDOWN] == 120 + + +def test_dyad_diagnostic_data_includes_redacted_traits(mock_channel): + """A01 diagnostics report the decoded state without identifiers.""" + device_status = { + "201": 3, + "10005": json.dumps( + { + "sn": "TESTSERIAL", + "ssid": "test-network", + "timezone": "Europe/Warsaw", + "posix_timezone": "CET-1CEST,M3.5.0/2,M10.5.0/3", + "ip": "192.0.2.1", + "mac": "00:00:5e:00:53:01", + "oba": {"bom": "A.01"}, + } + ), + } + device = RoborockDevice( + device_info=HomeDataDevice.from_dict({**mock_data.Q10_DEVICE_DATA, "deviceStatus": device_status}), + product=DEFAULT_DYAD_PRODUCT, + channel=AsyncMock(), + trait=create(DEFAULT_DYAD_PRODUCT, mock_channel, device_status=device_status), + ) + + diagnostics = device.diagnostic_data() + + dumped = json.dumps(diagnostics) + for private_value in ("TESTSERIAL", "test-network", "192.0.2.1", "00:00:5e:00:53:01"): + assert private_value not in dumped + assert diagnostics["traits"]["status"] == "charging" + assert diagnostics["traits"]["product_info"]["timezone"] == "Europe/Warsaw" diff --git a/tests/devices/traits/a01/test_init.py b/tests/devices/traits/a01/test_init.py index aea824e6f..ea9b53085 100644 --- a/tests/devices/traits/a01/test_init.py +++ b/tests/devices/traits/a01/test_init.py @@ -324,6 +324,73 @@ async def test_create_seeds_values_from_device_status(fake_channel: FakeChannel) } +PRODUCT_INFO_JSON = json.dumps( + { + "sn": "TESTSERIAL", + "ssid": "test-network", + "timezone": "Europe/Warsaw", + "posix_timezone": "CET-1CEST,M3.5.0/2,M10.5.0/3", + "ip": "192.0.2.1", + "mac": "00:00:5e:00:53:01", + "rssi": -53, + "oba": {"bom": "A.01"}, + } +) + + +async def test_dyad_decodes_json_string_data_points(fake_channel: FakeChannel): + """The device reports product info and voice pack state as JSON strings.""" + api = DyadApi( + fake_channel, # type: ignore[arg-type] + initial_status={ + 10004: '{"sid_in_use":21,"sid_version":3,"location":"de","bom":"A.01","language":"en"}', + 10005: PRODUCT_INFO_JSON, + }, + ) + + assert api.values[RoborockDyadDataProtocol.SND_STATE].sid_in_use == 21 + product_info = api.values[RoborockDyadDataProtocol.PRODUCT_INFO] + assert product_info.sn == "TESTSERIAL" + assert product_info.posix_timezone == "CET-1CEST,M3.5.0/2,M10.5.0/3" + assert product_info.oba == {"bom": "A.01"} + + +@pytest.mark.parametrize("value", ["", "not json", None]) +async def test_dyad_invalid_json_string_data_point(fake_channel: FakeChannel, value: str | None): + """A malformed JSON data point leaves the value unset.""" + api = DyadApi(fake_channel, initial_status={10005: value}) # type: ignore[arg-type] + + assert api.values == {} + + +async def test_dyad_as_dict_is_plain_data(fake_channel: FakeChannel): + """Decoded values are converted to JSON serializable data for diagnostics.""" + api = DyadApi( + fake_channel, # type: ignore[arg-type] + initial_status={ + 201: 3, + 227: 1320, + 10002: '[{"id":1,"on":false,"cron":"15 14 31 10 *","dpAction":{"200":1}}]', + 10005: PRODUCT_INFO_JSON, + }, + ) + + data = api.as_dict() + + json.dumps(data) + assert data["status"] == "charging" + assert data["silent_mode_start_time"] == "22:00:00" + assert data["schedule_task"][0]["cron"] == "15 14 31 10 *" + assert data["product_info"]["timezone"] == "Europe/Warsaw" + + +async def test_zeo_as_dict(fake_channel: FakeChannel): + """Zeo state is keyed by data point name for diagnostics.""" + api = ZeoApi(fake_channel, initial_status={RoborockZeoProtocol.COUNTDOWN.value: 5}) # type: ignore[arg-type] + + assert api.as_dict() == {RoborockZeoProtocol.COUNTDOWN.name: 5} + + async def test_dyad_close_stops_tracking(dyad_api: DyadApi, fake_channel: FakeChannel): """After close(), pushes no longer update `values`.""" await dyad_api.start() diff --git a/tests/test_diagnostics.py b/tests/test_diagnostics.py index 5de9a6950..f3c7e49e7 100644 --- a/tests/test_diagnostics.py +++ b/tests/test_diagnostics.py @@ -1,5 +1,8 @@ """Tests for diagnostics module.""" +import json +from typing import Any + import pytest from roborock.diagnostics import REDACTED, Diagnostics, redact_device_data, redact_device_uid, redact_topic_name @@ -106,3 +109,63 @@ def test_redact_q10_raw_room_name() -> None: assert redact_device_data( {"map": {"rooms": [{"rawName": "Owner’s bedroom"}], "currentRoom": {"rawName": "Salle d’eau"}}} ) == {"map": {"rooms": [{"rawName": REDACTED}], "currentRoom": {"rawName": REDACTED}}} + + +def test_redact_iot_room_name() -> None: + """Room names reported in the map list are as private as raw room names.""" + assert redact_device_data({"rooms": [{"iotName": "Owner’s bedroom", "tag": 1}]}) == { + "rooms": [{"iotName": REDACTED, "tag": 1}] + } + + +def test_redact_json_string_in_device_status() -> None: + """Identifiers inside a data point that holds a JSON string are redacted.""" + product_info = json.dumps( + { + "sn": "TESTSERIAL", + "ssid": "test-network", + "ip": "192.0.2.1", + "mac": "00:00:5e:00:53:01", + "rssi": -53, + "oba": {"bom": "A.01"}, + } + ) + + redacted = redact_device_data({"device": {"deviceStatus": {"10005": product_info, "209": 100}}}) + + status: dict[str, Any] = redacted["device"]["deviceStatus"] + assert status["209"] == 100 + assert json.loads(status["10005"]) == { + "sn": REDACTED, + "ssid": REDACTED, + "ip": REDACTED, + "mac": REDACTED, + "rssi": -53, + "oba": {"bom": "A.01"}, + } + + +def test_redact_json_string_in_list() -> None: + """JSON strings inside lists are redacted too.""" + redacted = redact_device_data({"items": ['{"ip": "192.0.2.1"}', "plain"]}) + + assert redacted["items"] == [json.dumps({"ip": REDACTED}), "plain"] + + +@pytest.mark.parametrize( + "value", + [ + "plain text", + "", + '{"RRPhotoPrivacyVersion": "1"}', # Not sensitive, so the formatting is kept + "[1, 2, 3]", + ], +) +def test_redact_keeps_strings_without_sensitive_data(value: str) -> None: + """Strings with nothing to redact are returned unchanged.""" + assert redact_device_data({"value": value}) == {"value": value} + + +def test_redact_unparsable_json_string() -> None: + """A string that looks like JSON but cannot be checked is fully redacted.""" + assert redact_device_data({"value": '{"sn": "TESTSERIAL"'}) == {"value": REDACTED} From 6e5aac16b3e06b3b184641ff6254f8ae71e9bc67 Mon Sep 17 00:00:00 2001 From: infy1995 Date: Sun, 15 Nov 2026 01:03:24 +0100 Subject: [PATCH 2/4] refactor: decode JSON strings before redacting diagnostics Decode JSON documents stored as strings and redact the result through the normal dict and list handling, instead of redacting and re-encoding them. JSON strings now appear as structured data in diagnostics. --- roborock/diagnostics.py | 21 +- .../__snapshots__/test_device_manager.ambr | 106 +++++++-- .../devices/__snapshots__/test_v1_device.ambr | 212 +++++++++++++++--- tests/devices/traits/a01/test_init.py | 1 - .../b01/q10/__snapshots__/test_status.ambr | 86 +++++-- tests/test_diagnostics.py | 46 ++-- 6 files changed, 368 insertions(+), 104 deletions(-) diff --git a/roborock/diagnostics.py b/roborock/diagnostics.py index d81c0f1c0..163d82932 100644 --- a/roborock/diagnostics.py +++ b/roborock/diagnostics.py @@ -126,27 +126,24 @@ def reset(self) -> None: REDACTED = "**REDACTED**" -def _redact_json_string(value: str, path: str) -> str: - """Redact sensitive data in a JSON document stored as a string. +def _decode_json_string(value: str) -> Any: + """Decode a JSON object or array stored as a string. Some devices report structured data this way, such as the A01 product info - data point. A string that looks like JSON but cannot be parsed is fully - redacted, since its contents cannot be checked. + data point. Any other string is returned unchanged. """ if not value.lstrip().startswith(("{", "[")): return value try: - decoded = json.loads(value) + return json.loads(value) except json.JSONDecodeError: - return REDACTED - redacted = redact_device_data(decoded, path) - return value if redacted == decoded else json.dumps(redacted) + return value def redact_device_data(data: T, path: str = "") -> T | dict[str, Any]: - """Redact sensitive data in a dict, including JSON documents stored as strings.""" + """Redact sensitive data in a dict, decoding JSON documents stored as strings.""" if isinstance(data, str): - return cast(T, _redact_json_string(data, path)) + data = _decode_json_string(data) if not isinstance(data, (Mapping, list)): return data @@ -164,8 +161,10 @@ def redact_device_data(data: T, path: str = "") -> T | dict[str, Any]: redacted[key] = REDACTED elif key == DEVICE_UID and isinstance(value, str): redacted[key] = redact_device_uid(value) - else: + elif isinstance(value, (dict, str)): redacted[key] = redact_device_data(value, curr_path) + elif isinstance(value, list): + redacted[key] = [redact_device_data(item, curr_path) for item in value] return redacted diff --git a/tests/devices/__snapshots__/test_device_manager.ambr b/tests/devices/__snapshots__/test_device_manager.ambr index dadae5983..a0dc2e419 100644 --- a/tests/devices/__snapshots__/test_device_manager.ambr +++ b/tests/devices/__snapshots__/test_device_manager.ambr @@ -18,7 +18,9 @@ '133': 1, }), 'duid': '******bc123', - 'extra': '{"RRPhotoPrivacyVersion": "1"}', + 'extra': dict({ + 'RRPhotoPrivacyVersion': '1', + }), 'featureSet': '2234201184108543', 'fv': '02.56.02', 'iconUrl': 'no_url', @@ -62,7 +64,10 @@ 'id': '120', 'mode': 'ro', 'name': '错误代码', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -70,7 +75,10 @@ 'id': '121', 'mode': 'ro', 'name': '设备状态', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -78,7 +86,10 @@ 'id': '122', 'mode': 'ro', 'name': '设备电量', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -86,7 +97,10 @@ 'id': '123', 'mode': 'rw', 'name': '清扫模式', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -94,7 +108,10 @@ 'id': '124', 'mode': 'rw', 'name': '拖地模式', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -102,7 +119,13 @@ 'id': '125', 'mode': 'rw', 'name': '主刷寿命', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -110,7 +133,13 @@ 'id': '126', 'mode': 'rw', 'name': '边刷寿命', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -118,7 +147,13 @@ 'id': '127', 'mode': 'rw', 'name': '滤网寿命', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -443,7 +478,9 @@ '133': 1, }), 'duid': '******bc123', - 'extra': '{"RRPhotoPrivacyVersion": "1"}', + 'extra': dict({ + 'RRPhotoPrivacyVersion': '1', + }), 'featureSet': '2234201184108543', 'fv': '02.56.02', 'iconUrl': 'no_url', @@ -491,7 +528,10 @@ 'id': '**REDACTED**', 'mode': 'ro', 'name': '**REDACTED**', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -499,7 +539,10 @@ 'id': '**REDACTED**', 'mode': 'ro', 'name': '**REDACTED**', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -507,7 +550,10 @@ 'id': '**REDACTED**', 'mode': 'ro', 'name': '**REDACTED**', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -515,7 +561,10 @@ 'id': '**REDACTED**', 'mode': 'rw', 'name': '**REDACTED**', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -523,7 +572,10 @@ 'id': '**REDACTED**', 'mode': 'rw', 'name': '**REDACTED**', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -531,7 +583,13 @@ 'id': '**REDACTED**', 'mode': 'rw', 'name': '**REDACTED**', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -539,7 +597,13 @@ 'id': '**REDACTED**', 'mode': 'rw', 'name': '**REDACTED**', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -547,7 +611,13 @@ 'id': '**REDACTED**', 'mode': 'rw', 'name': '**REDACTED**', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ diff --git a/tests/devices/__snapshots__/test_v1_device.ambr b/tests/devices/__snapshots__/test_v1_device.ambr index bc0d8fdb3..7653cf3f7 100644 --- a/tests/devices/__snapshots__/test_v1_device.ambr +++ b/tests/devices/__snapshots__/test_v1_device.ambr @@ -19,7 +19,9 @@ '133': 1, }), 'duid': '******bc123', - 'extra': '{"RRPhotoPrivacyVersion": "1"}', + 'extra': dict({ + 'RRPhotoPrivacyVersion': '1', + }), 'featureSet': '2234201184108543', 'fv': '02.56.02', 'iconUrl': 'no_url', @@ -63,7 +65,10 @@ 'id': '120', 'mode': 'ro', 'name': '错误代码', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -71,7 +76,10 @@ 'id': '121', 'mode': 'ro', 'name': '设备状态', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -79,7 +87,10 @@ 'id': '122', 'mode': 'ro', 'name': '设备电量', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -87,7 +98,10 @@ 'id': '123', 'mode': 'rw', 'name': '清扫模式', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -95,7 +109,10 @@ 'id': '124', 'mode': 'rw', 'name': '拖地模式', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -103,7 +120,13 @@ 'id': '125', 'mode': 'rw', 'name': '主刷寿命', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -111,7 +134,13 @@ 'id': '126', 'mode': 'rw', 'name': '边刷寿命', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -119,7 +148,13 @@ 'id': '127', 'mode': 'rw', 'name': '滤网寿命', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -467,7 +502,9 @@ '133': 1, }), 'duid': '******bc123', - 'extra': '{"RRPhotoPrivacyVersion": "1"}', + 'extra': dict({ + 'RRPhotoPrivacyVersion': '1', + }), 'featureSet': '2234201184108543', 'fv': '02.56.02', 'iconUrl': 'no_url', @@ -511,7 +548,10 @@ 'id': '120', 'mode': 'ro', 'name': '错误代码', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -519,7 +559,10 @@ 'id': '121', 'mode': 'ro', 'name': '设备状态', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -527,7 +570,10 @@ 'id': '122', 'mode': 'ro', 'name': '设备电量', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -535,7 +581,10 @@ 'id': '123', 'mode': 'rw', 'name': '清扫模式', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -543,7 +592,10 @@ 'id': '124', 'mode': 'rw', 'name': '拖地模式', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -551,7 +603,13 @@ 'id': '125', 'mode': 'rw', 'name': '主刷寿命', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -559,7 +617,13 @@ 'id': '126', 'mode': 'rw', 'name': '边刷寿命', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -567,7 +631,13 @@ 'id': '127', 'mode': 'rw', 'name': '滤网寿命', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -895,7 +965,9 @@ '133': 1, }), 'duid': '******bc123', - 'extra': '{"RRPhotoPrivacyVersion": "1"}', + 'extra': dict({ + 'RRPhotoPrivacyVersion': '1', + }), 'featureSet': '2234201184108543', 'fv': '02.56.02', 'iconUrl': 'no_url', @@ -939,7 +1011,10 @@ 'id': '120', 'mode': 'ro', 'name': '错误代码', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -947,7 +1022,10 @@ 'id': '121', 'mode': 'ro', 'name': '设备状态', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -955,7 +1033,10 @@ 'id': '122', 'mode': 'ro', 'name': '设备电量', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -963,7 +1044,10 @@ 'id': '123', 'mode': 'rw', 'name': '清扫模式', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -971,7 +1055,10 @@ 'id': '124', 'mode': 'rw', 'name': '拖地模式', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -979,7 +1066,13 @@ 'id': '125', 'mode': 'rw', 'name': '主刷寿命', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -987,7 +1080,13 @@ 'id': '126', 'mode': 'rw', 'name': '边刷寿命', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -995,7 +1094,13 @@ 'id': '127', 'mode': 'rw', 'name': '滤网寿命', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -1293,7 +1398,9 @@ '133': 1, }), 'duid': '******bc123', - 'extra': '{"RRPhotoPrivacyVersion": "1"}', + 'extra': dict({ + 'RRPhotoPrivacyVersion': '1', + }), 'featureSet': '2234201184108543', 'fv': '02.56.02', 'iconUrl': 'no_url', @@ -1337,7 +1444,10 @@ 'id': '120', 'mode': 'ro', 'name': '错误代码', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -1345,7 +1455,10 @@ 'id': '121', 'mode': 'ro', 'name': '设备状态', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -1353,7 +1466,10 @@ 'id': '122', 'mode': 'ro', 'name': '设备电量', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -1361,7 +1477,10 @@ 'id': '123', 'mode': 'rw', 'name': '清扫模式', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -1369,7 +1488,10 @@ 'id': '124', 'mode': 'rw', 'name': '拖地模式', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -1377,7 +1499,13 @@ 'id': '125', 'mode': 'rw', 'name': '主刷寿命', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -1385,7 +1513,13 @@ 'id': '126', 'mode': 'rw', 'name': '边刷寿命', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ @@ -1393,7 +1527,13 @@ 'id': '127', 'mode': 'rw', 'name': '滤网寿命', - 'property': '{"max": 100, "min": 0, "step": 1, "unit": null, "scale": 1}', + 'property': dict({ + 'max': 100, + 'min': 0, + 'scale': 1, + 'step': 1, + 'unit': None, + }), 'type': 'VALUE', }), dict({ diff --git a/tests/devices/traits/a01/test_init.py b/tests/devices/traits/a01/test_init.py index ea9b53085..b6ad85e87 100644 --- a/tests/devices/traits/a01/test_init.py +++ b/tests/devices/traits/a01/test_init.py @@ -377,7 +377,6 @@ async def test_dyad_as_dict_is_plain_data(fake_channel: FakeChannel): data = api.as_dict() - json.dumps(data) assert data["status"] == "charging" assert data["silent_mode_start_time"] == "22:00:00" assert data["schedule_task"][0]["cron"] == "15 14 31 10 *" diff --git a/tests/devices/traits/b01/q10/__snapshots__/test_status.ambr b/tests/devices/traits/b01/q10/__snapshots__/test_status.ambr index 2604e87be..ae15096ee 100644 --- a/tests/devices/traits/b01/q10/__snapshots__/test_status.ambr +++ b/tests/devices/traits/b01/q10/__snapshots__/test_status.ambr @@ -206,7 +206,10 @@ 'id': 120, 'mode': 'ro', 'name': '错误代码', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -222,7 +225,10 @@ 'id': 122, 'mode': 'ro', 'name': '设备电量', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -230,7 +236,10 @@ 'id': 123, 'mode': 'rw', 'name': '吸力档位', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -246,7 +255,10 @@ 'id': 125, 'mode': 'ro', 'name': '主刷寿命', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -254,7 +266,10 @@ 'id': 126, 'mode': 'ro', 'name': '边刷寿命', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -262,7 +277,10 @@ 'id': 127, 'mode': 'ro', 'name': '滤网寿命', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -270,7 +288,10 @@ 'id': 135, 'mode': 'ro', 'name': '离线原因', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -278,7 +299,10 @@ 'id': 136, 'mode': 'rw', 'name': '清洁次数', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -286,7 +310,10 @@ 'id': 137, 'mode': 'rw', 'name': '扫拖模式', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -294,7 +321,10 @@ 'id': 138, 'mode': 'ro', 'name': '清洁任务类型', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -302,7 +332,10 @@ 'id': 139, 'mode': 'ro', 'name': '返回基站类型', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -310,7 +343,10 @@ 'id': 140, 'mode': 'ro', 'name': '基站任务类型', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -318,7 +354,10 @@ 'id': 141, 'mode': 'ro', 'name': '清洁进度', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -334,7 +373,10 @@ 'id': 201, 'mode': 'wo', 'name': '启动清洁任务', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -342,7 +384,10 @@ 'id': 202, 'mode': 'wo', 'name': '返回基站任务', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -350,7 +395,10 @@ 'id': 203, 'mode': 'wo', 'name': '启动基站任务', - 'property': '{"range": []}', + 'property': dict({ + 'range': list([ + ]), + }), 'type': 'ENUM', }), dict({ @@ -382,7 +430,11 @@ 'id': 207, 'mode': 'rw', 'name': '用户改善计划', - 'property': '{"range": ["0,1"]}', + 'property': dict({ + 'range': list([ + '0,1', + ]), + }), 'type': 'ENUM', }), ]), diff --git a/tests/test_diagnostics.py b/tests/test_diagnostics.py index f3c7e49e7..81cfc8cf2 100644 --- a/tests/test_diagnostics.py +++ b/tests/test_diagnostics.py @@ -133,15 +133,20 @@ def test_redact_json_string_in_device_status() -> None: redacted = redact_device_data({"device": {"deviceStatus": {"10005": product_info, "209": 100}}}) - status: dict[str, Any] = redacted["device"]["deviceStatus"] - assert status["209"] == 100 - assert json.loads(status["10005"]) == { - "sn": REDACTED, - "ssid": REDACTED, - "ip": REDACTED, - "mac": REDACTED, - "rssi": -53, - "oba": {"bom": "A.01"}, + assert redacted == { + "device": { + "deviceStatus": { + "10005": { + "sn": REDACTED, + "ssid": REDACTED, + "ip": REDACTED, + "mac": REDACTED, + "rssi": -53, + "oba": {"bom": "A.01"}, + }, + "209": 100, + } + } } @@ -149,23 +154,22 @@ def test_redact_json_string_in_list() -> None: """JSON strings inside lists are redacted too.""" redacted = redact_device_data({"items": ['{"ip": "192.0.2.1"}', "plain"]}) - assert redacted["items"] == [json.dumps({"ip": REDACTED}), "plain"] + assert redacted == {"items": [{"ip": REDACTED}, "plain"]} @pytest.mark.parametrize( - "value", + ("value", "expected"), [ - "plain text", - "", - '{"RRPhotoPrivacyVersion": "1"}', # Not sensitive, so the formatting is kept - "[1, 2, 3]", + ('{"RRPhotoPrivacyVersion": "1"}', {"RRPhotoPrivacyVersion": "1"}), + ("[1, 2, 3]", [1, 2, 3]), ], ) -def test_redact_keeps_strings_without_sensitive_data(value: str) -> None: - """Strings with nothing to redact are returned unchanged.""" - assert redact_device_data({"value": value}) == {"value": value} +def test_redact_decodes_json_strings(value: str, expected: Any) -> None: + """JSON documents stored as strings are decoded even when there is nothing to redact.""" + assert redact_device_data({"value": value}) == {"value": expected} -def test_redact_unparsable_json_string() -> None: - """A string that looks like JSON but cannot be checked is fully redacted.""" - assert redact_device_data({"value": '{"sn": "TESTSERIAL"'}) == {"value": REDACTED} +@pytest.mark.parametrize("value", ["plain text", "", "{not json"]) +def test_redact_keeps_other_strings(value: str) -> None: + """Strings that are not JSON documents are returned unchanged.""" + assert redact_device_data({"value": value}) == {"value": value} From 1132fb611f20bb446088868bc8b15dc2009653d1 Mon Sep 17 00:00:00 2001 From: infy1995 Date: Sun, 15 Nov 2026 11:18:35 +0100 Subject: [PATCH 3/4] refactor: decode A01 JSON data point values when they arrive Decode data point values that hold a JSON object or array as a string in decode_rpc_response and in the home data status snapshot, instead of in the SND_STATE, PRODUCT_INFO and SCHEDULE_TASK converters. Query responses, pushes and listeners now all get structured values. --- roborock/devices/traits/a01/__init__.py | 18 ++++---- roborock/protocols/a01_protocol.py | 16 ++++++- tests/devices/traits/a01/test_init.py | 59 +++++++++++++++++-------- tests/protocols/test_a01_protocol.py | 27 ++++++++++- 4 files changed, 92 insertions(+), 28 deletions(-) diff --git a/roborock/devices/traits/a01/__init__.py b/roborock/devices/traits/a01/__init__.py index 152b76ece..512e7eb4f 100644 --- a/roborock/devices/traits/a01/__init__.py +++ b/roborock/devices/traits/a01/__init__.py @@ -79,7 +79,7 @@ from roborock.devices.traits.common import TraitUpdateListener from roborock.devices.transport.mqtt_channel import MqttChannel from roborock.exceptions import RoborockException -from roborock.protocols.a01_protocol import decode_rpc_response +from roborock.protocols.a01_protocol import decode_datapoint_value, decode_rpc_response from roborock.roborock_message import ( RoborockDyadDataProtocol, RoborockMessage, @@ -96,11 +96,13 @@ ] -def _decode_dyad_schedule(value: str) -> list[DyadScheduleTask]: - """Decode the schedule data point, which is a JSON list or an empty string.""" +def _decode_dyad_schedule(value: list[dict[str, Any]] | str) -> list[DyadScheduleTask]: + """Decode the schedule data point, a list of tasks or an empty string when there are none.""" if not value: return [] - return [DyadScheduleTask.from_dict(task) for task in json.loads(value)] + if not isinstance(value, list): + raise ValueError(f"Unexpected schedule value: {value!r}") + return [DyadScheduleTask.from_dict(task) for task in value] DYAD_PROTOCOL_ENTRIES: dict[RoborockDyadDataProtocol, Callable] = { @@ -151,8 +153,8 @@ def _decode_dyad_schedule(value: str) -> list[DyadScheduleTask]: RoborockDyadDataProtocol.SELF_CLEAN_CUR_TEMP: lambda val: DyadSelfCleanWaterTemp(val).name, RoborockDyadDataProtocol.DRY_CUR_MODE: lambda val: DyadDryingMode(val).name, RoborockDyadDataProtocol.SCHEDULE_TASK: _decode_dyad_schedule, - RoborockDyadDataProtocol.SND_STATE: lambda val: DyadSndState.from_dict(json.loads(val)), - RoborockDyadDataProtocol.PRODUCT_INFO: lambda val: DyadProductInfo.from_dict(json.loads(val)), + RoborockDyadDataProtocol.SND_STATE: lambda val: DyadSndState.from_dict(val), + RoborockDyadDataProtocol.PRODUCT_INFO: lambda val: DyadProductInfo.from_dict(val), } ZEO_PROTOCOL_ENTRIES: dict[RoborockZeoProtocol, Callable] = { @@ -481,11 +483,11 @@ async def set_value(self, protocol: RoborockZeoProtocol, value: Any) -> dict[Rob def _parse_device_status(device_status: dict | None) -> dict[int, Any] | None: - """Normalize the cloud home data status snapshot to integer datapoint codes.""" + """Normalize the cloud home data status snapshot to integer datapoint codes and decoded values.""" if not device_status: return None try: - return {int(code): value for code, value in device_status.items()} + return {int(code): decode_datapoint_value(value) for code, value in device_status.items()} except (TypeError, ValueError): _LOGGER.debug("Ignoring malformed device status snapshot: %s", device_status) return None diff --git a/roborock/protocols/a01_protocol.py b/roborock/protocols/a01_protocol.py index 27889752d..caeb88f73 100644 --- a/roborock/protocols/a01_protocol.py +++ b/roborock/protocols/a01_protocol.py @@ -55,6 +55,20 @@ def encode_mqtt_payload( ) +def decode_datapoint_value(value: Any) -> Any: + """Decode a data point value that holds a JSON object or array stored as a string. + + Some data points are sent this way, such as the Dyad schedule and product info. + Other values, including strings that are not valid JSON, are returned unchanged. + """ + if not isinstance(value, str) or not value.lstrip().startswith(("{", "[")): + return value + try: + return json.loads(value) + except json.JSONDecodeError: + return value + + def decode_rpc_response(message: RoborockMessage) -> dict[int, Any]: """Decode a V1 RPC_RESPONSE message.""" if not message.payload: @@ -73,7 +87,7 @@ def decode_rpc_response(message: RoborockMessage) -> dict[int, Any]: if not isinstance(datapoints, dict): raise RoborockException(f"Invalid A01 message format: 'dps' should be a dictionary for {message.payload!r}") try: - return {int(key): value for key, value in datapoints.items()} + return {int(key): decode_datapoint_value(value) for key, value in datapoints.items()} except ValueError as err: raise RoborockException( f"Invalid A01 message format: 'dps' key should be an integer for {message.payload!r}" diff --git a/tests/devices/traits/a01/test_init.py b/tests/devices/traits/a01/test_init.py index b6ad85e87..f8db9e91f 100644 --- a/tests/devices/traits/a01/test_init.py +++ b/tests/devices/traits/a01/test_init.py @@ -338,40 +338,63 @@ async def test_create_seeds_values_from_device_status(fake_channel: FakeChannel) ) -async def test_dyad_decodes_json_string_data_points(fake_channel: FakeChannel): - """The device reports product info and voice pack state as JSON strings.""" - api = DyadApi( - fake_channel, # type: ignore[arg-type] - initial_status={ - 10004: '{"sid_in_use":21,"sid_version":3,"location":"de","bom":"A.01","language":"en"}', - 10005: PRODUCT_INFO_JSON, - }, - ) +SND_STATE_JSON = '{"sid_in_use":21,"sid_version":3,"location":"de","bom":"A.01","language":"en"}' - assert api.values[RoborockDyadDataProtocol.SND_STATE].sid_in_use == 21 - product_info = api.values[RoborockDyadDataProtocol.PRODUCT_INFO] + +def assert_json_string_data_points_decoded(values: dict[RoborockDyadDataProtocol, Any]) -> None: + assert values[RoborockDyadDataProtocol.SND_STATE].sid_in_use == 21 + product_info = values[RoborockDyadDataProtocol.PRODUCT_INFO] assert product_info.sn == "TESTSERIAL" assert product_info.posix_timezone == "CET-1CEST,M3.5.0/2,M10.5.0/3" assert product_info.oba == {"bom": "A.01"} +async def test_dyad_decodes_json_string_data_points_from_status_snapshot(fake_channel: FakeChannel): + """The cloud status snapshot holds product info and voice pack state as JSON strings.""" + api = create( + DEFAULT_DYAD_PRODUCT, + fake_channel, # type: ignore[arg-type] + device_status={"10004": SND_STATE_JSON, "10005": PRODUCT_INFO_JSON}, + ) + + assert_json_string_data_points_decoded(api.values) # type: ignore[arg-type] + + +async def test_dyad_decodes_json_string_data_points_from_push(dyad_api: DyadApi, fake_channel: FakeChannel): + """The device pushes product info and voice pack state as JSON strings over MQTT.""" + await dyad_api.start() + fake_channel.notify_subscribers(build_a01_message({10004: SND_STATE_JSON, 10005: PRODUCT_INFO_JSON})) + + assert_json_string_data_points_decoded(dyad_api.values) + + +async def test_dyad_decodes_json_string_data_points_from_query(dyad_api: DyadApi, fake_channel: FakeChannel): + """Query responses over MQTT hold product info and voice pack state as JSON strings.""" + fake_channel.response_queue.append(build_a01_message({10004: SND_STATE_JSON, 10005: PRODUCT_INFO_JSON})) + + result = await dyad_api.query_values([RoborockDyadDataProtocol.SND_STATE, RoborockDyadDataProtocol.PRODUCT_INFO]) + + assert_json_string_data_points_decoded(result) + + @pytest.mark.parametrize("value", ["", "not json", None]) async def test_dyad_invalid_json_string_data_point(fake_channel: FakeChannel, value: str | None): """A malformed JSON data point leaves the value unset.""" - api = DyadApi(fake_channel, initial_status={10005: value}) # type: ignore[arg-type] + api = create(DEFAULT_DYAD_PRODUCT, fake_channel, device_status={"10005": value}) # type: ignore[arg-type] assert api.values == {} async def test_dyad_as_dict_is_plain_data(fake_channel: FakeChannel): """Decoded values are converted to JSON serializable data for diagnostics.""" - api = DyadApi( + api = create( + DEFAULT_DYAD_PRODUCT, fake_channel, # type: ignore[arg-type] - initial_status={ - 201: 3, - 227: 1320, - 10002: '[{"id":1,"on":false,"cron":"15 14 31 10 *","dpAction":{"200":1}}]', - 10005: PRODUCT_INFO_JSON, + device_status={ + "201": 3, + "227": 1320, + "10002": '[{"id":1,"on":false,"cron":"15 14 31 10 *","dpAction":{"200":1}}]', + "10005": PRODUCT_INFO_JSON, }, ) diff --git a/tests/protocols/test_a01_protocol.py b/tests/protocols/test_a01_protocol.py index f60cfdff3..e50f90d80 100644 --- a/tests/protocols/test_a01_protocol.py +++ b/tests/protocols/test_a01_protocol.py @@ -5,7 +5,7 @@ import pytest from Crypto.Cipher import AES -from Crypto.Util.Padding import unpad +from Crypto.Util.Padding import pad, unpad from roborock.exceptions import RoborockException from roborock.protocols.a01_protocol import decode_rpc_response, encode_mqtt_payload @@ -143,6 +143,31 @@ def test_decode_rpc_response_string_keys(): assert result[999] == {"nested": "data"} +def test_decode_rpc_response_decodes_json_string_values(): + """Values that hold a JSON object or array as a string are decoded, other values are kept.""" + payload_data = { + "dps": { + "10005": '{"sn": "TESTSERIAL", "rssi": -53}', + "10002": '[{"id": 1, "on": false}]', + "229": "", + "1": "plain", + "2": "{not json", + "209": 100, + } + } + padded_payload = pad(json.dumps(payload_data).encode("utf-8"), AES.block_size) + message = RoborockMessage(protocol=RoborockMessageProtocol.RPC_RESPONSE, payload=padded_payload) + + assert decode_rpc_response(message) == { + 10005: {"sn": "TESTSERIAL", "rssi": -53}, + 10002: [{"id": 1, "on": False}], + 229: "", + 1: "plain", + 2: "{not json", + 209: 100, + } + + def test_decode_rpc_response_missing_payload(): """Test decoding fails when payload is missing.""" message = RoborockMessage(protocol=RoborockMessageProtocol.RPC_RESPONSE, payload=None) From 6f51b4a6a507f8ef7298c61d253195d53fc9780c Mon Sep 17 00:00:00 2001 From: infy1995 Date: Sun, 15 Nov 2026 14:27:45 +0100 Subject: [PATCH 4/4] refactor: tidy A01 decoding follow-ups Drop the simulator's JSON string query branch, which decode_rpc_response made unreachable, and keep JSON values in the string form the device sends. Make the redact_device_data return type cover lists, type the schedule converter input as wire data, explain why diagnostics has its own JSON decoder, and run the A01 diagnostics test for both Dyad and Zeo. --- roborock/devices/traits/a01/__init__.py | 2 +- roborock/diagnostics.py | 24 ++++++-- roborock/testing/a01_simulator.py | 16 ++---- tests/devices/test_a01_traits.py | 73 +++++++++++++++++-------- 4 files changed, 75 insertions(+), 40 deletions(-) diff --git a/roborock/devices/traits/a01/__init__.py b/roborock/devices/traits/a01/__init__.py index 512e7eb4f..9d2cb47b2 100644 --- a/roborock/devices/traits/a01/__init__.py +++ b/roborock/devices/traits/a01/__init__.py @@ -96,7 +96,7 @@ ] -def _decode_dyad_schedule(value: list[dict[str, Any]] | str) -> list[DyadScheduleTask]: +def _decode_dyad_schedule(value: Any) -> list[DyadScheduleTask]: """Decode the schedule data point, a list of tasks or an empty string when there are none.""" if not value: return [] diff --git a/roborock/diagnostics.py b/roborock/diagnostics.py index 163d82932..e94cbb7f7 100644 --- a/roborock/diagnostics.py +++ b/roborock/diagnostics.py @@ -14,7 +14,7 @@ from collections import Counter from collections.abc import Generator, Mapping from contextlib import contextmanager -from typing import Any, Self, TypeVar, cast +from typing import Any, Self, TypeVar, overload class Diagnostics: @@ -131,6 +131,10 @@ def _decode_json_string(value: str) -> Any: Some devices report structured data this way, such as the A01 product info data point. Any other string is returned unchanged. + + `roborock.protocols.a01_protocol.decode_datapoint_value` does the same for A01 + data points. This module cannot import it: `roborock.util` imports this module, + so the import would be circular. """ if not value.lstrip().startswith(("{", "[")): return value @@ -140,8 +144,20 @@ def _decode_json_string(value: str) -> Any: return value -def redact_device_data(data: T, path: str = "") -> T | dict[str, Any]: - """Redact sensitive data in a dict, decoding JSON documents stored as strings.""" +@overload +def redact_device_data(data: Mapping[str, Any], path: str = "") -> dict[str, Any]: ... + + +@overload +def redact_device_data(data: T, path: str = "") -> T | dict[str, Any] | list[Any]: ... + + +def redact_device_data(data: Any, path: str = "") -> Any: + """Redact sensitive data in a dict or list. + + Strings holding a JSON object or array are decoded and redacted, so a string + input can return a dict or a list. Other values are returned unchanged. + """ if isinstance(data, str): data = _decode_json_string(data) @@ -149,7 +165,7 @@ def redact_device_data(data: T, path: str = "") -> T | dict[str, Any]: return data if isinstance(data, list): - return cast(T, [redact_device_data(item, path) for item in data]) + return [redact_device_data(item, path) for item in data] redacted = {**data} diff --git a/roborock/testing/a01_simulator.py b/roborock/testing/a01_simulator.py index 472d440cd..a246a4d03 100644 --- a/roborock/testing/a01_simulator.py +++ b/roborock/testing/a01_simulator.py @@ -161,17 +161,8 @@ async def _handle_publish(self, message: RoborockMessage, channel: Any) -> None: id_query_key = int(RoborockDyadDataProtocol.ID_QUERY) if query_raw := decoded_dps.get(id_query_key): - query_list: list[Any] = [] - if isinstance(query_raw, str): - try: - parsed = json.loads(query_raw) - if isinstance(parsed, list): - query_list = parsed - except (json.JSONDecodeError, TypeError): - pass - elif isinstance(query_raw, list): - query_list = query_raw - + # decode_rpc_response already decodes the JSON list of requested data points. + query_list: list[Any] = query_raw if isinstance(query_raw, list) else [] for key_val in query_list: key_int = int(_extract_int_value(key_val)) if key_int in self.status: @@ -184,7 +175,8 @@ async def _handle_publish(self, message: RoborockMessage, channel: Any) -> None: # Direct setters: update internal state for received DPS for code, val in decoded_dps.items(): if code != id_query_key: - self.status[code] = val + # Keep JSON values in the string form the device sends. + self.status[code] = json.dumps(val) if isinstance(val, (dict, list)) else val updated_dps[code] = val if updated_dps: diff --git a/tests/devices/test_a01_traits.py b/tests/devices/test_a01_traits.py index b7bb0ca1b..2250911d5 100644 --- a/tests/devices/test_a01_traits.py +++ b/tests/devices/test_a01_traits.py @@ -1,14 +1,18 @@ import json +from dataclasses import replace from unittest.mock import AsyncMock, Mock, patch import pytest -from roborock.data import HomeDataDevice from roborock.devices.device import RoborockDevice from roborock.devices.traits.a01 import DyadApi, ZeoApi, create from roborock.roborock_message import RoborockDyadDataProtocol, RoborockZeoProtocol -from roborock.testing.a01_simulator import DEFAULT_DYAD_PRODUCT -from tests import mock_data +from roborock.testing.a01_simulator import ( + DEFAULT_DYAD_DEVICE_INFO, + DEFAULT_DYAD_PRODUCT, + DEFAULT_ZEO_DEVICE_INFO, + DEFAULT_ZEO_PRODUCT, +) @pytest.fixture @@ -58,27 +62,46 @@ async def test_zeo_query_values(mock_channel): assert result[RoborockZeoProtocol.COUNTDOWN] == 120 -def test_dyad_diagnostic_data_includes_redacted_traits(mock_channel): - """A01 diagnostics report the decoded state without identifiers.""" - device_status = { - "201": 3, - "10005": json.dumps( - { - "sn": "TESTSERIAL", - "ssid": "test-network", - "timezone": "Europe/Warsaw", - "posix_timezone": "CET-1CEST,M3.5.0/2,M10.5.0/3", - "ip": "192.0.2.1", - "mac": "00:00:5e:00:53:01", - "oba": {"bom": "A.01"}, - } - ), +DYAD_PRODUCT_INFO_JSON = json.dumps( + { + "sn": "TESTSERIAL", + "ssid": "test-network", + "timezone": "Europe/Warsaw", + "posix_timezone": "CET-1CEST,M3.5.0/2,M10.5.0/3", + "ip": "192.0.2.1", + "mac": "00:00:5e:00:53:01", + "oba": {"bom": "A.01"}, } +) + + +@pytest.mark.parametrize( + ("product", "device_info", "device_status", "expected_traits"), + [ + ( + DEFAULT_DYAD_PRODUCT, + DEFAULT_DYAD_DEVICE_INFO, + {"201": 3, "10005": DYAD_PRODUCT_INFO_JSON}, + {"status": "charging", "product_info": {"timezone": "Europe/Warsaw"}}, + ), + ( + DEFAULT_ZEO_PRODUCT, + DEFAULT_ZEO_DEVICE_INFO, + {"203": 1, "217": 5}, + {"state": "standby", "countdown": 5}, + ), + ], + ids=["dyad", "zeo"], +) +def test_a01_diagnostic_data_includes_redacted_traits( + mock_channel, product, device_info, device_status, expected_traits +): + """A01 diagnostics report the decoded state without identifiers.""" device = RoborockDevice( - device_info=HomeDataDevice.from_dict({**mock_data.Q10_DEVICE_DATA, "deviceStatus": device_status}), - product=DEFAULT_DYAD_PRODUCT, + device_info=replace(device_info, device_status=device_status), + product=product, channel=AsyncMock(), - trait=create(DEFAULT_DYAD_PRODUCT, mock_channel, device_status=device_status), + trait=create(product, mock_channel, device_status=device_status), ) diagnostics = device.diagnostic_data() @@ -86,5 +109,9 @@ def test_dyad_diagnostic_data_includes_redacted_traits(mock_channel): dumped = json.dumps(diagnostics) for private_value in ("TESTSERIAL", "test-network", "192.0.2.1", "00:00:5e:00:53:01"): assert private_value not in dumped - assert diagnostics["traits"]["status"] == "charging" - assert diagnostics["traits"]["product_info"]["timezone"] == "Europe/Warsaw" + traits = diagnostics["traits"] + for name, expected in expected_traits.items(): + if isinstance(expected, dict): + assert expected.items() <= traits[name].items() + else: + assert traits[name] == expected