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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
from unittest.mock import AsyncMock, MagicMock, call
import gpiozero
import pytest
import serial.tools.list_ports
import serial_asyncio
import zigpy.config
from zigpy_zigate import common, uart
@pytest.fixture
def gw():
gw = uart.Gateway(MagicMock())
gw._transport = MagicMock()
return gw
@pytest.mark.asyncio
@pytest.mark.parametrize(
"port",
("/dev/null", "pizigate:/dev/ttyAMA0"),
)
async def test_connect(port, monkeypatch):
monkeypatch.setattr(gpiozero.Device, "_default_pin_factory", MagicMock())
api = MagicMock()
async def mock_conn(loop, protocol_factory, url, **kwargs):
protocol = protocol_factory()
loop.call_soon(protocol.connection_made, None)
assert url.startswith("/") is True
return None, protocol
monkeypatch.setattr(serial_asyncio, "create_serial_connection", mock_conn)
monkeypatch.setattr(common, "set_pizigate_running_mode", AsyncMock())
DEVICE_CONFIG = zigpy.config.SCHEMA_DEVICE({zigpy.config.CONF_DEVICE_PATH: port})
await uart.connect(DEVICE_CONFIG, api)
def test_send(gw):
frame = b"\x01\x02\x150\x02\x10\x02\x114\x02\x10\x03"
data = b"\x00"
gw.send(0x0530, data)
assert gw._transport.write.call_count == 1
assert gw._transport.write.call_args[0][0] == frame
def test_close(gw):
gw.close()
assert gw._transport.close.call_count == 1
def test_connection_lost(gw):
exc = RuntimeError()
gw.connection_lost(exc)
assert gw._api.connection_lost.mock_calls == [call(exc)]
def test_data_received_chunk_frame(gw):
data = b"\x01\x80\x10\x02\x10\x02\x15\xaa\x02\x10\x02\x1f?\xf0\xff\x03"
gw.data_received(data[:-4])
assert gw._api.data_received.call_count == 0
gw.data_received(data[-4:])
assert gw._api.data_received.call_count == 1
assert gw._api.data_received.call_args[0] == (0x8010, b"\x00\x0f?\xf0", 255)
def test_data_received_full_frame(gw):
data = b"\x01\x80\x10\x02\x10\x02\x15\xaa\x02\x10\x02\x1f?\xf0\xff\x03"
gw.data_received(data)
assert gw._api.data_received.call_count == 1
assert gw._api.data_received.call_args[0] == (0x8010, b"\x00\x0f?\xf0", 255)
def test_data_received_incomplete_frame(gw):
data = b"~\x00\x00"
gw.data_received(data)
assert gw._api.data_received.call_count == 0
def test_data_received_runt_frame(gw):
data = b"\x02\x44\xC0"
gw.data_received(data)
assert gw._api.data_received.call_count == 0
def test_data_received_extra(gw):
data = b"\x01\x80\x10\x02\x10\x02\x15\xaa\x02\x10\x02\x1f?\xf0\xff\x03\x00"
gw.data_received(data)
assert gw._api.data_received.call_count == 1
assert gw._api.data_received.call_args[0] == (0x8010, b"\x00\x0f?\xf0", 255)
assert gw._buffer == b"\x00"
def test_data_received_wrong_checksum(gw):
data = b"\x01\x80\x10\x02\x10\x02\x15\xab\x02\x10\x02\x1f?\xf0\xff\x03"
gw.data_received(data)
assert gw._api.data_received.call_count == 0
def test_unescape(gw):
data = b"\x80\x10\x02\x10\x02\x15\xaa\x02\x10\x02\x1f?\xf0\xff"
data_unescaped = b"\x80\x10\x00\x05\xaa\x00\x0f?\xf0\xff"
r = gw._unescape(data)
assert r == data_unescaped
def test_escape(gw):
data = b"\x80\x10\x00\x05\xaa\x00\x0f?\xf0\xff"
data_escaped = b"\x80\x10\x02\x10\x02\x15\xaa\x02\x10\x02\x1f?\xf0\xff"
r = gw._escape(data)
assert r == data_escaped
def test_checksum(gw):
data = b"\x00\x0f?\xf0"
checksum = 0xAA
r = gw._checksum(b"\x80\x10", 5, 0xFF, data)
assert r == checksum
@pytest.mark.parametrize(
"port",
("/dev/ttyAMA0", "/dev/serial0", "pizigate:/dev/ttyAMA0"),
)
def test_is_pizigate(port):
r = common.is_pizigate(port)
assert r is True
def test_is_not_pizigate():
port = "/dev/ttyUSB1"
r = common.is_pizigate(port)
assert r is False
def test_is_zigatedin(monkeypatch):
def mock_grep(*args, **kwargs):
device = MagicMock()
device.description = "ZiGate"
device.manufacturer = "FTDI"
return iter([device])
monkeypatch.setattr(serial.tools.list_ports, "grep", mock_grep)
port = "/dev/ttyUSB1"
r = common.is_zigate_din(port)
assert r is True
@pytest.mark.parametrize(
"port",
("/dev/ttyUSB1", "/dev/ttyAMA0", "/dev/serial0"),
)
def test_is_not_zigatedin(port, monkeypatch):
def mock_grep(*args, **kwargs):
device = MagicMock()
device.description = "Other"
device.manufacturer = "FTDI"
return iter([device])
monkeypatch.setattr(serial.tools.list_ports, "grep", mock_grep)
r = common.is_zigate_din(port)
assert r is False
def test_is_zigate_wifi():
port = "socket://192.168.1.10:9999"
r = common.is_zigate_wifi(port)
assert r is True
def test_is_not_zigate_wifi():
port = "/dev/ttyUSB1"
r = common.is_zigate_wifi(port)
assert r is False
def test_startup_gpio_toggling(monkeypatch):
monkeypatch.setattr(gpiozero.Device, "_default_pin_factory", MagicMock())
common.set_pizigate_running_mode()
common.set_pizigate_flashing_mode()
|