Skip to content

Commit 5d61994

Browse files
committed
Use zigpy for making serial connections
1 parent fc3f3ee commit 5d61994

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

zigpy_znp/uart.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1+
from __future__ import annotations
2+
13
import typing
24
import asyncio
35
import logging
4-
import warnings
56

6-
import serial
7+
import zigpy.serial
78

89
import zigpy_znp.config as conf
910
import zigpy_znp.frames as frames
1011
import zigpy_znp.logger as log
1112
from zigpy_znp.types import Bytes
1213
from zigpy_znp.exceptions import InvalidFrame
1314

14-
with warnings.catch_warnings():
15-
warnings.filterwarnings(
16-
action="ignore",
17-
module="serial_asyncio",
18-
message='"@coroutine" decorator is deprecated',
19-
category=DeprecationWarning,
20-
)
21-
import serial_asyncio # noqa: E402
22-
23-
2415
LOGGER = logging.getLogger(__name__)
2516

2617

@@ -29,12 +20,14 @@ class BufferTooShort(Exception):
2920

3021

3122
class ZnpMtProtocol(asyncio.Protocol):
32-
def __init__(self, api):
23+
def __init__(self, api, *, url: str | None = None) -> None:
3324
self._buffer = bytearray()
3425
self._api = api
3526
self._transport = None
3627
self._connected_event = asyncio.Event()
3728

29+
self.url = url
30+
3831
def close(self) -> None:
3932
"""Closes the port."""
4033

@@ -47,7 +40,7 @@ def close(self) -> None:
4740
self._transport.close()
4841
self._transport = None
4942

50-
def connection_lost(self, exc: typing.Optional[Exception]) -> None:
43+
def connection_lost(self, exc: Exception | None) -> None:
5144
"""Connection lost."""
5245

5346
if exc is not None:
@@ -56,7 +49,7 @@ def connection_lost(self, exc: typing.Optional[Exception]) -> None:
5649
if self._api is not None:
5750
self._api.connection_lost(exc)
5851

59-
def connection_made(self, transport: serial_asyncio.SerialTransport) -> None:
52+
def connection_made(self, transport: asyncio.BaseTransport) -> None:
6053
"""Opened serial port."""
6154
self._transport = transport
6255
LOGGER.debug("Opened %s serial port", transport.serial.name)
@@ -179,13 +172,11 @@ async def connect(config: conf.ConfigType, api) -> ZnpMtProtocol:
179172

180173
LOGGER.debug("Connecting to %s at %s baud", port, baudrate)
181174

182-
_, protocol = await serial_asyncio.create_serial_connection(
175+
_, protocol = await zigpy.serial.create_serial_connection(
183176
loop=loop,
184177
protocol_factory=lambda: ZnpMtProtocol(api),
185178
url=port,
186179
baudrate=baudrate,
187-
parity=serial.PARITY_NONE,
188-
stopbits=serial.STOPBITS_ONE,
189180
xonxoff=(flow_control == "software"),
190181
rtscts=(flow_control == "hardware"),
191182
)

0 commit comments

Comments
 (0)