Skip to content

Commit 9cd3843

Browse files
authored
Merge pull request #43 from Adminiuga/fixes/zdo-dev-annce
Fix ieee address deserialization on ZDO Device_annce frame.
2 parents c350485 + 5a2b8d0 commit 9cd3843

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

tests/test_application.py

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import pytest
55

66
from zigpy.exceptions import DeliveryError
7+
import zigpy.device
78
from zigpy.types import EUI64
9+
import zigpy.zdo.types as zdo_t
810
from zigpy_deconz.api import Deconz
911
from zigpy_deconz.zigbee.application import ControllerApplication
1012
from zigpy_deconz.zigbee import application
@@ -17,18 +19,28 @@ def app(database_file=None):
1719

1820

1921
@pytest.fixture
20-
def addr_ieee():
22+
def ieee():
23+
return EUI64.deserialize(b'\x00\x01\x02\x03\x04\x05\x06\x07')[0]
24+
25+
26+
@pytest.fixture
27+
def nwk():
28+
return t.uint16_t(0x0100)
29+
30+
31+
@pytest.fixture
32+
def addr_ieee(ieee):
2133
addr = t.DeconzAddress()
2234
addr.address_mode = t.ADDRESS_MODE.IEEE
23-
addr.address = b'\x00\x01\x02\x03\x04\x05\x06\x07'
35+
addr.address = ieee
2436
return addr
2537

2638

2739
@pytest.fixture
28-
def addr_nwk():
40+
def addr_nwk(nwk):
2941
addr = t.DeconzAddress()
3042
addr.address_mode = t.ADDRESS_MODE.NWK
31-
addr.address = b'\x00\x01'
43+
addr.address = nwk
3244
return addr
3345

3446

@@ -245,3 +257,44 @@ async def test_shutdown(app):
245257
app._api.close = mock.MagicMock()
246258
await app.shutdown()
247259
assert app._api.close.call_count == 1
260+
261+
262+
def test_rx_device_annce(app, addr_ieee, addr_nwk):
263+
dst_ep = 0
264+
cluster_id = zdo_t.ZDOCmd.Device_annce
265+
device = mock.MagicMock()
266+
device.status = zigpy.device.Status.NEW
267+
app.get_device = mock.MagicMock(return_value=device)
268+
app.deserialize = mock.MagicMock(return_value=(mock.sentinel.tsn,
269+
mock.sentinel.cmd_id,
270+
False,
271+
mock.sentinel.args, ))
272+
app.handle_join = mock.MagicMock()
273+
app._handle_reply = mock.MagicMock()
274+
app.handle_message = mock.MagicMock()
275+
276+
data = t.uint8_t(0xaa).serialize()
277+
data += addr_nwk.address.serialize()
278+
data += addr_ieee.address.serialize()
279+
data += t.uint8_t(0x8e).serialize()
280+
281+
app.handle_rx(
282+
addr_nwk,
283+
mock.sentinel.src_ep,
284+
dst_ep,
285+
mock.sentinel.profile_id,
286+
cluster_id,
287+
data,
288+
mock.sentinel.lqi,
289+
mock.sentinel.rssi,
290+
)
291+
292+
assert app.deserialize.call_count == 1
293+
assert app.deserialize.call_args[0][2] == cluster_id
294+
assert app.deserialize.call_args[0][3] == data
295+
assert app._handle_reply.call_count == 0
296+
assert app.handle_message.call_count == 1
297+
assert app.handle_join.call_count == 1
298+
assert app.handle_join.call_args[0][0] == addr_nwk.address
299+
assert app.handle_join.call_args[0][1] == addr_ieee.address
300+
assert app.handle_join.call_args[0][2] == 0

zigpy_deconz/zigbee/application.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ async def permit_ncp(self, time_s=60):
170170
def handle_rx(self, src_addr, src_ep, dst_ep, profile_id, cluster_id, data, lqi, rssi):
171171
# intercept ZDO device announce frames
172172
if dst_ep == 0 and cluster_id == 0x13:
173-
nwk, _ = t.uint16_t.deserialize(data[1:])
174-
ieee = zigpy.types.EUI64(map(t.uint8_t, data[7::-1]))
173+
nwk, rest = t.uint16_t.deserialize(data[1:])
174+
ieee, _ = zigpy.types.EUI64.deserialize(rest)
175175
LOGGER.info("New device joined: 0x%04x, %s", nwk, ieee)
176176
self.handle_join(nwk, ieee, 0)
177177

0 commit comments

Comments
 (0)