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-4.0.0.tar.gz (22.4 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-4.0.0-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for moonraker_api-4.0.0.tar.gz
Algorithm Hash digest
SHA256 f7ca9db4931410e75ce46e1a2d7be0adab6f5e19bc4a33ca0b83e620b4a462f5
MD5 615fe7f10738d0a9913c7568f7313262
BLAKE2b-256 5c66c08171aeacdeda25887dfbbdfd90b4f657f8b2659b03a62cba8ac44f1486

See more details on using hashes here.

File details

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

File metadata

  • Download URL: moonraker_api-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 22.8 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-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1ecfda4bb1be42a101d845e009dd404b71ffdf30811868bfce5c85311f55e784
MD5 f5c195dd72cfce6ede12dd6944b4c3a2
BLAKE2b-256 a45d4f912ce2e0a6d5782379e6a571305cc59e82ee8da805fa5a527b9597a7dd

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