Skip to main content

MQTT Entity helper library for Home Assistant

Workflow Status codecov

A Python helper library to manage Home Assistant entities over MQTT.

Updated for device based MQTT discovery.

Features:

  • MQTT client based on paho-mqtt
    • Retrieve MQTT service info from the Home Assistant Supervisor
  • Manage MQTT discovery info (adding/removing entities)
  • MQTTDevice class to manage devices
    • Availability management (client-wide and optional per-device availability_topics)
    • Manage entities per device
  • Home Assistant Entities modelled as dataclasses:
    • Read-only: Sensor, BinarySensor, DeviceTracker
    • Read & write: Select, Switch, Number, Text, Light
    • MQTT device events
  • Asyncio based
  • Helpers for Home Assistant add-ons (optional)
    • Add-on configuration modeled as dataclasses
      • Load from environment variables, HA's options.yaml or options.json
      • Load MQTT connection settings from the Supervisor
    • Enable add-on logging (incl colors & debug by config)

Discovery and state

Device discovery (homeassistant/device/.../config) is published when Home Assistant signals online on homeassistant/status (after a short debounce). By default it is not retained (MQTTClient(discovery_retain=False)). That way the broker does not keep configs after the publisher stops, and removing a device does not require publishing an empty discovery payload. Set discovery_retain=True on the client if you want the broker to replay discovery on subscribe—clearing removed devices is then your responsibility.

Entity state should be retained so Home Assistant gets the latest value when it subscribes to each state_topic after discovery. send_state and the tracker/light state helpers default to retain=True; pass retain=False when a value must not be replayed. JSON attributes and availability are retained by default. Do not retain device-trigger events.

Why?

This MQTT code was included in several of my home Assistant addons (SMA-EM / Sunsynk). It is easier to update a single library & add new features, like discovery removal.

Alternative options (not based on asyncio)

Credits

@Ivan-L contributed some of the writable entities to the Sunsynk addon project

Release

Semantic versioning is used for release.

To create a new release, include a commit with a 🐬 emoji as a prefix in the commit message. This will trigger a release on the master branch.

# Patch
git commit -m ":dolphin: Release 0.0.x"

# Minor - any commit

Development

To run the tests, you need to have Python 3.12+ installed.

The --mqtt connects to a live Home Assistant instance using the MQTT broker.

uv run pytest --mqtt

Metadata

Release files for mqtt-entity 1.2.15

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

Source distribution (sdist)

Source distribution for mqtt-entity 1.2.15
File Size Uploaded Provenance Actions
mqtt_entity-1.2.15.tar.gz 24.4 kB Without provenance Details for mqtt_entity-1.2.15.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mqtt-entity 1.2.15
File Interpreter ABI Platform Actions
mqtt_entity-1.2.15-py3-none-any.whl Python 3 none any Details for mqtt_entity-1.2.15-py3-none-any.whl

Total release size: 51.3 kB

Release files / mqtt_entity-1.2.15.tar.gz

Download URL mqtt_entity-1.2.15.tar.gz
Size 24.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f2dd7d772ec061276f904652818f6e5ba9459de6699a453dd457d72ef5fe4810
BLAKE2b-256 checksum
How to use checksums
e665a1136aeb888302219c8e489df786e7d60b0ca9f23b277d1a8289955c78e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / mqtt_entity-1.2.15-py3-none-any.whl

Download URL mqtt_entity-1.2.15-py3-none-any.whl
Size 26.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ffd00972014e72ff53da81f76cae668820664a1ab8989f089d0afd1e7be69619
BLAKE2b-256 checksum
How to use checksums
85a2ed68376567edbe429b4700ec9f9024bf2db5d5d8a503ca65ffa90a5f2f0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for mqtt-entity 1.2.15

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.2.15 This release

2 release files No provenance

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