File: test_types.py

package info (click to toggle)
zigpy-zigate 0.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 288 kB
  • sloc: python: 2,167; makefile: 3
file content (123 lines) | stat: -rw-r--r-- 3,879 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
import binascii

from zigpy_zigate import types as t
from zigpy_zigate.api import COMMANDS, RESPONSES


def test_deserialize():
    extra = b"\xBE\xEF"
    data = b"\x00\x01\x00\x02"
    schema = RESPONSES[0x8000]
    result, rest = t.deserialize(data + extra, schema)
    assert rest == b""
    assert result[0] == 0x00
    assert result[1] == 0x01
    assert result[2] == 0x0002
    assert result[3] == extra

    extra = b"\xBE\xEF"
    data = b"\x00\x00\x01\x00\x01\x01\x01\x02\x12\x34\x02\xab\xcd\x01\x00"
    schema = RESPONSES[0x8002]
    result, rest = t.deserialize(data + extra, schema)
    assert result[0] == 0x00
    assert result[1] == 0x0001
    assert result[2] == 0x0001
    assert result[3] == 0x01
    assert result[4] == 0x01
    assert result[5] == t.Address(address_mode=t.AddressMode.NWK, address=t.NWK(0x1234))
    assert result[6] == t.Address(address_mode=t.AddressMode.NWK, address=t.NWK(0xABCD))
    assert result[7] == b"\x01\x00\xBE\xEF"
    assert rest == b""

    data = b"\x00\x01\x01\x02\x12\x34\xff"
    schema = RESPONSES[0x8702]
    result, rest = t.deserialize(data + extra, schema)
    assert result[0] == 0x00
    assert result[1] == 0x01
    assert result[2] == 0x01
    assert result[3] == t.Address(address_mode=t.AddressMode.NWK, address=t.NWK(0x1234))
    assert result[4] == 0xFF

    data = b"\x00\x01\x01\x03\x12\x34\x56\x78\x9a\xbc\xde\xf0\xff"
    schema = RESPONSES[0x8702]
    result, rest = t.deserialize(data + extra, schema)
    assert result[0] == 0x00
    assert result[1] == 0x01
    assert result[2] == 0x01
    assert result[3] == t.Address(
        address_mode=t.AddressMode.IEEE,
        address=t.EUI64.deserialize(b"\x12\x34\x56\x78\x9a\xbc\xde\xf0")[0],
    )
    assert result[4] == 0xFF

    data = b"\x00\x01\x01\x12\x34\x56\x78\x9a\xbc\xde\xf0\xff"
    schema = RESPONSES[0x8024]
    result, rest = t.deserialize(data, schema)
    assert result[0] == 0x00
    assert result[1] == 0x0101
    assert result[2] == t.EUI64.deserialize(b"\x12\x34\x56\x78\x9a\xbc\xde\xf0")[0]
    assert result[3] == 0xFF

    data = b"\x06"
    schema = RESPONSES[0x8024]
    result, rest = t.deserialize(data, schema)
    assert result[0] == 0x06
    assert result[1] is None
    assert result[2] is None
    assert result[3] is None
    assert len(result) == 4

    # Frame received: 8012000a2800010102bc8c73000100
    # data received 0x8012 b'00010102bc8c730001'

    data = binascii.unhexlify(b"00010102bc8c730001")
    schema = RESPONSES[0x8012]
    result, rest = t.deserialize(data, schema)
    assert result[0] == 0x00
    assert result[1] == 0x01
    assert result[2] == 0x01
    assert result[3] == t.Address(
        address_mode=t.AddressMode.NWK, address=t.NWK.deserialize(b"\xbc\x8c")[0]
    )
    assert result[4] == 0x73
    assert len(result) == 5

    # Frame received: 99990002828000
    # data received 0x9999 b'80' LQI:0

    data = binascii.unhexlify(b"80")
    schema = RESPONSES[0x9999]
    result, rest = t.deserialize(data, schema)
    assert result[0] == 0x80
    assert len(result) == 1


def test_serialize():
    data = [True]
    schema = COMMANDS[0x0002]
    result = t.serialize(data, schema)
    assert result == b"\x01"

    data = [b"\x12\x34"]
    schema = (t.LBytes,)
    result = t.serialize(data, schema)
    assert result == b"\x02\x124"


def test_EUI64():
    data = b"\x12\x34\x56\x78\x9a\xbc\xde\xf0\x00"
    ieee, rest = t.EUI64.deserialize(data)
    assert rest == b"\x00"
    assert ieee == t.EUI64.deserialize(b"\x12\x34\x56\x78\x9a\xbc\xde\xf0")[0]
    data2 = ieee.serialize()
    assert data2 == b"\x12\x34\x56\x78\x9a\xbc\xde\xf0"
    assert str(ieee) == "12:34:56:78:9a:bc:de:f0"


def test_NWK():
    data = b"\x124"
    nwk, rest = t.NWK.deserialize(data)
    assert nwk == t.NWK(0x1234)
    data2 = nwk.serialize()
    assert data2 == data
    assert repr(nwk) == "0x1234"