Files
scrypted/packages/python-client/README.md
Raman Gupta 4d2d367651 python-client: package as scrypted-sdk for PyPI (#2095)
* python-client: link transitive server modules required by plugin_remote

plugin_remote now imports cluster_labels, cluster_setup, plugin_console,
plugin_pip, and plugin_volume (plus a lazy plugin_repl import), but
packages/python-client only symlinks plugin_remote, rpc, rpc_reader, and
scrypted_python. As a result the client cannot be imported at all:

    ModuleNotFoundError: No module named 'cluster_labels'

Add symlinks for the missing modules, matching the existing pattern of
sharing one implementation with server/python.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WaSWgBV1XsZxWiYM16sWME

* python-client: extract reusable scrypted_client library from test.py

The only way to connect to Scrypted from Python has been to copy the
bootstrap out of test.py, which is not importable (module-level event
loop) and had drifted from the current rpc_reader/PluginRemote APIs
(writeJSON vs writeSerialized, missing ClusterSetup argument).

Move the transport and connection handshake into an importable
scrypted_client module:

- EioRpcTransport gains a close() method, an optional injectable
  aiohttp session for the engine.io connection, and queues its send
  loop on the running loop instead of via run_coroutine_threadsafe.
- connect_scrypted_client() performs login, the engine.io connect, and
  the getRemote handshake, with a connect timeout and consistent
  ScryptedConnectionError on failure. It also attaches the constructed
  SystemManager to remote.systemManager, the same wiring loadZip does
  for plugins, so PluginRemote.notify can dispatch events to
  systemManager.listen() callbacks.
- test.py becomes a small demo of the library and exits cleanly
  without os._exit(); the server URL is configurable via
  SCRYPTED_BASE_URL.

Verified live against a Scrypted server (device enumeration and OnOff
state reads).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WaSWgBV1XsZxWiYM16sWME

* python-client: bound all connect phases and tear down tasks on close

Address review feedback:

- close() now cancels and awaits both the send loop and the peer read
  loop (previously the send task was cancelled but never awaited and
  the read task was untracked), so closing the event loop after
  close() no longer risks 'Task was destroyed but it is pending'
  warnings.
- connect_scrypted_client() stores the read-loop task on the transport
  and propagates read-loop failures into the pending handshake future,
  so a link that dies mid-handshake fails immediately with the
  underlying error instead of waiting out the timeout.
- The timeout parameter now bounds every phase: the login POST
  (aiohttp ClientTimeout), the engine.io connect (asyncio.wait_for),
  and the wait for initial system state.
- Document session ownership: login_session is borrowed and never
  closed; an http_session given to EioRpcTransport is owned by the
  transport and closed by close().

Verified live against a Scrypted server: clean run with empty stderr,
plus connection-refused and bad-credential paths both raising
ScryptedConnectionError.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WaSWgBV1XsZxWiYM16sWME

* python-client: lazily import plugin host modules in plugin_remote

A client consuming plugin_remote (for SystemManager, DeviceManager,
MediaManager) only needs the types and the engine.io/rpc bits. The
plugin host modules (cluster_labels, plugin_console, plugin_pip,
plugin_volume) are only used inside loadZipWrapped, so import them
there -- importing plugin_remote no longer requires them, and the
client directory drops those symlinks (plugin_repl was already a lazy
import). cluster_setup stays: the client bootstrap constructs a
ClusterSetup.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* python-client: package as scrypted-sdk for PyPI

Adds a pyproject.toml and a hatchling build hook that generates the
scrypted_sdk package at build time: it copies the client-required
modules (client.py, rpc, rpc_reader, cluster_setup, plugin_remote, and
the scrypted_python types -- reading through the symlinks into
server/python and sdk/types) and mechanically rewrites the flat imports
to package-qualified ones (import rpc -> from scrypted_sdk import rpc),
so nothing is installed into consumers' top-level namespace. No runtime
code is modified and nothing generated is committed.

A python-sdk-v* tag builds, smoke-tests, and publishes to the existing
scrypted-sdk PyPI project via trusted publishing; the same workflow
builds and smoke-tests on PRs that touch the shared Python sources.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* python-client: add examples/light.py mirroring the typescript client example

The Python equivalent of packages/client/examples/light.ts: connect,
look up a light by name, turn it on, wait, turn it off, disconnect.
Works both with the installed scrypted-sdk package and directly from a
repo checkout (falls back to the flat modules in the parent directory).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-27 13:53:04 -07:00

3.3 KiB

python-client

Connect to a Scrypted server from Python and use the same SDK objects (systemManager, deviceManager, mediaManager) that Python plugins see.

The modules in this directory are symlinks into ../../server/python and ../../sdk/types so the client and the plugin runtime share one implementation.

Usage

Installed from PyPI, everything lives under the scrypted_sdk package:

pip install scrypted-sdk
from scrypted_sdk import connect_scrypted_client

From a checkout of this repository, the modules are importable directly:

pip install -r requirements.txt
import asyncio

from scrypted_client import connect_scrypted_client


async def main(loop):
    transport, sdk = await connect_scrypted_client(
        loop, "https://localhost:10443", "username", "password"
    )
    for id in sdk.systemManager.getSystemState():
        print(sdk.systemManager.getDeviceById(id).name)
    await transport.close()


loop = asyncio.new_event_loop()
loop.run_until_complete(main(loop))

test.py is a runnable version of the above; point it at a server with the SCRYPTED_BASE_URL, SCRYPTED_USERNAME, and SCRYPTED_PASSWORD environment variables.

examples/light.py is the Python equivalent of packages/client/examples/light.ts — it turns a named light on and back off:

SCRYPTED_USERNAME=admin SCRYPTED_PASSWORD=swordfish python examples/light.py "Office Dimmer"

It works both with pip install scrypted-sdk and directly from a repo checkout with only requirements.txt installed.

By default the login request and the engine.io connection skip TLS certificate verification, since Scrypted servers use self-signed certificates out of the box. To control TLS (or connection pooling), pass your own login_session and a pre-built EioRpcTransport(loop, http_session=...).

Session ownership: a login_session you pass is only borrowed for the login request and is never closed. An http_session passed to EioRpcTransport becomes owned by the transport — transport.close() closes it along with the engine.io connection and background tasks — so don't share that session with anything else.

Packaging

The scrypted-sdk PyPI package is built from this directory. Because the modules here are flat top-level modules (that is how the plugin runtime loads them), publishing them as-is would install modules named rpc, plugin_remote, etc. into consumers' environments. Instead, a build hook (hatch_build.py) generates a scrypted_sdk package at build time: it copies each module (reading through the symlinks) and mechanically rewrites the flat imports to package-qualified ones (import rpcfrom scrypted_sdk import rpc). Nothing is committed and no runtime code is modified beyond that rewrite.

To build locally:

pip install build && python -m build   # or: uv build

To release: bump version in pyproject.toml and push a python-sdk-v* tag. The Python SDK workflow builds, smoke-tests, and publishes to PyPI via trusted publishing — the PyPI project just needs koush/scrypted + python-sdk.yml registered as a trusted publisher (no API tokens).