@@ -59,7 +59,7 @@ def __init__(self, *args, **kwargs):
59
59
def reply_once_to (self , request , responses ):
60
60
called_future = asyncio .get_running_loop ().create_future ()
61
61
62
- async def callback ():
62
+ async def callback (request ):
63
63
if callback .called :
64
64
return
65
65
@@ -68,27 +68,35 @@ async def callback():
68
68
for response in responses :
69
69
await asyncio .sleep (0.1 )
70
70
LOGGER .debug ("Replying to %s with %s" , request , response )
71
- self .send (response )
71
+
72
+ try :
73
+ self .send (response (request ))
74
+ except TypeError :
75
+ self .send (response )
72
76
73
77
called_future .set_result (True )
74
78
75
79
callback .called = False
76
- self .callback_for_response (request , lambda _ : asyncio .create_task (callback ()))
80
+ self .callback_for_response (request , lambda r : asyncio .create_task (callback (r )))
77
81
78
82
return called_future
79
83
80
84
def reply_to (self , request , responses ):
81
- async def callback ():
85
+ async def callback (request ):
82
86
callback .call_count += 1
83
87
84
88
for response in responses :
85
89
await asyncio .sleep (0.1 )
86
90
LOGGER .debug ("Replying to %s with %s" , request , response )
87
- self .send (response )
91
+
92
+ try :
93
+ self .send (response (request ))
94
+ except TypeError :
95
+ self .send (response )
88
96
89
97
callback .call_count = 0
90
98
91
- self .callback_for_response (request , lambda _ : asyncio .create_task (callback ()))
99
+ self .callback_for_response (request , lambda r : asyncio .create_task (callback (r )))
92
100
93
101
return callback
94
102
@@ -844,3 +852,75 @@ async def test_force_remove(application, mocker):
844
852
# Make sure the device is gone once we remove it
845
853
with pytest .raises (KeyError ):
846
854
app .get_device (nwk = device .nwk )
855
+
856
+
857
+ @pytest_mark_asyncio_timeout (seconds = 3 )
858
+ async def test_auto_form_unnecessary (application , mocker ):
859
+ app , znp_server = application
860
+
861
+ # b"\x55" means that Z-Stack is already configured?
862
+ read_zstack_configured = znp_server .reply_once_to (
863
+ request = c .Sys .OSALNVRead .Req (Id = NwkNvIds .HAS_CONFIGURED_ZSTACK3 , Offset = 0 ),
864
+ responses = [c .Sys .OSALNVRead .Rsp (Status = t .Status .Success , Value = b"\x55 " )],
865
+ )
866
+
867
+ mocker .patch .object (app , "form_network" )
868
+
869
+ await app .startup (auto_form = True )
870
+
871
+ await read_zstack_configured
872
+ assert app .form_network .call_count == 0
873
+
874
+
875
+ @pytest_mark_asyncio_timeout (seconds = 3 )
876
+ async def test_auto_form_necessary (application , mocker ):
877
+ app , znp_server = application
878
+ nvram = {}
879
+
880
+ mocker .patch .object (app , "update_network" , new = CoroutineMock ())
881
+ mocker .patch .object (app , "_reset" , new = CoroutineMock ())
882
+
883
+ def nvram_writer (req ):
884
+ if req .Id in nvram :
885
+ raise ValueError ("Unexpected overwrite" )
886
+
887
+ nvram [req .Id ] = req .Value
888
+
889
+ return c .Sys .OSALNVWrite .Rsp (Status = t .Status .Success )
890
+
891
+ read_zstack_configured = znp_server .reply_once_to (
892
+ request = c .Sys .OSALNVRead .Req (Id = NwkNvIds .HAS_CONFIGURED_ZSTACK3 , Offset = 0 ),
893
+ responses = [c .Sys .OSALNVRead .Rsp (Status = t .Status .Success , Value = b"\x00 " )],
894
+ )
895
+
896
+ znp_server .reply_to (
897
+ request = c .Sys .OSALNVWrite .Req (Offset = 0 , partial = True ), responses = [nvram_writer ]
898
+ )
899
+
900
+ znp_server .reply_to (
901
+ request = c .AppConfig .BDBStartCommissioning .Req (
902
+ Mode = c .app_config .BDBCommissioningMode .NwkFormation
903
+ ),
904
+ responses = [
905
+ c .AppConfig .BDBStartCommissioning .Rsp (Status = t .Status .Success ),
906
+ c .ZDO .StateChangeInd .Callback (State = t .DeviceState .StartedAsCoordinator ),
907
+ ],
908
+ )
909
+
910
+ znp_server .reply_to (
911
+ request = c .AppConfig .BDBStartCommissioning .Req (
912
+ Mode = c .app_config .BDBCommissioningMode .NwkSteering
913
+ ),
914
+ responses = [c .AppConfig .BDBStartCommissioning .Rsp (Status = t .Status .Success )],
915
+ )
916
+
917
+ await app .startup (auto_form = True )
918
+
919
+ await read_zstack_configured
920
+
921
+ assert app .update_network .call_count == 1
922
+ assert app ._reset .call_count == 2
923
+
924
+ assert nvram [NwkNvIds .STARTUP_OPTION ] == t .StartupOptions .ClearState .serialize ()
925
+ assert nvram [NwkNvIds .LOGICAL_TYPE ] == t .DeviceLogicalType .Coordinator .serialize ()
926
+ assert nvram [NwkNvIds .ZDO_DIRECT_CB ] == t .Bool (True ).serialize ()
0 commit comments