-
Notifications
You must be signed in to change notification settings - Fork 37
/
HASSintegrMQTT.py
403 lines (308 loc) · 12.4 KB
/
HASSintegrMQTT.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
from faulthandler import is_enabled
import hardwaremod
import time, logging
import statusdataDBmod
from datetime import datetime,date,timedelta
# import paho library only if installed, then set the flag MQTTlib
try:
__import__("paho")
except:
MQTTlib=False
else:
MQTTlib=True
import paho.mqtt.client as mqtt
logger = logging.getLogger("hydrosys4."+__name__)
class HASS_Client:
ClientObj=None
Workmode="Disabled"
def __init__(self, datadict):
self.get_HASS_client_info(datadict)
HASS_Client.ClientObj=MQTT_Client_Management(self.clientid)
HASS_Client.ClientObj.set_parameters(host=self.host, port=self.port, cleanstart=self.cleanstart,
keepalive=self.keepalive, newsocket=self.newsocket, protocolName=self.protocolName,
willFlag=False, willTopic=None, willMessage=None, willQoS=2, willRetain=False,
username=self.username, password=self.password)
def register_received_msg(self,callbeckfunc):
MQTT_Client_Management.on_message_callback = callbeckfunc
def is_enabled(self):
if HASS_Client.Workmode=="Disabled":
return False
return True
def is_send_state(self):
if self.is_enabled():
if (HASS_Client.Workmode=="Status") or (HASS_Client.Workmode=="Status+Commands"):
return True
return False
def is_get_command(self):
if self.is_enabled():
if HASS_Client.Workmode=="Status+Commands":
return True
return False
def connect(self):
if self.is_enabled():
isok=HASS_Client.ClientObj.connect()
def disconnect(self):
HASS_Client.ClientObj.disconnect()
def subscribe(self, topic):
if self.is_get_command():
isok=HASS_Client.ClientObj.connect()
HASS_Client.ClientObj.subscribe(topic, qos=2)
def publish(self, topic, payload , retained=False):
if self.is_send_state():
isok=HASS_Client.ClientObj.connect()
HASS_Client.ClientObj.publish(topic, payload , retained=retained)
def check_loop_and_connect(self):
if self.is_enabled():
return HASS_Client.ClientObj.check_loop_and_connect(self.testtopic)
return True, True
def check_changes_and_apply(self,datadict):
# special case Enable or disable
reset_connection=False
disconnect=False
print (" WorkMode ", datadict.get("Workmode","Disabled"))
if (HASS_Client.Workmode=="Disabled")and(datadict.get("Workmode","Disabled")!="Disabled"):
reset_connection=True
if (HASS_Client.Workmode!="Disabled")and(datadict.get("Workmode","Disabled")=="Disabled"):
disconnect=True
HASS_Client.Workmode=datadict.get("Workmode","Disabled")
if disconnect:
print("disconnect")
self.disconnect()
# this is the case where Workmode is not Disabled
if self.is_enabled():
if datadict:
if not self.host==datadict.get("Host"):
reset_connection=True
if not self.port==int(datadict.get("Port")):
reset_connection=True
if not self.username==datadict.get("Username"):
reset_connection=True
if not self.password==datadict.get("Password"):
reset_connection=True
if reset_connection:
print(" Need to reset connection")
HASS_Client.ClientObj.disconnect(ignore_feedback=True)
self.get_HASS_client_info(datadict)
HASS_Client.ClientObj.set_parameters(host=self.host, port=self.port, cleanstart=self.cleanstart,
keepalive=self.keepalive, newsocket=self.newsocket, protocolName=self.protocolName,
willFlag=False, willTopic=None, willMessage=None, willQoS=2, willRetain=False,
username=self.username, password=self.password)
self.connect()
def get_HASS_client_info(self,datadict):
# load info from configuration flie
HASS_Client.Workmode=datadict.get("Workmode", "Disabled")
clientID="HASSIO"+str(time.time()) # it is necessary to ensure that the clientID is unique.
self.clientid = clientID
self.host=datadict.get("Host","")
self.port=int(datadict.get("Port",0))
self.cleanstart=True
self.keepalive=180
self.newsocket=True
self.protocolName=None
self.username=datadict.get("Username","")
self.password=datadict.get("Password","")
self.testtopic=datadict.get("Identifier","")+"/testloop"
#print(self.host," ", self.port," ",self.username," ",self.password)
class MQTT_Client_Management:
MQTTregister={}
on_message_callback = None
def __init__(self, clientid):
self.clientid = clientid
# auxiliary variables
self.connected=False
# create paho client, associate the callbacks
self.clientobj=mqtt.Client(clientid) # create client object
self.clientobj.on_message = self.on_message
self.clientobj.on_connect = self.on_connect
self.clientobj.on_disconnect = self.on_disconnect
self.clientobj.on_publish = self.on_publish
self.clientobj.on_subscribe = self.on_subscribe
def set_parameters(self,host="localhost", port=1883, cleanstart=True, keepalive=180, newsocket=True, protocolName=None,
willFlag=False, willTopic=None, willMessage=None, willQoS=2, willRetain=False, username=None, password=None,
properties=None, willProperties=None):
self.host=host
self.port=port
self.cleanstart=cleanstart
self.keepalive=keepalive
self.newsocket=newsocket
self.protocolName=protocolName
self.username=username
self.password=password
# @staticmethod
def on_connect(self,mqttc, obj, flags, rc):
self.registerwrite("on_connect","answer",str(rc))
if rc==0:
self.connected=True
print(self.clientid + " " + "Connection Succesfull")
"""
RC values:
0: Connection successful
1: Connection refused – incorrect protocol version
2: Connection refused – invalid client identifier
3: Connection refused – server unavailable
4: Connection refused – bad username or password
5: Connection refused – not authorised
"""
def on_disconnect(self, mqttc, obj, rc):
self.registerwrite("on_disconnect","answer",str(rc))
if rc==0:
self.connected=False
print(self.clientid + " " + "Disconnection Succesfull")
def on_message(self,mqttc, obj, msg):
payload=msg.payload.decode('utf-8')
self.registerwrite("on_message",msg.topic,payload)
print("ON-Message: Topic-->" , msg.topic, " payload -->", payload)
if MQTT_Client_Management.on_message_callback:
MQTT_Client_Management.on_message_callback(msg.topic,payload,msg.retain)
else:
print(" NO callback for on_message ***")
def on_publish(self,mqttc, obj, mid):
self.registerwrite("on_publish","mid",str(mid))
print("ON-Publish: " , self.registerread("on_publish"))
def on_subscribe(self,mqttc, obj, mid, granted_qos):
self.registerwrite("on_subscribe","mid",str(mid))
def on_log(self, mqttc, obj, level, string):
print(self.clientid + " " +string)
def registerwrite(self, call, infotype,data):
if call not in self.MQTTregister:
self.MQTTregister[call]={}
if infotype not in self.MQTTregister[call]:
self.MQTTregister[call][infotype]={}
self.MQTTregister[call][infotype]=data
self.MQTTregister[call]["timestamp"]=timestamp=datetime.utcnow()
return
def registerread(self, call, infotype=""):
data={}
if infotype:
if (call in self.MQTTregister):
if infotype in self.MQTTregister[call]:
data=self.MQTTregister[call][infotype]
else:
if (call in self.MQTTregister):
data=self.MQTTregister[call]
return data
def check_loop_and_connect(self,topic):
current_connect=False
previous_connect=False
if not self.connected:
if not self.connect():
return current_connect , previous_connect
else:
current_connect=True
return current_connect , previous_connect
self.clientobj.subscribe(topic)
payload="test"+str(time.time())
self.clientobj.publish(topic,payload)
# wait for Answer Message
waitsec=5
while (not self.registerread("on_message",topic)==payload) and (waitsec>0):
time.sleep(1)
waitsec=waitsec-1
if not self.registerread("on_message",topic)==payload:
logger.error("MQTT Test Loop Failed , Try to reconnect")
print("MQTT Test Loop Failed, Try to reconnect")
# try to connect as last attempt
isok= self.connect(ignore_connected=True,ignore_feedback=False)
if not isok:
logger.error("MQTT not able to recover connection")
print("MQTT not able to recover connection")
return current_connect , previous_connect
else:
logger.error("MQTT able to recover connection")
print("MQTT able to recover connection")
current_connect=True
return current_connect , previous_connect
current_connect=True
previous_connect=True
logger.info("MQTT Test Loop passed")
print("MQTT Test Loop passed")
return current_connect , previous_connect
def connect(self,ignore_connected=False, ignore_feedback=False):
if self.connected and not ignore_connected:
#logger.warning("already connected, no action")
#print("already connected, no action")
return False
try:
if self.password and self.username:
self.clientobj.username_pw_set(username=self.username, password=self.password)
self.clientobj.connect(host=self.host, port=self.port, keepalive=self.keepalive) # connection
self.clientobj.loop_start() # start loop to enable the callbacks
except:
print ("MQTT connection Failed " + self.clientid)
logger.error("MQTT Connection failed")
return False
# wait for broker confirmation
if not ignore_feedback:
waitsec=5
while (not self.connected) and (waitsec>0):
time.sleep(1)
waitsec=waitsec-1
if not self.connected:
logger.warning("Not able to connect to MQTT broker, no feedback")
print("Not able to connect to MQTT broker, no feedback")
return False
return True
def subscribe(self, topic="", options=None, properties=None, qos=2):
if topic=="":
return
print("try to su scribe " + topic)
self.clientobj.subscribe(topic, qos)
def unsubscribe(self, topic):
self.clientobj.unsubscribe(topic)
def publish(self, topic, payload, qos=0, retained=False, properties=None):
self.clientobj.publish(topic, payload, qos, retained)
def publish_wait_QoS2(self, topic, payload, retained=False, properties=None):
infot=self.clientobj.publish(topic, payload, qos=2)
infot.wait_for_publish()
(rc, mid)=infot
print(self.clientid + " " + "Topic: " + topic + " Payload " + payload)
def run_loop(self, timesec=1):
self.clientobj.loop_start()
time.sleep(timesec)
self.clientobj.loop_stop()
def loop_start(self, timesec=1):
self.clientobj.loop_start()
def loop_stop(self, timesec=1):
self.clientobj.loop_stop()
def disconnect(self, ignore_feedback=False):
if not self.connected:
logger.warning("try to disconnect, but already disconnected")
print("try to disconnect, but already disconnected")
self.connected=True
try:
self.clientobj.disconnect()
except:
print ("disconnection Failed ")
logger.error("MQTT DisConnection failed")
return False
# wait fof broker confirmation
if not ignore_feedback:
waitsec=5
while (self.connected) and (waitsec>0):
time.sleep(1)
waitsec=waitsec-1
if self.connected:
return False
print ("MQTT disconnection Succesfull ")
logger.info("MQTT DisConnection Succesfull")
self.clientobj.loop_stop()
self.connected=False
return True
if __name__ == "__main__":
print(MQTTlib)
aclient=HASS_Client()
aclient.connect()
time.sleep(2)
aclient.subscribe("prova")
time.sleep(2)
aclient.publish("prova","sono il testo")
time.sleep(3)
aclient.disconnect()
"""aclient.ClientObj.subscribe("prova", qos=2)
time.sleep(2)
aclient.ClientObj.publish("prova", "qos 0")
aclient.ClientObj.publish("prova", "qos 1", 1)
aclient.ClientObj.publish_wait_QoS2("prova", "qos 2")
time.sleep(3)
aclient.ClientObj.disconnect() """
print (" Hello")