From e5289e9bb1b7c693352b64d62e6ff5766d4bd21d Mon Sep 17 00:00:00 2001 From: Harry Coureau <183213654+hCoureau@users.noreply.github.com> Date: Sat, 5 Dec 2026 00:52:10 +0000 Subject: [PATCH] fix(v1): rediscover device features when the pickled cache is stale FileCache pickles DeviceFeatures, and unpickling skips __init__ and DeviceFeatures.from_dict, so features cached by an older version lack any field added since. DeviceFeaturesTrait.refresh merged those as None, which makes discovery raise "Device feature ... is unknown" as soon as a trait gates on a new field. Treat an incomplete cached object as a cache miss, matching what from_dict already does for dict-based caches. Co-Authored-By: Claude Opus 5.5 --- roborock/devices/traits/v1/device_features.py | 6 ++-- tests/test_supported_features.py | 31 ++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/roborock/devices/traits/v1/device_features.py b/roborock/devices/traits/v1/device_features.py index 4a862a545..ad9752ec4 100644 --- a/roborock/devices/traits/v1/device_features.py +++ b/roborock/devices/traits/v1/device_features.py @@ -103,8 +103,10 @@ async def refresh(self) -> None: ever change with a firmware update, so caching is appropriate. """ cache_data = await self._device_cache.get() - if cache_data.device_features is not None: - common.merge_trait_values(self, cache_data.device_features) + cached = cache_data.device_features + # Features unpickled from an older version lack newer fields; rediscover rather than merge them as None. + if cached is not None and all(hasattr(cached, field.name) for field in fields(DeviceFeatures)): + common.merge_trait_values(self, cached) return # Save cached device features await super().refresh() diff --git a/tests/test_supported_features.py b/tests/test_supported_features.py index 4eaaa4193..fc4118b24 100644 --- a/tests/test_supported_features.py +++ b/tests/test_supported_features.py @@ -1,4 +1,5 @@ import logging +import pickle from dataclasses import asdict, fields from unittest.mock import AsyncMock @@ -16,7 +17,7 @@ is_valid_dock, is_wash_n_fill_dock, ) -from roborock.devices.cache import CacheData, DeviceCache, InMemoryCache +from roborock.devices.cache import CacheData, DeviceCache, DeviceCacheData, InMemoryCache from roborock.devices.traits.v1.device_features import DeviceFeaturesTrait from roborock.roborock_typing import RoborockCommand from tests import mock_data @@ -489,6 +490,34 @@ async def test_from_dict_rejects_legacy_missing_capabilities(caplog: pytest.LogC assert (await device_cache.get()).device_features is trait +async def test_refresh_rejects_pickled_features_missing_capabilities() -> None: + """Test features unpickled from an older version are rediscovered, not merged with missing values as None.""" + device_features = DeviceFeatures.from_feature_flags( + new_feature_info=262144, + new_feature_info_str="", + feature_info=[], + product_nickname=None, + ) + # Unpickling skips __init__, so fields added since the cache was written are absent from the instance. + del device_features.is_ai_recognition_obstacle_supported + legacy_features = pickle.loads(pickle.dumps(device_features)) + + cache = InMemoryCache() + await cache.set(CacheData(device_info={"legacy-device": DeviceCacheData(device_features=legacy_features)})) + device_cache = DeviceCache("legacy-device", cache) + product = HomeDataProduct.from_dict(mock_data.PRODUCTS["home_data_product_a27.json"]) + trait = DeviceFeaturesTrait(product, device_cache) + rpc_channel = AsyncMock() + rpc_channel.send_command.return_value = [mock_data.APP_GET_INIT_STATUS] + trait._rpc_channel = rpc_channel + + await trait.refresh() + + rpc_channel.send_command.assert_awaited_once_with(RoborockCommand.APP_GET_INIT_STATUS) + assert trait.is_ai_recognition_obstacle_supported is True + assert (await device_cache.get()).device_features is trait + + @pytest.mark.parametrize( ("dock_type", "is_collectable", "is_washable"), [