Skip to content

python-client: fix imports and extract a reusable scrypted_client library - #2094

Merged
koush merged 5 commits into
koush:mainfrom
raman325:python-client-library
Jul 27, 2026
Merged

koush merged 5 commits into
koush:mainfrom
raman325:python-client-library

Conversation

@raman325

@raman325 raman325 commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

What

Makes packages/python-client usable as an importable library instead of a copy-paste source:

  1. Fix import plugin_remote: plugin_remote imported the plugin host modules (cluster_labels, plugin_console, plugin_pip, plugin_volume) at module top level, but the client directory never got symlinks for them, so import plugin_remote fails with ModuleNotFoundError: No module named 'cluster_labels'. Per review on python-client: package as scrypted-sdk for PyPI #2095 (only vendor the types and engine.io bits in the SDK), those imports are now lazy inside loadZipWrapped — their only use site — so importing plugin_remote no longer requires the plugin host modules at all. The client directory gains a single new symlink, cluster_setup (the client bootstrap constructs a ClusterSetup), and the PyPI package built on top (python-client: package as scrypted-sdk for PyPI #2095) ships just scrypted_client, rpc, rpc_reader, cluster_setup, plugin_remote, and the scrypted_python types.

  2. Extract scrypted_client.py: moves the transport and connection bootstrap out of test.py into an importable module:

    • EioRpcTransport gains close(), an optional injectable aiohttp session for the engine.io connection, and schedules its send loop on the running loop.
    • connect_scrypted_client() performs login → engine.io connect → getRemote handshake with a timeout and a consistent ScryptedConnectionError. 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 (server URL configurable via SCRYPTED_BASE_URL, clean exit without os._exit()), and a short README documents usage.

Why

Anything consuming Scrypted from Python outside a plugin (e.g. a Home Assistant integration) currently has to vendor a modified copy of test.py's bootstrap, and test.py itself had drifted from the current rpc_reader/PluginRemote APIs (writeJSON vs writeSerialized, missing ClusterSetup argument).

Relationship to other PRs

Testing

Verified live against a Scrypted server (~100 devices): login, handshake, device enumeration, and OnOff state reads via test.py, clean exit.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WaSWgBV1XsZxWiYM16sWME

raman325 and others added 2 commits July 15, 2026 18:35
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
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
Copilot AI review requested due to automatic review settings July 15, 2026 22:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the Python client under packages/python-client into a reusable importable library, fixing importability and extracting the connection/bootstrap logic into a dedicated module so external consumers (outside plugins) can connect to a Scrypted server via engine.io RPC.

Changes:

  • Extracted engine.io transport and connection bootstrap into a new scrypted_client.py module.
  • Simplified test.py into a runnable demo that uses the new library and exits cleanly.
  • Added a README.md documenting intended usage and TLS/session configuration patterns.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.

File Description
packages/python-client/test.py Replaces inline bootstrap with connect_scrypted_client() demo and uses transport.close() for shutdown.
packages/python-client/scrypted_client.py Introduces reusable transport + connection helper with timeout/error handling and SDK wiring.
packages/python-client/README.md Documents installation and example usage for the extracted client library.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/python-client/README.md Resolved
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
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>
@koush

koush commented Jul 27, 2026

Copy link
Copy Markdown
Owner

@copilot resolve the merge conflicts in this pull request

@koush
koush merged commit c1ed7df into koush:main Jul 27, 2026
@raman325
raman325 deleted the python-client-library branch July 27, 2026 20:40
Recoveredd pushed a commit to Recoveredd/scrypted that referenced this pull request Sep 28, 2026
…rary (koush#2094)

* 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.


* 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).


* 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.


* 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.


---------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants