Skip to content

Commit 374c67d

Browse files
authored
Drop the zigpy Requests class (#267)
* Drop the zigpy `Requests` class * Fix unit tests * Pass kwargs through
1 parent 37241f1 commit 374c67d

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

tests/test_application.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ async def test_deconz_dev_remove_from_group(app, nwk, device_path):
276276
assert group.remove_member.call_count == 2
277277

278278

279-
def test_deconz_props(nwk, device_path):
279+
def test_deconz_props(app, nwk, device_path):
280280
deconz = application.DeconzDevice("Conbee II", app, sentinel.ieee, nwk)
281281
assert deconz.manufacturer is not None
282282
assert deconz.model is not None
@@ -305,20 +305,20 @@ async def test_deconz_new(app, nwk, device_path, monkeypatch):
305305

306306
def test_tx_confirm_success(app):
307307
tsn = 123
308-
req = app._pending[tsn] = MagicMock()
308+
req = app._pending_requests[tsn] = MagicMock()
309309
app.handle_tx_confirm(tsn, sentinel.status)
310-
assert req.result.set_result.call_count == 1
311-
assert req.result.set_result.call_args[0][0] is sentinel.status
310+
assert req.set_result.call_count == 1
311+
assert req.set_result.call_args[0][0] is sentinel.status
312312

313313

314314
def test_tx_confirm_dup(app, caplog):
315315
caplog.set_level(logging.DEBUG)
316316
tsn = 123
317-
req = app._pending[tsn] = MagicMock()
318-
req.result.set_result.side_effect = asyncio.InvalidStateError
317+
req = app._pending_requests[tsn] = MagicMock()
318+
req.set_result.side_effect = asyncio.InvalidStateError
319319
app.handle_tx_confirm(tsn, sentinel.status)
320-
assert req.result.set_result.call_count == 1
321-
assert req.result.set_result.call_args[0][0] is sentinel.status
320+
assert req.set_result.call_count == 1
321+
assert req.set_result.call_args[0][0] is sentinel.status
322322
assert any(r.levelname == "DEBUG" for r in caplog.records)
323323
assert "probably duplicate response" in caplog.text
324324

zigpy_deconz/zigbee/application.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def __init__(self, config: dict[str, Any]):
7373
super().__init__(config=config)
7474
self._api = None
7575

76-
self._pending = zigpy.util.Requests()
76+
self._pending_requests = {}
7777

7878
self._delayed_neighbor_scan_task = None
7979
self._reconnect_task = None
@@ -504,7 +504,14 @@ async def send_packet(self, packet):
504504
async with self._limit_concurrency(priority=packet.priority):
505505
req_id = self.get_sequence()
506506

507-
with self._pending.new(req_id) as req:
507+
if req_id in self._pending_requests:
508+
raise zigpy.exceptions.DeliveryError(
509+
f"Request with id {req_id} is already pending, cannot send"
510+
)
511+
512+
future = self._pending_requests[req_id] = asyncio.Future()
513+
514+
try:
508515
try:
509516
await self._api.aps_data_request(
510517
req_id=req_id,
@@ -525,31 +532,35 @@ async def send_packet(self, packet):
525532
)
526533

527534
async with asyncio_timeout(SEND_CONFIRM_TIMEOUT):
528-
status = await req.result
535+
status = await future
529536

530537
if status != TXStatus.SUCCESS:
531538
raise zigpy.exceptions.DeliveryError(
532539
f"Failed to deliver packet: {status!r}", status
533540
)
541+
finally:
542+
del self._pending_requests[req_id]
534543

535544
async def permit_ncp(self, time_s=60):
536545
assert 0 <= time_s <= 254
537546
await self._api.write_parameter(NetworkParameter.permit_join, time_s)
538547

539548
def handle_tx_confirm(self, req_id, status):
540549
try:
541-
self._pending[req_id].result.set_result(status)
542-
return
550+
future = self._pending_requests[req_id]
543551
except KeyError:
544552
LOGGER.warning(
545553
"Unexpected transmit confirm for request id %s, Status: %s",
546554
req_id,
547555
status,
548556
)
549-
except asyncio.InvalidStateError as exc:
550-
LOGGER.debug(
551-
"Invalid state on future - probably duplicate response: %s", exc
552-
)
557+
else:
558+
try:
559+
future.set_result(status)
560+
except asyncio.InvalidStateError as exc:
561+
LOGGER.debug(
562+
"Invalid state on future - probably duplicate response: %s", exc
563+
)
553564

554565
async def restore_neighbours(self) -> None:
555566
"""Restore children."""
@@ -599,10 +610,10 @@ async def _delayed_neighbour_scan(self) -> None:
599610
class DeconzDevice(zigpy.device.Device):
600611
"""Zigpy Device representing Coordinator."""
601612

602-
def __init__(self, model: str, *args):
613+
def __init__(self, model: str, *args, **kwargs):
603614
"""Initialize instance."""
604615

605-
super().__init__(*args)
616+
super().__init__(*args, **kwargs)
606617
self._model = model
607618

608619
async def add_to_group(self, grp_id: int, name: str = None) -> None:

0 commit comments

Comments
 (0)