Skip to content

NetworkPowerPort: add support for Shelly Gen2+ #1664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions labgrid/driver/power/shelly_gen2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Interface for controlling relays of Shelly devices using the Gen 2+ API

NetworkPowerPort:
model: shelly_gen2
host: 'http://192.168.0.42'
index: 0

Will do a POST request to http://192.168.0.42/rpc to get the current
relay state or change the state.

Also, see the official Gen 2+ Device API documentation:
https://shelly-api-docs.shelly.cloud/gen2/General/RPCProtocol
"""

import requests


def power_set(host: str, port: int, index: int = 0, value: bool = True):
assert not port
payload = {"id": 1, "method": "Switch.Set", "params": {"id": index, "on": value}}
r = requests.post(f"{host}/rpc", json=payload)
r.raise_for_status()


def power_get(host: str, port: int, index: int = 0):
assert not port
payload = {"id": 1, "method": "Switch.GetStatus", "params": {"id": index}}
r = requests.post(f"{host}/rpc", json=payload)
r.raise_for_status()
return r.json()["output"]