Skip to content

Commit cf69f7e

Browse files
authored
Pop correct future on request timeout. (#64)
1 parent 7fdfe29 commit cf69f7e

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

tests/test_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_commands():
5959
@pytest.mark.asyncio
6060
async def test_command(api, monkeypatch):
6161
def mock_api_frame(name, *args):
62-
return mock.sentinel.api_frame_data
62+
return mock.sentinel.api_frame_data, api._seq
6363
api._api_frame = mock.MagicMock(side_effect=mock_api_frame)
6464
api._uart.send = mock.MagicMock()
6565

@@ -82,7 +82,7 @@ async def mock_fut():
8282
@pytest.mark.asyncio
8383
async def test_command_timeout(api, monkeypatch):
8484
def mock_api_frame(name, *args):
85-
return mock.sentinel.api_frame_data
85+
return mock.sentinel.api_frame_data, api._seq
8686
api._api_frame = mock.MagicMock(side_effect=mock_api_frame)
8787
api._uart.send = mock.MagicMock()
8888

zigpy_deconz/api.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,27 +126,27 @@ def close(self):
126126

127127
async def _command(self, name, *args):
128128
LOGGER.debug("Command %s %s", name, args)
129-
self._seq = (self._seq % 255) + 1
130-
data = self._api_frame(name, *args)
129+
data, seq = self._api_frame(name, *args)
131130
self._uart.send(data)
132131
fut = asyncio.Future()
133-
self._awaiting[self._seq] = fut
132+
self._awaiting[seq] = fut
134133
try:
135134
return await asyncio.wait_for(fut, timeout=COMMAND_TIMEOUT)
136135
except asyncio.TimeoutError:
137136
LOGGER.warning("No response to '%s' command", name)
138-
self._awaiting.pop(self._seq)
137+
self._awaiting.pop(seq)
139138
raise
140139

141140
def _api_frame(self, name, *args):
142141
cmd_id, schema = TX_COMMANDS[name]
143142
d = t.serialize(args, schema)
144143
data = t.uint8_t(cmd_id).serialize()
144+
self._seq = (self._seq % 255) + 1
145145
data += t.uint8_t(self._seq).serialize()
146146
data += t.uint8_t(0).serialize()
147147
data += t.uint16_t(len(d) + 5).serialize()
148148
data += d
149-
return data
149+
return data, self._seq
150150

151151
def data_received(self, data):
152152
try:

0 commit comments

Comments
 (0)