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.0rc18-cp39-abi3-win_amd64.whl (1.9 MB view details)

Uploaded CPython 3.9+Windows x86-64

pyrustuyabridge-0.2.0rc18-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.0rc18-cp39-abi3-musllinux_1_2_aarch64.whl (2.4 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

pyrustuyabridge-0.2.0rc18-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.0rc18-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.0rc18-cp39-abi3-macosx_11_0_arm64.whl (2.0 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

pyrustuyabridge-0.2.0rc18-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.0rc18-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for pyrustuyabridge-0.2.0rc18-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 78e3c8fcbcc9945e47593c99eca5a005e7849d1b98af01dc2d714c71d3d296ee
MD5 8880a97ebd6616e6b3bdf53686ca1696
BLAKE2b-256 ceb55db9593cb4703dcfed96409ebe44a6f16bac90505f24771fcf9b44c3942e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrustuyabridge-0.2.0rc18-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.0rc18-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pyrustuyabridge-0.2.0rc18-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9264cfa6c4a6e419b57095109003846c4b4b79da03ba4f30e573a984097c5ecc
MD5 3b3a8a5dcf5f28ce08232ef59341f992
BLAKE2b-256 b9e0d1897b83a7c9cdf3fb7bb6c965f10385e11ac69c83c134f054003df281fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrustuyabridge-0.2.0rc18-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.0rc18-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pyrustuyabridge-0.2.0rc18-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 f47adf7ebe8d052d1b454310a0cd353ff8d5dfa5f995c1808f28004feb4bf586
MD5 03c42f71f1474463091de20d17cda4c1
BLAKE2b-256 ab5161a778508434f6782d5f71003ff698ea948cfa1a1a6255eaabc55ddd6fde

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrustuyabridge-0.2.0rc18-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.0rc18-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyrustuyabridge-0.2.0rc18-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 86acf7bd7cf365330cb45127bad3b15ef01dad044c6ac484422f8d5138d7d9d2
MD5 210ff8789fb1d11b6fbe01e7ed98bb14
BLAKE2b-256 d07665f82db0c4aafcb83746b74d2716f4b99490225192600ebf43f1eefea3c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrustuyabridge-0.2.0rc18-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.0rc18-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pyrustuyabridge-0.2.0rc18-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 be832e8d561e04f56131a82bd48c6b95319c0d597bb62c0d3f7460c7c0c71811
MD5 905ec3104e19f8905a61a9bc3244a63f
BLAKE2b-256 f61e233b12a73ead16408bb7fa45acaf73f421862f152978c0da7c25d2aea946

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrustuyabridge-0.2.0rc18-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.0rc18-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyrustuyabridge-0.2.0rc18-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a17ecd5a558cb88aaf73b04cec549726abe74b6e084a782e6021c34242aa5a80
MD5 18bb7f8655bc7bbf52200bea1f7039cc
BLAKE2b-256 8404661edbd2c308e6b9485834bfe3abb56fbaeff55a3007a14905cd4511e28e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrustuyabridge-0.2.0rc18-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.0rc18-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pyrustuyabridge-0.2.0rc18-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a168c736a67721ebc8d392f95be8a1ca6d20856d37d32307a8a17bfe7ae07f5d
MD5 b7c7efff023e32160ce88fc1faf677b1
BLAKE2b-256 3520927fe9cb3462a3b73fadf4239b88ce5e5d0f58932772a52289289f6727eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrustuyabridge-0.2.0rc18-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