Skip to content

Commit

Permalink
Match command responses by both command ID and the seq
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed Nov 9, 2023
1 parent cab88f1 commit 4362ac3
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions zigpy_deconz/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ async def _command(self, cmd, **kwargs):
self._seq = (self._seq % 255) + 1

fut = asyncio.Future()
self._awaiting[seq] = (fut, cmd)
self._awaiting[seq, cmd] = fut

try:
async with asyncio_timeout(COMMAND_TIMEOUT):
Expand All @@ -546,7 +546,7 @@ async def _command(self, cmd, **kwargs):
LOGGER.warning(
"No response to '%s' command with seq id '0x%02x'", cmd, seq
)
self._awaiting.pop(seq, None)
self._awaiting.pop((seq, cmd), None)
raise

def data_received(self, data: bytes) -> None:
Expand All @@ -558,13 +558,10 @@ def data_received(self, data: bytes) -> None:

_, rx_schema = COMMAND_SCHEMAS[command.command_id]

fut, cmd = self._awaiting.pop(command.seq, (None, None))
fut = self._awaiting.pop((command.seq, command.command_id), None)

try:
params, rest = t.deserialize_dict(command.payload, rx_schema)

if rest:
LOGGER.debug("Unparsed data remains after frame: %s, %s", command, rest)
except Exception:
LOGGER.warning("Failed to parse command %s", command, exc_info=True)

Expand All @@ -575,6 +572,9 @@ def data_received(self, data: bytes) -> None:

return

if rest:
LOGGER.debug("Unparsed data remains after frame: %s, %s", command, rest)

assert params["frame_length"] == len(data)

if "payload_length" in params:
Expand All @@ -593,18 +593,10 @@ def data_received(self, data: bytes) -> None:
)
status = params["status"]

if cmd != command.command_id:
exc = CommandError(
status,
(
f"Received invalid response {command.command_id}{params}"
f" to request {cmd}"
),
)
elif status != Status.SUCCESS:
exc = None

if status != Status.SUCCESS:
exc = CommandError(status, f"{command.command_id}, status: {status}")
else:
exc = None

if fut is not None:
try:
Expand Down Expand Up @@ -784,11 +776,11 @@ async def aps_data_request(
assert len(relays) <= 9
flags |= t.DeconzSendDataFlags.RELAYS

if not self._free_slots_available_event.is_set():
LOGGER.debug("Waiting for free slots to become available")
await self._free_slots_available_event.wait()

for delay in REQUEST_RETRY_DELAYS:
if not self._free_slots_available_event.is_set():
LOGGER.debug("Waiting for free slots to become available")
await self._free_slots_available_event.wait()

try:
rsp = await self._command(
CommandId.aps_data_request,
Expand Down

0 comments on commit 4362ac3

Please sign in to comment.