forked from stas-demydiuk/domoticz-zigbee2mqtt-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
254 lines (194 loc) · 9.36 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
"""
<plugin key="Zigbee2MQTT" name="Zigbee2MQTT" version="0.2.0">
<description>
Plugin to add support for <a href="https://github.com/Koenkk/zigbee2mqtt">zigbee2mqtt</a> project<br/><br/>
Specify MQTT server and port.<br/>
<br/>
Automatically creates Domoticz devices for connected device.<br/>
</description>
<params>
<param field="Address" label="MQTT Server address" width="300px" required="true" default="127.0.0.1"/>
<param field="Port" label="Port" width="300px" required="true" default="1883"/>
<param field="Username" label="MQTT Username (optional)" width="300px" required="false" default=""/>
<param field="Password" label="MQTT Password (optional)" width="300px" required="false" default="" password="true"/>
<param field="Mode3" label="MQTT Client ID (optional)" width="300px" required="false" default=""/>
<param field="Mode1" label="Zigbee2Mqtt Topic" width="300px" required="true" default="zigbee2mqtt"/>
<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 json
import time
import re
import os
from shutil import copy2, rmtree
from mqtt import MqttClient
from api import API
from devices_manager import DevicesManager
from groups_manager import GroupsManager
class BasePlugin:
mqttClient = 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.install()
self.base_topic = Parameters["Mode1"].strip()
self.subscribed_for_devices = False
mqtt_server_address = Parameters["Address"].strip()
mqtt_server_port = Parameters["Port"].strip()
mqtt_client_id = Parameters["Mode3"].strip()
self.mqttClient = MqttClient(mqtt_server_address, mqtt_server_port, mqtt_client_id, self.onMQTTConnected, self.onMQTTDisconnected, self.onMQTTPublish, self.onMQTTSubscribed)
self.api = API(Devices, self.publishToMqtt)
self.devices_manager = DevicesManager()
self.groups_manager = GroupsManager()
def checkDevices(self):
Domoticz.Debug("checkDevices called")
def onStop(self):
Domoticz.Debug("onStop called")
self.uninstall()
def onCommand(self, Unit, Command, Level, Color):
Domoticz.Debug("onCommand: " + Command + ", level (" + str(Level) + ") Color:" + Color)
message = None
device = Devices[Unit] #Devices is Domoticz collection of devices for this hardware
device_params = device.DeviceID.split('_')
entity_id = device_params[0]
if (self.devices_manager.get_device_by_id(entity_id) != None):
message = self.devices_manager.handle_command(Devices, device, Command, Level, Color)
elif(self.groups_manager.get_group_by_deviceid(device.DeviceID) != None):
message = self.groups_manager.handle_command(device, Command, Level, Color)
else:
Domoticz.Log('Can\'t process command from device "' + device.Name + '"')
if (message != None):
self.publishToMqtt(message['topic'], message['payload'])
def publishToMqtt(self, topic, payload):
self.mqttClient.publish(self.base_topic + '/' + topic, payload)
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 onDeviceModified(self, unit):
if (unit == 255):
self.api.handle_request(Devices[unit].sValue)
return
def onMessage(self, Connection, Data):
self.mqttClient.onMessage(Connection, Data)
def onHeartbeat(self):
self.mqttClient.onHeartbeat()
def onMQTTConnected(self):
self.mqttClient.subscribe([self.base_topic + '/bridge/#'])
def onMQTTDisconnected(self):
self.subscribed_for_devices = False
def onMQTTSubscribed(self):
Domoticz.Debug("onMQTTSubscribed")
def onMQTTPublish(self, topic, message):
Domoticz.Debug("MQTT message: " + topic + " " + str(message))
topic = topic.replace(self.base_topic + '/', '')
self.api.handle_mqtt_message(topic, message)
if (topic == 'bridge/config/permit_join' or topic == 'bridge/config/devices'):
return
if (topic == 'bridge/config'):
permit_join = 'enabled' if message['permit_join'] else 'disabled'
Domoticz.Debug('Zigbee2mqtt log level is ' + message['log_level'])
Domoticz.Log('Joining new devices is ' + permit_join + ' on the zigbee bridge')
return
if (topic == 'bridge/state'):
Domoticz.Log('Zigbee2mqtt bridge is ' + message)
if message == 'online':
self.publishToMqtt('bridge/config/devices', '')
self.publishToMqtt('bridge/config/groups', '')
return
if (topic == 'bridge/log'):
if message['type'] == 'devices':
Domoticz.Log('Received available devices list from bridge')
self.devices_manager.clear()
self.devices_manager.update(Devices, message['message'])
if self.subscribed_for_devices == False:
self.mqttClient.subscribe([self.base_topic + '/+'])
self.subscribed_for_devices = True
if message['type'] == 'groups':
Domoticz.Log('Received groups list from bridge')
self.groups_manager.register_groups(Devices, message['message'])
if message['type'] == 'device_connected' or message['type'] == 'device_removed':
self.publishToMqtt('bridge/config/devices', '')
if message['type'] == 'ota_update':
Domoticz.Log(message['message'])
return
if (self.devices_manager.get_device_by_name(topic) != None):
self.devices_manager.handle_mqtt_message(Devices, topic, message)
elif (self.groups_manager.get_group_by_name(topic) != None):
self.groups_manager.handle_mqtt_message(topic, message)
def install(self):
Domoticz.Log('Installing plugin custom page...')
try:
source_path = os.path.dirname(os.path.abspath(__file__)) + '/frontend'
templates_path = os.path.abspath(source_path + '/../../../www/templates')
dst_plugin_path = templates_path + '/zigbee2mqtt'
Domoticz.Debug('Copying files from ' + source_path + ' to ' + templates_path)
if not (os.path.isdir(dst_plugin_path)):
os.makedirs(dst_plugin_path)
copy2(source_path + '/zigbee2mqtt.html', templates_path)
copy2(source_path + '/zigbee2mqtt.js', templates_path)
copy2(source_path + '/zigbee_devices.js', dst_plugin_path)
copy2(source_path + '/zigbee_groups.js', dst_plugin_path)
copy2(source_path + '/libs/leaflet.js', dst_plugin_path)
copy2(source_path + '/libs/leaflet.css', dst_plugin_path)
copy2(source_path + '/libs/viz.js', dst_plugin_path)
copy2(source_path + '/libs/viz.full.render.js', dst_plugin_path)
Domoticz.Log('Installing plugin custom page completed.')
except Exception as e:
Domoticz.Error('Error during installing plugin custom page')
Domoticz.Error(repr(e))
def uninstall(self):
Domoticz.Log('Uninstalling plugin custom page...')
try:
templates_path = os.path.abspath(os.path.dirname(os.path.abspath(__file__)) + '/../../www/templates')
dst_plugin_path = templates_path + '/zigbee2mqtt'
Domoticz.Debug('Removing files from ' + templates_path)
if (os.path.isdir(dst_plugin_path)):
rmtree(dst_plugin_path)
if os.path.exists(templates_path + "/zigbee2mqtt.html"):
os.remove(templates_path + "/zigbee2mqtt.html")
if os.path.exists(templates_path + "/zigbee2mqtt.js"):
os.remove(templates_path + "/zigbee2mqtt.js")
Domoticz.Log('Uninstalling plugin custom page completed.')
except Exception as e:
Domoticz.Error('Error during uninstalling plugin custom page')
Domoticz.Error(repr(e))
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 onDeviceModified(Unit):
global _plugin
_plugin.onDeviceModified(Unit)
def onDisconnect(Connection):
global _plugin
_plugin.onDisconnect(Connection)
def onMessage(Connection, Data):
global _plugin
_plugin.onMessage(Connection, Data)
def onCommand(Unit, Command, Level, Color):
global _plugin
_plugin.onCommand(Unit, Command, Level, Color)
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()