Skip to content

Commit 28d275b

Browse files
committed
Centralize all command sending
1 parent 8314dcd commit 28d275b

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

zigpy_xbee/api.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ def __init__(self, device_config: Dict[str, Any]) -> None:
289289
self._cmd_mode_future: Optional[asyncio.Future] = None
290290
self._reset: asyncio.Event = asyncio.Event()
291291
self._running: asyncio.Event = asyncio.Event()
292+
self._send_lock = asyncio.Lock()
292293

293294
@property
294295
def reset_event(self):
@@ -353,12 +354,13 @@ async def _remote_at_command(self, ieee, nwk, options, name, *args):
353354
LOGGER.debug("Remote AT command: %s %s", name, args)
354355
data = t.serialize(args, (AT_COMMANDS[name],))
355356
try:
356-
return await asyncio.wait_for(
357-
self._command(
358-
"remote_at", ieee, nwk, options, name.encode("ascii"), data
359-
),
360-
timeout=REMOTE_AT_COMMAND_TIMEOUT,
361-
)
357+
async with self._send_lock:
358+
return await asyncio.wait_for(
359+
self._command(
360+
"remote_at", ieee, nwk, options, name.encode("ascii"), data
361+
),
362+
timeout=REMOTE_AT_COMMAND_TIMEOUT,
363+
)
362364
except asyncio.TimeoutError:
363365
LOGGER.warning("No response to %s command", name)
364366
raise
@@ -367,10 +369,11 @@ async def _at_partial(self, cmd_type, name, *args):
367369
LOGGER.debug("%s command: %s %s", cmd_type, name, args)
368370
data = t.serialize(args, (AT_COMMANDS[name],))
369371
try:
370-
return await asyncio.wait_for(
371-
self._command(cmd_type, name.encode("ascii"), data),
372-
timeout=AT_COMMAND_TIMEOUT,
373-
)
372+
async with self._send_lock:
373+
return await asyncio.wait_for(
374+
self._command(cmd_type, name.encode("ascii"), data),
375+
timeout=AT_COMMAND_TIMEOUT,
376+
)
374377
except asyncio.TimeoutError:
375378
LOGGER.warning("%s: No response to %s command", cmd_type, name)
376379
raise
@@ -597,9 +600,3 @@ async def _probe(self) -> None:
597600
raise APIException("Failed to configure XBee for API mode")
598601
finally:
599602
self.close()
600-
601-
def __getattr__(self, item):
602-
"""Handle supported command requests."""
603-
if item in COMMAND_REQUESTS:
604-
return functools.partial(self._command, item)
605-
raise AttributeError(f"Unknown command {item}")

zigpy_xbee/zigbee/application.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ async def send_packet(self, packet: zigpy.types.ZigbeePacket) -> None:
302302
"Cannot send a packet to a device without a known IEEE address"
303303
)
304304

305-
send_req = self._api.tx_explicit(
305+
send_req = self._api._command(
306+
"tx_explicit",
306307
long_addr,
307308
short_addr,
308309
packet.src_ep or 0,
@@ -356,7 +357,9 @@ async def permit_with_link_key(
356357
# Key type:
357358
# 0 = Pre-configured Link Key (KY command of the joining device)
358359
# 1 = Install Code With CRC (I? command of the joining device)
359-
await self._api.register_joining_device(node, reserved, key_type, link_key)
360+
await self._api._command(
361+
"register_joining_device", node, reserved, key_type, link_key
362+
)
360363

361364
def handle_modem_status(self, status):
362365
"""Handle changed Modem Status of the device."""

0 commit comments

Comments
 (0)