9
9
from pydantic import Field
10
10
from zigpy .types .named import EUI64
11
11
12
- from zha .websocket .const import GROUPS , APICommands
12
+ from zha .websocket .const import DEVICE , DEVICES , GROUPS , APICommands
13
13
from zha .websocket .server .api import decorators , register_api_command
14
14
from zha .websocket .server .api .model import (
15
15
GetApplicationStateResponse ,
@@ -101,16 +101,16 @@ async def get_devices(
101
101
) -> None :
102
102
"""Get Zigbee devices."""
103
103
try :
104
- response = GetDevicesResponse (
105
- success = True ,
106
- devices = {
107
- ieee : device .extended_device_info
108
- for ieee , device in gateway .devices .items ()
104
+ client .send_result_success (
105
+ command ,
106
+ data = {
107
+ DEVICES : {
108
+ ieee : device .extended_device_info
109
+ for ieee , device in gateway .devices .items ()
110
+ }
109
111
},
110
- message_id = command . message_id ,
112
+ response_type = GetDevicesResponse ,
111
113
)
112
- _LOGGER .info ("response: %s" , response )
113
- client .send_result_success (command , response )
114
114
except Exception as e :
115
115
_LOGGER .exception ("Error getting devices" , exc_info = e )
116
116
client .send_result_error (command , "Error getting devices" , str (e ))
@@ -154,12 +154,7 @@ async def get_groups(
154
154
) # maybe we should change the group_id type...
155
155
_LOGGER .info ("groups: %s" , groups )
156
156
client .send_result_success (
157
- command ,
158
- GroupsResponse (
159
- ** command .model_dump (exclude = "model_class_name" ),
160
- groups = groups ,
161
- success = True ,
162
- ),
157
+ command , data = {GROUPS : groups }, response_type = GroupsResponse
163
158
)
164
159
165
160
@@ -179,13 +174,7 @@ async def permit_joining(
179
174
"""Permit joining devices to the Zigbee network."""
180
175
# TODO add permit with code support
181
176
await gateway .application_controller .permit (command .duration , command .ieee )
182
- response = PermitJoiningResponse (
183
- ** command .model_dump (exclude = "model_class_name" ),
184
- success = True ,
185
- duration = command .duration ,
186
- ieee = command .ieee ,
187
- )
188
- client .send_result_success (command , response )
177
+ client .send_result_success (command , response_type = PermitJoiningResponse )
189
178
190
179
191
180
class RemoveDeviceCommand (WebSocketCommand ):
@@ -256,22 +245,22 @@ async def read_cluster_attributes(
256
245
attributes , allow_cache = False , only_cache = False , manufacturer = manufacturer
257
246
)
258
247
259
- response = ReadClusterAttributesResponse (
260
- message_id = command .message_id ,
261
- success = True ,
262
- device = device .extended_device_info ,
263
- cluster = {
248
+ data = {
249
+ DEVICE : device .extended_device_info ,
250
+ "cluster" : {
264
251
"id" : cluster .cluster_id ,
265
252
"name" : cluster .name ,
266
253
"type" : cluster .cluster_type ,
267
254
"endpoint_id" : cluster .endpoint .endpoint_id ,
268
255
"endpoint_attribute" : cluster .ep_attribute ,
269
256
},
270
- manufacturer_code = manufacturer ,
271
- succeeded = success ,
272
- failed = failure ,
257
+ "succeeded" : success ,
258
+ "failed" : failure ,
259
+ }
260
+
261
+ client .send_result_success (
262
+ command , data = data , response_type = ReadClusterAttributesResponse
273
263
)
274
- client .send_result_success (command , response )
275
264
276
265
277
266
class WriteClusterAttributeCommand (WebSocketCommand ):
@@ -332,24 +321,24 @@ async def write_cluster_attribute(
332
321
manufacturer = manufacturer ,
333
322
)
334
323
335
- api_response = WriteClusterAttributeResponse (
336
- message_id = command .message_id ,
337
- success = True ,
338
- device = device .extended_device_info ,
339
- cluster = {
324
+ data = {
325
+ DEVICE : device .extended_device_info ,
326
+ "cluster" : {
340
327
"id" : cluster .cluster_id ,
341
328
"name" : cluster .name ,
342
329
"type" : cluster .cluster_type ,
343
330
"endpoint_id" : cluster .endpoint .endpoint_id ,
344
331
"endpoint_attribute" : cluster .ep_attribute ,
345
332
},
346
- manufacturer_code = manufacturer ,
347
- response = {
333
+ "response" : {
348
334
"attribute" : attribute ,
349
335
"status" : response [0 ][0 ].status .name , # type: ignore
350
336
}, # TODO there has to be a better way to do this
337
+ }
338
+
339
+ client .send_result_success (
340
+ command , data = data , response_type = WriteClusterAttributeResponse
351
341
)
352
- client .send_result_success (command , api_response )
353
342
354
343
355
344
class CreateGroupCommand (WebSocketCommand ):
@@ -371,12 +360,9 @@ async def create_group(
371
360
members = command .members
372
361
group_id = command .group_id
373
362
group : Group = await gateway .async_create_zigpy_group (group_name , members , group_id )
374
- response = UpdateGroupResponse (
375
- ** command .model_dump (exclude = "model_class_name" ),
376
- group = group .info_object ,
377
- success = True ,
363
+ client .send_result_success (
364
+ command , data = {GROUP : group .info_object }, response_type = UpdateGroupResponse
378
365
)
379
- client .send_result_success (command , response )
380
366
381
367
382
368
class RemoveGroupsCommand (WebSocketCommand ):
@@ -405,7 +391,9 @@ async def remove_groups(
405
391
for group_id , group in gateway .groups .items ():
406
392
groups [int (group_id )] = group .info_object
407
393
_LOGGER .info ("groups: %s" , groups )
408
- client .send_result_success (command , {GROUPS : groups })
394
+ client .send_result_success (
395
+ command , data = {GROUPS : groups }, response_type = GroupsResponse
396
+ )
409
397
410
398
411
399
class AddGroupMembersCommand (WebSocketCommand ):
@@ -434,12 +422,9 @@ async def add_group_members(
434
422
if not group :
435
423
client .send_result_error (command , "G1" , "ZHA Group not found" )
436
424
return
437
- response = UpdateGroupResponse (
438
- ** command .model_dump (exclude = "model_class_name" ),
439
- group = group .info_object ,
440
- success = True ,
425
+ client .send_result_success (
426
+ command , data = {GROUP : group .info_object }, response_type = UpdateGroupResponse
441
427
)
442
- client .send_result_success (command , response )
443
428
444
429
445
430
class RemoveGroupMembersCommand (AddGroupMembersCommand ):
@@ -466,12 +451,9 @@ async def remove_group_members(
466
451
if not group :
467
452
client .send_result_error (command , "G1" , "ZHA Group not found" )
468
453
return
469
- response = UpdateGroupResponse (
470
- ** command .model_dump (exclude = "model_class_name" ),
471
- group = group .info_object ,
472
- success = True ,
454
+ client .send_result_success (
455
+ command , data = {GROUP : group .info_object }, response_type = UpdateGroupResponse
473
456
)
474
- client .send_result_success (command , response )
475
457
476
458
477
459
class StopServerCommand (WebSocketCommand ):
@@ -505,10 +487,9 @@ async def get_application_state(
505
487
) -> None :
506
488
"""Get the application state."""
507
489
state = gateway .application_controller .state
508
- response = GetApplicationStateResponse (
509
- success = True , message_id = command . message_id , state = state
490
+ client . send_result_success (
491
+ command , data = { "state" : state }, response_type = GetApplicationStateResponse
510
492
)
511
- client .send_result_success (command , data = response )
512
493
513
494
514
495
def load_api (gateway : WebSocketServerGateway ) -> None :
0 commit comments