Generate & sync Home Assistant MQTT discovery for rustuya-bridge Tuya devices
Project description
rustuya-homeassistant
Generate and sync Home Assistant MQTT Discovery payloads for Tuya devices bridged by rustuya-bridge.
It reads your device list, maps each Tuya datapoint (DP) to the right Home
Assistant entity (sensor / switch / light / climate / cover / fan / …), and
publishes the corresponding retained homeassistant/.../config topics.
Install
pip install --pre rustuya-homeassistant # from PyPI (pre-release only for now)
pip install -e . # or from a checkout
rustuya-ha --help
Only a
0.0.1rc1pre-release is published so far, so--preis required (a plainpip install rustuya-homeassistantfinds nothing until the first stable0.1.0release).
python -m rustuya_ha works as well.
Usage
rustuya-ha status # compare retained discovery vs current output
rustuya-ha status -c mismatched --detail # show field-level diffs
rustuya-ha preview 'guest_*' # dump generator output, no MQTT
rustuya-ha publish '*' --dry-run # preview the publish/clear plan
rustuya-ha publish '*' -y # apply
rustuya-ha clear '*' --stale-only # drop orphan/stale topics
rustuya-ha restore --last # undo the last publish/clear
Undo (backup / restore)
publish and clear already read the full retained discovery state before they
write, so each one drops a timestamped backup of it first (under --backup-dir,
default .rustuya-ha-backups/; --no-backup to skip). Since the discovery state
lives entirely in retained homeassistant/.../config topics, that backup is a
complete restore point.
rustuya-ha restore --last # revert to the most recent backup
rustuya-ha restore --list # list available backups
rustuya-ha restore <file> # revert to a specific backup
rustuya-ha restore --last --dry-run
Restore re-publishes the saved topics (retained) and clears any added since, so the live state matches the snapshot exactly. Restore also backs up the pre-restore state first, so an undo is itself undoable. Backups hold device names/ids (treat as private; the dir is gitignored).
PATTERN is an fnmatch on device id or name (default *). -c/--category
narrows by verifier category (see rustuya-ha -h).
Configuration
| Setting | Flag | Env | Default |
|---|---|---|---|
| MQTT broker | --broker HOST[:PORT] |
RUSTUYA_MQTT |
localhost:1883 |
| Device list | --devices PATH |
RUSTUYA_DEVICES |
tuyadevices.json |
| Custom converters | --converters PATH |
RUSTUYA_CONVERTERS |
./custom_converters.json |
Architecture
rustuya_ha/
core/ pure generation logic (no MQTT, no argparse) — usable as a library
generator.py DiscoveryGenerator: device -> {topic: payload}
mapping.py DP/category -> HA entity tables
converter.py user DP overrides (custom_converters.json)
scheme.py TopicScheme / PayloadCodec seams (topic layout + payload shape)
cli/ thin argparse wrapper (manager = MQTT I/O, verifier, render)
Use the core directly from other front-ends:
from rustuya_ha import initialize_generator
payloads, source = initialize_generator().generate(device)
Topic/payload schemes (TopicScheme / PayloadCodec)
Topics and the MQTT payload shape are injected via scheme.py rather than
hardcoded, so discovery follows whatever templates the bridge is configured
with. DefaultTopicScheme / DefaultPayloadCodec reproduce the historical
layout; BridgeTopicScheme / BridgePayloadCodec (core/bridge.py) derive the
layout from a rustuya-bridge config (mqtt_event_topic / mqtt_command_topic /
mqtt_message_topic / mqtt_payload_template).
The config is resolved per run: --bridge-config <file> > the retained
{root}/bridge/config topic (read over the same MQTT connection, like
rustuya-manager) > the legacy default. Derivation handles:
-
per-DP (
{dp}in the event topic) and multi-DP (fulldpsdict on one topic;value_templateindexes by DP). -
value path:
value_templatepoints at wherever{value}/{dps}sits in the payload template (e.g.{"value":{value}}→value_json.value). -
active vs passive: three classes —
evententities and incremental/delta DPs (e.g.add_ele; seemapping.ACTIVE_ONLY_CODES, or per-productcustom_converters"active": true) read the momentaryactivepush and ignore the retained snapshot;- all other (absolute-state) entities read only the retained
passivesnapshot.
This holds for any config — if the event topic separates
{type}the topics do it; if active and passive share a topic, thevalue_templatefilters by type instead. Filtering applies only whenmqtt_retainis on (a passive snapshot is guaranteed) and the payload carries{type}(so the two can be told apart); otherwise entities accept whatever arrives.
The LEGACY profile in core/bridge.py is the bridge config that reproduces the
historical output; tests/test_bridge_scheme_legacy.py asserts it stays
byte-identical to the golden snapshots.
Tests
python3 -m pytest
Golden snapshot tests lock generator output so refactors stay regression-free. Regenerate the baseline only on intentional behavior changes:
python3 tests/generate_snapshots.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rustuya_homeassistant-0.0.1rc3.tar.gz.
File metadata
- Download URL: rustuya_homeassistant-0.0.1rc3.tar.gz
- Upload date:
- Size: 77.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc4387c79b3b9ac8c199171ca57aba5e4e5083aaae1301108e678fb2ff06721f
|
|
| MD5 |
d2268153555b87264ae7b303bb5e274d
|
|
| BLAKE2b-256 |
9f4b2c29abfdc2fbdb8f3af292db8737b8602a219bdb988499c5993c3c94f70d
|
Provenance
The following attestation bundles were made for rustuya_homeassistant-0.0.1rc3.tar.gz:
Publisher:
publish.yml on 3735943886/rustuya-homeassistant
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rustuya_homeassistant-0.0.1rc3.tar.gz -
Subject digest:
bc4387c79b3b9ac8c199171ca57aba5e4e5083aaae1301108e678fb2ff06721f - Sigstore transparency entry: 1753546783
- Sigstore integration time:
-
Permalink:
3735943886/rustuya-homeassistant@90b5c876379a5fec8e7b42266e3023e4bc5134ea -
Branch / Tag:
refs/tags/v0.0.1rc3 - Owner: https://github.com/3735943886
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@90b5c876379a5fec8e7b42266e3023e4bc5134ea -
Trigger Event:
push
-
Statement type:
File details
Details for the file rustuya_homeassistant-0.0.1rc3-py3-none-any.whl.
File metadata
- Download URL: rustuya_homeassistant-0.0.1rc3-py3-none-any.whl
- Upload date:
- Size: 60.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc0875eaff4f7d1d3abd4cbac016aeb83629f9aeed00b02d6fa5d0f5c656921b
|
|
| MD5 |
07e61019857489fdf02855d22eb72874
|
|
| BLAKE2b-256 |
c11d77fc9c92c9e724d6a6a3a4c09d5d24461f59df089617597ce3d3d436ba11
|
Provenance
The following attestation bundles were made for rustuya_homeassistant-0.0.1rc3-py3-none-any.whl:
Publisher:
publish.yml on 3735943886/rustuya-homeassistant
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rustuya_homeassistant-0.0.1rc3-py3-none-any.whl -
Subject digest:
fc0875eaff4f7d1d3abd4cbac016aeb83629f9aeed00b02d6fa5d0f5c656921b - Sigstore transparency entry: 1753546832
- Sigstore integration time:
-
Permalink:
3735943886/rustuya-homeassistant@90b5c876379a5fec8e7b42266e3023e4bc5134ea -
Branch / Tag:
refs/tags/v0.0.1rc3 - Owner: https://github.com/3735943886
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@90b5c876379a5fec8e7b42266e3023e4bc5134ea -
Trigger Event:
push
-
Statement type: