Skip to content

Commit

Permalink
Default to endpoint 1 if no better endpoint exists (fixes #43)
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed Oct 28, 2020
1 parent 5bd9ff4 commit 486238a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
57 changes: 57 additions & 0 deletions tests/application/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,60 @@ async def callback(req):
assert all(status == t.Status.SUCCESS for status, msg in responses)

await app.shutdown()


@pytest.mark.parametrize("device", FORMED_DEVICES)
async def test_nonstandard_profile(device, make_application):
app, znp_server = make_application(server_cls=device)
await app.startup(auto_form=False)

device = app.add_device(ieee=t.EUI64(range(8)), nwk=0xFA9E)
device.status = zigpy.device.Status.ENDPOINTS_INIT
device.initializing = False
device.node_desc, _ = device.node_desc.deserialize(bytes(14))

ep = device.add_endpoint(2)
ep.profile_id = 0x9876 # non-standard profile
ep.add_input_cluster(0x0006)

# Respond to a light turn on request
data_req = znp_server.reply_once_to(
request=c.AF.DataRequestExt.Req(
DstAddrModeAddress=t.AddrModeAddress(
mode=t.AddrMode.NWK, address=device.nwk
),
DstEndpoint=2,
SrcEndpoint=1, # we default to endpoint 1 for unknown profiles
ClusterId=0x0006,
partial=True,
),
responses=[
c.AF.DataRequestExt.Rsp(Status=t.Status.SUCCESS),
lambda req: c.AF.DataConfirm.Callback(
Status=t.Status.SUCCESS,
Endpoint=2,
TSN=req.TSN,
),
lambda req: c.AF.IncomingMsg.Callback(
GroupId=0x0000,
ClusterId=0x0006,
SrcAddr=device.nwk,
SrcEndpoint=2,
DstEndpoint=1,
WasBroadcast=t.Bool(False),
LQI=63,
SecurityUse=t.Bool(False),
TimeStamp=12345678,
TSN=0,
Data=b"\x08" + bytes([req.TSN]) + b"\x0B\x00\x00",
MacSrcAddr=device.nwk,
MsgResultRadius=29,
),
],
)

await device.endpoints[2].on_off.off()

await data_req

await app.shutdown()
12 changes: 3 additions & 9 deletions zigpy_znp/zigbee/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,8 @@ def _find_endpoint(self, dst_ep: int, profile: int, cluster: int) -> int:
if dst_ep == ZDO_ENDPOINT:
return ZDO_ENDPOINT

candidates = []
# Always fall back to endpoint 1
candidates = [1]

for ep_id, endpoint in self.zigpy_device.endpoints.items():
if ep_id == ZDO_ENDPOINT:
Expand All @@ -1107,14 +1108,7 @@ def _find_endpoint(self, dst_ep: int, profile: int, cluster: int) -> int:
# if we don't find anything better
candidates.append(endpoint.endpoint_id)

if not candidates:
raise ValueError(
f"Could not pick endpoint for dst_ep={dst_ep},"
f" profile={profile}, and cluster={cluster}"
)

# XXX: pick the first one?
return candidates[0]
return candidates[-1]

async def _send_zdo_request(
self, dst_addr, dst_ep, src_ep, cluster, sequence, options, radius, data
Expand Down

0 comments on commit 486238a

Please sign in to comment.