Skip to main content

No project description provided

Project description

Table of Contents

ha-mqtt-discoverable

A python 3 module that takes advantage of HA(Home Assistant('s MQTT discovery protocol to create sensors without having to define anything on the HA side.

Using MQTT discoverable devices lets us add new sensors and devices to HA without having to restart HA. This module includes scripts to make it easy to create discoverable devices from the command line if you don't want to bother writing python.

Installing

Python

pip install ha-mqtt-discoverable if you want to use it in your python scripts. This will also install the hmd utility scripts.

Docker

If all you want to do is use the command line tools, the simplest way is to use them with docker or nerdctl. It won't interfere with your system python and potentially cause you issues there. You can use the unixorn/ha-mqtt-discoverable image on dockerhub directly, but if you add $reporoot/bin to your $PATH, the hmd script in there will automatically run the command line tools inside a docker container with docker or nerdctl, depending on what it finds in your $PATH.

Supported Types

Binary Sensors

Usage

Here is an example that creates a binary sensor.

from ha_mqtt_discoverable.sensors import BinarySensor

# Create a settings dictionary
#
# Mandatory Keys:
#  mqtt_server
#  mqtt_user
#  mqtt_password
#  device_id
#  device_name
#  device_class
#
# Optional Keys:
#  mqtt_prefix - defaults to homeassistant
#  payload_off
#  payload_on
#  unique_id

configd = {
    "mqtt_server": "mqtt.example.com",
    "mqtt_prefix": "homeassistant",
    "mqtt_user": "mqtt_user",
    "mqtt_password": "mqtt_password",
    "device_id": "device_id",
    "device_name":"MySensor",
    "device_class":"motion",
}

mysensor = BinarySensor(settings=configd)
mysensor.on()
mysensor.off()

Devices

Usage

Here's an example that will create a MQTT device and add multiple sensors to it.

from ha_mqtt_discoverable.device import Device

# Create a settings dictionary
#
# Mandatory Keys:
#  mqtt_server
#  mqtt_user
#  mqtt_password
#  device_id
#  device_name
#  device_class
#  unique_id
#
# Optional Keys:
#  client_name
#  manufacturer
#  model
#  mqtt_prefix - defaults to homeassistant

configd = {
    "mqtt_server": "mqtt.example.com",
    "mqtt_prefix": "homeassistant",
    "mqtt_user": "mqtt_user",
    "mqtt_password": "mqtt_password",
    "device_id": "device_id",
    "device_name":"MySensor",
    "device_class":"motion",
    "manufacturer":"Acme Products",
    "model": "Rocket Skates",
}

device = Device(settings=configd)

# You can add more than one metric to a device
device.add_metric(
    name="Left skate thrust",
    value=33,
    configuration={"name": f"Left Skate Thrust"},
)
device.add_metric(
    name="Right skate thrust",
    value=33,
    configuration={"name": f"Right Skate Thrust"},
)

# Nothing gets written to MQTT until we publish
device.publish()

# Do your own code

# If we add a metric using the same name as an existing metric, the value is updated
device.add_metric(
    name="Right skate thrust",
    value=99,
    configuration={"name": f"Right Skate Thrust"},
)
device.publish()

Scripts Provided

The ha_mqtt_discoverable module also installs the following helper scripts you can use in your own shell scripts.

hmd

Uses the gitlike-commands module to find and execute hmd subcommands. Allows you to run hmd create binary sensor and hmd will find and run hmd-create-binary-sensor and pass it all the command line options.

hmd create binary sensor

Create/Update a binary sensor and set its state.

Usage: hmd create binary sensor --device-name mfsmaster --device-id 8675309 --mqtt-user HASS_MQTT_USER --mqtt-password HASS_MQTT_PASSWORD --client-name inquisition --mqtt-server mqtt.unixorn.net --metric-name tamper --device-class motion --state off

hmd create device

Create/Update a device and set the state of multiple metrics on it.

Usage: hmd create device --device-name coyote --device-id 8675309 --mqtt-user HASS_MQTT_USER --mqtt-password HASS_MQTT_PASSWORD --mqtt-server mqtt.example.com --model 'Rocket Skates' --manufacturer 'Acme Products' --metric-data '{"name":"Left Rocket Skate","value":93}' --metric-data '{"name":"Right Rocket Skate","value":155}' --unique-id 'hmd-26536'

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

ha_mqtt_discoverable-0.4.2.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ha_mqtt_discoverable-0.4.2-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

File details

Details for the file ha_mqtt_discoverable-0.4.2.tar.gz.

File metadata

  • Download URL: ha_mqtt_discoverable-0.4.2.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.9.13 Darwin/22.3.0

File hashes

Hashes for ha_mqtt_discoverable-0.4.2.tar.gz
Algorithm Hash digest
SHA256 afd76185c2118225c54528b0b3f6d733f7999d3c4a34d552a165d8c2bceeb3ea
MD5 11d45e347c969389a1f7d7dc97d13bac
BLAKE2b-256 6f4c6b1ede415c6896b9c2af08aa2157fdd3bb9e1c21b935172ecc5b0d3c6509

See more details on using hashes here.

File details

Details for the file ha_mqtt_discoverable-0.4.2-py3-none-any.whl.

File metadata

File hashes

Hashes for ha_mqtt_discoverable-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 24dfe90c07894e348c977c472e2d534f0b65784f94838a155b749664db30ea61
MD5 97787720afda033ea5808b4069d029bd
BLAKE2b-256 69568c7225a86917e1e8e68e39f64c1a54d79b3a5685081b0a89097a93bfcde5

See more details on using hashes here.

Supported by

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