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_PASSED:
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-1.0.0.tar.gz
(20.5 kB
view hashes)
Built Distribution
Close
Hashes for moonraker_api-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eada56b46b5aecc6029af4f483a6278b3a916c565f1bc70ba55731c43f9e821b |
|
MD5 | 1d480609bcba62bcbc37ddd6a279c3a6 |
|
BLAKE2b-256 | 3465f19e9382b6cf0e6076a659044adbe563b37fb6e6134ac6eb85ba5b5ffd93 |