forked from mrwiwi/tydom2mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_client.py
226 lines (174 loc) · 9.29 KB
/
mqtt_client.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
import asyncio
import time
import json
import socket
import sys
from datetime import datetime
from gmqtt import Client as MQTTClient
from cover import Cover
from alarm_control_panel import Alarm
# Globals
####################################### MQTT
from light import Light
from boiler import Boiler
tydom_topic = "+/tydom/#"
refresh_topic = "homeassistant/requests/tydom/refresh"
hostname = socket.gethostname()
# STOP = asyncio.Event()
class MQTT_Hassio():
def __init__(self, broker_host, port, user, password, mqtt_ssl, home_zone=1, night_zone=2, tydom = None, tydom_alarm_pin = None):
self.broker_host = broker_host
self.port = port
self.user = user
self.password = password
self.ssl = mqtt_ssl
self.tydom = tydom
self.tydom_alarm_pin = tydom_alarm_pin
self.mqtt_client = None
self.home_zone = home_zone
self.night_zone = night_zone
async def connect(self):
try:
print('""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""')
print('Attempting MQTT connection...')
print('MQTT host : ', self.broker_host)
print('MQTT user : ', self.user)
adress = hostname+str(datetime.fromtimestamp(time.time()))
# print(adress)
client = MQTTClient(adress)
# print(client)
client.on_connect = self.on_connect
client.on_message = self.on_message
client.on_disconnect = self.on_disconnect
# client.on_subscribe = self.on_subscribe
client.set_auth_credentials(self.user, self.password)
await client.connect(self.broker_host, self.port, self.ssl)
self.mqtt_client = client
return self.mqtt_client
except Exception as e:
print("MQTT connection Error : ", e)
print('MQTT error, restarting in 8s...')
await asyncio.sleep(8)
await self.connect()
def on_connect(self, client, flags, rc, properties):
print("##################################")
try:
print("Subscribing to : ", tydom_topic)
# client.subscribe('homeassistant/#', qos=0)
client.subscribe(tydom_topic, qos=0)
except Exception as e:
print("Error on connect : ", e)
async def on_message(self, client, topic, payload, qos, properties):
# print('Incoming MQTT message : ', topic, payload)
if ('update' in str(topic)):
# if "update" in topic:
print('Incoming MQTT update request : ', topic, payload)
await self.tydom.get_data()
elif ('kill' in str(topic)):
# if "update" in topic:
print('Incoming MQTT kill request : ', topic, payload)
print('Exiting...')
sys.exit()
elif (topic == "homeassistant/requests/tydom/refresh"):
print('Incoming MQTT refresh request : ', topic, payload)
await self.tydom.post_refresh()
elif (topic == "homeassistant/requests/tydom/scenarii"):
print('Incoming MQTT scenarii request : ', topic, payload)
await self.tydom.get_scenarii()
elif (topic == "/tydom/init"):
print('Incoming MQTT init request : ', topic, payload)
await self.tydom.connect()
# elif ('set_scenario' in str(topic)):
# print('Incoming MQTT set_scenario request : ', topic, payload)
# get_id = (topic.split("/"))[3] #extract id from mqtt
# # print(tydom, str(get_id), 'position', json.loads(payload))
# if not self.tydom.connection.open:
# print('Websocket not opened, reconnect...')
# await self.tydom.connect()
# await self.tydom.put_devices_data(str(get_id), 'position', str(json.loads(payload)))
# else:
# await self.tydom.put_devices_data(str(get_id), 'position', str(json.loads(payload)))
elif 'set_positionCmd' in str(topic):
print('Incoming MQTT set_positionCmd request : ', topic, payload)
value = str(payload).strip('b').strip("'")
get_id = (topic.split("/"))[2] #extract ids from mqtt
device_id = (get_id.split("_"))[0] #extract id from mqtt
endpoint_id = (get_id.split("_"))[1] #extract id from mqtt
print(str(get_id), 'positionCmd', value)
await Cover.put_positionCmd(tydom_client=self.tydom, device_id=device_id, cover_id=endpoint_id, positionCmd=str(value))
elif ('set_position' in str(topic)) and not ('set_positionCmd'in str(topic)):
print('Incoming MQTT set_position request : ', topic, json.loads(payload))
value = json.loads(payload)
# print(value)
get_id = (topic.split("/"))[2] #extract ids from mqtt
device_id = (get_id.split("_"))[0] #extract id from mqtt
endpoint_id = (get_id.split("_"))[1] #extract id from mqtt
await Cover.put_position(tydom_client=self.tydom, device_id=device_id, cover_id=endpoint_id, position=str(value))
elif 'set_levelCmd' in str(topic):
print('Incoming MQTT set_positionCmd request : ', topic, payload)
value = str(payload).strip('b').strip("'")
get_id = (topic.split("/"))[2] # extract ids from mqtt
device_id = (get_id.split("_"))[0] # extract id from mqtt
endpoint_id = (get_id.split("_"))[1] # extract id from mqtt
print(str(get_id), 'levelCmd', value)
await Light.put_levelCmd(tydom_client=self.tydom, device_id=device_id, light_id=endpoint_id,
levelCmd=str(value))
elif ('set_level' in str(topic)) and not ('set_levelCmd' in str(topic)):
print('Incoming MQTT set_position request : ', topic, json.loads(payload))
value = json.loads(payload)
# print(value)
get_id = (topic.split("/"))[2] # extract ids from mqtt
device_id = (get_id.split("_"))[0] # extract id from mqtt
endpoint_id = (get_id.split("_"))[1] # extract id from mqtt
await Light.put_level(tydom_client=self.tydom, device_id=device_id, light_id=endpoint_id,
level=str(value))
elif ('set_alarm_state' in str(topic)) and not ('homeassistant'in str(topic)):
# print(topic, payload, qos, properties)
command = str(payload).strip('b').strip("'")
get_id = (topic.split("/"))[2] #extract ids from mqtt
device_id = (get_id.split("_"))[0] #extract id from mqtt
endpoint_id = (get_id.split("_"))[1] #extract id from mqtt
await Alarm.put_alarm_state(tydom_client=self.tydom, device_id=device_id, alarm_id=endpoint_id, asked_state=command, home_zone=self.home_zone, night_zone=self.night_zone)
elif ('set_setpoint' in str(topic)):
value = str(payload).strip('b').strip("'")
print('Incoming MQTT setpoint request : ', topic, value)
value = json.loads(payload)
# print(value)
get_id = (topic.split("/"))[2] # extract ids from mqtt
device_id = (get_id.split("_"))[0] # extract id from mqtt
endpoint_id = (get_id.split("_"))[1] # extract id from mqtt
await Boiler.put_temperature(tydom_client=self.tydom, device_id=device_id, boiler_id=endpoint_id,
set_setpoint=str(value))
elif ('set_hvacMode' in str(topic)):
value = str(payload).strip('b').strip("'")
print('Incoming MQTT set_hvacMode request : ', topic, value)
# print(value)
get_id = (topic.split("/"))[2] # extract ids from mqtt
device_id = (get_id.split("_"))[0] # extract id from mqtt
endpoint_id = (get_id.split("_"))[1] # extract id from mqtt
await Boiler.put_hvacMode(tydom_client=self.tydom, device_id=device_id, boiler_id=endpoint_id,
set_hvacMode=str(value))
elif ('set_thermicLevel' in str(topic)):
value = str(payload).strip('b').strip("'")
print('Incoming MQTT set_thermicLevel request : ', topic, value)
# print(value)
get_id = (topic.split("/"))[2] # extract ids from mqtt
device_id = (get_id.split("_"))[0] # extract id from mqtt
endpoint_id = (get_id.split("_"))[1] # extract id from mqtt
await Boiler.put_thermicLevel(tydom_client=self.tydom, device_id=device_id, boiler_id=endpoint_id,
set_thermicLevel=str(value))
else:
pass
# print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
# print('MQTT incoming : ', topic, payload.decode())
def on_disconnect(self, client, packet, exc=None):
print('MQTT Disconnected !')
print("##################################")
# self.connect()
def on_subscribe(self, client, mid, qos):
print("MQTT is connected and suscribed ! =)", client)
try:
pyld = str(datetime.fromtimestamp(time.time()))
client.publish('homeassistant/sensor/tydom/last_clean_startup', pyld, qos=1, retain=True)
except Exception as e:
print("on subscribe error : ", e)