forked from theyosh/TerrariumPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrariumSwitchKasa.py
92 lines (74 loc) · 2.63 KB
/
terrariumSwitchKasa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
import terrariumLogging
logger = terrariumLogging.logging.getLogger(__name__)
from terrariumSwitch import terrariumPowerSwitchSource
from terrariumUtils import terrariumUtils
from hashlib import md5
import asyncio
from kasa import Discover, SmartStrip, SmartPlug
class terrariumPowerSwitchTPLinkKasa(terrariumPowerSwitchSource):
TYPE = 'tplinkkasa'
URL = '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$'
def __get_address(self):
data = self.get_address().strip().split(',')
if len(data) == 2 and '' == data[1]:
del(data[1])
return data
def load_hardware(self):
address = self.__get_address()
if len(address) == 2:
self._device = SmartStrip(address[0])
else:
self._device = SmartPlug(address[0])
def set_hardware_state(self, state, force = False):
async def __set_hardware_state(device,address,state):
await device.update()
strip = len(address) == 2
plug = (device if not strip else device.plugs[int(address[1])-1])
if state is True:
await plug.turn_on()
else:
await plug.turn_off()
return True
address = self.__get_address()
try:
asyncio.run(__set_hardware_state(self._device,address,state))
return True
except RuntimeError as err:
return True
def get_hardware_state(self):
data = []
async def __get_hardware_state(device,address):
await device.update()
strip = len(address) == 2
if strip:
data.append(device.plugs[int(address[1])-1].is_on)
else:
data.append(device.is_on)
address = self.__get_address()
try:
asyncio.run(__get_hardware_state(self._device,address))
except RuntimeError as err:
return None
return len(data) == 1 and terrariumUtils.is_true(data[0])
@staticmethod
def scan_power_switches(callback=None, **kwargs):
found_devices = []
async def scan():
devices = await Discover.discover()
for ip_address in devices:
device = devices[ip_address]
await device.update()
if device.is_strip:
for counter in range(1,len(device.plugs)+1):
found_devices.append(terrariumPowerSwitchTPLinkKasa(md5(('{}{}{}'.format(terrariumPowerSwitchTPLinkKasa.TYPE,device.device_id,counter)).encode()).hexdigest(),
'{},{}'.format(device.host,counter),
device.plugs[counter-1].alias,
None,
callback))
try:
asyncio.run(scan())
except RuntimeError as err:
pass
for device in found_devices:
yield device