-
Notifications
You must be signed in to change notification settings - Fork 7
/
plugin.py
executable file
·200 lines (174 loc) · 7.45 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
"""
MoonPhases Plugin
Author: Ycahome, 2017 CREDITS TO jackslayter
Version: 1.0.0: Initial Release
Version: 1.0.1: Southern hemisphere moon images
Version: 1.0.4: Changed icon/zip names to avoid underscores - something fishy in domoticz images or the python api
"""
"""
<plugin key="MoonPhases" name="Moon Phases" author="ycahome ft. jackslayter"
version="1.0.0" wikilink="http://www.domoticz.com/wiki/plugins/"
externallink="http://www.domoticz.com/forum/viewtopic.php?f=65&t=21993">
<description>
<h3>----------------------------------------------------------------------</h3>
<h2>Moon Phases v.1.0.5</h2><br/>
<h3>----------------------------------------------------------------------</h3>
</description>
<params>
<param field="Mode1" label="WU Key" width="200px" required="true" default="your_Wunderground_key"/>
<param field="Mode2" label="CountryCode" width="100px" required="true" default="au"/>
<param field="Mode3" label="City" width="300px" required="true" default="sydney"/>
<param field="Mode4" label="Polling interval (minutes)" width="40px" required="true" default="60"/>
<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
import urllib.request
import json
from datetime import datetime
from datetime import timedelta
icons = {"MoonPhases1NM": "MoonPhases1NM.zip",
"MoonPhases2WC": "MoonPhases2WC.zip",
"MoonPhases3FQ": "MoonPhases3FQ.zip",
"MoonPhases4WG": "MoonPhases4WG.zip",
"MoonPhases5FM": "MoonPhases5FM.zip",
"MoonPhases6WG": "MoonPhases6WG.zip",
"MoonPhases7LQ": "MoonPhases7LQ.zip",
"MoonPhases8WC": "MoonPhases8WC.zip",
"MoonPhases1NMSH": "MoonPhases1NMSH.zip",
"MoonPhases2WCSH": "MoonPhases2WCSH.zip",
"MoonPhases3FQSH": "MoonPhases3FQSH.zip",
"MoonPhases4WGSH": "MoonPhases4WGSH.zip",
"MoonPhases5FMSH": "MoonPhases5FMSH.zip",
"MoonPhases6WGSH": "MoonPhases6WGSH.zip",
"MoonPhases7LQSH": "MoonPhases7LQSH.zip",
"MoonPhases8WCSH": "MoonPhases8WCSH.zip"}
class BasePlugin:
def __init__(self):
self.debug = False
self.nextupdate = datetime.now()
self.pollinterval = 60 # default polling interval in minutes
self.error = False
self.southern_hemi = False
self.suffix= 'SH'
self.reload = True
return
def onStart(self):
Domoticz.Debug("onStart called")
global icons
if Parameters["Mode6"] == 'Debug':
self.debug = True
Domoticz.Debugging(1)
DumpConfigToLog()
else:
Domoticz.Debugging(0)
# load custom MoonPhase images
for key, value in icons.items():
if key not in Images:
Domoticz.Image(value).Create()
Domoticz.Debug("Added icon: " + key + " from file " + value)
Domoticz.Debug("Number of icons loaded = " + str(len(Images)))
for image in Images:
Domoticz.Debug("Icon " + str(Images[image].ID) + " " + Images[image].Name)
# create the mandatory child device if it does not yet exist
if 1 not in Devices:
Domoticz.Device(Name="Status", Unit=1, TypeName="Custom",Options={"Custom": "1;Days"},Used=1).Create()
# check polling interval parameter
try:
temp = int(Parameters["Mode4"])
except:
Domoticz.Error("Invalid polling interval parameter")
else:
if temp < 60:
temp = 60 # minimum polling interval
Domoticz.Error("Specified polling interval too short: changed to 60 minutes")
elif temp > 1440:
temp = 1440 # maximum polling interval is 1 day
Domoticz.Error("Specified polling interval too long: changed to 1440 minutes (24 hours)")
self.pollinterval = temp
Domoticz.Log("Using polling interval of {} minutes".format(str(self.pollinterval)))
def onStop(self):
Domoticz.Debug("onStop called")
Domoticz.Debugging(0)
def onHeartbeat(self):
Domoticz.Debug("onStop called")
now = datetime.now()
if now >= self.nextupdate:
self.nextupdate = now + timedelta(minutes=self.pollinterval)
u = "http://api.wunderground.com/api/%s/astronomy/q/%s/%s.json" % (Parameters["Mode1"],Parameters["Mode2"],Parameters["Mode3"])
Domoticz.Debug('Moon URL:%s' % u)
data = json.loads(urllib.request.urlopen(u).read().decode('ascii'))
lune = data['moon_phase']['phaseofMoon'].rstrip()
luneage = data['moon_phase']['ageOfMoon'].strip()
self.southern_hemi = (data['moon_phase']['hemisphere'].rstrip() == "South")
Domoticz.Log("Moon Phase:"+str(lune))
Domoticz.Log("Moon Age:"+str(luneage))
self.UpdateDevice(lune,luneage)
def UpdateDevice(self, lune, luneage):
Domoticz.Debug("UpdateDevice called")
# Make sure that the Domoticz device still exists (they can be deleted) before updating it
datafr = ""
if 1 in Devices:
if lune == "New Moon":
datafr = "MoonPhases1NM"
phase = 1
elif lune == "Waxing Crescent":
datafr = "MoonPhases2WC"
phase = 2
elif lune == "First Quarter":
datafr = "MoonPhases3FQ"
phase = 3
elif lune == "Waxing Gibbous":
datafr = "MoonPhases4WG"
phase = 4
elif lune == "Full":
datafr = "MoonPhases5FM"
phase = 5
elif lune == "Waning Gibbous":
datafr = "MoonPhases6WG"
phase = 6
elif lune == "Last Quarter":
datafr = "MoonPhases7LQ"
phase = 7
elif lune == "Waning Crescent":
datafr = "MoonPhases8WC"
phase = 8
if self.southern_hemi:
datafr = '%s%s' % (datafr,self.suffix)
Domoticz.Debug("Setting Icon " + str(datafr))
try:
Devices[1].Update(nValue=0, Name=str(lune), sValue=str(luneage), Image=Images[datafr].ID)
Devices[1].Update(nValue=0, sValue=str(luneage), Image=Images[datafr].ID)
except:
Domoticz.Error("Failed to update device unit 1 with values %s:%s:" % (lune,luneage))
return
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
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 + "'")
return