Skip to content

Commit 99b8f44

Browse files
committed
fix comments
1 parent 6215697 commit 99b8f44

4 files changed

Lines changed: 214 additions & 174 deletions

File tree

src/mistapi/device_utils/__tools/__ws_wrapper.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,13 @@ def start_with_trigger(
530530
ws_factory_fn: Callable | None = None,
531531
) -> UtilResponse:
532532
"""
533-
Run the trigger API call (and optional WS setup) in a background thread.
533+
Run the trigger API call and optionally start a WebSocket stream.
534534
535-
Returns the ``UtilResponse`` immediately. The trigger HTTP request and
536-
the subsequent WebSocket connection both run in background threads, so
537-
the calling code is never blocked.
535+
If ``ws_factory_fn`` is provided, the trigger and WebSocket setup
536+
run in a background thread (non-blocking). If ``ws_factory_fn`` is
537+
``None``, the trigger runs synchronously so that
538+
``trigger_api_response`` is immediately available on the returned
539+
``UtilResponse``.
538540
539541
PARAMS
540542
-----------
@@ -547,6 +549,8 @@ def start_with_trigger(
547549
If ``None``, no WebSocket is started and the ``UtilResponse``
548550
completes as soon as the trigger finishes.
549551
"""
552+
if ws_factory_fn is None:
553+
return self._trigger_only(trigger_fn)
550554

551555
def _run():
552556
try:
@@ -555,12 +559,11 @@ def _run():
555559
if trigger.status_code == 200:
556560
LOGGER.info("Trigger succeeded: %s", trigger.data)
557561
self._extract_trigger_ids()
558-
if ws_factory_fn:
559-
ws = ws_factory_fn(trigger)
560-
if ws:
561-
self.start(ws)
562-
return # start() / _on_close manages _closed
563-
LOGGER.error("WS factory returned None")
562+
ws = ws_factory_fn(trigger)
563+
if ws:
564+
self.start(ws)
565+
return # start() / _on_close manages _closed
566+
LOGGER.error("WS factory returned None")
564567
else:
565568
LOGGER.error(
566569
"Failed to trigger command: %s - %s",
@@ -569,9 +572,28 @@ def _run():
569572
)
570573
except Exception as e:
571574
LOGGER.error("Error during trigger: %s", e)
572-
# Mark done (success without WS, or failure)
575+
# Mark done (failure or WS factory returned None)
573576
self.util_response._queue.put(None)
574577
self.util_response._closed.set()
575578

576579
threading.Thread(target=_run, daemon=True).start()
577580
return self.util_response
581+
582+
def _trigger_only(self, trigger_fn: Callable) -> UtilResponse:
583+
"""Run a trigger-only command synchronously (no WebSocket needed)."""
584+
try:
585+
trigger = trigger_fn()
586+
self.util_response.trigger_api_response = trigger
587+
if trigger.status_code == 200:
588+
LOGGER.info("Trigger succeeded: %s", trigger.data)
589+
else:
590+
LOGGER.error(
591+
"Failed to trigger command: %s - %s",
592+
trigger.status_code,
593+
trigger.data,
594+
)
595+
except Exception as e:
596+
LOGGER.error("Error during trigger: %s", e)
597+
self.util_response._queue.put(None)
598+
self.util_response._closed.set()
599+
return self.util_response

src/mistapi/device_utils/__tools/miscellaneous.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def top_command(
353353
def _ws_factory(trigger):
354354
if isinstance(trigger.data, dict) and "url" in trigger.data:
355355
return SessionWithUrl(apisession, url=trigger.data.get("url", ""))
356-
LOGGER.error("Top command command did not return a valid URL: %s", trigger.data)
356+
LOGGER.error("Top command did not return a valid URL: %s", trigger.data)
357357
return None
358358

359359
util_response = UtilResponse()

src/mistapi/device_utils/__tools/shell.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,15 @@ def _on_key_release(key: str) -> None:
327327
elif key == "tab":
328328
k = "\t"
329329
elif key == "up":
330-
k = "\x00\x1b[A"
330+
k = "\x1b[A"
331331
elif key == "right":
332-
k = "\x00\x1b[C"
332+
k = "\x1b[C"
333333
elif key == "down":
334-
k = "\x00\x1b[B"
334+
k = "\x1b[B"
335335
elif key == "left":
336-
k = "\x00\x1b[D"
336+
k = "\x1b[D"
337337
elif key == "backspace":
338-
k = "\x00\x7f"
338+
k = "\x7f"
339339
else:
340340
k = key
341341
data = f"\x00{k}"

0 commit comments

Comments
 (0)