forked from ffes/domoticz-buienradar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
301 lines (252 loc) · 12 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#
# Buienradar.nl Weather Lookup Plugin
#
# Frank Fesevur, 2017
# https://github.com/ffes/domoticz-buienradar
#
# About the weather service:
# https://www.buienradar.nl/overbuienradar/gratis-weerdata
#
# Updates by G3rard, August 2017
# Rain rate, Visibility, Solar Radiation, Rain forecast and Weather forecast added and some minor changes
# Rain prediction from Rainfuture script by gerardvs - https://github.com/seventer/raintocome and Buienradar script from mjj4791 - https://github.com/mjj4791/python-buienradar
#
"""
<plugin key="Buienradar" name="Buienradar.nl (Weather lookup)" author="ffes" version="2.0" wikilink="https://github.com/ffes/domoticz-buienradar" externallink="https://www.buienradar.nl/overbuienradar/gratis-weerdata">
<params>
<param field="Mode2" label="Update every x minutes" width="200px" required="true" default="10"/>
<param field="Mode3" label="Rain forecast timeframe in minutes" width="200px" required="true" default="30"/>
<param field="Mode4" label="Temperature and humidity" width="200px" required="true">
<options>
<option label="Combined in one device" value="True" default="true" />
<option label="Two separate devices" value="False" />
</options>
</param>
<param field="Mode5" label="Include Wind chill in Wind device" width="200px" required="true">
<options>
<option label="Yes" value="True" default="true" />
<option label="No" value="False"/>
</options>
</param>
<param field="Mode6" label="Debug" width="100px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
<option label="Logging" value="File"/>
</options>
</param>
</params>
</plugin>
"""
try:
import Domoticz
except ImportError:
import fakeDomoticz as Domoticz
import urllib.request
import urllib.error
import xml.etree.ElementTree as ET
from math import radians, cos, sin, asin, sqrt
from datetime import datetime, timedelta
from buienradar import Buienradar
from rainforecast import RainForecast
#############################################################################
# Domoticz call back functions #
#############################################################################
class BasePlugin:
myLat = myLon = 0
br = rf = None
interval = timeframe = None
def onStart(self):
#pylint: disable=undefined-variable
global br, rf
if Parameters["Mode6"] != "Normal":
Domoticz.Debugging(1)
DumpConfigToLog()
# Get the location from the Settings
if not "Location" in Settings:
Domoticz.Log("Location not set in Preferences")
return False
# The location is stored in a string in the Settings
loc = Settings["Location"].split(";")
self.myLat = float(loc[0])
self.myLon = float(loc[1])
Domoticz.Debug("Coordinates from Domoticz: " + str(self.myLat) + ";" + str(self.myLon))
if self.myLat == None or self.myLon == None:
Domoticz.Log("Unable to parse coordinates")
return False
# Get the interval specified by the user
self.interval = int(Parameters["Mode2"])
if self.interval == None:
Domoticz.Log("Unable to parse interval, so set it to 10 minutes")
self.interval = 10
# Buienradar only updates the info every 10 minutes.
# Allowing values below 10 minutes will not get you more info
if self.interval < 10:
Domoticz.Log("Interval too small, changed to 10 minutes because Buienradar only updates the info every 10 minutes")
self.interval = 10
# Get the timeframe for the rain forecast
self.timeframe = int(Parameters["Mode3"])
if self.timeframe == None:
Domoticz.Log("Unable to parse timeframe, set to 30 minutes")
self.timeframe = 30
if self.timeframe < 5 or self.timeframe > 120:
Domoticz.Log("Timeframe must be >=5 and <=120. Now set to 30 minutes")
self.timeframe = 30
br = Buienradar(self.myLat, self.myLon, self.interval)
rf = RainForecast(self.myLat, self.myLon, self.timeframe)
# Check if devices need to be created
createDevices()
# Check if images are in database
if 'BuienradarRainLogo' not in Images: Domoticz.Image('buienradar.zip').Create()
if 'BuienradarLogo' not in Images: Domoticz.Image('buienradar-logo.zip').Create()
# Get data from Buienradar
br.getBuienradarXML()
br.getNearbyWeatherStation()
# Fill the devices with the Buienradar values
fillDevices()
Domoticz.Heartbeat(30)
return True
def onHeartbeat(self):
# Does the weather information needs to be updated?
if br.needUpdate():
# Get new information and update the devices
br.getBuienradarXML()
fillDevices()
return True
_plugin = BasePlugin()
def onStart():
_plugin.onStart()
def onHeartbeat():
_plugin.onHeartbeat()
#############################################################################
# Domoticz helper functions #
#############################################################################
def LogMessage(Message):
if Parameters["Mode6"] == "File":
f = open(Parameters["HomeFolder"] + "plugin.log", "a")
f.write(Message + "\r\n")
f.close()
Domoticz.Debug(Message)
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
LogMessage( "'" + x + "':'" + str(Parameters[x]) + "'")
LogMessage("Device count: " + str(len(Devices)))
for x in Devices:
LogMessage("Device: " + str(x) + " - " + str(Devices[x]))
LogMessage("Internal ID: '" + str(Devices[x].ID) + "'")
LogMessage("External ID: '" + str(Devices[x].DeviceID) + "'")
LogMessage("Device Name: '" + Devices[x].Name + "'")
LogMessage("Device nValue: " + str(Devices[x].nValue))
LogMessage("Device sValue: '" + Devices[x].sValue + "'")
LogMessage("Device LastLevel: " + str(Devices[x].LastLevel))
return
# Update Device into database
def UpdateDevice(Unit, nValue, sValue, AlwaysUpdate=False):
# Make sure that the Domoticz device still exists (they can be deleted) before updating it
if Unit in Devices:
if Devices[Unit].nValue != nValue or Devices[Unit].sValue != sValue or AlwaysUpdate == True:
Devices[Unit].Update(nValue, str(sValue))
Domoticz.Log("Update " + Devices[Unit].Name + ": " + str(nValue) + " - '" + str(sValue) + "'")
return
#############################################################################
# Device specific functions #
#############################################################################
def createDevices():
# Are there any devices?
###if len(Devices) != 0:
# Could be the user deleted some devices, so do nothing
###return
# Give the devices a unique unit number. This makes updating them more easy.
# UpdateDevice() checks if the device exists before trying to update it.
# Add the temperature and humidity device(s)
if Parameters["Mode4"] == "True":
if 3 not in Devices:
Domoticz.Device(Name="Temperature", Unit=3, TypeName="Temp+Hum", Used=1).Create()
else:
if 1 and 2 not in Devices:
Domoticz.Device(Name="Temperature", Unit=1, TypeName="Temperature", Used=1).Create()
Domoticz.Device(Name="Humidity", Unit=2, TypeName="Humidity", Used=1).Create()
# Add the barometer device
if 4 not in Devices:
Domoticz.Device(Name="Barometer", Unit=4, TypeName="Barometer", Used=1).Create()
# Add the wind (and wind chill?) device
if Parameters["Mode5"] == "True":
if 6 not in Devices:
Domoticz.Device(Name="Wind", Unit=6, TypeName="Wind+Temp+Chill", Used=1).Create()
else:
if 5 not in Devices:
Domoticz.Device(Name="Wind", Unit=5, TypeName="Wind", Used=1).Create()
if 7 not in Devices:
Domoticz.Device(Name="Visibility", Unit=7, TypeName="Visibility", Used=1).Create()
if 8 not in Devices:
Domoticz.Device(Name="Solar Radiation", Unit=8, TypeName="Solar Radiation", Used=1).Create()
if 9 not in Devices:
Domoticz.Device(Name="Rain rate", Unit=9, TypeName="Custom", Options = { "Custom" : "1;mm/h"}, Used=1).Create()
UpdateImage(9, 'BuienradarRainLogo')
if 10 not in Devices:
Domoticz.Device(Name="Rain forecast [0-255]", Unit=10, TypeName="Custom", Used=1).Create()
UpdateImage(10, 'BuienradarRainLogo')
if 11 not in Devices:
Domoticz.Device(Name="Rain forecast", Unit=11, TypeName="Custom", Options = { "Custom" : "1;mm/h"}, Used=1).Create()
UpdateImage(11, 'BuienradarRainLogo')
if 12 not in Devices:
Domoticz.Device(Name="Weather forecast", Unit=12, TypeName="Text", Used=1).Create()
#UpdateImage(12, 'BuienradarLogo') # Logo update doesn't work for text device
Domoticz.Log("Devices checked and created/updated if necessary")
def fillDevices():
# Did we get new weather info? Update all the possible devices
if br.getWeather():
# Temperature
if br.temperature != None:
UpdateDevice(1, 0, str(round(br.temperature, 1)))
# Humidity
if br.humidity != None:
UpdateDevice(2, br.humidity, str(br.getHumidityStatus()))
# Temperature and Humidity
if br.temperature != None and br.humidity != None:
UpdateDevice(3, 0,
str(round(br.temperature, 1))
+ ";" + str(br.humidity)
+ ";" + str(br.getHumidityStatus()))
# Barometer
if br.pressure != None:
UpdateDevice(4, 0,
str(round(br.pressure, 1))
+ ";" + str(br.getBarometerForecast()))
# Wind
if br.windBearing != None and br.windSpeed != None and br.windSpeedGusts != None:
UpdateDevice(5, 0, str(br.windBearing)
+ ";" + br.getWindDirection()
+ ";" + str(round(br.windSpeed * 10))
+ ";" + str(round(br.windSpeedGusts * 10))
+ ";0;0")
# Wind and Wind Chill
UpdateDevice(6, 0, str(br.windBearing)
+ ";" + br.getWindDirection()
+ ";" + str(round(br.windSpeed * 10))
+ ";" + str(round(br.windSpeedGusts * 10))
+ ";" + str(round(br.temperature, 1))
+ ";" + str(br.getWindChill()))
# Visibility
if br.visibility != None:
UpdateDevice(7, 0, str(round((br.visibility/1000), 1))) # Visibility is m in Buienradar and km in Domoticz
# Solar Radiation
if br.solarIrradiance != None:
UpdateDevice(8, 0, str(br.solarIrradiance))
# Rain rate
if br.rainRate == None: br.rainRate = 0
UpdateDevice(9, 0, str(br.rainRate))
# Rain forecast
result = rf.get_precipfc_data() ###30 nog nakijken
UpdateDevice(10, 0, str(result['average']))
UpdateDevice(11, 0, str(result['averagemm']))
if br.weatherForecast != None:
UpdateDevice(12, 0, str(br.weatherForecast))
# Synchronise images to match parameter in hardware page
def UpdateImage(Unit, Logo):
if Unit in Devices and Logo in Images:
if Devices[Unit].Image != Images[Logo].ID:
Domoticz.Log("Device Image update: 'Buienradar', Currently " + str(Devices[Unit].Image) + ", should be " + str(Images[Logo].ID))
Devices[Unit].Update(nValue=Devices[Unit].nValue, sValue=str(Devices[Unit].sValue), Image=Images[Logo].ID)
return