Async websocket API client for Moonraker
Project description
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 ...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file moonraker-api-2.0.5.tar.gz.
File metadata
- Download URL: moonraker-api-2.0.5.tar.gz
- Upload date:
- Size: 20.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72665aac4a07a7e694545d956e76bf555249a21098de952f2b00e17565a3d019
|
|
| MD5 |
cf497ce0c70a0595c10c1ee4c0364078
|
|
| BLAKE2b-256 |
1839942ba48d08a2451b242c48a0db3dd5d83e091c35d99cdf7571fbf2472043
|
File details
Details for the file moonraker_api-2.0.5-py3-none-any.whl.
File metadata
- Download URL: moonraker_api-2.0.5-py3-none-any.whl
- Upload date:
- Size: 22.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
24eae03e64107a4d8bdccc6c4e697d72faee1006cd128e93b1a4cfef9e68b483
|
|
| MD5 |
f9cd1124f905db41870b477f9a1daaf3
|
|
| BLAKE2b-256 |
a1eba562e802763c09fcdf2f1ec9438bae0d1d3859cc873ed8397588aaaa7ea2
|