Repository navigation
python-client: fix imports and extract a reusable scrypted_client library - #2094
Merged
Merged
Conversation
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
Contributor
There was a problem hiding this comment.
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.pymodule. - Simplified
test.pyinto a runnable demo that uses the new library and exits cleanly. - Added a
README.mddocumenting 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.
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>
Owner
|
@copilot resolve the merge conflicts in this pull request |
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. ---------
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Makes
packages/python-clientusable as an importable library instead of a copy-paste source:Fix
import plugin_remote:plugin_remoteimported 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, soimport plugin_remotefails withModuleNotFoundError: 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 insideloadZipWrapped— their only use site — so importingplugin_remoteno longer requires the plugin host modules at all. The client directory gains a single new symlink,cluster_setup(the client bootstrap constructs aClusterSetup), and the PyPI package built on top (python-client: package as scrypted-sdk for PyPI #2095) ships justscrypted_client,rpc,rpc_reader,cluster_setup,plugin_remote, and thescrypted_pythontypes.Extract
scrypted_client.py: moves the transport and connection bootstrap out oftest.pyinto an importable module:EioRpcTransportgainsclose(), 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 →getRemotehandshake with a timeout and a consistentScryptedConnectionError. It also attaches the constructedSystemManagertoremote.systemManager— the same wiringloadZipdoes for plugins — soPluginRemote.notifycan dispatch events tosystemManager.listen()callbacks.test.pybecomes a small demo of the library (server URL configurable viaSCRYPTED_BASE_URL, clean exit withoutos._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, andtest.pyitself had drifted from the currentrpc_reader/PluginRemoteAPIs (writeJSONvswriteSerialized, missingClusterSetupargument).Relationship to other PRs
PluginRemote.notifyevents tosystemManagerlisteners) has since merged tomain; combined with theremote.systemManagerwiring here,systemManager.listen()callbacks fire in this client.test.pyrewrite here incorporates the API fixes from python-client: fix test.py for current rpc_reader and PluginRemote APIs #2087, so if this merges first, python-client: fix test.py for current rpc_reader and PluginRemote APIs #2087 can be closed.scrypted-sdkon PyPI.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