def test_neighbors(): """Test ZDO neignbors struct.""" data = ( b"\x05\x00\x03\xa2\xaf\x8cY\xf5\x03\x96\xb4:p\x07\xfe\xffW\xb4\x14\xd1" b"\xb7\x12\x01\x01H\xa2\xaf\x8cY\xf5\x03\x96\xb4X\xb76\x02\x00\x8d\x15\x00=X" b"\x12\x01\x01:\xa2\xaf\x8cY\xf5\x03\x96\xb4\x9b-0\xfe\xff\xbd\x1b\xec$\xcb\x12" b"\x01\x01F") extra = b"\x55\xaaextra\x00" neigbours, rest = types.Neighbors().deserialize(data + extra) assert rest == extra
async def test_neighbors(tmpdir): """Test neighbor loading.""" ext_pid = t.EUI64.convert("aa:bb:cc:dd:ee:ff:01:02") ieee_1 = make_ieee(1) nwk_1 = 0x1111 nei_1 = zdo_t.Neighbor(ext_pid, ieee_1, nwk_1, 2, 1, 1, 0, 0, 0, 15, 250) ieee_2 = make_ieee(2) nwk_2 = 0x2222 nei_2 = zdo_t.Neighbor(ext_pid, ieee_2, nwk_2, 1, 1, 2, 0, 0, 0, 15, 250) ieee_3 = make_ieee(3) nwk_3 = 0x3333 nei_3 = zdo_t.Neighbor(ext_pid, ieee_3, nwk_3, 1, 1, 2, 0, 0, 0, 15, 250) db = os.path.join(str(tmpdir), "test.db") app = await make_app(db) app.handle_join(nwk_1, ieee_1, 0) dev_1 = app.get_device(ieee_1) dev_1.node_desc = zdo_t.NodeDescriptor(2, 64, 128, 4174, 82, 82, 0, 82, 0) ep1 = dev_1.add_endpoint(1) ep1.status = zigpy.endpoint.Status.ZDO_INIT ep1.profile_id = 260 ep1.device_type = 0x1234 app.device_initialized(dev_1) # 2nd device app.handle_join(nwk_2, ieee_2, 0) dev_2 = app.get_device(ieee_2) dev_2.node_desc = zdo_t.NodeDescriptor(1, 64, 142, 4476, 82, 82, 0, 82, 0) ep2 = dev_2.add_endpoint(1) ep2.status = zigpy.endpoint.Status.ZDO_INIT ep2.profile_id = 260 ep2.device_type = 0x1234 app.device_initialized(dev_2) neighbors = zdo_t.Neighbors(2, 0, [nei_2, nei_3]) p1 = patch.object( dev_1.zdo, "request", new=AsyncMock(return_value=(zdo_t.Status.SUCCESS, neighbors)), ) with p1: res = await dev_1.neighbors.scan() assert res neighbors = zdo_t.Neighbors(2, 0, [nei_1, nei_3]) p1 = patch.object( dev_2.zdo, "request", new=AsyncMock(return_value=(zdo_t.Status.SUCCESS, neighbors)), ) with p1: res = await dev_2.neighbors.scan() assert res await app.pre_shutdown() del dev_1, dev_2 # Everything should've been saved - check that it re-loads app2 = await make_app(db) dev_1 = app2.get_device(ieee_1) dev_2 = app2.get_device(ieee_2) assert len(dev_1.neighbors) == 2 assert dev_1.neighbors[0].device is dev_2 assert dev_1.neighbors[1].device is None assert dev_1.neighbors[1].neighbor.ieee == ieee_3 assert len(dev_2.neighbors.neighbors) == 2 assert dev_2.neighbors[0].device is dev_1 assert dev_2.neighbors[1].device is None assert dev_2.neighbors[1].neighbor.ieee == ieee_3 await app2.pre_shutdown() os.unlink(db)