Exemple #1
0
def test_bind_req_serialize():
    payload = {"dstaddr": NWK(25234),
               "srcaddr": EUI64(reversed(b'\x00\x0b\x57\xff\xfe\x27\x78\x3c')),
               "srcendpoint": 1,
               "clusterid": 8,
               "dstaddrmode": 3,
               "dstaddress": EUI64(reversed(b'\x00\x12\x4b\x00\x18\xed\x25\x0c')),
               "dstendpoint": 1}
    obj = ZpiObject.from_command(t.Subsystem.ZDO, 'bindReq', payload)
    assert (
            "SREQ ZDO bindReq tsn: None {"
            "'dstaddr': 0x6292, "
            "'srcaddr': 00:0b:57:ff:fe:27:78:3c, "
            "'srcendpoint': 1, "
            "'clusterid': 8, "
            "'dstaddrmode': 3, "
            "'dstaddress': 00:12:4b:00:18:ed:25:0c, "
            "'dstendpoint': 1}" == str(obj)
    )
    assert (
            bytes(
                [254, 23, 37, 33, 146, 98, 60, 120, 39, 254, 255, 87, 11, 0, 1, 8, 0, 3,
                 12, 37, 237, 24, 0, 75, 18, 0, 1, 83])
            == obj.to_unpi_frame().to_buffer()
    )
Exemple #2
0
def test_list_nighbor_lqi():
    value = [
        {
            "extPanId": EUI64.convert("d8:dd:dd:dd:d0:dd:ed:dd"),
            "extAddr": EUI64.convert("00:15:8d:00:04:21:dc:b3"),
            "nwkAddr": NWK(0xE961),
            "deviceType": 1,
            "rxOnWhenIdle": 2,
            "relationship": 2,
            "permitJoin": 2,
            "depth": 255,
            "lqi": 69,
        }
    ]
    data_out = Buffalo(b"")
    data_out.write_parameter(t.ParameterType.LIST_NEIGHBOR_LQI, value, {})
    assert (
        b"\xdd\xed\xdd\xd0\xdd\xdd\xdd\xd8\xb3\xdc!\x04\x00\x8d\x15\x00a\xe9)\x02\xffE"
        == data_out.buffer
    )

    data_in = Buffalo(data_out.buffer)
    options = BuffaloOptions()
    options.length = len(value)
    act = data_in.read_parameter("test", t.ParameterType.LIST_NEIGHBOR_LQI, options)
    assert value == act
Exemple #3
0
def convert_ieee(ieee_str):
    """Convert given ieee string to EUI64."""
    from zigpy.types import EUI64, uint8_t

    if ieee_str is None:
        return None
    return EUI64([uint8_t(p, base=16) for p in ieee_str.split(":")])
Exemple #4
0
async def test_startup_no_api_mode(app):
    auto_form = True
    await _test_startup(app, 0x00, auto_form, api_mode=False)
    assert app._nwk == 0x0000
    assert app._ieee == EUI64(range(1, 9))
    assert app.form_network.call_count == 0
    assert app._api.init_api_mode.call_count == 1
    assert app._api._at_command.call_count >= 16
def _test_rx(app, addr_ieee, addr_nwk, device, data):
    app.get_device = MagicMock(return_value=device)
    app.devices = (EUI64(addr_ieee.address), )

    app.handle_rx(
        addr_nwk,
        sentinel.src_ep,
        sentinel.dst_ep,
        sentinel.profile_id,
        sentinel.cluster_id,
        data,
        sentinel.lqi,
        sentinel.rssi,
    )
Exemple #6
0
    def deserialize(cls, data: bytes):
        """Deserialize data."""
        mode, data = AddrMode.deserialize(data)
        if mode in (AddrMode.NWK, AddrMode.Group, AddrMode.Broadcast):
            # a value of 2 indicates 2-byte (16-bit) address mode,
            # using only the 2 LSB’s of the DstAddr field to form
            # a 2-byte short address.
            addr64, data = basic.uint64_t.deserialize(data)
            addr = NWK(addr64 & 0xFFFF)
        elif mode == AddrMode.IEEE:
            addr, data = EUI64.deserialize(data)
        else:
            raise ValueError(f"Unknown address mode: {mode}")

        return cls(mode=mode, address=addr), data
Exemple #7
0
async def test_startup_ai(app):
    auto_form = True
    await _test_startup(app, 0x00, auto_form)
    assert app._nwk == 0x0000
    assert app._ieee == EUI64(range(1, 9))
    assert app.form_network.call_count == 0

    auto_form = False
    await _test_startup(app, 0x00, auto_form)
    assert app._nwk == 0x0000
    assert app._ieee == EUI64(range(1, 9))
    assert app.form_network.call_count == 0

    auto_form = True
    await _test_startup(app, 0x06, auto_form)
    assert app._nwk == 0xfffe
    assert app._ieee == EUI64(range(1, 9))
    assert app.form_network.call_count == 1

    auto_form = False
    await _test_startup(app, 0x06, auto_form)
    assert app._nwk == 0xfffe
    assert app._ieee == EUI64(range(1, 9))
    assert app.form_network.call_count == 0
