Moonracker Websocket API Client
Connect, request and subscribe to the Moonraker Websockets API without polling.
moonraker-api is a fully async Python 3 based package for interfacing with Moonraker's API.
Getting the package
The easiest way to grab moonraker-api is through PyPI
pip3 install moonraker-api
Use Moonraker-API
Connect and Disconnect
class APIConnector(MoonrakerListener):
def __init__():
self.running = False
self.client = MoonrakerClient(
self,
HOST,
PORT,
API-KEY,
)
async def start(self) -> None:
"""Start the websocket connection."""
self.running = True
return await self.client.connect()
async def stop(self) -> None:
"""Stop the websocket connection."""
self.running = False
await self.client.disconnect()
Query the API
api_connector = APIConnector()
response = await api_connector.client.request("printer.info")
Handle Push Notifications
class APIConnector(MoonrakerListener):
# Other class details, see above ...
async def state_changed(self, state: str) -> None:
"""Notifies of changing websocket state."""
_LOGGER.debug("Stated changed to %s", state)
if state == WEBSOCKET_STATE_CONNECTING:
pass
elif state == WEBSOCKET_STATE_CONNECTED:
pass
elif state == WEBSOCKET_STATE_READY:
pass
elif state == WEBSOCKET_STATE_PAUSED:
pass
elif state == WEBSOCKET_STATE_STOPPED:
pass
async def on_exception(self, exception: BaseException) -> None:
"""Notifies of exceptions from the websocket run loop."""
_LOGGER.debug("Received exception from API websocket %s", str(exception))
if isinstance(exception, ClientNotAuthenticatedError):
self.entry.async_start_reauth(self.hass)
else:
raise exception
async def on_notification(self, method: str, data: Any) -> None:
"""Notifies of state updates."""
_LOGGER.debug("Received notification %s -> %s", method, data)
# Subscription notifications
if method == "notify_status_update":
message = data[0]
timestamp = data[1]
# Do stuff ...
Metadata
Release files for moonraker-api 4.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| moonraker_api-4.0.0.tar.gz | 22.4 kB | Without provenance | Details for moonraker_api-4.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| moonraker_api-4.0.0-py3-none-any.whl | Python 3 | none | any | Details for moonraker_api-4.0.0-py3-none-any.whl |
Total release size: 45.2 kB
Release files / moonraker_api-4.0.0.tar.gz
| Download URL | moonraker_api-4.0.0.tar.gz |
|---|---|
| Size | 22.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f7ca9db4931410e75ce46e1a2d7be0adab6f5e19bc4a33ca0b83e620b4a462f5
|
|
BLAKE2b-256 checksum How to use checksums |
5c66c08171aeacdeda25887dfbbdfd90b4f657f8b2659b03a62cba8ac44f1486
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.20
|
Release files / moonraker_api-4.0.0-py3-none-any.whl
| Download URL | moonraker_api-4.0.0-py3-none-any.whl |
|---|---|
| Size | 22.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1ecfda4bb1be42a101d845e009dd404b71ffdf30811868bfce5c85311f55e784
|
|
BLAKE2b-256 checksum How to use checksums |
a45d4f912ce2e0a6d5782379e6a571305cc59e82ee8da805fa5a527b9597a7dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.20
|
Security for moonraker-api 4.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.