Home Assistant MQTT autodiscovery sensor registration and publishing library
Project description
hamqa - Home Assistant MQTT autodiscovery sensor registration library
The hamqa library provides an easy way to register and manage MQTT-based sensors for Home Assistant. It handles both devices producing a single or multiple values and allows to easily push updates from those devices to be consumed by Home Assistant.
Features
- Register and manage multiple sensors for a single device.
- Automatically handle Home Assistant MQTT discovery.
- Publish sensor values in both single-sensor and multi-sensor devices.
- Flexible configuration of MQTT topic paths.
- Easily remove devices from Home Assistant via MQTT.
- Includes a micropython equivalent implementation
Installation
Install the dependencies required for this library:
pip install hamqa
Example Usage
Below are examples for one or more sensors.
import paho.mqtt.client as mqtt
from hamqa import HAMQTTDevice
ip_address = 'xxx.xxx.xxx.xxx'
mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqtt_client.connect(ip_address, 1883, 60)
single_sensor = HAMQTTDevice(client=mqtt_client,
base_topic="home",
device_id="lx_device")
single_sensor.add_sensor(sensor_name="illumination",
kwargs={"device_class":"illuminance", "unit_of_measurement":"lx"})
single_sensor.register_sensors()
single_sensor.publish_value({'illumination': 300})
import paho.mqtt.client as mqtt
from hamqa import HAMQTTDevice
ip_address = 'xxx.xxx.xxx.xxx'
mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqtt_client.connect(ip_address, 1883, 60)
multi_sensor = HAMQTTDevice(client=mqtt_client,
base_topic="home",
device_id="temp_hum_device")
multi_sensor.add_sensor(sensor_name="temperature",
kwargs={"device_class":"temperature", "unit_of_measurement":"°C"})
multi_sensor.add_sensor(sensor_name="humidity",
kwargs={"device_class":"humidity", "unit_of_measurement":"%"})
multi_sensor.register_sensors()
sensor_values = {"temperature": 22, "humidity": 60}
multi_sensor.publish_value(sensor_values)
Home Assistant Discovery Path Example
For a temperature sensor, the discovery topic will look like:
homeassistant/sensor/multi_sensor/temperature/config
Sensor Value Topic Example
For the same temperature sensor, the sensor value topic will be:
home/multi_sensor/temperature/state
Custom Path Patterns
You can customize the MQTT path for Home Assistant discovery by providing a custom path_pattern. The placeholders {device_id}, {sensor_type}, and {sensor_name} will be replaced with the appropriate values.
Example:
custom_pattern = "custom/{device_id}/{sensor_type}/{sensor_name}/config"
multi_sensor.set_path_pattern(custom_pattern)
This will change the discovery path to:
custom/multi_sensor/sensor/temperature/config
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
Built Distribution
File details
Details for the file hamqa-0.4.0.tar.gz
.
File metadata
- Download URL: hamqa-0.4.0.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.1 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f05c47ecd2e3532f5882ec2590d59ed0c76aaa06a9ad3075439e3337d983feb |
|
MD5 | caef70c1d117146c96899ee818c127ed |
|
BLAKE2b-256 | 2acb4bebb828bb1daf188cdcc8120dd991a8f43d0414e361620cff4ff17e9204 |
File details
Details for the file hamqa-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: hamqa-0.4.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.1 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 033076453974dcf0cd2d31c14b89a0cb604acec5511f70be778ed9c5764f954c |
|
MD5 | bb2eb7f738a1bcabfbf84ae48f7249fe |
|
BLAKE2b-256 | f6a0a56c161e980a6aea41e6c041e615fe9b1af698449de7c69e55f5b91d5bda |