Skip to main content

Python library for MQTT communication with Dyson devices

Project description

libdyson-mqtt

A Python library for MQTT communication with Dyson devices.

Overview

libdyson-mqtt provides a clean, non-blocking interface for communicating with Dyson devices over MQTT. The library handles connection management, message queuing, and provides callbacks for real-time message processing.

Features

  • Non-blocking operations: All operations are asynchronous and won't block your application
  • Clean connection management: Automatic connection handling with proper cleanup
  • Message queuing: Messages are queued internally for processing
  • Callback support: Real-time callbacks for messages and connection status
  • Type safety: Full type annotations and mypy support
  • Comprehensive testing: Unit and integration tests included

Installation

pip install libdyson-mqtt

For development:

pip install -e .[dev]

Quick Start

from libdyson_mqtt import DysonMqttClient, ConnectionConfig

# Configure connection
config = ConnectionConfig(
    host="192.168.1.100",  # Your Dyson device IP
    mqtt_username="your_username",
    mqtt_password="your_password", 
    mqtt_topics=["475/device/status", "475/device/command"],
    port=1883,
    keepalive=60
)

# Create and connect client
client = DysonMqttClient(config)
client.connect()

# Check if connected
if client.is_connected():
    print("Connected successfully!")
    
    # Publish a command
    client.publish("475/device/command", '{"command": "status"}')
    
    # Get received messages
    messages = client.get_messages()
    for msg in messages:
        print(f"Topic: {msg.topic}, Payload: {msg.payload_str}")

# Clean disconnect
client.disconnect()

Using Context Manager

For automatic connection management:

from libdyson_mqtt import DysonMqttClient, ConnectionConfig

config = ConnectionConfig(
    host="192.168.1.100",
    mqtt_username="your_username",
    mqtt_password="your_password",
    mqtt_topics=["475/device/status", "475/device/command"]
)

# Automatically connects and disconnects
with DysonMqttClient(config) as client:
    client.publish("475/device/command", '{"command": "status"}')
    messages = client.get_messages()

Using Callbacks

For real-time message processing:

def on_message(message):
    print(f"Received: {message.topic} -> {message.payload_str}")

def on_connection_change(connected, error):
    if connected:
        print("Connected to device!")
    else:
        print(f"Connection lost: {error}")

client = DysonMqttClient(config)
client.set_message_callback(on_message)
client.set_connection_callback(on_connection_change)

client.connect()
# Messages will now be processed in real-time via callbacks

API Reference

ConnectionConfig

Configuration class for MQTT connection:

  • host: IPv4 address or IPv6 .local DNS address
  • mqtt_username: MQTT username
  • mqtt_password: MQTT password
  • mqtt_topics: List of topics to subscribe to
  • port: MQTT port (default: 1883)
  • keepalive: Keep-alive interval in seconds (default: 60)
  • client_id: Optional custom client ID

DysonMqttClient

Main client class for MQTT communication:

Methods

  • connect(): Connect to MQTT broker (non-blocking)
  • disconnect(): Disconnect from broker
  • publish(topic, payload, qos=2, retain=False): Publish message
  • get_messages(clear_queue=True): Get queued messages
  • set_message_callback(callback): Set message received callback
  • set_connection_callback(callback): Set connection status callback
  • is_connected(): Check connection status
  • get_status(): Get detailed connection status

MqttMessage

Represents a received MQTT message:

  • topic: Message topic
  • payload: Raw payload bytes
  • payload_str: Payload as UTF-8 string
  • qos: Quality of Service level
  • retain: Whether message is retained
  • timestamp: When message was received
  • to_dict(): Convert to dictionary

Error Handling

The library defines several exception types:

  • DysonMqttError: Base exception
  • ConnectionError: Connection issues
  • AuthenticationError: Authentication failures
  • TopicError: Topic subscription/publishing issues
  • ClientNotConnectedError: Operations on disconnected client
  • CleanupError: Cleanup failures
from libdyson_mqtt.exceptions import ConnectionError, ClientNotConnectedError

try:
    client.connect()
except ConnectionError as e:
    print(f"Failed to connect: {e}")

try:
    client.publish("test/topic", "message")
except ClientNotConnectedError:
    print("Not connected to broker")

Development

Install development dependencies:

pip install -e .[dev]

Run tests:

pytest

Run type checking:

mypy src/

Format code:

black src/ tests/
isort src/ tests/

Requirements

  • Python 3.9+
  • paho-mqtt >= 1.6.0

License

MIT License. See LICENSE file for details.

Contributing

Contributions are welcome! Please read the contributing guidelines and submit pull requests to the main repository.

Home Assistant Integration

This library is designed to work well with Home Assistant integrations. The non-blocking design and callback system make it suitable for use in Home Assistant custom components:

# In your Home Assistant integration
import asyncio
from libdyson_mqtt import DysonMqttClient, ConnectionConfig

class DysonDevice:
    def __init__(self, hass, config):
        self.hass = hass
        self.client = DysonMqttClient(config)
        self.client.set_message_callback(self._handle_message)
        
    def _handle_message(self, message):
        # Process device status updates
        self.hass.loop.call_soon_threadsafe(
            self._update_state, message
        )
        
    async def _update_state(self, message):
        # Update Home Assistant entity state
        pass

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

libdyson_mqtt-0.1.1b1.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

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

libdyson_mqtt-0.1.1b1-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file libdyson_mqtt-0.1.1b1.tar.gz.

File metadata

  • Download URL: libdyson_mqtt-0.1.1b1.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for libdyson_mqtt-0.1.1b1.tar.gz
Algorithm Hash digest
SHA256 3e097ae5b0807c72b2a22f6e97fa4ae865b54784c1a008a7a4e11acb0661ad56
MD5 59380112321d0ead513d57a3ebc8223f
BLAKE2b-256 0930ef9e96e58afdede26f05f6d476af9cbff0c194ab7b410795279b557c8fd1

See more details on using hashes here.

Provenance

The following attestation bundles were made for libdyson_mqtt-0.1.1b1.tar.gz:

Publisher: publish-to-pypi.yml on cmgrayb/libdyson-mqtt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file libdyson_mqtt-0.1.1b1-py3-none-any.whl.

File metadata

File hashes

Hashes for libdyson_mqtt-0.1.1b1-py3-none-any.whl
Algorithm Hash digest
SHA256 8513763b3887e97fd3c856905b533e63b45ecc1c122c482a9ef87a3393ed4a75
MD5 463a6b9f39f684b5ea7d045f2e7d48de
BLAKE2b-256 88398d8bb650305acf1ed2cfd9b1a14c5ef487f941b20f3365939ec4220c396a

See more details on using hashes here.

Provenance

The following attestation bundles were made for libdyson_mqtt-0.1.1b1-py3-none-any.whl:

Publisher: publish-to-pypi.yml on cmgrayb/libdyson-mqtt

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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