API reference
The public API lives in st_uhubm; the implementation is in
st_uhubm.cli_backend.
Hubs and the manager
- class st_uhubm.cli_backend.HubManager(binary: str = 'cusbi', use_sudo: bool = True, password: str = '', persist: bool = False, timeout: int = 10, logger: Callable[[str], None] | None = None)[source]
Configuration and command execution for one or more managed hubs.
A
HubManagerholds how to invoke StarTech’scusbi/cusbabinary — path,sudo, password, persistence, timeout — and turns its output intoHubobjects. It is the single entry point to the hardware:discover()enumerates connected hubs andhub()returns one known hub with its state populated. EveryHubit produces is attached back to this manager, so the hub’s own methods reuse this config.- binary
Binary name or path (
"cusbi"on x86/AMD64,"cusba"on ARM).- Type:
str
- use_sudo
Run the binary via
sudo(needed to open the tty unless a udev rule grants access).- Type:
bool
- password
Hub password; sent with mutating commands when non-empty.
- Type:
str
- persist
When
True, writes go straight to flash (/F) instead of the volatile/Sform.- Type:
bool
- timeout
Per-command timeout in seconds.
- Type:
int
- logger
Optional callable invoked with the exact argv and the binary’s output; used by the CLI
--verboseflag and the GUI console.- Type:
Callable[[str], None] | None
Example
Discover hubs and turn every port on:
mgr = HubManager(binary="cusbi", use_sudo=True) for hub in mgr.discover(): hub.set_all(True)
- class st_uhubm.cli_backend.Hub(port: str, n_ports: int = 0, states: dict[int, bool]=<factory>, firmware: str = '?', serial: str = '?', model: str = '?', manager: HubManager | None = None)[source]
A single managed USB hub, addressed by its serial control port.
A
Hubis normally obtained fromHubManager.discover()orHubManager.hub()rather than built directly, so it arrives attached to aHubManagerthat runs the binary on its behalf. The query and mutation methods go through that manager and keepstatesin sync, so readingstates(oris_on()) right after a call reflects the change with no extra round-trip. Callrefresh()to re-read the live state from hardware.- port
Control-port path the hub is addressed by, e.g.
"/dev/ttyUSB0".- Type:
str
- n_ports
Number of switchable downstream ports.
- Type:
int
- states
Map of 1-based port number to
True(on) /False(off).- Type:
dict[int, bool]
- firmware
Firmware revision reported by the hub, e.g.
"v04".- Type:
str
- serial
Hub serial number, or
"?"if the hub did not report one.- Type:
str
- model
Human-readable model string, or
"?"if unknown.- Type:
str
- manager
HubManagerused to run commands;Noneuntil attached.- Type:
Example
Read a hub and switch one port off (cached state updates in place):
mgr = HubManager(use_sudo=False) hub = mgr.hub("/dev/ttyUSB0") hub.set_port(3, False) assert hub.is_on(3) is False
- change_password(old: str, new: str) None[source]
Change the hub password (
/P). Updates the manager’s password.
- set_all(on: bool, persist: bool | None = None) None[source]
Turn every port on or off in one command.
- set_port(n: int, on: bool, persist: bool | None = None) None[source]
Turn a single port on or off (convenience wrapper for one port).
Parsers
- st_uhubm.cli_backend.parse_query_all(raw: str) list[str][source]
Parse
cusbi /Q -Foutput into a list of control ports (firmware v04+).The record is the 4-digit hub count, a comma, then the full device paths:
"0001,/dev/ttyUSB0" -> ["/dev/ttyUSB0"] "0002,/dev/ttyUSB0,/dev/ttyUSB1" -> ["/dev/ttyUSB0", "/dev/ttyUSB1"]
Returns an empty list if the input is empty or malformed; discovery falls back to device enumeration in that case rather than raising.
- st_uhubm.cli_backend.parse_hub_info(raw: str) tuple[int, dict[int, bool], str, str, str][source]
Parse
cusbi /Q:<port> -Foutput (firmware v04+).The comma-delimited record is:
"FBFFFFFF,7,v04,00020000149E,7-port Managed USB Hub" # bitmap(hex), port count(dec), firmware, serial, model
Returns
(n_ports, states, firmware, serial, model). The port-state bitmap is 32 bits, little-endian by byte; bitn-1corresponds to portn(1= on).Raises
HubParseErroron malformed input.
- st_uhubm.cli_backend.discover(**kwargs) list[Hub][source]
Build a
HubManagerfromkwargsand discover hubs.
Errors
Exception hierarchy for st_uhubm.
- exception st_uhubm.errors.BinaryNotFound(binary: str)[source]
The StarTech binary (cusbi/cusba) could not be located.
- exception st_uhubm.errors.HubCommandError(argv: list[str], returncode: int, stderr: str = '')[source]
The Startech binary returned a non-zero exit code.