|
1 | 1 | """ Tested with TP Link KP303, and should be compatible with any strip supported by kasa """ |
2 | 2 |
|
3 | 3 | import asyncio |
| 4 | +from kasa import DeviceType, Discover |
4 | 5 | from kasa.iot import IotStrip |
5 | 6 |
|
6 | 7 |
|
7 | 8 | async def _power_set(host, port, index, value): |
8 | 9 | """We embed the coroutines in an `async` function to minimise calls to `asyncio.run`""" |
9 | 10 | assert port is None |
10 | 11 | index = int(index) |
11 | | - strip = IotStrip(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() |
| 12 | + dev = await Discover.discover_single(host) |
| 13 | + if dev.device_type == DeviceType("strip"): |
| 14 | + strip = IotStrip(host) |
| 15 | + await strip.update() |
| 16 | + assert ( |
| 17 | + len(strip.children) > index |
| 18 | + ), "Trying to access non-existant plug socket on strip" |
| 19 | + if value is True: |
| 20 | + await strip.children[index].turn_on() |
| 21 | + elif value is False: |
| 22 | + await strip.children[index].turn_off() |
| 23 | + elif dev.device_type == DeviceType("plug"): |
| 24 | + await dev.update() |
| 25 | + if value is True: |
| 26 | + await dev.turn_on() |
| 27 | + elif value is False: |
| 28 | + await dev.turn_off() |
20 | 29 |
|
21 | 30 |
|
22 | 31 | def power_set(host, port, index, value): |
23 | 32 | asyncio.run(_power_set(host, port, index, value)) |
24 | 33 |
|
25 | | - |
26 | | -def power_get(host, port, index): |
| 34 | +async def _power_get(host, port, index): |
27 | 35 | assert port is None |
28 | 36 | index = int(index) |
29 | | - strip = IotStrip(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 |
| 37 | + dev = await Discover.discover_single(host) |
| 38 | + if dev.device_type == DeviceType("strip"): |
| 39 | + strip = IotStrip(host) |
| 40 | + await strip.update() |
| 41 | + assert ( |
| 42 | + len(strip.children) > index |
| 43 | + ), "Trying to access non-existant plug socket on strip" |
| 44 | + return strip.children[index].is_on |
| 45 | + elif dev.device_type == DeviceType("plug"): |
| 46 | + await dev.update() |
| 47 | + return dev.is_on |
| 48 | + |
| 49 | +def power_get(host, port, index): |
| 50 | + return asyncio.run(_power_get(host, port, index)) |
0 commit comments