-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.py
executable file
·221 lines (181 loc) · 7.22 KB
/
plugin.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# TP-Link Tapo Plugin
#
# Author: 593304
#
"""
<plugin key="TapoPlugin" name="TP-Link Tapo Plugin" author="593304" version="0.2" externallink="">
<description>
<h2>TP-Link Tapo Plugin</h2><br/>
<p>The plugin will connect to a Tapo device with the given IP address, username(e-mail address) and password.</p>
<p>Before using this plugin, you have to install the<a href="https://pypi.org/project/PyP100/" style="margin-left: 5px">PyP100 module</a></p>
<br />
<br />
</description>
<params>
<param field="Mode1" label="Username" width="250px" required="true"/>
<param field="Password" label="Password" width="250px" required="true" password="true"/>
<param field="Address" label="IP address" width="250px" required="true"/>
<param field="Mode2" label="Debug" width="50px">
<options>
<option label="on" value="on"/>
<option label="Off" value="off" default="off"/>
</options>
</param>
</params>
</plugin>
"""
import Domoticz
from PyP100 import PyP100
import json
# Simple heartbeat with a 10 secs interval
class Heartbeat():
def __init__(self):
self.callback = None
self.interval = 1
def setHeartbeat(self, callback):
Domoticz.Heartbeat(self.interval)
Domoticz.Log("Heartbeat interval is " + str(self.interval) + ".")
self.callback = callback
def beatHeartbeat(self):
self.callback()
class TapoPlugin:
def __init__(self):
self.unit = None
self.p100 = None
self.lastState = None
return
def onStart(self):
Domoticz.Log("onStart called")
# Setting up debug mode
if (Parameters["Mode2"] != "off"):
Domoticz.Debugging(1)
Domoticz.Debug("Debug mode enabled")
# Setting up heartbeat
self.heartbeat = Heartbeat()
self.heartbeat.setHeartbeat(self.update)
# Creating PyP100 object
ip = Parameters["Address"]
email = Parameters["Mode1"]
password = Parameters["Password"]
self.p100 = PyP100.P100(ip, email, password)
Domoticz.Debug("Tapo object created with IP: " + ip)
# Getting last state to get device type
self.update(False)
# Creating device
self.unit = 1
if self.unit not in Devices:
typeName = "Selector Switch"
switchType = 0
# ToDo: Need a real Tapo bulb to create the dimmer/color switch device for Domoticz
#if "bulb" in self.lastState["type"].lower():
# typeName = ...
# switchType = ...
Domoticz.Device(
Name = self.lastState["type"],
Unit = self.unit,
TypeName = typeName,
Switchtype = switchType,
Image = 9,
Options = {}).Create()
self.update()
DumpConfigToLog()
return
def onStop(self):
Domoticz.Log("onStop called")
return
def onConnect(self, Connection, Status, Description):
Domoticz.Log("onConnect called; connection: %s, status: %s, description: %s" % (str(Connection), str(Status), str(Description)))
return
def onMessage(self, Connection, Data):
Domoticz.Log("onMessage called; connection: %s, data: %s" % (str(Connection), str(Data)))
return
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
if Unit != self.unit:
Domoticz.Error("Unknown device with unit: " + str(Unit))
return
commandValue = 1 if Command == "On" else 0
if self.lastState["device_on"] == commandValue:
Domoticz.Log("Command and last state is the same, nothing to do")
return
self.p100.handshake()
self.p100.login()
if Command == "On":
self.p100.turnOn()
else:
self.p100.turnOff()
self.update()
return
def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Log("Notification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(Priority) + "," + Sound + "," + ImageFile)
return
def onDisconnect(self, Connection):
Domoticz.Log("onDisconnect called")
return
def onHeartbeat(self):
self.heartbeat.beatHeartbeat()
return
def update(self, updateDomoticz = True):
self.p100.handshake()
self.p100.login()
deviceInfo = self.p100.getDeviceInfo()
if not isinstance(deviceInfo, dict):
lastState = json.loads(deviceInfo)
else:
lastState = deviceInfo
if lastState["error_code"] != 0:
self.lastState = None
Domoticz.Error("Cannot get last state from device error code: " + str(lastState["error_code"]))
else:
self.lastState = lastState["result"]
Domoticz.Debug(json.dumps(self.lastState))
# Update device
if self.unit not in Devices or not updateDomoticz:
return
# ToDo: Need a real Tapo bulb to properly update Domoticz device with not just the power state
powerState = self.lastState["device_on"]
powerStateValue = 1 if powerState else 0
powerStateStr = "On" if powerState else "Off"
if (Devices[self.unit].nValue != powerStateValue) or (Devices[self.unit].sValue != powerStateStr):
Domoticz.Debug("Updating %s (%d, %s)" % (Devices[self.unit].Name, powerStateValue, powerStateStr))
Devices[self.unit].Update(nValue = powerStateValue, sValue = powerStateStr)
return
global _plugin
_plugin = TapoPlugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
def onConnect(Connection, Status, Description):
global _plugin
_plugin.onConnect(Connection, Status, Description)
def onMessage(Connection, Data):
global _plugin
_plugin.onMessage(Connection, Data)
def onCommand(Unit, Command, Level, Hue):
global _plugin
_plugin.onCommand(Unit, Command, Level, Hue)
def onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile):
global _plugin
_plugin.onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile)
def onDisconnect(Connection):
global _plugin
_plugin.onDisconnect(Connection)
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel))
return