This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
241 lines (174 loc) · 8.03 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# Ween thermostat plugin for Domoticz
# Author: Joggee Software
#
"""
<plugin key="WeenThermostat" name="Ween Thermostat" author="joggee-fr" version="0.0.1" externallink="https://gitlab.com/joggee-fr/ween-thermostat-domoticz">
<description>
<h2>Ween Thermostat</h2><br/>
This plugin add devices for the Ween Thermostat based on the WiFi local API.
</description>
<params>
<param field="Address" label="IP address" width="600px" required="true"/>
<param field="Mode2" label="Token" width="600px" required="true"/>
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="false" value="0" default="true"/>
<option label="true" value="1"/>
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import json
import time
class BasePlugin:
_ipAddress = ""
_token = ""
_conditionsConnection = None
_lastConditionsTimestamp = None
_setpointConnection = None
_pendingSetpoint = 0
# Constants
_conditionsPeriod = 120 # seconds
_conditionsUnit = 1
_setpointUnit = 2
def __init__(self):
return
def _updateDevice(self, Unit, nValue, sValue):
if (Unit in Devices):
Devices[Unit].Update(nValue=nValue, sValue=str(sValue))
Domoticz.Debug("Updated device " + str(nValue) + " with value: " + str(sValue))
def _disconnect(self, Connection):
if (Connection != None and (Connection.Connected() or Connection.Connecting())):
self.__connection.Disconnect()
def _setSetpoint(self):
Domoticz.Debug("Init setpoint connection")
self._setpointConnection = Domoticz.Connection(Name="Ween", Transport="TCP/IP", Protocol="HTTP", Address=self._ipAddress, Port="80")
self._setpointConnection.Connect()
def _getBaseConnectionData(self, method, endpoint):
return {
"Verb" : method,
"URL" : "/" + endpoint + ".cgi?token=" + self._token,
"Headers" : {
"Host" : self._ipAddress,
"User-Agent": 'Domoticz/1.0'
}
}
def _getHumidityStatus(self, humidity):
if humidity < 30:
return 2
if humidity > 70:
return 3
return 1
def onStart(self):
# Debug mode
Domoticz.Debugging(int(Parameters["Mode6"]))
Domoticz.Debug("onStart called")
self._ipAddress = Parameters["Address"]
self._token = Parameters["Mode2"]
# Create devices
if (1 not in Devices):
Domoticz.Device(Name="Conditions", Unit=self._conditionsUnit, TypeName="Temp+Hum").Create()
Domoticz.Debug("Creating Conditions device")
if (2 not in Devices):
Domoticz.Device(Name="Thermostat", Unit=self._setpointUnit, Type=242, Subtype=1).Create()
Domoticz.Debug("Creating Thermostat device")
def onStop(self):
Domoticz.Debug("onStop called")
self._disconnect(self._setpointConnection)
self._disconnect(self._conditionsConnection)
def onConnect(self, Connection, Status, Description):
Domoticz.Debug("onConnect called")
if (Status == 0):
if (Connection == self._conditionsConnection):
Domoticz.Debug("Conditions connection succeed")
data = self._getBaseConnectionData("GET", "conditions")
Connection.Send(data)
elif (Connection == self._setpointConnection):
Domoticz.Debug("Conditions connection succeed")
data = self._getBaseConnectionData("GET", "setpoint")
data["URL"] = data["URL"] + "&value=" + str(self._pendingSetpoint)
Connection.Send(data)
else:
Domoticz.Error("Failed to connect (" + str(Status) + ") to " + self._ipAddress + " with error: " + Description)
def onMessage(self, Connection, Data):
Domoticz.Debug("onMessage called")
receivedData = Data["Data"].decode("utf-8", "ignore")
status = int(Data["Status"])
# Always reset setpoint
if (Connection == self._setpointConnection):
self._pendingSetpoint = 0
if (status == 200):
if (Connection == self._conditionsConnection):
conditions = json.loads(receivedData)
if ("temperature" in conditions) and ("humidity" in conditions):
temperature = round(conditions["temperature"], 1)
humidity = int(conditions["humidity"])
value = str(temperature) + ';' + str(humidity) + ';' + str(self._getHumidityStatus(humidity))
self._updateDevice(Unit=self._conditionsUnit, nValue=0, sValue=value)
self._lastConditionsTimestamp = time.monotonic()
else:
Domoticz.Error("Invalid received format for conditions")
elif (Connection == self._setpointConnection):
success = json.loads(receivedData)
if ("success" in success):
if (success["success"] != True):
Domoticz.Error("Error setting setpoint")
else:
Domoticz.Error("Invalid received format for setpoint")
else:
Domoticz.Error("Connection returned error status: " + str(status))
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Debug("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
if (Unit == self._setpointUnit) and (Command == "Set Level"):
self._disconnect(self._setpointConnection)
self._pendingSetpoint = int(Level)
if (self._setpointConnection == None):
self._setSetpoint()
def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Debug("Notification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(Priority) + "," + Sound + "," + ImageFile)
def onDisconnect(self, Connection):
Domoticz.Debug("onDisconnect called")
if (Connection == self._conditionsConnection):
self._conditionsConnection = None
elif (Connection == self._setpointConnection):
self._setpointConnection = None
if (self._pendingSetpoint > 0):
self._setSetpoint()
def onHeartbeat(self):
Domoticz.Debug("onHeartbeat called")
if (self._conditionsConnection == None or self._conditionsConnection.Connected() != True):
now = time.monotonic()
elapsed = now - self._lastConditionsTimestamp if self._lastConditionsTimestamp != None else None
# Get current conditions every two minutes should be enough
if (self._lastConditionsTimestamp == None or elapsed >= (self._conditionsPeriod - 1)):
Domoticz.Debug("Init conditions connection")
self._conditionsConnection = Domoticz.Connection(Name="Ween", Transport="TCP/IP", Protocol="HTTP", Address=self._ipAddress, Port="80")
self._conditionsConnection.Connect()
global _plugin
_plugin = BasePlugin()
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()