Skip to content

mpolinowski/ha-mqtt-python

Repository files navigation

Home Assistant :: MQTT Discovery

The discovery of MQTT devices will enable one to use MQTT devices with only minimal configuration effort on the side of Home Assistant. The configuration is done on the device itself and the topic used by the device. Similar to the HTTP binary sensor and the HTTP sensor. To prevent multiple identical entries if a device reconnects, a unique identifier is necessary. Two parts are required on the device side: The configuration topic which contains the necessary device type and unique identifier, and the remaining device configuration without the device type.

  1. Start by defining entities for your device, e.g. INSTAR IN-9408 2k+ WQHD IP Camera.
  2. Prepare the data to be used by the mqtt5-client script by adding all topics and payloads to an JSON array.
  3. Add your MQTT broker credentials to the configuration file.
  4. You can test run the script python mqtt5_client.py.
  5. Add an automation to Home Assistant that runs the Python script through the Shell Extension whenever your camera connects to your broker - making sure that your camera "entities" remain available in Home Assistant.

Home Assistant :: MQTT Discovery :: INSTAR IN-9408 2k+ WQHD IP Camera

UPDATE: Added Option Parse to the client script to allow you to define the configuration file you want to use when you call the script. Point the client to the JSON file you want to use with an -f flag. This way it is now possible to add several configuration files for all your MQTT devices and use the same Python MQTT client to register those your devices when they connect.

Adding the Script as a Service

  1. Upload this repository into a folder python_scripts inside HA's config directory.
  2. Create another folder next to it called shell.
  3. Add the following shell script that points the Python binary inside the HA docker container to your Python script:
mkdir config/shell
nano config/shell/mqtt_autodiscover_9408_garden.sh

mqtt_autodiscover_9408_garden.sh

#!/bin/bash
python /config/python_scripts/mqtt5_client.py -f /config/python_scripts/test.json
  1. To activate the Shell Extension and expose our script to HA we need to add the following lines to the HA configuration.yaml:
# Auto configure 9408 with mqtt
shell_command:
  mqtt_autodiscover_9408_garden: /bin/ash /config/shell//mqtt_autodiscover_9408_garden.sh
  1. Restart HA - select Call service as your Automation Action and you should find your shell script listed as a service:

Home Assistant :: MQTT Discovery :: INSTAR IN-9408 2k+ WQHD IP Camera