Skip to content

Commit ada2f9b

Browse files
CardboardTurkeyEmantor
authored andcommitted
driver/power: add network power driver for TP-Link power strips
Implemented using the python-kasa library. Signed-off-by: kiran ostrolenk <kiran.ostrolenk@codethink.co.uk> [r.czerwinski@pengutronix.de: added kasa-requirements to dev-requirements.txt] Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
1 parent bd0ede7 commit ada2f9b

5 files changed

Lines changed: 38 additions & 1 deletion

File tree

CHANGES.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Release 0.5.0 (unreleased)
33

44
New Features in 0.5.0
55
~~~~~~~~~~~~~~~~~~~~~
6-
- Support for Eaton ePDU added, and can be used as a NetworkPowerPort.
6+
- Support for Eaton ePDU and TP-Link power strips added, either can be used as a NetworkPowerPort.
77
- Consider a combination of multiple "lg_feature" markers instead of
88
considering only the closest marker.
99

dev-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ psutil==5.8.0
1616
-r pyvisa-requirements.txt
1717
-r vxi11-requirements.txt
1818
-r mqtt-requirements.txt
19+
-r kasa-requirements.txt

kasa-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-kasa==0.4.0

labgrid/driver/power/tplink.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
""" Tested with TP Link KP303, and should be compatible with any strip supported by kasa """
2+
3+
import asyncio
4+
from kasa import SmartStrip
5+
6+
7+
async def _power_set(host, port, index, value):
8+
"""We embed the coroutines in an `async` function to minimise calls to `asyncio.run`"""
9+
assert port is None
10+
index = int(index)
11+
strip = SmartStrip(host)
12+
await strip.update()
13+
assert (
14+
len(strip.children) > index
15+
), "Trying to access non-existant plug socket on strip"
16+
if value is True:
17+
await strip.children[index].turn_on()
18+
elif value is False:
19+
await strip.children[index].turn_off()
20+
21+
22+
def power_set(host, port, index, value):
23+
asyncio.run(_power_set(host, port, index, value))
24+
25+
26+
def power_get(host, port, index):
27+
assert port is None
28+
index = int(index)
29+
strip = SmartStrip(host)
30+
asyncio.run(strip.update())
31+
assert (
32+
len(strip.children) > index
33+
), "Trying to access non-existant plug socket on strip"
34+
return strip.children[index].is_on

tests/test_powerdriver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ def test_import_backends(self):
166166
import labgrid.driver.power.digipower
167167
import labgrid.driver.power.gude
168168
import labgrid.driver.power.gude24
169+
import labgrid.driver.power.tplink
169170
import labgrid.driver.power.netio
170171
import labgrid.driver.power.netio_kshell
171172
import labgrid.driver.power.rest

0 commit comments

Comments
 (0)