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 addressmqtt_username: MQTT usernamemqtt_password: MQTT passwordmqtt_topics: List of topics to subscribe toport: 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 brokerpublish(topic, payload, qos=2, retain=False): Publish messageget_messages(clear_queue=True): Get queued messagesset_message_callback(callback): Set message received callbackset_connection_callback(callback): Set connection status callbackis_connected(): Check connection statusget_status(): Get detailed connection status
MqttMessage
Represents a received MQTT message:
topic: Message topicpayload: Raw payload bytespayload_str: Payload as UTF-8 stringqos: Quality of Service levelretain: Whether message is retainedtimestamp: When message was receivedto_dict(): Convert to dictionary
Error Handling
The library defines several exception types:
DysonMqttError: Base exceptionConnectionError: Connection issuesAuthenticationError: Authentication failuresTopicError: Topic subscription/publishing issuesClientNotConnectedError: Operations on disconnected clientCleanupError: 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
Metadata
Release files for libdyson-mqtt 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| libdyson_mqtt-0.2.1.tar.gz | 12.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| libdyson_mqtt-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.4 kB
Release files / libdyson_mqtt-0.2.1.tar.gz
| Download URL | libdyson_mqtt-0.2.1.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
acb54cb3f4d6aa9423a8718f7a3d4b5da0f3959fcdc1392cd1cbaa1957d1189d
|
|
BLAKE2b-256 checksum How to use checksums |
fa8896ec216477743e6f49f47fccae0d30c6c000a53a66e2fc579a0abd3afe6d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 25, 2025.
Transparency logRelease files / libdyson_mqtt-0.2.1-py3-none-any.whl
| Download URL | libdyson_mqtt-0.2.1-py3-none-any.whl |
|---|---|
| Size | 10.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5173f165b18181553fb01f5f5935a548257f1eab3e9ebf1702fa3b40513e9be6
|
|
BLAKE2b-256 checksum How to use checksums |
ae6b412e5821c4f2ac103643203ef3479ad99d46ca4b638723ecfab30537c306
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 25, 2025.
Transparency log