Skip to content

Commit 6f6d085

Browse files
author
Sébastien RAMAGE
committed
improve probing test
1 parent 62d4144 commit 6f6d085

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

tests/test_api.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import asyncio
2-
2+
import sys
33
import pytest
44
import serial
55
import serial_asyncio
@@ -12,6 +12,7 @@
1212
DEVICE_CONFIG = config.SCHEMA_DEVICE({config.CONF_DEVICE_PATH: "/dev/null"})
1313

1414

15+
1516
@pytest.fixture
1617
def api():
1718
api = zigate_api.ZiGate(DEVICE_CONFIG)
@@ -58,17 +59,26 @@ async def test_api_new(conn_mck):
5859

5960
@pytest.mark.asyncio
6061
@patch.object(zigate_api.ZiGate, "set_raw_mode", new_callable=AsyncMock)
61-
@patch.object(zigpy_zigate.uart, "connect")
62-
async def test_probe_success(mock_connect, mock_raw_mode):
62+
@pytest.mark.parametrize(
63+
"port",
64+
('/dev/null', 'pizigate:/dev/ttyAMA0'),
65+
)
66+
async def test_probe_success(mock_raw_mode, port, monkeypatch):
6367
"""Test device probing."""
6468

69+
async def mock_conn(loop, protocol_factory, **kwargs):
70+
protocol = protocol_factory()
71+
loop.call_soon(protocol.connection_made, None)
72+
return None, protocol
73+
monkeypatch.setattr(serial_asyncio, "create_serial_connection", mock_conn)
74+
DEVICE_CONFIG = zigpy_zigate.config.SCHEMA_DEVICE(
75+
{zigpy_zigate.config.CONF_DEVICE_PATH: port}
76+
)
77+
sys.modules['RPi'] = MagicMock()
78+
sys.modules['RPi.GPIO'] = MagicMock()
6579
res = await zigate_api.ZiGate.probe(DEVICE_CONFIG)
6680
assert res is True
67-
assert mock_connect.call_count == 1
68-
assert mock_connect.await_count == 1
69-
assert mock_connect.call_args[0][0] == DEVICE_CONFIG
7081
assert mock_raw_mode.call_count == 1
71-
assert mock_connect.return_value.close.call_count == 1
7282

7383

7484
@pytest.mark.asyncio

zigpy_zigate/uart.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def connection_made(self, transport):
3232
self._connected_future.set_result(True)
3333

3434
def close(self):
35-
self._transport.close()
35+
if self._transport:
36+
self._transport.close()
3637

3738
def send(self, cmd, data=b''):
3839
"""Send data, taking care of escaping and framing"""
@@ -145,7 +146,7 @@ async def connect(device_config: Dict[str, Any], api, loop=None):
145146
# in case of pizigate:/dev/ttyAMA0 syntax
146147
if port.startswith('pizigate:'):
147148
port = port[9:]
148-
elif c.is_zigate_din:
149+
elif c.is_zigate_din(port):
149150
LOGGER.debug('ZiGate USB DIN detected')
150151
await c.set_zigatedin_running_mode()
151152

0 commit comments

Comments
 (0)