Skip to content

Commit a9d50d2

Browse files
committed
Migrate zigate to zigpy serial protocol
1 parent 93c7358 commit a9d50d2

File tree

3 files changed

+19
-49
lines changed

3 files changed

+19
-49
lines changed

zigpy_zigate/api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,9 @@ def connection_lost(self, exc: Exception) -> None:
246246
if self._app is not None:
247247
self._app.connection_lost(exc)
248248

249-
def close(self):
250-
if self._uart:
251-
self._uart.close()
249+
async def disconnect(self):
250+
if self._uart is not None:
251+
await self._uart.disconnect()
252252
self._uart = None
253253

254254
def set_application(self, app):

zigpy_zigate/uart.py

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,24 @@
1212
LOGGER = logging.getLogger(__name__)
1313

1414

15-
class Gateway(asyncio.Protocol):
15+
class Gateway(zigpy.serial.SerialProtocol):
1616
START = b"\x01"
1717
END = b"\x03"
1818

19-
def __init__(self, api, connected_future=None):
20-
self._buffer = b""
21-
self._connected_future = connected_future
19+
def __init__(self, api):
20+
super().__init__()
2221
self._api = api
2322

2423
def connection_lost(self, exc) -> None:
25-
"""Port was closed expecteddly or unexpectedly."""
26-
if self._connected_future and not self._connected_future.done():
27-
if exc is None:
28-
self._connected_future.set_result(True)
29-
else:
30-
self._connected_future.set_exception(exc)
31-
if exc is None:
32-
LOGGER.debug("Closed serial connection")
33-
return
34-
35-
LOGGER.error("Lost serial connection: %s", exc)
36-
self._api.connection_lost(exc)
24+
"""Port was closed expectedly or unexpectedly."""
25+
super().connection_lost(exc)
3726

38-
def connection_made(self, transport):
39-
"""Callback when the uart is connected"""
40-
LOGGER.debug("Connection made")
41-
self._transport = transport
42-
if self._connected_future:
43-
self._connected_future.set_result(True)
27+
if self._api is not None:
28+
self._api.connection_lost(exc)
4429

4530
def close(self):
46-
if self._transport:
47-
self._transport.close()
31+
super().close()
32+
self._api = None
4833

4934
def send(self, cmd, data=b""):
5035
"""Send data, taking care of escaping and framing"""
@@ -60,8 +45,7 @@ def send(self, cmd, data=b""):
6045

6146
def data_received(self, data):
6247
"""Callback when there is data received from the uart"""
63-
self._buffer += data
64-
# LOGGER.debug('data_received %s', self._buffer)
48+
super().data_received(data)
6549
endpos = self._buffer.find(self.END)
6650
while endpos != -1:
6751
startpos = self._buffer.rfind(self.START, 0, endpos)
@@ -71,7 +55,7 @@ def data_received(self, data):
7155
cmd, length, checksum, f_data, lqi = struct.unpack(
7256
"!HHB%dsB" % (len(frame) - 6), frame
7357
)
74-
if self._length(frame) != length:
58+
if len(frame) - 5 != length:
7559
LOGGER.warning(
7660
"Invalid length: %s, data: %s", length, len(frame) - 6
7761
)
@@ -126,34 +110,20 @@ def _checksum(self, *args):
126110
chcksum ^= x
127111
return chcksum
128112

129-
def _length(self, frame):
130-
length = len(frame) - 5
131-
return length
132-
133113

134114
async def connect(device_config: Dict[str, Any], api, loop=None):
135-
if loop is None:
136-
loop = asyncio.get_event_loop()
137-
138-
connected_future = asyncio.Future()
139-
protocol = Gateway(api, connected_future)
140-
115+
loop = asyncio.get_running_loop()
141116
port = device_config[zigpy.config.CONF_DEVICE_PATH]
142-
if port == "auto":
143-
port = await loop.run_in_executor(None, c.discover_port)
144117

145118
if await c.async_is_pizigate(port):
146119
LOGGER.debug("PiZiGate detected")
147120
await c.async_set_pizigate_running_mode()
148-
# in case of pizigate:/dev/ttyAMA0 syntax
149-
if port.startswith("pizigate:"):
150-
port = port.replace("pizigate:", "", 1)
121+
port = port.replace("pizigate:", "", 1)
151122
elif await c.async_is_zigate_din(port):
152123
LOGGER.debug("ZiGate USB DIN detected")
153124
await c.async_set_zigatedin_running_mode()
154-
elif c.is_zigate_wifi(port):
155-
LOGGER.debug("ZiGate WiFi detected")
156125

126+
protocol = Gateway(api)
157127
_, protocol = await zigpy.serial.create_serial_connection(
158128
loop,
159129
lambda: protocol,
@@ -162,6 +132,6 @@ async def connect(device_config: Dict[str, Any], api, loop=None):
162132
xonxoff=False,
163133
)
164134

165-
await connected_future
135+
await protocol.wait_until_connected()
166136

167137
return protocol

zigpy_zigate/zigbee/application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async def disconnect(self):
6363
except Exception as e:
6464
LOGGER.warning("Failed to reset before disconnect: %s", e)
6565
finally:
66-
self._api.close()
66+
await self._api.disconnect()
6767
self._api = None
6868

6969
async def start_network(self):

0 commit comments

Comments
 (0)