Skip to main content

ha-mqtt-models-pydantic

Typed Pydantic v2 models for Home Assistant MQTT discovery payloads.

This library validates Home Assistant MQTT discovery payloads as plain Python data, without depending on Home Assistant itself. It is useful when you want to inspect, validate, transform, or generate discovery payloads before publishing them to MQTT.

What It Includes

  • Pydantic models for Home Assistant MQTT discovery components
  • Helpers to parse raw discovery payloads into the right model
  • Support for Home Assistant discovery abbreviations such as stat_t, cmd_t, and dev
  • Dedicated light parsing for basic, json, and template schemas
  • Dedicated infrared parsing for emitter and receiver schemas
  • Typed shared models for entity metadata, device info, and origin info

Installation

From PyPI:

pip install ha-mqtt-models-pydantic

With uv:

uv add ha-mqtt-models-pydantic

Quick Start

from ha_mqtt_models_pydantic import parse_component_payload

payload = {
    "dev_cla": "temperature",
    "name": "Living Room Temperature",
    "stat_t": "homeassistant/sensor/living_room_temperature/state",
    "unit_of_meas": "°C",
    "val_tpl": "{{ value_json.temperature }}",
    "def_ent_id": "sensor.living_room_temperature",
    "dev": {
        "ids": ["living_room_sensor_01"],
        "name": "Living Room Sensor",
    },
}

sensor = parse_component_payload("sensor", payload)

print(sensor.name)
print(sensor.state_topic)
print(sensor.device.identifiers)

parse_component_payload() expands Home Assistant abbreviations before validation, so both abbreviated and expanded payloads are accepted.

When the component name is a string literal, type checkers infer the concrete model: parse_component_payload("sensor", ...) is typed as Sensor. light and infrared return a union of their schema models, and a non-literal str returns the ComponentModel union.

Annotate a variable with ComponentName to keep the literal typing when the name is not hard-coded. EntityModel is the ComponentModel union without device_automation and tag, so every member is a MqttEntityCommon with device, unique_id and the availability fields.

Main APIs

Parse a component payload

from ha_mqtt_models_pydantic import parse_component_payload

switch = parse_component_payload(
    "switch",
    {
        "cmd_t": "devices/lamp/set",
        "stat_t": "devices/lamp/state",
        "pl_on": "ON",
        "pl_off": "OFF",
        "uniq_id": "lamp_switch_01",
    },
)

Parse a light payload

from ha_mqtt_models_pydantic import parse_light_payload

light = parse_light_payload(
    {
        "schema": "json",
        "cmd_t": "devices/light/set",
        "stat_t": "devices/light/state",
        "name": "Desk Light",
    }
)

Parse an infrared payload

from ha_mqtt_models_pydantic import parse_infrared_payload

emitter = parse_infrared_payload(
    {
        "schema": "emitter",
        "cmd_t": "devices/ir_blaster/send",
        "name": "IR Blaster",
    }
)

Infrared payloads have no default schema, so schema must be emitter or receiver.

Parse a device discovery payload

from ha_mqtt_models_pydantic import parse_device_discovery_payload

device_payload = parse_device_discovery_payload(
    {
        "dev": {"ids": ["device_01"], "name": "Example Device"},
        "o": {"name": "example-generator"},
        "cmps": {
            "temperature": {
                "p": "sensor",
                "dev_cla": "temperature",
                "stat_t": "devices/example/temperature",
                "unit_of_meas": "°C",
            }
        },
    }
)

Supported Components

  • alarm_control_panel
  • binary_sensor
  • button
  • camera
  • climate
  • cover
  • date
  • datetime
  • device_automation
  • device_tracker
  • event
  • fan
  • humidifier
  • image
  • infrared
  • lawn_mower
  • light
  • lock
  • notify
  • number
  • scene
  • select
  • sensor
  • siren
  • switch
  • tag
  • text
  • time
  • update
  • vacuum
  • valve
  • water_heater

Notes

  • The import package is ha_mqtt_models_pydantic.
  • The PyPI distribution name is ha-mqtt-models-pydantic.
  • The library targets Python 3.11+.
  • The package is marked as typed with py.typed.
  • The models match the MQTT discovery schemas of Home Assistant core 2026.10.4 (master), exposed as HA_CORE_VERSION.

Development

Install development dependencies:

uv sync --dev

Run checks:

uv run ruff check .
uv run ruff format --check .
uv run mypy .
uv run pytest -q

Releasing

  1. Bump the version with uv version <new-version> and merge it to main.
  2. Publish a GitHub release whose tag is v<new-version>.

The publish workflow refuses tags that do not match the pyproject.toml version, runs the CI checks, then uploads to PyPI.

License

MIT

Metadata

Release files for ha-mqtt-models-pydantic 1.2.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 ha-mqtt-models-pydantic 1.2.0
File Size Uploaded Provenance Actions
ha_mqtt_models_pydantic-1.2.0.tar.gz 32.4 kB With provenance Details for ha_mqtt_models_pydantic-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ha-mqtt-models-pydantic 1.2.0
File Interpreter ABI Platform Actions
ha_mqtt_models_pydantic-1.2.0-py3-none-any.whl Python 3 none any Details for ha_mqtt_models_pydantic-1.2.0-py3-none-any.whl

Total release size: 77.8 kB

Release files / ha_mqtt_models_pydantic-1.2.0.tar.gz

Download URL ha_mqtt_models_pydantic-1.2.0.tar.gz
Size 32.4 kB
Tags Source
SHA-256 checksum
How to use checksums
548c758b2205d4a44f63ab38c88b1acaf42c9c525144c85373d9741f3d16c3d9
BLAKE2b-256 checksum
How to use checksums
47977f30abdf4dd795f8e8c5e2233e248b66637b08f5ba288ea33a879c93b1de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.

Transparency log

Release files / ha_mqtt_models_pydantic-1.2.0-py3-none-any.whl

Download URL ha_mqtt_models_pydantic-1.2.0-py3-none-any.whl
Size 45.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2012325144bb22071342c53fa834e16d6ad62097d940a81bd86b527a61c75f32
BLAKE2b-256 checksum
How to use checksums
666926384986a49da7ab25e585bc08a0d1c042d3466895766068c4344cee86f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.

Transparency log

Security for ha-mqtt-models-pydantic 1.2.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ha_mqtt_models_pydantic-1.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.

Transparency log

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.

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