forked from Turbox35/MQTTlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqttlink.py
94 lines (78 loc) · 2.7 KB
/
mqttlink.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
# python 3.6
import time
import random
import json
import yaml
from paho.mqtt import client as mqtt_client
# generate client ID with pub prefix randomly
client_id = f'python-mqtt-{random.randint(0, 1000)}'
# username = 'emqx'
# password = 'public'
def connect_mqtt(config):
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(client_id)
#client.username_pw_set(username, password)
client.on_connect = on_connect
broker = config['broker']
print(broker)
port = config['port']
client.connect(broker, port)
return client
def publish(client, config):
msg_count = 0
while True:
time.sleep(1)
x = len(config['sensors'])
print("Sensors to declare :" + str(x))
for i in range(x):
device_class = config['sensors'][i]['device_class']
friendly_name = config['sensors'][i]['friendly_name']
unit_of_measurement = config['sensors'][i]['unit_of_measurement']
topic = config['topic']
name = friendly_name + "_" + unit_of_measurement
state_topic = "Qdata/last/" + name + "/" + str(i) + "/value/1"
unique_id = name + "_" + str(i)
fulltopic = topic + "/sensor/" + name + "/1/config"
mqttmessage = {
"value_template":"{{ value_json.value }}",
"device_class":device_class,
"unit_of_measurement":unit_of_measurement,
"state_topic":state_topic,
"json_attributes_topic":state_topic,
"device": {
"identifiers":[
friendly_name
],
"manufacturer":"TITA",
"model":"L150",
"name":friendly_name,
"sw_version":"1.0"
},
"name":name,
"unique_id":unique_id
}
msg = json.dumps(mqttmessage)
result = client.publish(fulltopic, msg, retain=True)
status = result[0]
if status == 0:
print(f"Send ok to topic `{fulltopic}`")
else:
print(f"Failed to send message to topic {fulltopic}")
msg_count += 1
exit()
def read_config():
with open('config.yaml') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
print(f"config readed")
return config
def run():
config = read_config()
client = connect_mqtt(config)
client.loop_start()
publish(client, config)
if __name__ == '__main__':
run()