12
12
ApiTimeoutError ,
13
13
DeebotError ,
14
14
)
15
+ from deebot_client .util import verify_required_class_variables_exists
15
16
16
17
from .const import PATH_API_IOT_DEVMANAGER , REQUEST_HEADERS , DataType
17
18
from .logging_filter import get_logger
@@ -64,24 +65,18 @@ class Command(ABC):
64
65
"""Abstract command object."""
65
66
66
67
_targets_bot : bool = True
68
+ NAME : str
69
+ DATA_TYPE : DataType
70
+
71
+ def __init_subclass__ (cls ) -> None :
72
+ verify_required_class_variables_exists (cls , ("NAME" , "DATA_TYPE" ))
73
+ return super ().__init_subclass__ ()
67
74
68
75
def __init__ (self , args : dict [str , Any ] | list [Any ] | None = None ) -> None :
69
76
if args is None :
70
77
args = {}
71
78
self ._args = args
72
79
73
- @property # type: ignore[misc]
74
- @classmethod
75
- @abstractmethod
76
- def name (cls ) -> str :
77
- """Command name."""
78
-
79
- @property # type: ignore[misc]
80
- @classmethod
81
- @abstractmethod
82
- def data_type (cls ) -> DataType :
83
- """Data type."""
84
-
85
80
@abstractmethod
86
81
def _get_payload (self ) -> dict [str , Any ] | list [Any ] | str :
87
82
"""Get the payload for the rest call."""
@@ -115,7 +110,7 @@ async def execute(
115
110
except Exception : # pylint: disable=broad-except
116
111
_LOGGER .warning (
117
112
"Could not execute command %s" ,
118
- self .name ,
113
+ self .NAME ,
119
114
exc_info = True ,
120
115
)
121
116
return DeviceCommandResult (device_reached = False )
@@ -132,14 +127,14 @@ async def _execute(
132
127
except ApiTimeoutError :
133
128
_LOGGER .warning (
134
129
"Could not execute command %s: Timeout reached" ,
135
- self .name ,
130
+ self .NAME ,
136
131
)
137
132
return CommandResult (HandlingState .ERROR ), {}
138
133
139
134
result = self .__handle_response (event_bus , response )
140
135
if result .state == HandlingState .ANALYSE :
141
136
_LOGGER .debug (
142
- "ANALYSE: Could not handle command: %s with %s" , self .name , response
137
+ "ANALYSE: Could not handle command: %s with %s" , self .NAME , response
143
138
)
144
139
return (
145
140
CommandResult (
@@ -150,16 +145,16 @@ async def _execute(
150
145
response ,
151
146
)
152
147
if result .state == HandlingState .ERROR :
153
- _LOGGER .warning ("Could not parse %s: %s" , self .name , response )
148
+ _LOGGER .warning ("Could not parse %s: %s" , self .NAME , response )
154
149
return result , response
155
150
156
151
async def _execute_api_request (
157
152
self , authenticator : Authenticator , device_info : ApiDeviceInfo
158
153
) -> dict [str , Any ]:
159
154
payload = {
160
- "cmdName" : self .name ,
155
+ "cmdName" : self .NAME ,
161
156
"payload" : self ._get_payload (),
162
- "payloadType" : self .data_type .value ,
157
+ "payloadType" : self .DATA_TYPE .value ,
163
158
"td" : "q" ,
164
159
"toId" : device_info ["did" ],
165
160
"toRes" : device_info ["resource" ],
@@ -195,7 +190,7 @@ def __handle_response(
195
190
result = self ._handle_response (event_bus , response )
196
191
if result .state == HandlingState .ANALYSE :
197
192
_LOGGER .debug (
198
- "ANALYSE: Could not handle command: %s with %s" , self .name , response
193
+ "ANALYSE: Could not handle command: %s with %s" , self .NAME , response
199
194
)
200
195
return CommandResult (
201
196
HandlingState .ANALYSE_LOGGED ,
@@ -206,7 +201,7 @@ def __handle_response(
206
201
except Exception : # pylint: disable=broad-except
207
202
_LOGGER .warning (
208
203
"Could not parse response for %s: %s" ,
209
- self .name ,
204
+ self .NAME ,
210
205
response ,
211
206
exc_info = True ,
212
207
)
@@ -223,12 +218,12 @@ def _handle_response(
223
218
224
219
def __eq__ (self , obj : object ) -> bool :
225
220
if isinstance (obj , Command ):
226
- return self .name == obj .name and self ._args == obj ._args
221
+ return self .NAME == obj .NAME and self ._args == obj ._args
227
222
228
223
return False
229
224
230
225
def __hash__ (self ) -> int :
231
- return hash (self .name ) + hash (self ._args )
226
+ return hash (self .NAME ) + hash (self ._args )
232
227
233
228
234
229
class CommandWithMessageHandling (Command , Message , ABC ):
@@ -253,24 +248,24 @@ def _handle_response(
253
248
case 4200 :
254
249
# bot offline
255
250
_LOGGER .info (
256
- 'Device is offline. Could not execute command "%s"' , self .name
251
+ 'Device is offline. Could not execute command "%s"' , self .NAME
257
252
)
258
253
event_bus .notify (AvailabilityEvent (available = False ))
259
254
return CommandResult (HandlingState .FAILED )
260
255
case 500 :
261
256
if self ._is_available_check :
262
257
_LOGGER .info (
263
258
'No response received for command "%s" during availability-check.' ,
264
- self .name ,
259
+ self .NAME ,
265
260
)
266
261
else :
267
262
_LOGGER .warning (
268
263
'No response received for command "%s". This can happen if the device has network issues or does not support the command' ,
269
- self .name ,
264
+ self .NAME ,
270
265
)
271
266
return CommandResult (HandlingState .FAILED )
272
267
273
- _LOGGER .warning ('Command "%s" was not successfully.' , self .name )
268
+ _LOGGER .warning ('Command "%s" was not successfully.' , self .NAME )
274
269
return CommandResult (HandlingState .ANALYSE )
275
270
276
271
0 commit comments