Exemple #8
0
def _test_rx(app, addr_ieee, addr_nwk, device, deserialized):
    app.get_device = mock.MagicMock(return_value=device)
    app.deserialize = mock.MagicMock(return_value=deserialized)

    app.devices = (EUI64(addr_ieee.address), )

    app.handle_rx(
        addr_nwk,
        mock.sentinel.src_ep,
        mock.sentinel.dst_ep,
        mock.sentinel.profile_id,
        mock.sentinel.cluster_id,
        b'',
        mock.sentinel.lqi,
        mock.sentinel.rssi,
    )

    assert app.deserialize.call_count == 1
def test_rx_wrong_addr_mode(app, addr_ieee, addr_nwk, caplog):
    device = MagicMock()
    app.handle_message = MagicMock()
    app.get_device = MagicMock(return_value=device)

    app.devices = (EUI64(addr_ieee.address), )

    with pytest.raises(Exception):  # TODO: don't use broad exceptions
        addr_nwk.address_mode = 0x22
        app.handle_rx(
            addr_nwk,
            sentinel.src_ep,
            sentinel.dst_ep,
            sentinel.profile_id,
            sentinel.cluster_id,
            b"",
            sentinel.lqi,
            sentinel.rssi,
        )

    assert app.handle_message.call_count == 0
Exemple #10
0
def test_rx_failed_deserialize(app, addr_ieee, addr_nwk, caplog):
    app._handle_reply = mock.MagicMock()
    app.handle_message = mock.MagicMock()
    app.get_device = mock.MagicMock(return_value=mock.MagicMock())
    app.deserialize = mock.MagicMock(side_effect=ValueError)

    app.devices = (EUI64(addr_ieee.address), )

    app.handle_rx(
        addr_nwk,
        mock.sentinel.src_ep,
        mock.sentinel.dst_ep,
        mock.sentinel.profile_id,
        mock.sentinel.cluster_id,
        b'',
        mock.sentinel.lqi,
        mock.sentinel.rssi,
    )

    assert any(record.levelname == 'ERROR' for record in caplog.records)

    assert app._handle_reply.call_count == 0
    assert app.handle_message.call_count == 0
Exemple #11
0
def ieee():
    return EUI64.deserialize(b"\x00\x01\x02\x03\x04\x05\x06\x07")[0]
Exemple #12
0
import zigpy_cc.types as t
from zigpy.types import EUI64

from zigpy_cc.buffalo import Buffalo

ieeeAddr1 = {
    "string": EUI64.convert("ae:44:01:12:00:4b:12:00"),
    "hex": bytes([0x00, 0x12, 0x4B, 0x00, 0x12, 0x01, 0x44, 0xAE]),
}

ieeeAddr2 = {
    "string": EUI64.convert("af:44:01:12:00:5b:12:00"),
    "hex": bytes([0x00, 0x12, 0x5B, 0x00, 0x12, 0x01, 0x44, 0xAF]),
}


def test_write_ieee():
    data_out = Buffalo(b"")
    data_out.write_parameter(t.ParameterType.IEEEADDR, ieeeAddr1["string"], {})
    assert ieeeAddr1["hex"] == data_out.buffer


def test_write_ieee2():
    data_out = Buffalo(b"")
    data_out.write_parameter(t.ParameterType.IEEEADDR, ieeeAddr2["string"], {})
    assert ieeeAddr2["hex"] == data_out.buffer


def test_read_ieee():
    data_in = Buffalo(ieeeAddr1["hex"])
    actual = data_in.read_parameter(t.ParameterType.IEEEADDR, {})
Exemple #13
0
from zigpy.zdo.types import IEEE

import zigpy_cc.types as t
from zigpy.types import EUI64

from zigpy_cc.buffalo import Buffalo

ieeeAddr1 = {
    "string": EUI64.convert('ae:44:01:12:00:4b:12:00'),
    "hex": bytes([0x00, 0x12, 0x4b, 0x00, 0x12, 0x01, 0x44, 0xae]),
}

ieeeAddr2 = {
    "string": EUI64.convert('af:44:01:12:00:5b:12:00'),
    "hex": bytes([0x00, 0x12, 0x5b, 0x00, 0x12, 0x01, 0x44, 0xaf]),
}


def test_write_ieee():
    data_out = Buffalo(b"")
    data_out.write_parameter(t.ParameterType.IEEEADDR, ieeeAddr1['string'], {})
    assert ieeeAddr1['hex'] == data_out.buffer


def test_write_ieee2():
    data_out = Buffalo(b"")
    data_out.write_parameter(t.ParameterType.IEEEADDR, ieeeAddr2['string'], {})
    assert ieeeAddr2['hex'] == data_out.buffer


def test_read_ieee():