Skip to main content

Asynchronous Python SDK for Tonies API and MQTT WebSocket

Project description

Tonies API Client

A Python library for interacting with the Tonies API. This library provides an asynchronous client to manage your Tonies, Creative-Tonies, households, and more. It supports both standard data fetching via GraphQL/REST and real-time event monitoring via WebSocket/MQTT.

Features

  • Asynchronous Client: Built on httpx and websockets.
  • Authentication: Full OAuth2 flow via Keycloak.
  • Data Fetching (GraphQL):
    • User details
    • Households and Members
    • Tonieboxes
    • Creative-Tonies and Content-Tonies
    • Children profiles
  • Device Configuration (REST):
    • Volume limits (Speaker & Headphones)
    • LED & Lightring brightness
    • Toniebox name
    • Accelerometer & Tap gestures
    • Bedtime settings
  • Real-time Events (WebSocket):
    • Monitor connection state (online/offline)
    • Battery levels
    • Tonie placement detection
    • Headphone connection status
    • Bedtime configuration
  • Type Safety: Comprehensive Pydantic models for all data structures.

Installation

You can install the library directly from the source:

git clone https://github.com/Raphzer/tonies-api.git
cd tonies-api
pip install .

For development (editable mode):

pip install -e .

Usage

First, create a .env file in your project root with your Tonies credentials:

TONIE_USERNAME=your_email@example.com
TONIE_PASSWORD=your_password

Basic Data & Configuration

import asyncio
import os
from dotenv import load_dotenv
from tonies_api.client import TonieAPIClient

async def main():
    load_dotenv()
    username = os.getenv("TONIE_USERNAME")
    password = os.getenv("TONIE_PASSWORD")

    async with TonieAPIClient(username, password) as client:
        # Get Households
        households = await client.tonies.get_households()
        
        # Get Tonieboxes
        tonieboxes = await client.tonies.get_households_boxes()
        if tonieboxes:
            box = tonieboxes[0]
            print(f"Found box: {box.name} (Battery: {box.mac_address})")

            # Update Volume
            await client.tonies.set_max_volume(box.household_id, box.id, 75)
            print("Volume updated!")

if __name__ == "__main__":
    asyncio.run(main())

Real-time Events (WebSocket)

import asyncio
import os
from dotenv import load_dotenv
from tonies_api.client import TonieAPIClient

async def event_handler(topic, payload):
    print(f"Received event on {topic}: {payload}")

async def main():
    load_dotenv()
    async with TonieAPIClient(os.getenv("TONIE_USERNAME"), os.getenv("TONIE_PASSWORD")) as client:
        # Register callback
        client.ws.register_callback(event_handler)
        
        # Connect and subscribe
        await client.ws.connect()
        
        # get all box 
        boxes = await client.tonies.get_households_boxes()

        # subscribe to all compatible boxes (v2 gen)
        for box in boxes:
            await client.ws.subscribe_to_toniebox(box)

        # Keep alive
        await asyncio.sleep(60)

if __name__ == "__main__":
    asyncio.run(main())

API Reference

The TonieAPIClient provides access to TonieResources via client.tonies and TonieWebSocket via client.ws.

Data Methods (client.tonies)

  • get_user_details() -> User
  • get_households() -> List[Household]
  • get_households_boxes() -> List[Toniebox]
  • get_tonies() -> List[HouseholdWithTonies]
  • get_children(household_id: str) -> List[Child]
  • get_household_members(household_id: str) -> HouseholdMembersResponse
  • get_content_tonie_details(household_id: str, tonie_id: str) -> List[ContentTonieDetails]

Configuration Methods (client.tonies)

  • set_max_volume(household_id, toniebox_id, volume) -> Toniebox
  • set_max_headphone_volume(household_id, toniebox_id, volume) -> Toniebox
  • set_led_brightness(household_id, toniebox_id, level) -> Toniebox (level: 'on', 'off', 'dimmed')
  • set_toniebox_name(household_id, toniebox_id, name) -> Toniebox
  • set_accelerometer(household_id, toniebox_id, enabled) -> Toniebox
  • set_tap_direction(household_id, toniebox_id, direction) -> Toniebox ('left' or 'right')
  • set_lightring_brightness(household_id, toniebox_id, brightness) -> Toniebox
  • set_bedtime_max_volume(household_id, toniebox_id, volume) -> Toniebox
  • set_bedtime_headphone_max_volume(household_id, toniebox_id, volume) -> Toniebox
  • set_bedtime_lightring_brightness(household_id, toniebox_id, brightness) -> Toniebox

WebSocket Methods (client.ws)

  • connect(): Connects to the real-time server.
  • disconnect(): Closes the WebSocket connection.
  • subscribe_to_toniebox(toniebox): Subscribes to all events for a box.
  • subscribe(topics: List[str]): Manually subscribes to a list of MQTT topics.
  • register_callback(callback): Registers a function async def callback(topic, payload) to handle events.
  • send_toniebox_command(mac_address, command, payload): Sends a control command to the Toniebox (e.g., 'sleep', 'stl').
  • sleep_now(mac_address): Sends a command to immediately put the Toniebox to sleep.

Available MQTT Events

When you subscribe to a Toniebox using client.ws.subscribe_to_toniebox(mac_address), the client automatically subscribes to the wildcard topic external/toniebox/{mac_address}/#.

Common events you might receive include:

  • .../online-state: Updates when the box goes online or offline.
  • .../metrics/battery: Battery level updates.
  • .../metrics/headphones: Headphone connection status.
  • .../app-reply/bedtime-state: Current status of bedtime mode.
  • .../changed-properties: General property changes.
  • .../settings-applied: Confirmation that settings have been applied.
  • .../setup/status: Setup status updates.
  • .../playback/state: Current Tonie on the box.

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

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

tonies_api-0.1.3.tar.gz (24.7 kB view details)

Uploaded Source

Built Distribution

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

tonies_api-0.1.3-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file tonies_api-0.1.3.tar.gz.

File metadata

  • Download URL: tonies_api-0.1.3.tar.gz
  • Upload date:
  • Size: 24.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tonies_api-0.1.3.tar.gz
Algorithm Hash digest
SHA256 df25c6bfa4a32b81f4426c1dda2a0cfb0e665ae5f5939df2f450e066e254cfda
MD5 d7c9466468ccd945f05afa069b166964
BLAKE2b-256 df56d356d7b7a43e1d6c2062665c4d3af87dc7ee9a493ced23b941187093d342

See more details on using hashes here.

File details

Details for the file tonies_api-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: tonies_api-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 24.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tonies_api-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e47a323adc1b09a582bfb839bc5a33a5a8ec25556dcf3e216af97af522b30175
MD5 6b8108e85a692d748a1e3cb038e75596
BLAKE2b-256 b2cb7d90dccaf04c64903f8869d78238f830072650d32fcc79481563f225460f

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