Skip to main content

Small Python client for Shelly Gen2/Gen3/Gen4 local RPC API

Project description

shelly-rpc

A Python library for the local Shelly RPC API (Gen2/Gen3/Gen4).

The library provides:

  • a single HTTP JSON-RPC client for http://<device>/rpc;
  • digest authentication support;
  • convenience methods for common operations (Shelly.GetStatus, Switch.Set);
  • a unified transport and RPC error layer.

Contents

  • Quick Start
  • Authentication
  • Supported Methods
  • Error Handling
  • API Documentation
  • Usage Examples
  • Build and Release
  • Development and Tests

Quick Start

1) Install

cd /mnt/NetworkBackupShare/api_doc/shelly_rpc
python3 -m pip install -e .

2) Basic usage

from shelly_rpc import ShellyClient, ShellyRpcError, ShellyTransportError

client = ShellyClient("192.168.33.1")

try:
    info = client.get_device_info()
    status = client.get_status()

    relay0_before = client.switch_get_status(0)
    client.switch_set(0, True)
    relay0_after = client.switch_get_status(0)

    print("model:", info.get("model"))
    print("before:", relay0_before.get("output"))
    print("after:", relay0_after.get("output"))
except ShellyRpcError as e:
    print(f"RPC error: code={e.code} message={e.message}")
except ShellyTransportError as e:
    print(f"Transport error: {e}")

Authentication

If digest authentication is enabled on the device:

from shelly_rpc import ShellyClient

client = ShellyClient(
    "192.168.33.1",
    username="admin",
    password="secret",
)

For HTTPS, pass use_https=True.

Supported Methods (v1)

Generic methods

  • call(method, params=None)
  • call_method_endpoint(method, params=None)

Convenience methods

  • get_device_info() -> Shelly.GetDeviceInfo
  • get_status() -> Shelly.GetStatus
  • switch_get_status(switch_id=0) -> Switch.GetStatus
  • switch_set(switch_id, on) -> Switch.Set
  • switch_toggle(switch_id) -> Switch.Toggle

Error Handling

  • ShellyTransportError
    • network errors;
    • HTTP errors;
    • invalid JSON responses.
  • ShellyRpcError
    • RPC errors returned by the device in the error field;
    • includes code, message, and data.

Example Shelly API error codes (from local docs):

  • -103 -> invalid argument (for example, missing required id)
  • -105 -> not found (for example, Bad id=7)

API Documentation

Development and Tests

Run tests:

PYTHONPATH=src python3 -m unittest discover -s tests -v

Sources (local docs snapshot)

  • shelly-api-docs.shelly.cloud/gen2/General/RPCChannels/index.html
  • shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/Shelly/index.html
  • shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/Switch/index.html
  • shelly-api-docs.shelly.cloud/gen2/General/CommonErrors/index.html

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

shelly_rpc-0.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

shelly_rpc-0.1.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file shelly_rpc-0.1.0.tar.gz.

File metadata

  • Download URL: shelly_rpc-0.1.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.10

File hashes

Hashes for shelly_rpc-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9442c3d8e6faf94af5157420aa6ef1927020b67fc79f3fa43a135f297497b55b
MD5 4545485bddc516c346739ccd37a6f6ab
BLAKE2b-256 ebf0783bec3d7261eee3b281e9d61a5f7a1415e6ba5b28057ae6ef7349e0493e

See more details on using hashes here.

File details

Details for the file shelly_rpc-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: shelly_rpc-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.10

File hashes

Hashes for shelly_rpc-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7e6e6b36812443d5d08b11c54776d83cab857d6d2f0dde40fdbbe77c1630bd62
MD5 9b4bc456607945f040b8cd18d33b65bb
BLAKE2b-256 d26dd7a344c8130bc76311b2c9bba08fc6ddf39fdd2302f032c5639ca62b9a94

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