Skip to main content

Add your description here

Project description

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
  • 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.

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 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
  • device_automation
  • device_tracker
  • event
  • fan
  • humidifier
  • image
  • lawn_mower
  • light
  • lock
  • notify
  • number
  • scene
  • select
  • sensor
  • siren
  • switch
  • tag
  • text
  • 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.

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

License

MIT

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_models_pydantic-1.0.1.tar.gz (48.0 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_models_pydantic-1.0.1-py3-none-any.whl (37.3 kB view details)

Uploaded Python 3

File details

Details for the file ha_mqtt_models_pydantic-1.0.1.tar.gz.

File metadata

  • Download URL: ha_mqtt_models_pydantic-1.0.1.tar.gz
  • Upload date:
  • Size: 48.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ha_mqtt_models_pydantic-1.0.1.tar.gz
Algorithm Hash digest
SHA256 3def3aa89f7a53b9222af6b17908508cbfe2b21b35bc28c715b96061f77e87e1
MD5 890225cc7a3114db8ca2609d7c0092d4
BLAKE2b-256 6dcec16cc71701849086121e726f7f16656fcacb5a58a666c76719445432bd4b

See more details on using hashes here.

Provenance

The following attestation bundles were made for ha_mqtt_models_pydantic-1.0.1.tar.gz:

Publisher: publish.yml on PolarBearEs/ha_mqtt_models_pydantic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ha_mqtt_models_pydantic-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ha_mqtt_models_pydantic-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 da03b6cead0402e7e427f33293942405a83a6a7be305a63b778b763ccbfa3e27
MD5 f09c3c7c1f64ae2c57fa3e4f02492edb
BLAKE2b-256 98e56d9071b41c0c6f997a9ec46394bbd2ac5fa1d4528e097f65c611a3f973e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for ha_mqtt_models_pydantic-1.0.1-py3-none-any.whl:

Publisher: publish.yml on PolarBearEs/ha_mqtt_models_pydantic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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