Skip to main content

Homeassistant MQTT Binding for python

PyPI version

This package enables you to implement arbitrary devices in python supported in homeassistant. The communication with homeassistant is handled by MQTT. For example you could write an simple program running on a raspberry pi controlling an LED. By exposing this configuration to homeassistant you can easily control the LED via HA.

The base class handles the following things automatically:

  • registering devices in homeassistant through the mqtt discovery protocol
  • setting the device as available once fully setup
  • setting the device unavailable when quitting the application

Installation

python3 -m pip install homeassistant-mqtt-binding the dependencies should be installed automatically

Usage

  1. create an paho mqtt Client instance connected to your mqtt server
  2. instantiate your desired device and pass the Client instance to the constructor
  3. when your program closes make sure to call the close() function of each device for cleanup

Examples

The following examples require an already running stack of homeassistant and an MQTT server.
See the examples folder for all demo scripts.

  • switch.py: simple switch device that can be toggled by homeassistant

Expanding the application

Since I don't use all devices I haven't implemented them yet. You can easily implement any missing device:

  1. create an empty class subclassing any of the devices (normally you would subclass MQTTDevice or MQTTSensor).
  2. implement the pre_discovery() if necessary:
    It gets called before sending out the discovery payload to the broker. This is the right place to place custom config options for the config dictionary via add_config_option()
  3. implement the post_discovery() if necessary:
    It gets called right after sending out the discovery payload to the broker. Use it to send initial values to HA, if it is necessary that HA knows about the device beforehand.
  4. Implement any callbacks that might be necessary for all devices that can receive data. (commands for switches for example).
  5. If your device needs more topics than state, configuration and set, feel free to implement them additionally.

Metadata

Release files for homeassistant-mqtt-binding 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for homeassistant-mqtt-binding 3.0.0
File Size Uploaded
homeassistant_mqtt_binding-3.0.0.tar.gz 11.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for homeassistant-mqtt-binding 3.0.0
File Interpreter ABI Platform
homeassistant_mqtt_binding-3.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.5 kB

Release files / homeassistant_mqtt_binding-3.0.0.tar.gz

Download URL homeassistant_mqtt_binding-3.0.0.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2f97c3daf2a42d63eb6988cff1d5c38820bec88cda2a0d1179c75d5b362a8578
BLAKE2b-256 checksum
How to use checksums
818d2af162ae13cc15e7583e218cde757e294e9bfb77f074d54ca2fae1bec02b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.8.19 Linux/5.4.109+

Release files / homeassistant_mqtt_binding-3.0.0-py3-none-any.whl

Download URL homeassistant_mqtt_binding-3.0.0-py3-none-any.whl
Size 13.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
31cc0999f20a55cc1d97f5e1fe12dedbbbadcfd317657730a8df367dd1783194
BLAKE2b-256 checksum
How to use checksums
1df74e7182867dfb299ca326c7a0782bcf50904ee4ad5cc9aa055a4c7a04aad2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.8.19 Linux/5.4.109+

Release history Release notifications | RSS feed

This release

3.0.0 This release

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page