Skip to main content

Python bindings for rustuyabridge — an MQTT bridge for Tuya devices

Project description

pyrustuyabridge

Python bindings for rustuya-bridge, an MQTT bridge for managing Tuya devices via the Tuya Local API.

This package exposes the bridge server as an embeddable component, so Python code can run it alongside and interpret MQTT topics/payloads identically to the native bridge.

Install

pip install pyrustuyabridge

Pre-built wheels are provided for Linux (manylinux2014 / musllinux_1_2 on x86_64 and aarch64), Windows x64, and macOS (x86_64 / arm64). Wheels are built with PyO3 abi3-py39, so a single wheel per platform supports CPython 3.9 and newer.

Quick start

import asyncio
from pyrustuyabridge import PyBridgeServer

async def main():
    server = PyBridgeServer(
        mqtt_broker="mqtt://localhost:1883",
        mqtt_root_topic="rustuya",
    )
    await server.start_async()

asyncio.run(main())

Keeping the bridge running across reconfigure

The bridge is meant to be driven entirely over MQTT — including config changes. To apply a change to the topic/payload templates or mqtt_retain, edit the config_path file and publish a reconfigure action to the command topic. The bridge clears the retained messages published under the old scheme and then exits cleanly so the new config can take effect — exactly like the native binary under systemd Restart=always.

In an embedded host there is no external supervisor, so wrap the server in a small recreate loop. An intentional shutdown stops the supervisor (not the bridge); any other exit — reconfigure or a crash — recreates it, re-reading config_path:

# asyncio
import asyncio
from pyrustuyabridge import PyBridgeServer

class AsyncBridgeSupervisor:
    def __init__(self, **kwargs):
        self._kwargs = {**kwargs, "no_signals": True}  # host owns signals
        self._stop, self._server = False, None

    async def run(self):
        while not self._stop:
            self._server = PyBridgeServer(**self._kwargs)  # re-reads config_path
            try:
                await self._server.start_async()           # returns on reconfigure/stop
            except Exception as e:
                print("bridge exited with error, restarting:", e)
                await asyncio.sleep(5)

    def stop(self):
        self._stop = True
        if self._server:
            self._server.stop()

# asyncio.run(AsyncBridgeSupervisor(config_path="/etc/rustuya/config.json").run())
# thread
import threading, time
from pyrustuyabridge import PyBridgeServer

class BridgeSupervisor:
    def __init__(self, **kwargs):
        self._kwargs = {**kwargs, "no_signals": True}
        self._stop = threading.Event()
        self._server = None

    def run(self):                                   # run in a thread (blocking)
        while not self._stop.is_set():
            self._server = PyBridgeServer(**self._kwargs)
            try:
                self._server.start()                 # blocks until reconfigure/stop
            except Exception as e:
                print("bridge exited with error, restarting:", e)
                time.sleep(5)

    def stop(self):
        self._stop.set()
        if self._server:
            self._server.stop()

Config precedence is kwargs > config_path file > defaults (the file only fills fields left unset). So put any field you want to change via reconfigure in the file, not in kwargs — a field passed as a kwarg wins and the file edit is ignored (same rule as CLI flags vs. config file in the native binary).

Helpers

The module also exposes topic/payload utilities for code that needs to match the bridge's wire format:

  • tpl_to_wildcard(template, root_topic) — template → MQTT wildcard.
  • match_topic(topic, template) — returns extracted variables or None.
  • render_template(template, vars) — substitutes {key} placeholders.
  • parse_payload(payload, vars) — parses an MQTT payload into a structured value.
  • parse_payload_with_template(payload, template) — reverse of render_template: recovers placeholder values from a rendered payload, or None if not reversible.
  • validate_payload_template(template)(ok, message); checks whether a payload template can be reverse-parsed.

See the project repository for full documentation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

pyrustuyabridge-0.2.0rc16-cp39-abi3-win_amd64.whl (1.9 MB view details)

Uploaded CPython 3.9+Windows x86-64

pyrustuyabridge-0.2.0rc16-cp39-abi3-musllinux_1_2_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ x86-64

