Skip to main content

Lightweight Python UNS MQTT client (pub/sub + infra topics)

Project description

uns-kit (Python)

Lightweight UNS MQTT client for Python. Provides:

  • Topic builder compatible with UNS infra topics (uns-infra/<package>/<version>/<process>/).
  • Async publish/subscribe via MQTT (using aiomqtt).
  • Process + instance status topics (active/heap/uptime/alive + stats).
  • Minimal UNS packet builder/parser (data/table) aligned with TS core.

Install (editable)

cd packages/uns-py
poetry install

CLI

After poetry install, an uns-kit-py command is available (renamed to avoid clashing with the Node CLI):

poetry run uns-kit-py publish --host localhost:1883 --topic raw/data/ --value 1
poetry run uns-kit-py subscribe --host localhost:1883 --topic 'uns-infra/#'

Quick start

import asyncio
from pathlib import Path
from uns_kit import ConfigFile, UnsPacket, UnsProcessParameters, UnsProxyProcess

async def main():
    config = ConfigFile.load_config(Path("config.json"))
    infra = config["infra"]
    uns = config["uns"]
    process = UnsProxyProcess(
        infra["host"],
        UnsProcessParameters(process_name=uns.get("processName", "uns-process")),
    )
    await process.start()
    mqtt = await process.create_mqtt_proxy("py")

    # Subscribe
    async with mqtt.client.messages("uns-infra/#") as messages:
        await mqtt.publish_packet("raw/data/", UnsPacket.data(value=1, uom="count"))
        msg = await messages.__anext__()
        print(msg.topic, msg.payload.decode())

    await mqtt.close()
    await process.stop()

asyncio.run(main())

Config placeholders (env + Infisical)

uns-py now resolves config placeholders in the same style as uns-core. For Infisical placeholders, install the optional extra:

pip install "uns-kit[infisical]"

Example config.json:

{
  "uns": {
    "graphql": "https://example/graphql",
    "rest": "https://example/rest",
    "email": "service@example.com",
    "password": { "provider": "env", "key": "UNS_PASSWORD" },
    "processName": "my-process"
  },
  "infra": {
    "host": "mqtt.example.local",
    "port": 1883,
    "username": "mqtt-user",
    "password": {
      "provider": "infisical",
      "path": "/mqtt",
      "key": "password",
      "environment": "dev"
    }
  }
}

Load resolved config with cache semantics:

from uns_kit import ConfigFile, SecretResolverOptions, InfisicalResolverOptions

resolved = ConfigFile.load_config(
    "config.json",
    SecretResolverOptions(
        infisical=InfisicalResolverOptions(
            environment="dev",
            project_id="your-project-id"
        )
    )
)

Resilient subscriber

async for msg in client.resilient_messages("uns-infra/#"):
    print(msg.topic, msg.payload.decode())

Examples

  • examples/publish.py — publish 5 data packets.
  • examples/subscribe.py — resilient subscription with auto-reconnect.
  • examples/load_test.py — interactive publish burst.

Create a new project

uns-kit-py create my-uns-py-app
cd my-uns-py-app
poetry install
poetry run python src/main.py

Create a new project from a service bundle

uns-kit-py create --bundle ./service.bundle.json
uns-kit-py create --bundle ./service.bundle.json --dest ./my-dir
uns-kit-py create --bundle ./service.bundle.json --dest . --allow-existing

Bundle-driven create uses service.bundle.json as the source of truth. The Python CLI:

  • scaffolds the base Python app from the existing default template
  • copies the original bundle into the project root as service.bundle.json
  • generates SERVICE_SPEC.md and AGENTS.md
  • applies supported bundle features such as vscode and devops

When --bundle is used, the default destination is ./<metadata.name>. The Python CLI only accepts bundles with scaffold.stack = "python" and currently supports scaffold.template = "default" for this MVP. If the bundle targets TypeScript instead, use uns-kit create --bundle ....

Create a sandbox app in this repo

From the monorepo root:

pnpm run py:sandbox

This creates sandbox-app-py/ using the default Python template. When created inside this monorepo, pyproject.toml is automatically set to use local editable uns-kit: uns-kit = { path = "../packages/uns-py", develop = true }.

Notes

  • Default QoS is 0.
  • Instance status topics are published every 10 seconds; stats every 60 seconds.
  • Packet shape mirrors the TypeScript core: {"version":"1.3.0","message":{"data":{...}},"sequenceId":0}.
  • Windows: the library sets WindowsSelectorEventLoopPolicy() to avoid add_reader/add_writer NotImplementedError.

TODO (parity with TS core)

  • Handover manager parity: subscribe to wildcard active and handover topics, keep new instances passive until timeout or handover completion, and support handover_intent, handover_request, handover_subscriber, handover_fin, and handover_ack.
  • Publish throttling / queue parity: add buffered ordered publishing instead of direct proxy-path publish, plus publisher/subscriber active-passive controls and passive-drain behavior.
  • Status parity: add process-level alive and uptime, publisher/subscriber active flags everywhere, published/subscribed message count and byte metrics, and process identity on active status packets.
  • API endpoints registry: mirror @uns-kit/api produced endpoints when Python gets an API surface, and use full UNS identity for endpoint keys and paths: topic + asset + objectType + objectId + attribute.
  • Optional: dictionary/measurement helpers + CLI wrapper.

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

uns_kit-0.0.40.tar.gz (39.5 kB view details)

Uploaded Source

Built Distribution

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

uns_kit-0.0.40-py3-none-any.whl (47.5 kB view details)

Uploaded Python 3

File details

Details for the file uns_kit-0.0.40.tar.gz.

File metadata

  • Download URL: uns_kit-0.0.40.tar.gz
  • Upload date:
  • Size: 39.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.1 CPython/3.14.2 Darwin/25.2.0

File hashes

Hashes for uns_kit-0.0.40.tar.gz
Algorithm Hash digest
SHA256 e1e469907d128f62ddf18ec9c73f3ee32d3f7ff4ec80e5c3dbb555faa46176d1
MD5 fe138fda7d98bcdfdca59433729c4710
BLAKE2b-256 9af2ab7300601b26b6a929f5fad06bd289bd316e17007756f6923c3386151896

See more details on using hashes here.

File details

Details for the file uns_kit-0.0.40-py3-none-any.whl.

File metadata

  • Download URL: uns_kit-0.0.40-py3-none-any.whl
  • Upload date:
  • Size: 47.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.1 CPython/3.14.2 Darwin/25.2.0

File hashes

Hashes for uns_kit-0.0.40-py3-none-any.whl
Algorithm Hash digest
SHA256 528b70dd21460323113ee610e0a5eadff89a2f203ddf2deb772e2abb7f830b8d
MD5 aac56ad26b559c5a336f833a19b6d842
BLAKE2b-256 72f29e6e35a77ecb014e347f7cc636999a167af82b54244a4ae90855d63990f3

See more details on using hashes here.

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