File: test_serial.py

package info (click to toggle)
zigpy 0.80.1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,012 kB
  • sloc: python: 34,822; sql: 2,109; makefile: 7
file content (168 lines) | stat: -rw-r--r-- 5,541 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from __future__ import annotations

import asyncio
import fcntl
import pathlib
from unittest.mock import AsyncMock, Mock, call, patch

import pytest

import zigpy.serial
from zigpy.typing import UNDEFINED, UndefinedType


# fmt: off
@pytest.mark.parametrize(("url", "flow_control", "xonxoff", "rtscts", "expected_kwargs"), [
    # `flow_control` on its own
    ("/dev/ttyUSB1", "hardware", UNDEFINED, UNDEFINED, {"xonxoff": False, "rtscts": True}),
    ("/dev/ttyUSB1", "software", UNDEFINED, UNDEFINED, {"xonxoff": True,  "rtscts": False}),
    ("/dev/ttyUSB1", None,       UNDEFINED, UNDEFINED, {"xonxoff": False, "rtscts": False}),

    # `flow_control` overrides `xonxoff` and `rtscts`
    ("/dev/ttyUSB1", "hardware", True,      False,     {"xonxoff": False, "rtscts": True}),
    ("/dev/ttyUSB1", "software", False,      True,     {"xonxoff": True,  "rtscts": False}),
    ("/dev/ttyUSB1", None,       True,      False,     {"xonxoff": False, "rtscts": False}),

    # `flow_control` defaults to undefined so `xonxoff` and `rtscts` are used
    ("/dev/ttyUSB1", UNDEFINED,  True,      False,     {"xonxoff": True,  "rtscts": False}),
    ("/dev/ttyUSB1", UNDEFINED,  False,      True,     {"xonxoff": False, "rtscts": True}),
    ("/dev/ttyUSB1", UNDEFINED,  True,       True,     {"xonxoff": True,  "rtscts": True}),

    # The defaults are used when `flow_control`, `xonxoff`, and `rtscts` are all undefined
    ("/dev/ttyUSB1", UNDEFINED,  UNDEFINED, UNDEFINED, {"xonxoff": False, "rtscts": False}),
])
# fmt: on
async def test_serial_normal(
    url: str,
    flow_control: str | UndefinedType,
    xonxoff: bool | UndefinedType,
    rtscts: bool | UndefinedType,
    expected_kwargs: dict[str, bool],
) -> None:
    loop = asyncio.get_running_loop()
    protocol_factory = Mock()

    kwargs = {"url": url}

    if flow_control is not UNDEFINED:
        kwargs["flow_control"] = flow_control

    if xonxoff is not UNDEFINED:
        kwargs["xonxoff"] = xonxoff

    if rtscts is not UNDEFINED:
        kwargs["rtscts"] = rtscts

    with patch(
        "zigpy.serial.pyserial_asyncio.create_serial_connection",
        AsyncMock(
            return_value=(AsyncMock(), AsyncMock())
        ),
    ) as mock_create_serial_connection:
        await zigpy.serial.create_serial_connection(loop, protocol_factory, **kwargs)

    mock_calls = mock_create_serial_connection.mock_calls
    assert len(mock_calls) == 1

    assert mock_calls[0].kwargs["url"] == "/dev/ttyUSB1"
    assert mock_calls[0].kwargs["baudrate"] == 115200

    for kwarg in expected_kwargs:
        assert mock_calls[0].kwargs[kwarg] == expected_kwargs[kwarg]


async def test_serial_socket() -> None:
    loop = asyncio.get_running_loop()
    protocol_factory = Mock()

    with patch.object(
        loop,
        "create_connection",
        AsyncMock(
            return_value=(AsyncMock(), AsyncMock())
        ),
    ):
        await zigpy.serial.create_serial_connection(
            loop, protocol_factory, "socket://1.2.3.4:5678"
        )
        await zigpy.serial.create_serial_connection(
            loop, protocol_factory, "socket://1.2.3.4"
        )

        assert len(loop.create_connection.mock_calls) == 2
        assert loop.create_connection.mock_calls[0].kwargs["host"] == "1.2.3.4"
        assert loop.create_connection.mock_calls[0].kwargs["port"] == 5678
        assert loop.create_connection.mock_calls[1].kwargs["host"] == "1.2.3.4"
        assert loop.create_connection.mock_calls[1].kwargs["port"] == 6638


async def test_pyserial_error_remapping(tmp_path: pathlib.Path) -> None:
    loop = asyncio.get_running_loop()
    protocol_factory = Mock()

    # FileNotFoundError
    missing_port = tmp_path / "missing"
    assert not missing_port.exists()

    with pytest.raises(FileNotFoundError):
        await zigpy.serial.create_serial_connection(
            loop, protocol_factory, url=missing_port
        )

    # PermissionError
    denied_port = tmp_path / "denied"
    denied_port.touch()
    denied_port.chmod(0o000)

    with pytest.raises(PermissionError):
        await zigpy.serial.create_serial_connection(
            loop, protocol_factory, url=denied_port
        )

    # IsADirectoryError
    a_folder = tmp_path / "a_folder"
    a_folder.mkdir()

    with pytest.raises(IsADirectoryError):
        await zigpy.serial.create_serial_connection(
            loop, protocol_factory, url=a_folder
        )

    # Locked
    locked_port = tmp_path / "locked"
    with locked_port.open("w") as f:
        # Lock the file
        fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)

        with pytest.raises(
            PermissionError, match="The serial port is locked by another application"
        ):
            await zigpy.serial.create_serial_connection(
                loop, protocol_factory, url=locked_port
            )


async def test_serial_protocol() -> None:
    class SampleSerialProtocol(zigpy.serial.SerialProtocol):
        pass

    loop = asyncio.get_running_loop()

    protocol = SampleSerialProtocol()

    transport = Mock()
    loop.call_soon(protocol.connection_made, transport)

    # Connect
    await protocol.wait_until_connected()

    # Receive some data
    protocol.data_received(b"Hello")
    protocol.data_received(b" ")
    protocol.data_received(b"world")
    assert protocol._buffer == b"Hello world"

    # Close the transport
    asyncio.get_event_loop().call_soon(protocol.connection_lost, None)
    await protocol.disconnect()
    assert transport.close.mock_calls == [call()]