pyrustuyabridge-0.2.0rc16-cp39-abi3-musllinux_1_2_aarch64.whl (2.4 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

pyrustuyabridge-0.2.0rc16-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

pyrustuyabridge-0.2.0rc16-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.3 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

pyrustuyabridge-0.2.0rc16-cp39-abi3-macosx_11_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

pyrustuyabridge-0.2.0rc16-cp39-abi3-macosx_10_12_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file pyrustuyabridge-0.2.0rc16-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for pyrustuyabridge-0.2.0rc16-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0834ec598af4d0bf847f813eaa032c452845457fb3ae2d8efc46737229117df2
MD5 17dea836c6b5b8869e1f63220fbe7a98
BLAKE2b-256 84fefacd4956c6d99e85c2cf59f567288340c0f7478c8176cfb34c34062c57b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrustuyabridge-0.2.0rc16-cp39-abi3-win_amd64.whl:

Publisher: python-publish.yml on 3735943886/rustuya-bridge

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

File details

Details for the file pyrustuyabridge-0.2.0rc16-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pyrustuyabridge-0.2.0rc16-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 af0e1b8aaf3247f0cba2934946baf398a845b612a395192a0a7522fd5d3ff833
MD5 07ceafb6256b75adec2936fc0f96ffcb
BLAKE2b-256 cf9ea431d84e56e430b07d52439c804475838cd86d5f79e7a067e01b3a337a00

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrustuyabridge-0.2.0rc16-cp39-abi3-musllinux_1_2_x86_64.whl:

Publisher: python-publish.yml on 3735943886/rustuya-bridge

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

File details

Details for the file pyrustuyabridge-0.2.0rc16-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pyrustuyabridge-0.2.0rc16-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 dab2e2b0881fe8aa4b4c7d83cbc0dae047f5ca2caffa3727729de1a7410a554f
MD5 bdf4adf362acc5a7dfa76247964c002b
BLAKE2b-256 fa26f6ba43812e0c937e1e65d0096f90e1969daa52e2fb516c957e8c11140deb

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrustuyabridge-0.2.0rc16-cp39-abi3-musllinux_1_2_aarch64.whl:

Publisher: python-publish.yml on 3735943886/rustuya-bridge

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

File details

Details for the file pyrustuyabridge-0.2.0rc16-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyrustuyabridge-0.2.0rc16-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7113630731264992df9b480a68ae16107beebc2ccc162ed9135f5be5d7e11d18
MD5 37533f099077943b18174dd4ba01024a
BLAKE2b-256 088e7454a5d5c7b7aced17ac45d798165741f656433000a9684f4fda847c05f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrustuyabridge-0.2.0rc16-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: python-publish.yml on 3735943886/rustuya-bridge

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

File details

Details for the file pyrustuyabridge-0.2.0rc16-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pyrustuyabridge-0.2.0rc16-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 90e904fc9fa5235cb26449d4b01de713c955d1cc8c44151c4b22137b67057ec4
MD5 d518ac0ce5b8ebd02962a639c23908e0
BLAKE2b-256 5372d75d1a02b2ad266a914dc1ad386d1da7d7c2a121b52ca37758c96cd82924

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrustuyabridge-0.2.0rc16-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: python-publish.yml on 3735943886/rustuya-bridge

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

File details

Details for the file pyrustuyabridge-0.2.0rc16-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyrustuyabridge-0.2.0rc16-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a05aef8ffc145adb29167dab5ee8d9bc45fa3f752cae83eaac32fbc0b56a7ba6
MD5 ea04be612495eb6b46124a0d651f0b7d
BLAKE2b-256 c7f31e10936f17347ebc3c37094388ec0c1ccd4bc56cc7f20b113b139b3f9a70

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrustuyabridge-0.2.0rc16-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: python-publish.yml on 3735943886/rustuya-bridge

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

File details

Details for the file pyrustuyabridge-0.2.0rc16-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pyrustuyabridge-0.2.0rc16-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ac12ae804cc6e44facdd9109c0c814aa45502dc9befb608c9a177c6339a530fa
MD5 4093c2c851f00d86d4c00419c9667b24
BLAKE2b-256 ea3b2d2e4482faa778bd366df9e118d8eed9439d3936e74e634d13e413c4a7cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrustuyabridge-0.2.0rc16-cp39-abi3-macosx_10_12_x86_64.whl:

Publisher: python-publish.yml on 3735943886/rustuya-bridge

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