Skip to content

Commit e3c9fcf

Browse files
authored
Implement command priority (#237)
1 parent 8b86f67 commit e3c9fcf

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ readme = "README.md"
1414
license = {text = "GPL-3.0"}
1515
requires-python = ">=3.8"
1616
dependencies = [
17-
"zigpy>=0.60.0",
17+
"zigpy>=0.60.2",
1818
"async_timeout",
1919
"voluptuous",
2020
"coloredlogs",

zigpy_znp/api.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import zigpy.zdo.types as zdo_t
1717
import zigpy.exceptions
1818
from zigpy.exceptions import NetworkNotFormed
19+
from zigpy.datastructures import PriorityLock
1920

2021
import zigpy_znp.const as const
2122
import zigpy_znp.types as t
@@ -58,7 +59,7 @@ def __init__(self, config: conf.ConfigType):
5859
self._config = config
5960

6061
self._listeners = defaultdict(list)
61-
self._sync_request_lock = asyncio.Lock()
62+
self._sync_request_lock = PriorityLock()
6263

6364
self.capabilities = None # type: int
6465
self.version = None # type: float
@@ -963,6 +964,27 @@ def wait_for_response(self, response: t.CommandBase) -> asyncio.Future:
963964

964965
return self.wait_for_responses([response])
965966

967+
def get_request_priority(self, request: t.CommandBase) -> int:
968+
"""
969+
Returns the priority of a request.
970+
"""
971+
972+
return {
973+
# Reset requests always take priority
974+
c.SYS.ResetReq.Req: 9999,
975+
# Watchdog pings are a close second
976+
c.SYS.Ping.Req: 9998,
977+
# Network requests are deprioritized
978+
c.ZDO.ExtRouteChk.Req: -1,
979+
c.ZDO.ExtRouteDisc.Req: -1,
980+
c.UTIL.AssocGetWithAddress.Req: -1,
981+
c.UTIL.AssocRemove.Req: -1,
982+
c.UTIL.AssocAdd.Req: -1,
983+
c.AF.DataRequestExt.Req: -1,
984+
c.AF.DataRequestSrcRtg.Req: -1,
985+
c.ZDO.MgmtPermitJoinReq.Req: -1,
986+
}.get(type(request), 0)
987+
966988
async def request(
967989
self, request: t.CommandBase, timeout: int | None = None, **response_params
968990
) -> t.CommandBase | None:
@@ -1004,7 +1026,7 @@ async def request(
10041026
ctx = (
10051027
contextlib.AsyncExitStack()
10061028
if isinstance(request, c.SYS.ResetReq.Req)
1007-
else self._sync_request_lock
1029+
else self._sync_request_lock(priority=self.get_request_priority(request))
10081030
)
10091031

10101032
# We should only be sending one SREQ at a time, according to the spec

0 commit comments

Comments
 (0)