11from __future__ import annotations
22
3- import time
43from dataclasses import dataclass
54from enum import Enum
5+ from typing import Mapping
6+
7+ from roborock import RoborockCommand
68
79GET_PREFIX = "get_"
810SET_PREFIX = ("set_" , "change_" , "close_" )
@@ -30,29 +32,108 @@ class CacheableAttribute(str, Enum):
3032 wash_towel_mode = "wash_towel_mode"
3133
3234
33- EVICT_TIME = 60
34-
35-
36- class AttributeCache :
37- _value : dict | None = None
38- last_update : float | None = None
39-
40- def __init__ (self , attribute : str ):
41- self .attribute = attribute
42-
43- @property
44- def value (self ):
45- if self .last_update is None or time .monotonic () - self .last_update > EVICT_TIME :
46- self ._value = None
47- return self ._value
48-
49- def load (self , value : dict ):
50- self ._value = value
51- self .last_update = time .monotonic ()
52- return self ._value
53-
54- def evict (self ):
55- self ._value = None
35+ @dataclass
36+ class RoborockAttribute :
37+ attribute : str
38+ get_command : RoborockCommand
39+ set_command : RoborockCommand
40+
41+
42+ def create_cache_map ():
43+ cache_map : Mapping [CacheableAttribute , RoborockAttribute ] = {
44+ CacheableAttribute .sound_volume : RoborockAttribute (
45+ attribute = "sound_volume" ,
46+ get_command = RoborockCommand .GET_SOUND_VOLUME ,
47+ set_command = RoborockCommand .CHANGE_SOUND_VOLUME ,
48+ ),
49+ CacheableAttribute .camera_status : RoborockAttribute (
50+ attribute = "camera_status" ,
51+ get_command = RoborockCommand .GET_CAMERA_STATUS ,
52+ set_command = RoborockCommand .SET_CAMERA_STATUS ,
53+ ),
54+ CacheableAttribute .carpet_clean_mode : RoborockAttribute (
55+ attribute = "carpet_clean_mode" ,
56+ get_command = RoborockCommand .GET_CARPET_CLEAN_MODE ,
57+ set_command = RoborockCommand .SET_CARPET_CLEAN_MODE ,
58+ ),
59+ CacheableAttribute .carpet_mode : RoborockAttribute (
60+ attribute = "carpet_mode" ,
61+ get_command = RoborockCommand .GET_CARPET_MODE ,
62+ set_command = RoborockCommand .SET_CARPET_MODE ,
63+ ),
64+ CacheableAttribute .child_lock_status : RoborockAttribute (
65+ attribute = "child_lock_status" ,
66+ get_command = RoborockCommand .GET_CHILD_LOCK_STATUS ,
67+ set_command = RoborockCommand .SET_CHILD_LOCK_STATUS ,
68+ ),
69+ CacheableAttribute .collision_avoid_status : RoborockAttribute (
70+ attribute = "collision_avoid_status" ,
71+ get_command = RoborockCommand .GET_COLLISION_AVOID_STATUS ,
72+ set_command = RoborockCommand .SET_COLLISION_AVOID_STATUS ,
73+ ),
74+ CacheableAttribute .customize_clean_mode : RoborockAttribute (
75+ attribute = "customize_clean_mode" ,
76+ get_command = RoborockCommand .GET_CUSTOMIZE_CLEAN_MODE ,
77+ set_command = RoborockCommand .SET_CUSTOMIZE_CLEAN_MODE ,
78+ ),
79+ CacheableAttribute .custom_mode : RoborockAttribute (
80+ attribute = "custom_mode" ,
81+ get_command = RoborockCommand .GET_CUSTOM_MODE ,
82+ set_command = RoborockCommand .SET_CUSTOM_MODE ,
83+ ),
84+ CacheableAttribute .dnd_timer : RoborockAttribute (
85+ attribute = "dnd_timer" , get_command = RoborockCommand .GET_DND_TIMER , set_command = RoborockCommand .SET_DND_TIMER
86+ ),
87+ CacheableAttribute .dust_collection_mode : RoborockAttribute (
88+ attribute = "dust_collection_mode" ,
89+ get_command = RoborockCommand .GET_DUST_COLLECTION_MODE ,
90+ set_command = RoborockCommand .SET_DUST_COLLECTION_MODE ,
91+ ),
92+ CacheableAttribute .flow_led_status : RoborockAttribute (
93+ attribute = "flow_led_status" ,
94+ get_command = RoborockCommand .GET_FLOW_LED_STATUS ,
95+ set_command = RoborockCommand .SET_FLOW_LED_STATUS ,
96+ ),
97+ CacheableAttribute .identify_furniture_status : RoborockAttribute (
98+ attribute = "identify_furniture_status" ,
99+ get_command = RoborockCommand .GET_IDENTIFY_FURNITURE_STATUS ,
100+ set_command = RoborockCommand .SET_IDENTIFY_FURNITURE_STATUS ,
101+ ),
102+ CacheableAttribute .identify_ground_material_status : RoborockAttribute (
103+ attribute = "identify_ground_material_status" ,
104+ get_command = RoborockCommand .GET_IDENTIFY_GROUND_MATERIAL_STATUS ,
105+ set_command = RoborockCommand .SET_IDENTIFY_GROUND_MATERIAL_STATUS ,
106+ ),
107+ CacheableAttribute .led_status : RoborockAttribute (
108+ attribute = "led_status" ,
109+ get_command = RoborockCommand .GET_LED_STATUS ,
110+ set_command = RoborockCommand .SET_LED_STATUS ,
111+ ),
112+ CacheableAttribute .server_timer : RoborockAttribute (
113+ attribute = "server_timer" ,
114+ get_command = RoborockCommand .GET_SERVER_TIMER ,
115+ set_command = RoborockCommand .SET_SERVER_TIMER ,
116+ ),
117+ CacheableAttribute .smart_wash_params : RoborockAttribute (
118+ attribute = "smart_wash_params" ,
119+ get_command = RoborockCommand .GET_SMART_WASH_PARAMS ,
120+ set_command = RoborockCommand .SET_SMART_WASH_PARAMS ,
121+ ),
122+ CacheableAttribute .timezone : RoborockAttribute (
123+ attribute = "timezone" , get_command = RoborockCommand .GET_TIMEZONE , set_command = RoborockCommand .SET_TIMEZONE
124+ ),
125+ CacheableAttribute .valley_electricity_timer : RoborockAttribute (
126+ attribute = "valley_electricity_timer" ,
127+ get_command = RoborockCommand .GET_VALLEY_ELECTRICITY_TIMER ,
128+ set_command = RoborockCommand .SET_VALLEY_ELECTRICITY_TIMER ,
129+ ),
130+ CacheableAttribute .wash_towel_mode : RoborockAttribute (
131+ attribute = "wash_towel_mode" ,
132+ get_command = RoborockCommand .GET_WASH_TOWEL_MODE ,
133+ set_command = RoborockCommand .SET_WASH_TOWEL_MODE ,
134+ ),
135+ }
136+ return cache_map
56137
57138
58139class CommandType (Enum ):
@@ -78,9 +159,7 @@ def parse_method(method: str):
78159 for prefix in SET_PREFIX :
79160 attribute = attribute .removeprefix (prefix )
80161 command_type = CommandType .SET
81- try :
82- cacheable_attribute = CacheableAttribute (attribute )
83- return ParserCommand (type = command_type , attribute = cacheable_attribute )
84- except ValueError :
85- pass
162+ cacheable_attribute = next ((attr for attr in CacheableAttribute if attr == attribute ), None )
163+ if cacheable_attribute :
164+ return ParserCommand (type = command_type , attribute = CacheableAttribute (cacheable_attribute ))
86165 return None
0 commit comments