File: command_cache.py

package info (click to toggle)
python-roborock 2.49.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,360 kB
  • sloc: python: 11,539; makefile: 17
file content (212 lines) | stat: -rw-r--r-- 7,912 bytes parent folder | download | duplicates (3)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from __future__ import annotations

from collections.abc import Mapping
from dataclasses import dataclass, field
from enum import Enum

from roborock import RoborockCommand

GET_PREFIX = "get_"
SET_PREFIX = ("set_", "change_", "close_")


class CacheableAttribute(str, Enum):
    status = "status"
    consumable = "consumable"
    sound_volume = "sound_volume"
    camera_status = "camera_status"
    carpet_clean_mode = "carpet_clean_mode"
    carpet_mode = "carpet_mode"
    child_lock_status = "child_lock_status"
    collision_avoid_status = "collision_avoid_status"
    customize_clean_mode = "customize_clean_mode"
    custom_mode = "custom_mode"
    dnd_timer = "dnd_timer"
    dust_collection_mode = "dust_collection_mode"
    flow_led_status = "flow_led_status"
    identify_furniture_status = "identify_furniture_status"
    identify_ground_material_status = "identify_ground_material_status"
    led_status = "led_status"
    server_timer = "server_timer"
    smart_wash_params = "smart_wash_params"
    timezone = "timezone"
    valley_electricity_timer = "valley_electricity_timer"
    wash_towel_mode = "wash_towel_mode"


@dataclass
class RoborockAttribute:
    attribute: str
    get_command: RoborockCommand
    add_command: RoborockCommand | None = None
    set_command: RoborockCommand | None = None
    close_command: RoborockCommand | None = None
    additional_change_commands: list[RoborockCommand] = field(default_factory=list)


