-
Notifications
You must be signed in to change notification settings - Fork 4
/
plugin.py
218 lines (179 loc) · 9.08 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
"""
<plugin key="gBridge" name="gBridge" version="0.0.14">
<description>
Plugin to add support for <a href="https://github.com/kservices/gBridge">gBridge</a> project<br/><br/>
Specify MQTT server, the base topic, so gBridge/{your user id} and the gBridge url + credentials<br/>
If using local MQTT server, add also "MQTT Client Gateway with LAN Interface", config on Topic "out" to have the live update.<br/>
</description>
<params>
<param field="Address" label="MQTT Server address" width="300px" required="true" default="127.0.0.1:1883"/>
<param field="Username" label="MQTT username" width="300px" required="false" default=""/>
<param field="Password" label="MQTT password" width="300px" required="false" default="" password="true"/>
<param field="Mode1" label="MQTT base topic" width="300px" required="true" default="gBridge/u1"/>
<param field="Port" label="Domoticz port" width="300px" required="true" default="8080"/>
<param field="Mode2" label="gBridge url" width="300px" required="true" default="http://localhost:8082"/>
<param field="Mode3" label="gBridge username" width="300px" required="true" default="username"/>
<param field="Mode4" label="gBridge password" width="300px" required="true" default="password" password="true"/>
<param field="Mode5" label="Delete removed Domoticz devices from gBridge" width="75px">
<options>
<option label="True" value="True"/>
<option label="False" value="False" default="true" />
</options>
</param>
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="Verbose" value="Verbose"/>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import re
from adapters import getAdapter
from gbridge_client import gBridgeClient
from domoticz_client import DomoticzClient
from mqtt import MqttClient
class BasePlugin:
mqttClient = None
domoticzDevicesByName = None
domoticzDevicesById = None
linkedDevices = None
def onStart(self):
self.debugging = Parameters["Mode6"]
if self.debugging == "Verbose":
Domoticz.Debugging(2 + 4 + 8 + 16 + 64)
if self.debugging == "Debug":
Domoticz.Debugging(2)
Domoticz.Debug("onStart called")
self.base_topic = Parameters["Mode1"].strip()
self.domoticz_port = int(Parameters["Port"].strip())
self.delete_removed_devices = Parameters["Mode5"].strip()
if Parameters["Address"].find('mqtt.gbridge.io') >= 0:
self.domoticz_mqtt_used = False
else:
self.domoticz_mqtt_used = True
self.mqttClient = MqttClient(Parameters["Address"].strip().split(":")[0],
Parameters["Address"].strip().split(":")[1],
self.onMQTTConnected,
self.onMQTTDisconnected,
self.onMQTTPublish,
self.onMQTTSubscribed)
self.gBridgeClient = gBridgeClient(Parameters["Mode2"].strip(),
Parameters["Mode3"].strip(),
Parameters["Mode4"].strip())
self.domoticz_client = DomoticzClient(self.domoticz_port)
self.syncDevices()
Domoticz.Debug("Done")
def syncDevices(self):
Domoticz.Debug('Starting sync')
bridge_devices = self.gBridgeClient.fetchDevicesFromBridge()
domoticz_devices = self.domoticz_client.fetchDevicesFromDomoticz()
self.linkedDevices = self.domoticz_client.getLinkedDevices(domoticz_devices)
Domoticz.Debug('Linked devices: ' + str(self.linkedDevices))
self.domoticzDevicesByName = self.domoticz_client.getDevicesByName(domoticz_devices)
self.domoticzDevicesById = {x['idx']: x for x in list(self.domoticzDevicesByName.values())}
Domoticz.Debug('Domoticz devices available for gBridge: ' + str(self.domoticzDevicesByName.keys()))
self.gBridgeClient.syncDevices(self.domoticzDevicesByName, bridge_devices,
self.delete_removed_devices == 'True')
def onStop(self):
Domoticz.Debug("onStop called")
def onCommand(self, Unit, Command, Level, Color):
Domoticz.Debug("onCommand: " + Command + ", level (" + str(Level) + ") Color:" + Color)
def onConnect(self, Connection, Status, Description):
Domoticz.Debug("onConnect called")
self.mqttClient.onConnect(Connection, Status, Description)
def onDisconnect(self, Connection):
self.mqttClient.onDisconnect(Connection)
def onMessage(self, Connection, Data):
Domoticz.Debug('Incoming message!' + str(Data))
self.mqttClient.onMessage(Connection, Data)
def onHeartbeat(self):
Domoticz.Debug("Heartbeating...")
# Reconnect if connection has dropped
if self.mqttClient.mqttConn is None or (
not self.mqttClient.mqttConn.Connecting() and not self.mqttClient.mqttConn.Connected() or not self.mqttClient.isConnected):
Domoticz.Debug("Reconnecting")
self.mqttClient.Open()
else:
self.mqttClient.Ping()
def onMQTTConnected(self):
self.mqttClient.Subscribe([self.base_topic + '/#'])
if self.domoticz_mqtt_used:
self.mqttClient.Subscribe(['domoticz/out'])
def onMQTTDisconnected(self):
Domoticz.Debug("onMQTTDisconnected")
def onMQTTSubscribed(self):
Domoticz.Debug("onMQTTSubscribed")
def onMQTTPublish(self, topic, message):
Domoticz.Debug("MQTT message: " + topic + " " + str(message))
if str(topic) == 'domoticz/out':
if message.get('idx') is not None:
domoticz_id = str(message['idx'])
if message.get('Type') is not None and (message['Type'] == 'Scene' or message['Type'] == 'Group'):
domoticz_id = 'group_' + domoticz_id
else:
return
device = None
if domoticz_id in self.domoticzDevicesById:
device = self.domoticzDevicesById[domoticz_id]
elif domoticz_id in self.linkedDevices and self.linkedDevices[domoticz_id] in self.domoticzDevicesById:
# Get the device to which this device message is linked to, for example, device id 13 -> update device 12
device = self.domoticzDevicesById[self.linkedDevices[domoticz_id]]
if device is not None:
adapter = getAdapter(device)
if adapter is not None:
adapter.publishStateFromDomoticzTopic(self.mqttClient, device, self.base_topic, message)
else:
Domoticz.Error('No adapter registered to publish state for device: %s' % str(device))
else:
if message == 'SYNC':
self.syncDevices()
elif topic.endswith('/set'):
Domoticz.Debug('Published new state for device, topic: %s, state: %s' % (topic, message))
else:
match = re.search(self.base_topic + '/(.*)/(.*)', topic)
if match:
device_id = match.group(1)
# Backwards compatibility, previously the device name was used as topic name
if device_id in self.domoticzDevicesByName:
device = self.domoticzDevicesByName[device_id]
elif device_id in self.domoticzDevicesById:
device = self.domoticzDevicesById[device_id]
else:
Domoticz.Log('Received message for device which is not in Domoticz: %s, skipping' % device_id)
return
action = match.group(2)
adapter = getAdapter(device)
if adapter is not None:
adapter.handleMqttMessage(device, str(message), action, self.domoticz_port)
if not self.domoticz_mqtt_used:
adapter.publishState(self.mqttClient, device, self.base_topic, message) # answer directly
else:
Domoticz.Error('No adapter registered for action: %s for device: %s' % (action, str(device)))
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 onDisconnect(Connection):
global _plugin
_plugin.onDisconnect(Connection)
def onMessage(Connection, Data):
global _plugin
Domoticz.Debug('Message from base')
_plugin.onMessage(Connection, Data)
def onCommand(Unit, Command, Level, Color):
global _plugin
_plugin.onCommand(Unit, Command, Level, Color)
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()