MoonrakerClient¶
pymoonraker.client.MoonrakerClient
¶
Async-first client for the Moonraker API.
Provides: * WebSocket RPC with auto-reconnect. * HTTP transport for file operations and one-shot queries. * Typed event system for Moonraker notifications. * Auth management (API key, JWT, oneshot).
Usage::
async with MoonrakerClient("192.168.1.100") as client:
info = await client.server_info()
print(info.klippy_state)
client.on(EventType.STATUS_UPDATE, my_handler)
await client.subscribe_objects({"toolhead": None})
Source code in src/pymoonraker/client.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 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 226 227 228 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 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 347 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 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | |
announcements
property
¶
announcements: AnnouncementsNamespace
Announcements namespace: list, dismiss, dismiss_wake.
database
property
¶
database: DatabaseNamespace
Database namespace: get_item, list_namespaces, get_namespace_item.
job_queue
property
¶
job_queue: JobQueueNamespace
Job queue namespace: status, add, delete, pause, resume, start.
machine
property
¶
machine: MachineNamespace
Machine namespace: system_info, proc_stats, reboot, shutdown.
power
property
¶
power: PowerNamespace
Power namespace: get_devices, get_device_status, toggle_device.
printer
property
¶
printer: PrinterNamespace
Printer namespace: info, gcode, objects, print control.
update_manager
property
¶
update_manager: UpdateManagerNamespace
Update manager namespace: get_status, refresh.
call
async
¶
call(method: str, params: dict[str, Any] | None = None, *, timeout: float | None = None) -> Any
Send a JSON-RPC request over WebSocket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
Moonraker JSON-RPC method (for example |
required |
params
|
dict[str, Any] | None
|
Optional JSON-RPC params object. |
None
|
timeout
|
float | None
|
Optional request timeout override in seconds. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Decoded |
Source code in src/pymoonraker/client.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
connect
async
¶
connect() -> None
Establish WebSocket and HTTP connections, identify, and start listeners.
Source code in src/pymoonraker/client.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
disconnect
async
¶
disconnect() -> None
Gracefully shut down all connections and background tasks.
Source code in src/pymoonraker/client.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
download_file
async
¶
download_file(root: str, file_path: str) -> bytes
Download a file from Moonraker over HTTP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
str
|
Moonraker file root. |
required |
file_path
|
str
|
Path to the file inside |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
Raw file bytes. |
Source code in src/pymoonraker/client.py
440 441 442 443 444 445 446 447 448 449 450 451 | |
emergency_stop
async
¶
emergency_stop() -> None
Trigger an immediate emergency stop.
Source code in src/pymoonraker/client.py
392 393 394 | |
firmware_restart
async
¶
firmware_restart() -> None
Restart Klipper with a firmware restart (MCU reset).
Source code in src/pymoonraker/client.py
459 460 461 | |
gcode
async
¶
gcode(script: str) -> str
Execute a G-code script.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
script
|
str
|
G-code commands to execute. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Moonraker response string. |
Source code in src/pymoonraker/client.py
380 381 382 383 384 385 386 387 388 389 390 | |
http_request
async
¶
http_request(method: str, path: str, **kwargs: Any) -> Any
Send an HTTP request through Moonraker's REST endpoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
HTTP method such as |
required |
path
|
str
|
Endpoint path beginning with |
required |
**kwargs
|
Any
|
Additional arguments forwarded to the HTTP transport. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Decoded HTTP response payload. |
Source code in src/pymoonraker/client.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |
klippy_state
async
¶
klippy_state() -> KlippyState
Return the current Klippy state.
Source code in src/pymoonraker/client.py
325 326 327 328 329 330 331 | |
on
¶
on(event: str | EventType, callback: Callback) -> Callable[[], None]
Register a callback for a Moonraker notification event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
str | EventType
|
Moonraker notification name or |
required |
callback
|
Callback
|
Sync or async callback invoked when the event is received. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[], None]
|
Callable with no arguments that unsubscribes the callback. |
Source code in src/pymoonraker/client.py
244 245 246 247 248 249 250 251 252 253 254 255 | |
once
¶
once(event: str | EventType, callback: Callback) -> None
Register a one-shot callback for an event.
The callback is removed after the first matching notification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
str | EventType
|
Moonraker notification name or |
required |
callback
|
Callback
|
Sync or async callback invoked once. |
required |
Source code in src/pymoonraker/client.py
257 258 259 260 261 262 263 264 265 266 267 | |
print_cancel
async
¶
print_cancel() -> None
Cancel the current print.
Source code in src/pymoonraker/client.py
410 411 412 | |
print_pause
async
¶
print_pause() -> None
Pause the current print.
Source code in src/pymoonraker/client.py
402 403 404 | |
print_resume
async
¶
print_resume() -> None
Resume the paused print.
Source code in src/pymoonraker/client.py
406 407 408 | |
print_start
async
¶
print_start(filename: str) -> None
Start printing the specified file.
Source code in src/pymoonraker/client.py
398 399 400 | |
printer_info
async
¶
printer_info() -> PrinterInfo
Query printer.info and return a typed model.
Source code in src/pymoonraker/client.py
320 321 322 323 | |
query_objects
async
¶
query_objects(objects: dict[str, list[str] | None]) -> dict[str, Any]
Query one or more printer objects.
Example::
result = await client.query_objects({
"toolhead": ["position", "homed_axes"],
"heater_bed": None, # all fields
})
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objects
|
dict[str, list[str] | None]
|
Mapping of object name to field list or |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
|
Source code in src/pymoonraker/client.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | |
restart_klipper
async
¶
restart_klipper() -> None
Soft-restart Klipper.
Source code in src/pymoonraker/client.py
455 456 457 | |
restart_moonraker
async
¶
restart_moonraker() -> None
Restart the Moonraker server process.
Source code in src/pymoonraker/client.py
463 464 465 | |
server_info
async
¶
server_info() -> ServerInfo
Query server.info and return a typed model.
Source code in src/pymoonraker/client.py
315 316 317 318 | |
subscribe_objects
async
¶
subscribe_objects(objects: dict[str, list[str] | None]) -> dict[str, Any]
Subscribe to printer object updates.
Updated values arrive as notify_status_update events.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objects
|
dict[str, list[str] | None]
|
Mapping of object name to field list or |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Initial snapshot payload returned by Moonraker. |
Source code in src/pymoonraker/client.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
upload_file
async
¶
upload_file(file_path: str, content: bytes, *, root: str = 'gcodes', target_path: str | None = None) -> Any
Upload a file to Moonraker over HTTP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Source filename (used as upload name unless |
required |
content
|
bytes
|
File bytes. |
required |
root
|
str
|
Moonraker file root, usually |
'gcodes'
|
target_path
|
str | None
|
Optional path within the selected root. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Moonraker upload response payload. |
Source code in src/pymoonraker/client.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | |
wait_connected
async
¶
wait_connected(timeout: float | None = None) -> None
Block until the client is connected (useful after auto-reconnect).
Source code in src/pymoonraker/client.py
171 172 173 | |