forked from tnys/nikohomecontrol-domoticz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
executable file
·186 lines (149 loc) · 6.16 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
# Python Plugin MQTT Subscribe Example
#
# Author: Dnpwwo
#
"""
<plugin key="NHC" name="Niko Home Control Bridge" author="tnys" version="1.0.0" externallink="https://niko.eu/">
<params>
<param field="Address" label="IP Address" width="200px" required="true" default="127.0.0.1"/>
<param field="Password" label="Password" width="200px"/>
<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 random
import json
from mqtt import MqttClient
from device import Device
class BasePlugin:
enabled = False
mqttConn = None
counter = 0
def __init__(self):
return
def onStart(self):
if Parameters["Mode6"] == "Debug":
Domoticz.Debugging(1)
DumpConfigToLog()
self.devices = {}
Parameters["Username"]="hobby"
Parameters["Port"]="8884"
Parameters["Protocol"]="MQTTS"
mqtt_server_address = Parameters["Address"].strip()
mqtt_server_port = Parameters["Port"].strip()
self.mqttClient = MqttClient(mqtt_server_address, mqtt_server_port, "NHCDomoticz", self.onMQTTConnected, self.onMQTTDisconnected, self.onMQTTPublish, self.onMQTTSubscribed)
def onStop(self):
Domoticz.Debug("onStop called")
def onCommand(self, Unit, Command, Level, Hue):
for uuid, device in self.devices.items():
if device.getUnit() == Unit:
device.handleCommand(Command, Level, Hue)
#MQTT forwards..
def onConnect(self, Connection, Status, Description):
self.mqttClient.onConnect(Connection, Status, Description)
def onMessage(self, Connection, Data):
self.mqttClient.onMessage(Connection, Data)
def onDisconnect(self, Connection):
self.mqttClient.onDisconnect(Connection)
def onHeartbeat(self):
self.mqttClient.onHeartbeat()
# MQTT stuff
def onMQTTConnected(self):
self.mqttClient.subscribe(['hobby/control/devices/rsp'])
self.mqttClient.subscribe(['hobby/control/devices/evt'])
self.mqttClient.subscribe(['hobby/control/devices/err'])
# fetch all devices
self.mqttClient.publish('hobby/control/devices/cmd', '{"Method": "devices.list"}')
def onMQTTDisconnected(self):
Domoticz.Log("onMQTT disconnected")
def onMQTTSubscribed(self):
Domoticz.Log("onMQTTSubscribed")
def onMQTTPublish(self, topic, message):
if topic == "hobby/control/devices/rsp":
if message["Params"]:
nhcDeviceIDs = []
if message["Params"][0]["Devices"]:
for device in message["Params"][0]["Devices"]:
if device["Type"]=="action":
uuid = device["Uuid"]
self.devices[uuid] = Device(Devices, device, self.mqttClient)
self.devices[uuid].handleMessage(topic, device)
if topic == "hobby/control/devices/evt":
if message["Method"] == "devices.status":
if message["Params"]:
if message["Params"][0]["Devices"]:
for device in message["Params"][0]["Devices"]:
uuid = device["Uuid"]
if uuid in self.devices:
self.devices[uuid].handleMessage(topic, device)
if topic == "hobby/control/devices/err":
Domoticz.Log("Err")
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 onDisconnect(Connection):
global _plugin
_plugin.onDisconnect(Connection)
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
def onCommand(Unit, Command, Level, Hue):
global _plugin
_plugin.onCommand(Unit, Command, Level, Hue)
# 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
def DumpDictionaryToLog(theDict, Depth=""):
if isinstance(theDict, dict):
for x in theDict:
if isinstance(theDict[x], dict):
Domoticz.Log(Depth+"> Dict '"+x+"' ("+str(len(theDict[x]))+"):")
DumpDictionaryToLog(theDict[x], Depth+"---")
elif isinstance(theDict[x], list):
Domoticz.Log(Depth+"> List '"+x+"' ("+str(len(theDict[x]))+"):")
DumpListToLog(theDict[x], Depth+"---")
elif isinstance(theDict[x], str):
Domoticz.Log(Depth+">'" + x + "':'" + str(theDict[x]) + "'")
else:
Domoticz.Log(Depth+">'" + x + "': " + str(theDict[x]))
def DumpListToLog(theList, Depth):
if isinstance(theList, list):
for x in theList:
if isinstance(x, dict):
Domoticz.Log(Depth+"> Dict ("+str(len(x))+"):")
DumpDictionaryToLog(x, Depth+"---")
elif isinstance(x, list):
Domoticz.Log(Depth+"> List ("+str(len(theList))+"):")
DumpListToLog(x, Depth+"---")
elif isinstance(x, str):
Domoticz.Log(Depth+">'" + x + "':'" + str(theList[x]) + "'")
else:
Domoticz.Log(Depth+">'" + x + "': " + str(theList[x]))