@@ -148,13 +148,41 @@ class TuyaDPType(t.enum8):
148
148
BITMAP = 0x05
149
149
150
150
151
- class TuyaData ( t . Struct ) :
151
+ class TuyaData :
152
152
"""Tuya Data type."""
153
153
154
154
dp_type : TuyaDPType
155
155
function : t .uint8_t
156
156
raw : t .LVBytes
157
157
158
+ def __init__ (
159
+ self ,
160
+ value : TuyaDPType | None = None ,
161
+ function : t .uint8_t = t .uint8_t (0 ),
162
+ raw : bytes | None = None ,
163
+ ):
164
+ """Convert from a zigpy typed value to a tuya data payload."""
165
+ self .dp_type = None
166
+ self .function = function
167
+ self .raw = raw
168
+
169
+ if value is None :
170
+ return
171
+ elif isinstance (value , (t .bitmap8 , t .bitmap16 , t .bitmap32 )):
172
+ self .dp_type = TuyaDPType .BITMAP
173
+ elif isinstance (value , (bool , t .Bool )):
174
+ self .dp_type = TuyaDPType .BOOL
175
+ elif isinstance (value , enum .Enum ):
176
+ self .dp_type = TuyaDPType .ENUM
177
+ elif isinstance (value , int ):
178
+ self .dp_type = TuyaDPType .VALUE
179
+ elif isinstance (value , str ):
180
+ self .dp_type = TuyaDPType .STRING
181
+ else :
182
+ self .dp_type = TuyaDPType .RAW
183
+
184
+ self .payload = value
185
+
158
186
@property
159
187
def payload (
160
188
self ,
@@ -208,30 +236,28 @@ def payload(self, value):
208
236
else :
209
237
raise ValueError (f"Unknown { self .dp_type } datapoint type" )
210
238
211
- def __new__ (cls , * args , ** kwargs ):
212
- """Disable copy constructor."""
213
- return super ().__new__ (cls )
239
+ def serialize (self ) -> bytes :
240
+ """Serialize Tuya data."""
241
+ return (
242
+ self .dp_type .serialize ()
243
+ + self .function .serialize ()
244
+ + bytes ([len (self .raw )])
245
+ + self .raw
246
+ )
214
247
215
- def __init__ (self , value = None , function = 0 , * args , ** kwargs ):
216
- """Convert from a zigpy typed value to a tuya data payload."""
217
- self .function = function
248
+ @classmethod
249
+ def deserialize (cls , data : bytes ) -> tuple [TuyaData , bytes ]:
250
+ """Deserialize Tuya data."""
251
+ dp_type , data = TuyaDPType .deserialize (data )
252
+ function , data = t .uint8_t .deserialize (data )
253
+ raw , data = t .LVBytes .deserialize (data )
218
254
219
- if value is None :
220
- return
221
- elif isinstance (value , (t .bitmap8 , t .bitmap16 , t .bitmap32 )):
222
- self .dp_type = TuyaDPType .BITMAP
223
- elif isinstance (value , (bool , t .Bool )):
224
- self .dp_type = TuyaDPType .BOOL
225
- elif isinstance (value , enum .Enum ): # type: ignore
226
- self .dp_type = TuyaDPType .ENUM
227
- elif isinstance (value , int ):
228
- self .dp_type = TuyaDPType .VALUE
229
- elif isinstance (value , str ):
230
- self .dp_type = TuyaDPType .STRING
231
- else :
232
- self .dp_type = TuyaDPType .RAW
255
+ instance = cls ()
256
+ instance .dp_type = dp_type
257
+ instance .function = function
258
+ instance .raw = raw
233
259
234
- self . payload = value
260
+ return instance , data
235
261
236
262
237
263
class Data (t .List , item_type = t .uint8_t ):
@@ -1584,7 +1610,7 @@ def handle_cluster_request(
1584
1610
self .send_default_rsp (
1585
1611
hdr , status = foundation .Status .UNSUP_CLUSTER_COMMAND
1586
1612
)
1587
- return
1613
+ return
1588
1614
1589
1615
try :
1590
1616
status = getattr (self , handler_name )(* args )
0 commit comments