Files
MeshTerm/meshtools/ui/device_picker.py
T
Jean-Pierre MartineauandClaude Opus 4.8 43353f83a9 Initial commit: MeshTools with device discovery & selection
MeshTools is a Rich+Questionary interactive / Typer-scriptable toolkit for
tuning a MeshCore mesh over a serial companion device, with SQLite-logged runs
and Plotly visualizations. Includes the skeleton (trace, info, history),
TX-power optimization, and a MockDevice simulator (--mock).

Device discovery & selection:
- core/discovery.py: enumerate serial ports (pyserial), flag/sort likely LoRa
  hardware by USB vendor id, stable_id for replug-safe identity.
- core/device_store.py: remember the last successfully connected device in
  <config_dir>/devices.json (independent of --db).
- core/selection.py: non-interactive resolver (--port > profile > remembered >
  sole device > guidance error) for the CLI.
- ui/device_picker.py + menu startup: interactive picker preselecting the
  remembered default; banner reflects the choice.
- tools/devices.py: `meshtools devices` lists/marks attached devices.
- AppContext resolves the port and records last-known-good on connect success.

Fixes:
- theme.py: force UTF-8 on stdout/stderr so glyphs don't crash legacy consoles.
- connection.py: friendly message when meshcore is missing; pin meshcore>=2.3.
- trace/tx-optimize: fall back to free-text target entry when the device
  reports no contacts (autocomplete requires non-empty choices).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-20 17:02:34 -04:00

69 lines
2.6 KiB
Python

"""Interactive companion-device picker (questionary).
Shown at the start of the interactive menu (and from the ``devices`` tool) when no port
was given explicitly. It lists the discovered devices, marks the remembered "last known
good" one, and preselects it as the default. The chosen device becomes the session's
active device; it is persisted as the new default once it actually connects.
"""
from __future__ import annotations
from typing import Optional
import questionary
from rich.console import Console
from ..core.device_store import RememberedDevice
from ..core.discovery import DiscoveredDevice
from .menu import _MENU_STYLE
async def prompt_device(
console: Console,
devices: list[DiscoveredDevice],
remembered: Optional[RememberedDevice],
) -> Optional[DiscoveredDevice]:
"""Prompt the user to choose a companion device.
Args:
console: Console used for any surrounding messaging.
devices: Discovered devices (likely-LoRa first).
remembered: The remembered default, if any, used to mark and preselect a row.
Returns:
The chosen :class:`DiscoveredDevice`, or ``None`` if the user cancelled or chose
to skip device selection (e.g. to run against ``--mock`` / configure later).
"""
if not devices:
console.print(
"[warn]No serial devices detected.[/warn] "
"Plug one in, pass [accent]--port[/accent], or run with [accent]--mock[/accent]."
)
return None
default_choice: Optional[questionary.Choice] = None
choices: list[questionary.Choice | questionary.Separator] = []
for device in devices:
is_remembered = remembered is not None and remembered.matches(device)
star = "★ " if is_remembered else " "
vendor = f" [{device.vendor_label}]" if device.vendor_label else ""
lora = " · likely LoRa" if device.is_likely_lora else ""
choice = questionary.Choice(title=f"{star}{device.label}{vendor}{lora}", value=device)
choices.append(choice)
# Preselect the remembered device; else default to the first (top-ranked) row.
if is_remembered:
default_choice = choice
if default_choice is None:
default_choice = choices[0] # type: ignore[assignment] # always a Choice here
choices.append(questionary.Separator(" "))
choices.append(questionary.Choice(title="skip (use --mock / configure later)", value=None))
return await questionary.select(
"Select a companion device:",
choices=choices,
default=default_choice,
style=_MENU_STYLE,
qmark="◆",
).ask_async()