-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
183 lines (146 loc) · 5.48 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
# Rain prediction plugin
#
# Author: Gerardvs
#
"""
<plugin key="FutureRainPlug" name="Rain Predictor Buienradar" author="gerardvs" version="1.1.0" wikilink="http://www.domoticz.com/wiki/plugins/plugin.html" externallink="http://www.buienradar.nl/overbuienradar/gratis-weerdata">
<params>
<param field="Mode1" label="DeviceID" width="125px" required="true" default="321H123U"/>
<param field="Mode2" label="Emulate" width="75px">
<options>
<option label="True" value="Emulate"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
<param field="Mode3" label="Lookahead minutes" width="75px" required="true" default="45"/>
<param field="Mode4" label="Update every x minutes" width="75px" required="true" default="15"/>
<param field="Mode5" label="Value or mm" width="75px">
<options>
<option label="value" value="value"/>
<option label="mm" value="mm" default="mm" />
</options>
</param>
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
from rainfuture import RainFuture
from datetime import datetime, timedelta
lastUpdate = datetime.now()
location = ""
def onStart():
global lastUpdate
if Parameters["Mode6"] == "Debug":
Domoticz.Debugging(1)
Domoticz.Debug("onStart called")
DumpConfigToLog()
lastUpdate = datetime.now()
CreateSensor()
#DumpSettingsToLog()
UpdateSensor()
Domoticz.Heartbeat(30)
return True
def onStop():
Domoticz.Debug("onStop called")
return True
def onConnect(Status, Description):
Domoticz.Debug("onConnect called:")
return True
def onMessage(Data, Status, Extra):
Domoticz.Debug("onMessage called")
return True
def onCommand(Unit, Command, Level, Hue):
Domoticz.Debug("onCommand called")
return True
def onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Debug("onNotification called")
return True
def onDisconnect():
Domoticz.Debug("onDisconnect called")
return True
def onHeartbeat():
Domoticz.Debug("onHeartbeat called")
interval = int(Parameters["Mode4"])
if isActionTime(interval):
UpdateSensor()
# 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 Idx: '" + str(Devices[x].ID) + "'")
# Domoticz.Debug("Device ID: '" + Devices[x].DeviceID + "'")
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
def DumpSettingsToLog():
for x in Settings:
if Settings[x] != "":
Domoticz.Debug( "'" + x + "':'" + str(Settings[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
return
def GetLocation():
global location
if Settings["Location"] != "":
location = Settings["Location"].split(';')
if (len(location)==2):
Domoticz.Log( "'Lat':'" + location[0] + " Lon:" + location[1] + "'")
else:
LocationError()
else:
LocationError()
return
def LocationError():
Domoticz.Error("Error reading location. Please enter the location coordinates in the system settings.")
Domoticz.Error("Using default location. 52.09,5.11")
location[0]="52.09"
location[1]="5.11"
def CreateSensor():
if (len(Devices) == 0):
devID=Parameters["Mode1"]
Domoticz.Device(Name="Rain2Come", Unit=1, TypeName="Custom",Used=1, DeviceID=devID).Create()
Domoticz.Log("Rain2Come Device created")
def UpdateSensor():
global lastUpdate
global location
nVal=0
sVal=255
if Parameters["Mode2"] == "Emulate":
nVal = Devices[1].nValue
nVal += 5
if nVal>255:
nVal=0
sVal = 255 - nVal
Devices[1].Update(nVal,str(sVal))
else:
GetLocation()
r = RainFuture()
if Parameters["Mode6"] == "Debug":
r.debug=True
ahead = int(Parameters["Mode3"])
if Parameters["Mode5"] == "value":
nVal = r.getPredictionValue(location[0],location[1],ahead)
else:
nVal = r.getPrediction(location[0],location[1],ahead)
sVal = nVal
Devices[1].Update(0,str(nVal))
Domoticz.Log("Sensor updated: " + str(nVal) + ";" + str(sVal))
Domoticz.Debug("Devices[1].Idx=" + str(Devices[1].ID))
Domoticz.Debug("Devices[1].nValue=" + str(Devices[1].nValue))
Domoticz.Debug("Devices[1].sValue=" + Devices[1].sValue)
lastUpdate = datetime.now()
def isActionTime(minutes):
nextUpdate = lastUpdate + timedelta(minutes=minutes)
Domoticz.Debug("isActionTime:nextUpdate=" + str(nextUpdate))
return datetime.now() > nextUpdate