Skip to main content

Async websocket API client for Moonraker

Project description

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 ...

Project details


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-3.0.1.tar.gz (22.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

moonraker_api-3.0.1-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

Details for the file moonraker_api-3.0.1.tar.gz.

File metadata

  • Download URL: moonraker_api-3.0.1.tar.gz
  • Upload date:
  • Size: 22.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.20

File hashes

Hashes for moonraker_api-3.0.1.tar.gz
Algorithm Hash digest
SHA256 34885f36adaefb844fcaf01e3ca17b9158728b80f0045b1f767857f44fc38cf1
MD5 cf7157abcd0269cdefb24675924d6c23
BLAKE2b-256 2e9d08f8d9a31371dc5c6a3cb99fa5902d2dcd64dd61764165e1fc92670e8374

See more details on using hashes here.

File details

Details for the file moonraker_api-3.0.1-py3-none-any.whl.

File metadata

  • Download URL: moonraker_api-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 22.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.20

File hashes

Hashes for moonraker_api-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2db85ccfb8f97832b31d293ea1814b40b83db89c6a0fc69173be4486d2e3ddda
MD5 99086dc4f346a57cb0b7a01f4b39cea0
BLAKE2b-256 afc73c4920fe49258926d223ae63f6f358d65ef6e38291f5beaabd3f4a39320d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page