Skip to main content

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.

Installation

Install the dependencies required for this library:

pip install hamqa

Example Usage

Below are examples for single value sensors and multi-sensor devices.

Single Value Sensors

Here is an example of how to register a single value sensor and publish its values:

import paho.mqtt.client as mqtt
from hamqa import HAMQTTDevice

mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqtt_client.connect('192.168.1.10', 1883, 60)

lux_sensor = HAMQTTDevice(client=mqtt_client, 
                          base_topic="home",
                          device_id="lx_device")

single_sensor.add_sensor(sensor_name="illumination", 
                         sensor_type="sensor", 
                         device_class="illuminance", 
                         unit_of_measurement="lx")

single_sensor.register_sensors()
single_sensor.publish_value(300)
# single_sensor.remove_device()

Multi-Sensor Devices

Here is an example of how to register a device with multiple sensors and publish their values as a JSON object:

import paho.mqtt.client as mqtt
from hamqa import HAMQTTDevice

mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqtt_client.connect('192.168.1.10', 1883, 60)

# Create a multi-sensor device (for temperature and humidity)
multi_sensor = HAMQTTDevice(client=mqtt_client, 
                            base_topic="home",
                            device_id="temp_hum_device")

# Add sensors for the multi-sensor device
multi_sensor.add_sensor(sensor_name="temperature", 
                        sensor_type="sensor", 
                        device_class="temperature", 
                        unit_of_measurement="°C")
multi_sensor.add_sensor(sensor_name="humidity", 
                        sensor_type="sensor", 
                        device_class="humidity", 
                        unit_of_measurement="%")

multi_sensor.register_sensors()
sensor_values = {"temperature": 22, "humidity": 60}
multi_sensor.publish_value(sensor_values)
# multi_sensor.remove_device()

Key Functions

__init__(self, client, base_topic, device_id, path_pattern=None)

Initializes a new device instance.

  • client: The MQTT client instance.
  • base_topic: The base MQTT topic for sensor values.
  • device_id: Unique identifier for the device.
  • path_pattern: Optional path pattern for Home Assistant MQTT discovery topics.

add_sensor(self, sensor_name, sensor_type, device_class, unit_of_measurement)

Adds a new sensor to the device.

  • sensor_name: The name of the sensor (e.g., temperature, humidity).
  • sensor_type: The type of sensor (e.g., sensor, binary_sensor).
  • device_class: The device class (e.g., temperature, humidity).
  • unit_of_measurement: Unit of measurement for the sensor (e.g., °C, %).

register_sensors(self)

Registers all added sensors with Home Assistant, configuring MQTT discovery topics.

publish_value(self, value)

Publishes the sensor value(s) to MQTT. If the device has multiple sensors, the values are published as a JSON object.

  • value: Either a single value (for single-sensor devices) or a dictionary of values (for multi-sensor devices).

remove_device(self)

Removes all sensors associated with the device from Home Assistant by publishing an empty payload to the discovery topics.

set_path_pattern(self, path_pattern)

Sets a custom MQTT path pattern with placeholders such as {device_id}, {sensor_type}, and {sensor_name}.

Path Structure

The MQTT paths for Home Assistant discovery and sensor values are handled automatically. The discovery paths will include sensor_type, while the state topics used for publishing sensor values will not.

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

Multi-Sensor Value Publishing

When multiple sensors are registered for the same device, the values are published as a JSON object:

{
  "temperature": 22,
  "humidity": 60
}

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:

python 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

hamqa-0.2.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

hamqa-0.2.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file hamqa-0.2.0.tar.gz.

File metadata

  • Download URL: hamqa-0.2.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.1 Linux/6.5.0-1025-azure

File hashes

Hashes for hamqa-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7729c24ff622cd253f49e7b1b4203243a09d321ba34b612113b8015188ec1095
MD5 20554bed9fe5e99228f5ccdb654bf973
BLAKE2b-256 21172195ab859ae36b8b446aa08a3229a8c79af75b34a385d4ab231620156df2

See more details on using hashes here.

File details

Details for the file hamqa-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: hamqa-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.1 Linux/6.5.0-1025-azure

File hashes

Hashes for hamqa-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f9c0e13e09621c528bd337fd68ba8c7d2323147734f34a30298eb0980adfd705
MD5 88e2ca5e6b0166cba5c22ecec4eff392
BLAKE2b-256 e020c0d77c674ecb44cd9af6315dac5a494f4de05b5aa109a49b47682bf4354a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page