-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.py
179 lines (144 loc) · 5.52 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
# Plugin that makes it easier to install other plugins. Just insert the URL.
# It checks if the URL in the input field was already installed in a previous round.
"""
<plugin key="python_plugin_installer" name="Python Plugin installer" author="blauwebuis" version="1.0.0" wikilink="http://www.domoticz.com/wiki/plugins/plugin.html" externallink="https://www.domoticz.com/forum/viewtopic.php?f=65&t=22310">
<params>
<param field="Mode1" label="Plugin URL (ending with .zip)" width="300px" required="false" default=""/>
</params>
</plugin>
"""
try:
import Domoticz
except ImportError:
import fakeDomoticz as Domoticz
import shelve
import os
from subprocess import call
import time
import sys
import urllib.request
import zipfile
class BasePlugin:
def __init__(self):
return
def onStart(self):
Domoticz.Debugging(1)
self.dirName = os.path.dirname(__file__)
Domoticz.Debug("Plugin folder = " + str(self.dirName))
pluginUrl = str(Parameters["Mode1"])
Domoticz.Log("Found plugin URL: " + pluginUrl)
pluginsFolder = str(self.dirName.rsplit('/', 1)[0])
Domoticz.Debug("pluginsFolder = " + pluginsFolder)
if pluginUrl.startswith("http") and pluginUrl.endswith(".zip"):
databaseFile = self.dirName + "/pythonPluginsInstaller"
database = shelve.open(databaseFile)
try:
lastInstalledPlugin = str(database['lastInstalledPluginUrl'])
Domoticz.Log("Previously installed plugin URL: " + str(lastInstalledPlugin))
except:
lastInstalledPlugin = ""
Domoticz.Debug("couldn't find which plugin was last installed")
database['lastInstalledPluginUrl'] = ""
if str(lastInstalledPlugin) != pluginUrl:
Domoticz.Log("INSTALLING NEW PLUGIN")
try:
zipFile = pluginsFolder + "/plugin.zip"
Domoticz.Debug("zip file: " + str(zipFile))
urllib.request.urlretrieve(pluginUrl, zipFile)
try:
with zipfile.ZipFile(zipFile,"r") as zip_ref:
zip_ref.extractall(pluginsFolder)
database['lastInstalledPluginUrl'] = pluginUrl
Domoticz.Log("unzipped plugin (in theory..)")
except:
Domoticz.Error("Unable to extract plugin zip file")
database['lastInstalledPluginUrl'] = ""
os.remove(zipFile)
directories = os.listdir(pluginsFolder)
Domoticz.Log("You now have the following python plugins installed:")
for directory in directories:
possibleDirectory = pluginsFolder + "/" + directory
if os.path.isdir(possibleDirectory):
Domoticz.Log(directory)
startCommand = "sudo chmod +x " + pluginsFolder + "/" + directory + "/plugin.py"
Domoticz.Debug(str(startCommand))
try:
try:
call (startCommand, shell=True)
except:
cloner = os.popen(startCommand).read()
except:
pass
#Domoticz.Error("Unable to chmod plugin.py file")
except:
Domoticz.Error("Unable to install plugin (Check zip file URL?)")
else:
Domoticz.Log("Plugin already installed.")
database.close()
# Perhaps in the future add a git option here.
#if pluginUrl.startswith("http") and pluginUrl.endswith(".git"):
# Domoticz.Log("git file")
else:
if pluginUrl != "":
Domoticz.Error("Not a correct URL. It must start with http and end with .zip")
else:
Domoticz.Log("no url provided")
databaseFile = self.dirName + "/pythonPluginsInstaller"
database = shelve.open(databaseFile)
database['lastInstalledPluginUrl'] = ""
database.close()
return
def onStop(self):
Domoticz.Log("onStop called")
def onConnect(self, Connection, Status, Description):
Domoticz.Log("onConnect called")
def onMessage(self, Connection, Data):
Domoticz.Log("onMessage called")
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Log("Notification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(Priority) + "," + Sound + "," + ImageFile)
def onDisconnect(self, Connection):
Domoticz.Log("onDisconnect called")
def onHeartbeat(self):
Domoticz.Log("onHeartbeat called")
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