Code reference¶
Auto-generated from the NumPy-style docstrings in src/ via
mkdocstrings. For the HTTP endpoints, see the
HTTP API page instead.
hid_keyboard_server¶
The HTTP bridge: turns requests into USB-HID reports, serves the web remote and HLS segments, and relays RTSP to HLS/WebRTC.
hid_keyboard_server ¶
pi-remote HID keyboard + consumer bridge + web remote + Broadlink IR.
Stdlib only (no third-party imports in this process). Endpoints: GET / -> web remote UI (remote.html) GET/POST /type text -> type a string on the USB keyboard GET/POST /key key[+mod] -> single key (named or single char) GET/POST /press key+mods -> key combo, e.g. Ctrl+A, Alt+Tab GET/POST /media name -> consumer control (HOME/PLAYPAUSE/VOLUP/...) GET/POST /ir cmd=NAME -> send a learned IR code via Broadlink (ir_tool.py)
Auth (optional): set PI_REMOTE_API_KEY; then every request must pass ?token=... or the header X-API-Key. The remote UI is always served so it can read the token from its own URL.
Configuration is read from environment variables (see config/config.example.env): PI_REMOTE_PORT, PI_REMOTE_API_KEY, PI_REMOTE_KEY_DELAY, PI_REMOTE_HTML, PI_REMOTE_IR_TOOL, PI_REMOTE_HID_KBD, PI_REMOTE_HID_CONSUMER
Handler ¶
Bases: BaseHTTPRequestHandler
HTTP request handler exposing the pi-remote API and web remote.
Routes requests to the keyboard / consumer / IR / preview helpers, serves the
remote UI and HLS segments, applies optional API-key auth, and emits CORS
headers. Methods prefixed with _ are internal helpers; the do_*
methods are the standard
:class:http.server.BaseHTTPRequestHandler entry points.
Source code in src/hid_keyboard_server.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | |
do_OPTIONS ¶
do_OPTIONS()
Answer a CORS preflight request with the allowed methods and headers.
Source code in src/hid_keyboard_server.py
463 464 465 466 467 | |
do_GET ¶
do_GET()
Handle an HTTP GET request (parameters from the query string).
Source code in src/hid_keyboard_server.py
468 469 470 | |
do_POST ¶
do_POST()
Handle an HTTP POST request (JSON body, with query-string fallback).
Source code in src/hid_keyboard_server.py
471 472 473 | |
log_message ¶
log_message(*a)
Suppress the default per-request logging to stderr.
Source code in src/hid_keyboard_server.py
517 518 519 | |
char_to_report ¶
char_to_report(c)
Map a single character to its USB HID keyboard report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c
|
str
|
A single character. |
required |
Returns:
| Type | Description |
|---|---|
tuple of (int, int) or None
|
|
Source code in src/hid_keyboard_server.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
type_text ¶
type_text(text)
Type a string on the USB keyboard, one character at a time.
Characters with no HID mapping are skipped. A KEY_DELAY pause is inserted
between keystrokes so the host does not drop fast input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to type. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of characters actually sent. |
Source code in src/hid_keyboard_server.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
press ¶
press(key, mods=None)
Press a single key (named key or character) with optional modifiers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
A named key (e.g. |
required |
mods
|
iterable of str
|
Modifier names to hold while pressing (e.g. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Source code in src/hid_keyboard_server.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
media ¶
media(name)
Send a consumer-control (media) usage to the second HID device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Consumer-control name (e.g. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Source code in src/hid_keyboard_server.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
ir_send ¶
ir_send(name)
Send a learned IR code by shelling out to ir_tool.py.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of a previously learned IR code (e.g. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
RuntimeError
|
If the |
Source code in src/hid_keyboard_server.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
stream_start ¶
stream_start(url)
Start an ffmpeg RTSP->HLS relay (remux only) for the live preview.
Any previous relay is stopped and the output directory is cleared first.
Video is copied (no transcoding) and audio is dropped, so it is light enough
for a Pi Zero. The RTSP_TRANSPORT setting is applied unless it is
"auto".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The RTSP source URL. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The path of the generated HLS playlist ( |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If the ffmpeg binary is not installed. |
Source code in src/hid_keyboard_server.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
stream_stop ¶
stream_stop()
Stop the running ffmpeg relay, if any.
Terminates the process (escalating to kill on timeout) and resets the module-level handle. Safe to call when no relay is running.
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in src/hid_keyboard_server.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
webrtc_start ¶
webrtc_start(url, host)
Register an RTSP source with go2rtc and build a WebRTC player URL.
If url is given it is registered as the preview stream via the go2rtc
API (trying PUT then POST); otherwise the preconfigured
GO2RTC_STREAM is used. The returned URL uses GO2RTC_PUBLIC if set
(for HTTPS / reverse-proxy setups) or the request host otherwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
RTSP source URL, or empty to use the preconfigured stream. |
required |
host
|
str
|
The request's |
required |
Returns:
| Type | Description |
|---|---|
str
|
An embeddable go2rtc |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If go2rtc cannot be reached to register the stream. |
Source code in src/hid_keyboard_server.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
wait_for ¶
wait_for(path, timeout=30)
Block until a filesystem path exists or a timeout elapses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to wait for (e.g. the HID device node). |
required |
timeout
|
float
|
Maximum seconds to wait (default |
30
|
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in src/hid_keyboard_server.py
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | |
ir_tool¶
The Broadlink RM4 IR helper (discover / learn / send).
ir_tool ¶
pi-remote Broadlink RM4 IR helper (multi-device aware).
Requires the broadlink package (see docs/ir.md for armv6-safe install).
Usage:
ir_tool.py discover # list every Broadlink device on the LAN
ir_tool.py use
Paths are configurable via environment (defaults shown): PI_REMOTE_IR_STORE = /var/lib/pi-remote/ir_codes.json PI_REMOTE_IR_DEVFILE = /var/lib/pi-remote/ir_device.json
get_device ¶
get_device(host=None, save=True)
Resolve and authenticate the Broadlink device to use.
Resolution order: the explicit host (matched against discovery), then the
cached device in DEVFILE, then LAN discovery. With multiple devices and no
host/cache, the user is asked to pick one and the process exits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
IP/host of a specific device to use. If omitted, use the cached device or auto-discover. |
None
|
save
|
bool
|
Whether to persist the resolved device to |
True
|
Returns:
| Type | Description |
|---|---|
Device
|
An authenticated device. |
Raises:
| Type | Description |
|---|---|
SystemExit
|
If no device is found, the requested |
Source code in src/ir_tool.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
cmd_discover ¶
cmd_discover()
List every Broadlink device on the LAN (host, MAC, device type).
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
SystemExit
|
If no Broadlink device is found. |
Source code in src/ir_tool.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
cmd_use ¶
cmd_use(host)
Select and cache the active IR blaster by host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
IP/host of the device to make the default (saved to |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
SystemExit
|
If no device is found at |
Source code in src/ir_tool.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
cmd_learn ¶
cmd_learn(name, host=None)
Learn an IR code from the original remote and save it under name.
Puts the device into learning mode and polls for up to ~10 seconds while you press the button on the source remote.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Key to store the captured code under (e.g. |
required |
host
|
str
|
Specific device to learn on; otherwise the cached/auto-resolved device. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
SystemExit
|
If no device is available or no IR signal is captured before timeout. |
Source code in src/ir_tool.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
cmd_send ¶
cmd_send(name, host=None)
Send a previously learned IR code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the saved code to transmit. |
required |
host
|
str
|
Specific device to send from; otherwise the cached/auto-resolved device. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
SystemExit
|
If no code named |
Source code in src/ir_tool.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |