Skip to content

Commit 6f8946b

Browse files
committed
Fix UART not closing during probe
1 parent a2e2984 commit 6f8946b

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

tests/test_api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ def pingable_serial_port(mocker):
7171
port_name = "/dev/ttyWorkingUSB1"
7272
old_serial_connect = serial_asyncio.create_serial_connection
7373

74+
def dummy_serial_close():
75+
dummy_serial_conn.mock_transport._connected = False
76+
7477
def dummy_serial_conn(loop, protocol_factory, url, *args, **kwargs):
7578
# Only our virtual port is handled differently
7679
if url != port_name:
@@ -84,6 +87,9 @@ def dummy_serial_conn(loop, protocol_factory, url, *args, **kwargs):
8487

8588
fut.set_result((dummy_serial_conn.mock_transport, dummy_serial_conn.protocol))
8689

90+
assert not dummy_serial_conn.mock_transport._connected
91+
dummy_serial_conn.mock_transport._connected = True
92+
8793
return fut
8894

8995
def ping_responder(data):
@@ -93,7 +99,9 @@ def ping_responder(data):
9399
dummy_serial_conn.protocol.data_received(b"\xFE\x02\x61\x01\x59\x06\x3D")
94100

95101
dummy_serial_conn.mock_transport = mocker.Mock()
102+
dummy_serial_conn.mock_transport._connected = False
96103
dummy_serial_conn.mock_transport.write = mocker.Mock(side_effect=ping_responder)
104+
dummy_serial_conn.mock_transport.close = mocker.Mock(side_effect=dummy_serial_close)
97105

98106
mocker.patch("serial_asyncio.create_serial_connection", new=dummy_serial_conn)
99107

tests/test_application.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,18 +726,31 @@ async def test_on_af_message_callback(application, mocker):
726726

727727

728728
@pytest_mark_asyncio_timeout(seconds=3)
729-
async def test_probe(pingable_serial_port): # noqa: F811
729+
async def test_probe_unsuccessful(pingable_serial_port): # noqa: F811
730730
assert not (
731731
await ControllerApplication.probe(
732732
conf.SCHEMA_DEVICE({conf.CONF_DEVICE_PATH: "/dev/null"})
733733
)
734734
)
735735

736+
737+
@pytest_mark_asyncio_timeout(seconds=3)
738+
async def test_probe_successful(pingable_serial_port): # noqa: F811
736739
assert await ControllerApplication.probe(
737740
conf.SCHEMA_DEVICE({conf.CONF_DEVICE_PATH: pingable_serial_port})
738741
)
739742

740743

744+
@pytest_mark_asyncio_timeout(seconds=3)
745+
async def test_probe_multiple(pingable_serial_port): # noqa: F811
746+
config = conf.SCHEMA_DEVICE({conf.CONF_DEVICE_PATH: pingable_serial_port})
747+
748+
assert await ControllerApplication.probe(config)
749+
assert await ControllerApplication.probe(config)
750+
assert await ControllerApplication.probe(config)
751+
assert await ControllerApplication.probe(config)
752+
753+
741754
@pytest_mark_asyncio_timeout(seconds=5)
742755
async def test_reconnect(event_loop, application):
743756
app, znp_server = application()

zigpy_znp/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ async def connect(self, *, test_port=True, check_version=True) -> None:
220220
else:
221221
LOGGER.warning(old_version_msg)
222222
except Exception:
223-
self._uart = None
223+
self.close()
224224
raise
225225

226226
LOGGER.debug(

zigpy_znp/zigbee/application.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,16 @@ async def probe(cls, device_config: conf.ConfigType) -> bool:
114114

115115
try:
116116
await znp.connect()
117-
return True
117+
result = True
118118
except Exception as e:
119+
result = False
119120
LOGGER.warning(
120121
"Failed to probe ZNP radio with config %s", device_config, exc_info=e
121122
)
122-
return False
123-
finally:
124-
znp.close()
123+
124+
znp.close()
125+
126+
return result
125127

126128
async def shutdown(self):
127129
"""

0 commit comments

Comments
 (0)