-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplugin.py
148 lines (111 loc) · 4.92 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
"""
<plugin key="EwpeSmartMqtt" name="EwpeSmart Air Conditioners via MQTT" version="1.0.3">
<description>
Plugin to control EwpeSmart powered air conditioners via MQTT bridge including:
<ul>
<li>Gree series with WiFi: Smart, U-CROWN</li>
<li>Cooper&Hunter series with WiFi: Supreme, Vip Inverter, ICY II, Arctic, Alpha, Alpha NG, Veritas, Veritas NG, ...</li>
<li><a href="https://www.ecoair.org/x-series-inverter-air-conditioning">EcoAir X series</a></li>
</ul>
</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" width="300px" required="false" default=""/>
<param field="Password" label="MQTT Password" width="300px" required="false" default="" password="true"/>
<param field="Mode1" label="EWPE Smart MQTT Topic" width="300px" required="true" default="ewpe-smart"/>
<param field="Mode3" label="MQTT Client ID (optional)" width="300px" required="false" default=""/>
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="Verbose" value="Verbose"/>
<option label="True" value="Debug"/>
<option label="False" value="No" default="true" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import json
import time
import re
from mqtt import MqttClient
from device import Device
class EwpeSmartOverMqttPlugin:
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)
self.devices = {}
self.base_topic = Parameters["Mode1"].strip()
mqttserver_address = Parameters["Address"].strip()
mqttserver_port = Parameters["Port"].strip()
mqtt_client_id = Parameters["Mode3"].strip()
self.mqttClient = MqttClient(mqttserver_address, mqttserver_port, mqtt_client_id,
self.onMQTTConnected, self.onMQTTDisconnected, self.onMQTTPublish, self.onMQTTSubscribed)
Domoticz.Debug("onStart called")
def onMQTTConnected(self):
self.mqttClient.subscribe([self.base_topic + '/#'])
self.mqttClient.publish(self.base_topic + '/devices/list', '')
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 + '/devices':
Domoticz.Log('Received available devices list')
self.devices = {}
for item in message:
mac = item['mac']
Domoticz.Log('Device ' + item['name'].strip() + ' (MAC: ' + mac + ')')
self.devices[mac] = Device(Devices, self.base_topic, item)
else:
for mac, device in self.devices.items():
if topic.startswith(self.base_topic + '/' + mac):
Domoticz.Debug(self.base_topic + '/' + mac)
device.handle_message(topic, message)
def onStop(self):
Domoticz.Debug("onStop called")
def onCommand(self, Unit, Command, Level, Color):
Domoticz.Debug("onCommand: " + Command + ", level (" + str(Level) + ") Color:" + Color)
[mac, alias] = Devices[Unit].DeviceID.split('_')
if mac in self.devices:
state = self.devices[mac].handle_command(alias, Command, Level, Color)
topic = '/'.join([self.base_topic, mac, 'set'])
self.mqttClient.publish(topic, json.dumps(state))
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()
global _plugin
_plugin = EwpeSmartOverMqttPlugin()
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()