-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.py
199 lines (175 loc) · 7.27 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
# LazyRolls roller blind plugin
#
# Author: ACE [email protected]
#
# https://github.com/ACE1046/LazyRollsDomoticz
#
# v0.01 29.02.2020
# v0.02 30.02.2020 Fixed some errors in domoticz log file
# v0.03 30.02.2020 Fixed some more errors in domoticz log file
# v0.04 06.12.2022 Plugin updated for Domoticz 2022.2
#
"""
<plugin key="LazyRolls" name="LazyRolls roller blinds" author="ACE" version="0.04" wikilink="http://imlazy.ru/rolls/domoticz.html" externallink="http://imlazy.ru/">
<description>
<h2>LazyRolls roller blinds plugin</h2><br/>
<h3>Configuration</h3>
Configuration options:
</description>
<params>
<param field="Address" label="IP Address" width="200px" required="true" default="127.0.0.1"/>
</params>
</plugin>
"""
import Domoticz
class BasePlugin:
httpXml = None
httpCmd = None
GoTo = 0
NextUpdate = 0
CmdStop = 0
def __init__(self):
return
def onStart(self):
# Domoticz.Log("onStart called")
if Parameters["Mode6"] == "Debug":
Domoticz.Debugging(1)
DumpConfigToLog()
if (len(Devices) == 0):
Domoticz.Device(Name="LazyRoll", Unit=1, Type=244, Subtype=73, Switchtype=13, Image=3).Create()
Domoticz.Log("Device created.")
self.httpCmd = Domoticz.Connection(Name="LazyRollCmd", Transport="TCP/IP", Protocol="HTTP", Address=Parameters["Address"], Port="80")
self.httpXml = Domoticz.Connection(Name="LazyRollXml", Transport="TCP/IP", Protocol="HTTP", Address=Parameters["Address"], Port="80")
def onStop(self):
Domoticz.Log("onStop called")
def onConnect(self, Connection, Status, Description):
#Domoticz.Log("onConnect called")
if (Connection == self.httpCmd):
if (self.CmdStop == 1):
sendData = { 'Verb' : 'GET',
'URL' : '/stop',
'Headers' : { 'Content-Type': 'text/xml; charset=utf-8', \
'Connection': 'keep-alive', \
'Accept': 'Content-Type: text/html; charset=UTF-8', \
'Host': Parameters["Address"]+":80", \
'User-Agent':'Domoticz/1.0' }
}
self.CmdStop = 0
else:
sendData = { 'Verb' : 'GET',
'URL' : '/set?pos='+str(self.GoTo),
'Headers' : { 'Content-Type': 'text/xml; charset=utf-8', \
'Connection': 'keep-alive', \
'Accept': 'Content-Type: text/html; charset=UTF-8', \
'Host': Parameters["Address"]+":80", \
'User-Agent':'Domoticz/1.0' }
}
self.httpCmd.Send(sendData)
if (Connection == self.httpXml):
sendData = { 'Verb' : 'GET',
'URL' : '/xml',
'Headers' : { 'Content-Type': 'text/xml; charset=utf-8', \
'Connection': 'keep-alive', \
'Accept': 'Content-Type: text/html; charset=UTF-8', \
'Host': Parameters["Address"]+":80", \
'User-Agent':'Domoticz/1.0' }
}
self.httpXml.Send(sendData)
def onMessage(self, Connection, Data):
#Status = int(Data["Status"])
if (Connection == self.httpCmd):
Domoticz.Heartbeat(1);
self.NextUpdate=1;
if (Connection == self.httpXml):
try:
strData = Data["Data"].decode("utf-8", "ignore")
#Domoticz.Log("onMessage Xml called")
import xml.etree.ElementTree as ET
root = ET.fromstring(strData)
p_now = int(root.find("Position/Now").text)
p_dst = int(root.find("Position/Dest").text)
p_max = int(root.find("Position/Max").text)
if (p_max == 0): p_max=100;
percent = round(p_now*100/p_max)
nVal = 2
if (p_now <= 0): nVal = 1
if (p_now >= p_max): nVal = 0
for d in Devices:
Devices[d].Update(nVal, str(100-percent))
#Domoticz.Log(str(percent))
if (p_now == p_dst):
Domoticz.Heartbeat(10);
self.NextUpdate=3;
#Domoticz.Log('30s')
else:
Domoticz.Heartbeat(1);
self.NextUpdate=1;
#Domoticz.Log('1s')
except:
pass
if (Connection.Connected()): Connection.Disconnect()
def onCommand(self, Unit, Command, Level, Hue):
self.CmdStop = 0
#Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
if (Command == "Set Level"): self.GoTo=100-Level
if (Command == "Off"): self.GoTo=100
if (Command == "On"): self.GoTo=0
if (Command == "Open"): self.GoTo=0
if (Command == "Close"): self.GoTo=100
if (Command == "Stop"): self.CmdStop = 1
self.httpCmd.Connect()
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):
# Domoticz.Log("onHeartbeat called")
if (self.NextUpdate > 0): self.NextUpdate=self.NextUpdate-1;
if (self.NextUpdate == 0):
NextUpdate=3;
if (self.httpXml.Connected() or self.httpXml.Connecting()):
self.httpXml.Disconnect()
else:
self.httpXml.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()
# Generic helper functions
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