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
moonraker_api-2.0.6.tar.gz
(21.8 kB
view details)
Built Distribution
File details
Details for the file moonraker_api-2.0.6.tar.gz
.
File metadata
- Download URL: moonraker_api-2.0.6.tar.gz
- Upload date:
- Size: 21.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b48cd50578d203cd97927cc45c8cb77ca9fbae0eab3b57c859efb6869fe4a6ac |
|
MD5 | 523a05332f3d1ced413de0c04910677e |
|
BLAKE2b-256 | a6cf63ca85b63d8572489ebf8deee2753baf56209407bc5db11de36370b9e7b4 |
File details
Details for the file moonraker_api-2.0.6-py3-none-any.whl
.
File metadata
- Download URL: moonraker_api-2.0.6-py3-none-any.whl
- Upload date:
- Size: 22.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f4d0e9300dd6d932045ea13e38bc783ac69831eb126686b91c9f0f1b3a71c1c3 |
|
MD5 | d457b813d2da518a95b09173806f82d4 |
|
BLAKE2b-256 | 4a200468b347b7f0e897184cbf08ab4e1b14da37e1f0d78e6ab1f527edc29415 |