1818RESET_TIMEOUT = 5
1919
2020
21- class Gateway (asyncio .Protocol ):
22- FLAG = b"\x7E " # Marks end of frame
23- ESCAPE = b"\x7D "
24- XON = b"\x11 " # Resume transmission
25- XOFF = b"\x13 " # Stop transmission
26- SUBSTITUTE = b"\x18 "
27- CANCEL = b"\x1A " # Terminates a frame in progress
28- STUFF = 0x20
29- RANDOMIZE_START = 0x42
30- RANDOMIZE_SEQ = 0xB8
31-
32- RESERVED = FLAG + ESCAPE + XON + XOFF + SUBSTITUTE + CANCEL
33-
34- class Terminator :
35- pass
36-
37- def __init__ (self , application , connected_future = None , connection_done_future = None ):
21+ class Gateway (zigpy .serial .SerialProtocol ):
22+ def __init__ (self , application , connection_done_future = None ):
23+ super ().__init__ ()
3824 self ._application = application
3925
4026 self ._reset_future = None
4127 self ._startup_reset_future = None
42- self ._connected_future = connected_future
4328 self ._connection_done_future = connection_done_future
4429
45- self ._transport = None
46-
47- def close (self ):
48- self ._transport .close ()
49-
50- def connection_made (self , transport ):
51- """Callback when the uart is connected"""
52- self ._transport = transport
53- if self ._connected_future is not None :
54- self ._connected_future .set_result (True )
55-
5630 async def send_data (self , data : bytes ) -> None :
5731 await self ._transport .send_data (data )
5832
@@ -92,12 +66,9 @@ def _reset_cleanup(self, future):
9266 """Delete reset future."""
9367 self ._reset_future = None
9468
95- def eof_received (self ):
96- """Server gracefully closed its side of the connection."""
97- self .connection_lost (ConnectionResetError ("Remote server closed connection" ))
98-
9969 def connection_lost (self , exc ):
10070 """Port was closed unexpectedly."""
71+ super ().connection_lost (exc )
10172
10273 LOGGER .debug ("Connection lost: %r" , exc )
10374 reason = exc or ConnectionResetError ("Remote server closed connection" )
@@ -117,11 +88,6 @@ def connection_lost(self, exc):
11788 self ._reset_future .set_exception (reason )
11889 self ._reset_future = None
11990
120- if exc is None :
121- LOGGER .debug ("Closed serial connection" )
122- return
123-
124- LOGGER .error ("Lost serial connection: %r" , exc )
12591 self ._application .connection_lost (exc )
12692
12793 async def reset (self ):
@@ -144,10 +110,9 @@ async def reset(self):
144110async def _connect (config , application ):
145111 loop = asyncio .get_event_loop ()
146112
147- connection_future = loop .create_future ()
148113 connection_done_future = loop .create_future ()
149114
150- gateway = Gateway (application , connection_future , connection_done_future )
115+ gateway = Gateway (application , connection_done_future )
151116 protocol = AshProtocol (gateway )
152117
153118 if config [zigpy .config .CONF_DEVICE_FLOW_CONTROL ] is None :
@@ -164,7 +129,7 @@ async def _connect(config, application):
164129 rtscts = rtscts ,
165130 )
166131
167- await connection_future
132+ await gateway . wait_until_connected ()
168133
169134 thread_safe_protocol = ThreadsafeProxy (gateway , loop )
170135 return thread_safe_protocol , connection_done_future
0 commit comments