Skip to main content

Python package

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)

Source distribution for moonraker-api 4.0.0
File Size Uploaded
moonraker_api-4.0.0.tar.gz 22.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for moonraker-api 4.0.0
File Interpreter ABI Platform
moonraker_api-4.0.0-py3-none-any.whl Python 3 none any Details

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

Release history Release notifications | RSS feed

This release

4.0.0 This release

2 release files

3.0.1

2 release files

2.0.6

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page