18
18
RESET_TIMEOUT = 5
19
19
20
20
21
- class Gateway (asyncio .Protocol ):
22
- def __init__ (self , application , connected_future = None , connection_done_future = None ):
23
- self ._application = application
21
+ class Gateway (zigpy .serial .SerialProtocol ):
22
+ def __init__ (self , api , connection_done_future = None ):
23
+ super ().__init__ ()
24
+ self ._api = api
24
25
25
26
self ._reset_future = None
26
27
self ._startup_reset_future = None
27
- self ._connected_future = connected_future
28
28
self ._connection_done_future = connection_done_future
29
29
30
- self ._transport = None
31
-
32
- def close (self ):
33
- self ._transport .close ()
34
-
35
- def connection_made (self , transport ):
36
- """Callback when the uart is connected"""
37
- self ._transport = transport
38
- if self ._connected_future is not None :
39
- self ._connected_future .set_result (True )
40
-
41
30
async def send_data (self , data : bytes ) -> None :
42
31
await self ._transport .send_data (data )
43
32
44
33
def data_received (self , data ):
45
34
"""Callback when there is data received from the uart"""
46
- self ._application .frame_received (data )
35
+
36
+ # We intentionally do not call `SerialProtocol.data_received`
37
+ self ._api .frame_received (data )
47
38
48
39
def reset_received (self , code : t .NcpResetCode ) -> None :
49
40
"""Reset acknowledgement frame receive handler"""
50
- # not a reset we've requested. Signal application reset
41
+ # not a reset we've requested. Signal api reset
51
42
if code is not t .NcpResetCode .RESET_SOFTWARE :
52
- self ._application .enter_failed_state (code )
43
+ self ._api .enter_failed_state (code )
53
44
return
54
45
55
46
if self ._reset_future and not self ._reset_future .done ():
@@ -61,7 +52,7 @@ def reset_received(self, code: t.NcpResetCode) -> None:
61
52
62
53
def error_received (self , code : t .NcpResetCode ) -> None :
63
54
"""Error frame receive handler."""
64
- self ._application .enter_failed_state (code )
55
+ self ._api .enter_failed_state (code )
65
56
66
57
async def wait_for_startup_reset (self ) -> None :
67
58
"""Wait for the first reset frame on startup."""
@@ -77,12 +68,9 @@ def _reset_cleanup(self, future):
77
68
"""Delete reset future."""
78
69
self ._reset_future = None
79
70
80
- def eof_received (self ):
81
- """Server gracefully closed its side of the connection."""
82
- self .connection_lost (ConnectionResetError ("Remote server closed connection" ))
83
-
84
71
def connection_lost (self , exc ):
85
72
"""Port was closed unexpectedly."""
73
+ super ().connection_lost (exc )
86
74
87
75
LOGGER .debug ("Connection lost: %r" , exc )
88
76
reason = exc or ConnectionResetError ("Remote server closed connection" )
@@ -102,12 +90,7 @@ def connection_lost(self, exc):
102
90
self ._reset_future .set_exception (reason )
103
91
self ._reset_future = None
104
92
105
- if exc is None :
106
- LOGGER .debug ("Closed serial connection" )
107
- return
108
-
109
- LOGGER .error ("Lost serial connection: %r" , exc )
110
- self ._application .connection_lost (exc )
93
+ self ._api .connection_lost (exc )
111
94
112
95
async def reset (self ):
113
96
"""Send a reset frame and init internal state."""
@@ -126,13 +109,12 @@ async def reset(self):
126
109
return await self ._reset_future
127
110
128
111
129
- async def _connect (config , application ):
112
+ async def _connect (config , api ):
130
113
loop = asyncio .get_event_loop ()
131
114
132
- connection_future = loop .create_future ()
133
115
connection_done_future = loop .create_future ()
134
116
135
- gateway = Gateway (application , connection_future , connection_done_future )
117
+ gateway = Gateway (api , connection_done_future )
136
118
protocol = AshProtocol (gateway )
137
119
138
120
if config [zigpy .config .CONF_DEVICE_FLOW_CONTROL ] is None :
@@ -149,25 +131,25 @@ async def _connect(config, application):
149
131
rtscts = rtscts ,
150
132
)
151
133
152
- await connection_future
134
+ await gateway . wait_until_connected ()
153
135
154
136
thread_safe_protocol = ThreadsafeProxy (gateway , loop )
155
137
return thread_safe_protocol , connection_done_future
156
138
157
139
158
- async def connect (config , application , use_thread = True ):
140
+ async def connect (config , api , use_thread = True ):
159
141
if use_thread :
160
- application = ThreadsafeProxy (application , asyncio .get_event_loop ())
142
+ api = ThreadsafeProxy (api , asyncio .get_event_loop ())
161
143
thread = EventLoopThread ()
162
144
await thread .start ()
163
145
try :
164
146
protocol , connection_done = await thread .run_coroutine_threadsafe (
165
- _connect (config , application )
147
+ _connect (config , api )
166
148
)
167
149
except Exception :
168
150
thread .force_stop ()
169
151
raise
170
152
connection_done .add_done_callback (lambda _ : thread .force_stop ())
171
153
else :
172
- protocol , _ = await _connect (config , application )
154
+ protocol , _ = await _connect (config , api )
173
155
return protocol
0 commit comments