Skip to content

Commit 948ad80

Browse files
Merge pull request #1664 from aparcar/shelly2
NetworkPowerPort: add support for Shelly Gen2+
2 parents f9d8ca0 + 8d3ec78 commit 948ad80

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

doc/configuration.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ Currently available are:
228228
<https://github.com/labgrid-project/labgrid/blob/master/labgrid/driver/power/shelly_gen1.py>`__
229229
for details.
230230

231+
``shelly_gen2``
232+
Controls relays of *Shelly* devices using the
233+
`Gen2+ API <https://shelly-api-docs.shelly.cloud/gen2/General/RPCProtocol/>`__.
234+
See the `docstring in the module
235+
<https://github.com/labgrid-project/labgrid/blob/master/labgrid/driver/power/shelly_gen2.py>`__
236+
for details.
237+
231238
``siglent``
232239
Controls *Siglent SPD3000X* series modules via the `vxi11 Python module
233240
<https://pypi.org/project/python-vxi11/>`_.

labgrid/driver/power/shelly_gen2.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Interface for controlling relays of Shelly devices using the Gen 2+ API
2+
3+
NetworkPowerPort:
4+
model: shelly_gen2
5+
host: 'http://192.168.0.42'
6+
index: 0
7+
8+
Will do a POST request to http://192.168.0.42/rpc to get the current
9+
relay state or change the state.
10+
11+
Also, see the official Gen 2+ Device API documentation:
12+
https://shelly-api-docs.shelly.cloud/gen2/General/RPCProtocol
13+
"""
14+
15+
import requests
16+
17+
18+
def power_set(host: str, port: int, index: int = 0, value: bool = True):
19+
assert not port
20+
payload = {"id": 1, "method": "Switch.Set", "params": {"id": index, "on": value}}
21+
r = requests.post(f"{host}/rpc", json=payload)
22+
r.raise_for_status()
23+
24+
25+
def power_get(host: str, port: int, index: int = 0):
26+
assert not port
27+
payload = {"id": 1, "method": "Switch.GetStatus", "params": {"id": index}}
28+
r = requests.post(f"{host}/rpc", json=payload)
29+
r.raise_for_status()
30+
return r.json()["output"]

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ include = [
219219
"labgrid/driver/httpvideodriver.py",
220220
"labgrid/driver/manualswitchdriver.py",
221221
"labgrid/driver/power/gude8031.py",
222+
"labgrid/driver/power/shelly_gen2.py",
222223
"labgrid/driver/rawnetworkinterfacedriver.py",
223224
"labgrid/protocol/**/*.py",
224225
"labgrid/remote/**/*.py",

0 commit comments

Comments
 (0)