-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathdevice_features.py
More file actions
54 lines (46 loc) · 2.47 KB
/
device_features.py
File metadata and controls
54 lines (46 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from dataclasses import fields
from roborock.data import AppInitStatus, RoborockProductNickname
from roborock.device_features import DeviceFeatures
from roborock.devices.cache import DeviceCache
from roborock.devices.traits.v1 import common
from roborock.roborock_typing import RoborockCommand
class DeviceFeaturesTrait(DeviceFeatures, common.V1TraitMixin):
"""Trait for managing Do Not Disturb (DND) settings on Roborock devices."""
command = RoborockCommand.APP_GET_INIT_STATUS
def __init__(self, product_nickname: RoborockProductNickname, device_cache: DeviceCache) -> None: # pylint: disable=super-init-not-called
"""Initialize MapContentTrait."""
self._nickname = product_nickname
self._device_cache = device_cache
# All fields of DeviceFeatures are required. Initialize them to False
# so we have some known state.
for field in fields(self):
setattr(self, field.name, False)
async def refresh(self) -> None:
"""Refresh the contents of this trait.
This will use cached device features if available since they do not
change often and this avoids unnecessary RPC calls. This would only
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:
self._update_trait_values(cache_data.device_features)
return
# Save cached device features
await super().refresh()
# Create a pure DeviceFeatures instance without runtime objects
device_features_data = DeviceFeatures()
for field in fields(DeviceFeatures):
setattr(device_features_data, field.name, getattr(self, field.name))
cache_data.device_features = device_features_data
await self._device_cache.set(cache_data)
def _parse_response(self, response: common.V1ResponseData) -> DeviceFeatures:
"""Parse the response from the device into a MapContentTrait instance."""
if not isinstance(response, list):
raise ValueError(f"Unexpected AppInitStatus response format: {type(response)}")
app_status = AppInitStatus.from_dict(response[0])
return DeviceFeatures.from_feature_flags(
new_feature_info=app_status.new_feature_info,
new_feature_info_str=app_status.new_feature_info_str,
feature_info=app_status.feature_info,
product_nickname=self._nickname,
)