forked from stas-demydiuk/domoticz-zigbee2mqtt-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
191 lines (151 loc) · 7.09 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
"""
<plugin key="Zigbee2MQTT" name="Zigbee2MQTT" version="0.0.16">
<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="Mode2" label="Zigbee pairing" width="75px" required="true">
<options>
<option label="Enabled" value="true"/>
<option label="Disabled" 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 json
import time
import re
from mqtt import MqttClient
from zigbee_message import ZigbeeMessage
from device_storage import DeviceStorage
from adapters import adapter_by_model
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.base_topic = Parameters["Mode1"].strip()
self.pairing_enabled = True if Parameters["Mode2"] == 'true' else False
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.available_devices = DeviceStorage.getInstance()
def checkDevices(self):
Domoticz.Debug("checkDevices called")
def onStop(self):
Domoticz.Debug("onStop called")
def handlePairingMode(self):
if (self.pairing_enabled):
self.mqttClient.Publish(self.base_topic + '/bridge/config/permit_join', 'true')
Domoticz.Log("Joining new devices is enabled on the zigbee bridge")
else:
self.mqttClient.Publish(self.base_topic + '/bridge/config/permit_join', 'false')
Domoticz.Log("Joining new devices is disabled on the zigbee bridge")
def onCommand(self, Unit, Command, Level, Color):
Domoticz.Debug("onCommand: " + Command + ", level (" + str(Level) + ") Color:" + Color)
device = Devices[Unit] #Devices is Domoticz collection of devices for this hardware
device_params = device.DeviceID.split('_')
device_id = device_params[0]
alias = device_params[1]
device_data = self.available_devices.get_device_by_id(device_id)
if (device_data == None):
Domoticz.Log('Device ' + device.Name + ' does not have registered zigbee2mqtt device')
return
model = device_data['model']
if (model in adapter_by_model):
adapter = adapter_by_model[model](Devices)
message = adapter.handleCommand(alias, device, device_data, Command, Level, Color)
if (message != None):
self.mqttClient.Publish(self.base_topic + '/' + message['topic'], message['payload'])
else:
Domoticz.Log('Device ' + device.Name + ' does not have adapter (model: "' + model + '"')
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):
self.mqttClient.onMessage(Connection, Data)
def onHeartbeat(self):
self.mqttClient.onHeartbeat()
def onMQTTConnected(self):
self.mqttClient.Subscribe([self.base_topic + '/bridge/#'])
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 (topic == self.base_topic + '/bridge/state'):
Domoticz.Log('Zigbee2mqtt bridge is ' + message)
if message == 'online':
self.mqttClient.Publish(self.base_topic + '/bridge/config/devices', '')
self.handlePairingMode()
return
if (topic == self.base_topic + '/bridge/log'):
if message['type'] == 'devices':
Domoticz.Log('Received available devices list from bridge')
self.available_devices.update(Devices, message['message'])
if self.subscribed_for_devices == False:
self.mqttClient.Subscribe([self.base_topic + '/+'])
self.subscribed_for_devices = True
return
device_name = topic.replace(self.base_topic + "/", "")
device_data = self.available_devices.get_device_by_name(device_name)
if (device_data != None):
model = device_data['model']
if (model in adapter_by_model):
zigbee_message = ZigbeeMessage(message)
adapter = adapter_by_model[model](Devices)
adapter.handleMqttMessage(device_data, zigbee_message)
else:
Domoticz.Debug('Unsupported zigbee device type with model "' + model + '"')
else:
Domoticz.Debug('Unhandled message from zigbee2mqtt: ' + topic + ' ' + str(message))
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
_plugin.onMessage(Connection, Data)
def onCommand(Unit, Command, Level, Color):
global _plugin
_plugin.onCommand(Unit, Command, Level, Color)
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()