cache_map: Mapping[CacheableAttribute, RoborockAttribute] = {
    CacheableAttribute.status: RoborockAttribute(
        attribute="status",
        get_command=RoborockCommand.GET_STATUS,
        additional_change_commands=[
            RoborockCommand.SET_WATER_BOX_CUSTOM_MODE,
            RoborockCommand.SET_MOP_MODE,
        ],
    ),
    CacheableAttribute.consumable: RoborockAttribute(
        attribute="consumable",
        get_command=RoborockCommand.GET_CONSUMABLE,
    ),
    CacheableAttribute.sound_volume: RoborockAttribute(
        attribute="sound_volume",
        get_command=RoborockCommand.GET_SOUND_VOLUME,
        set_command=RoborockCommand.CHANGE_SOUND_VOLUME,
    ),
    CacheableAttribute.camera_status: RoborockAttribute(
        attribute="camera_status",
        get_command=RoborockCommand.GET_CAMERA_STATUS,
        set_command=RoborockCommand.SET_CAMERA_STATUS,
    ),
    CacheableAttribute.carpet_clean_mode: RoborockAttribute(
        attribute="carpet_clean_mode",
        get_command=RoborockCommand.GET_CARPET_CLEAN_MODE,
        set_command=RoborockCommand.SET_CARPET_CLEAN_MODE,
    ),
    CacheableAttribute.carpet_mode: RoborockAttribute(
        attribute="carpet_mode",
        get_command=RoborockCommand.GET_CARPET_MODE,
        set_command=RoborockCommand.SET_CARPET_MODE,
    ),
    CacheableAttribute.child_lock_status: RoborockAttribute(
        attribute="child_lock_status",
        get_command=RoborockCommand.GET_CHILD_LOCK_STATUS,
        set_command=RoborockCommand.SET_CHILD_LOCK_STATUS,
    ),
    CacheableAttribute.collision_avoid_status: RoborockAttribute(
        attribute="collision_avoid_status",
        get_command=RoborockCommand.GET_COLLISION_AVOID_STATUS,
        set_command=RoborockCommand.SET_COLLISION_AVOID_STATUS,
    ),
    CacheableAttribute.customize_clean_mode: RoborockAttribute(
        attribute="customize_clean_mode",
        get_command=RoborockCommand.GET_CUSTOMIZE_CLEAN_MODE,
        set_command=RoborockCommand.SET_CUSTOMIZE_CLEAN_MODE,
    ),
    CacheableAttribute.custom_mode: RoborockAttribute(
        attribute="custom_mode",
        get_command=RoborockCommand.GET_CUSTOM_MODE,
        set_command=RoborockCommand.SET_CUSTOM_MODE,
    ),
    CacheableAttribute.dnd_timer: RoborockAttribute(
        attribute="dnd_timer",
        get_command=RoborockCommand.GET_DND_TIMER,
        set_command=RoborockCommand.SET_DND_TIMER,
        close_command=RoborockCommand.CLOSE_DND_TIMER,
    ),
    CacheableAttribute.dust_collection_mode: RoborockAttribute(
        attribute="dust_collection_mode",
        get_command=RoborockCommand.GET_DUST_COLLECTION_MODE,
        set_command=RoborockCommand.SET_DUST_COLLECTION_MODE,
    ),
    CacheableAttribute.flow_led_status: RoborockAttribute(
        attribute="flow_led_status",
        get_command=RoborockCommand.GET_FLOW_LED_STATUS,
        set_command=RoborockCommand.SET_FLOW_LED_STATUS,
    ),
    CacheableAttribute.identify_furniture_status: RoborockAttribute(
        attribute="identify_furniture_status",
        get_command=RoborockCommand.GET_IDENTIFY_FURNITURE_STATUS,
        set_command=RoborockCommand.SET_IDENTIFY_FURNITURE_STATUS,
    ),
    CacheableAttribute.identify_ground_material_status: RoborockAttribute(
        attribute="identify_ground_material_status",
        get_command=RoborockCommand.GET_IDENTIFY_GROUND_MATERIAL_STATUS,
        set_command=RoborockCommand.SET_IDENTIFY_GROUND_MATERIAL_STATUS,
    ),
    CacheableAttribute.led_status: RoborockAttribute(
        attribute="led_status",
        get_command=RoborockCommand.GET_LED_STATUS,
        set_command=RoborockCommand.SET_LED_STATUS,
    ),
    CacheableAttribute.server_timer: RoborockAttribute(
        attribute="server_timer",
        get_command=RoborockCommand.GET_SERVER_TIMER,
        add_command=RoborockCommand.SET_SERVER_TIMER,
        set_command=RoborockCommand.UPD_SERVER_TIMER,
        close_command=RoborockCommand.DEL_SERVER_TIMER,
    ),
    CacheableAttribute.smart_wash_params: RoborockAttribute(
        attribute="smart_wash_params",
        get_command=RoborockCommand.GET_SMART_WASH_PARAMS,
        set_command=RoborockCommand.SET_SMART_WASH_PARAMS,
    ),
    CacheableAttribute.timezone: RoborockAttribute(
        attribute="timezone", get_command=RoborockCommand.GET_TIMEZONE, set_command=RoborockCommand.SET_TIMEZONE
    ),
    CacheableAttribute.valley_electricity_timer: RoborockAttribute(
        attribute="valley_electricity_timer",
        get_command=RoborockCommand.GET_VALLEY_ELECTRICITY_TIMER,
        set_command=RoborockCommand.SET_VALLEY_ELECTRICITY_TIMER,
        close_command=RoborockCommand.CLOSE_VALLEY_ELECTRICITY_TIMER,
    ),
    CacheableAttribute.wash_towel_mode: RoborockAttribute(
        attribute="wash_towel_mode",
        get_command=RoborockCommand.GET_WASH_TOWEL_MODE,
        set_command=RoborockCommand.SET_WASH_TOWEL_MODE,
    ),
}


def get_change_commands(attr: RoborockAttribute) -> list[RoborockCommand]:
    commands = [
        attr.add_command,
        attr.set_command,
        attr.close_command,
        *attr.additional_change_commands,
    ]

    return [command for command in commands if command is not None]


cache_map_by_get_command: dict[RoborockCommand | str, CacheableAttribute] = {
    attribute.get_command: cacheable_attribute for cacheable_attribute, attribute in cache_map.items()
}

cache_map_by_change_command: dict[RoborockCommand | str, CacheableAttribute] = {
    command: cacheable_attribute
    for cacheable_attribute, attribute in cache_map.items()
    for command in get_change_commands(attribute)
}


def get_cache_map():
    return cache_map


class CommandType(Enum):
    OTHER = -1
    GET = 0
    CHANGE = 1


@dataclass
class CacheableAttributeResult:
    attribute: CacheableAttribute
    type: CommandType


def find_cacheable_attribute(method: RoborockCommand | str) -> CacheableAttributeResult | None:
    if method is None:
        return None

    cacheable_attribute = None
    command_type = CommandType.OTHER
    if cacheable_attribute := cache_map_by_get_command.get(method, None):
        command_type = CommandType.GET
    elif cacheable_attribute := cache_map_by_change_command.get(method, None):
        command_type = CommandType.CHANGE

    if cacheable_attribute:
        return CacheableAttributeResult(attribute=CacheableAttribute(cacheable_attribute), type=command_type)
    else:
        return None