-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·104 lines (90 loc) · 4.13 KB
/
setup.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
#!/usr/bin/env python
# -*- coding: utf-8
# Setup room names for all componentes in my configuration.
# Holger Mueller
# 2017/10/16 initial revision
# 2017/03/12 added gas-meter
# 2020/10/15 made script Python3 compatible
import paho.mqtt.client as mqtt
import ssl
import mqtt_config # defines host, port, user, pwd, ca_certs
# config here ...
debug = False
# config components device name and room name here
mqtt_devices_arr = [
{'device': '123456-energy', 'room': 'Sensors', 'name': 'Energie'},
{'device': '123456-solar', 'room': 'Sensors', 'name': 'Solar'},
{'device': '123456-vito', 'room': 'Sensors', 'name': 'Heizung'},
{'device': '123456-windsensor', 'room': 'Sensors', 'name': 'Windsensor'},
{'device': '123456-gas-meter', 'room': 'Sensors', 'name': 'Gas Meter'}]
# {'device': '123456-rcplugs-11111-10000', 'room': 'Plugs', 'name': 'rcPlug A'},
# {'device': '123456-rcplugs-11111-01000', 'room': 'Plugs', 'name': 'rcPlug B'},
# {'device': '123456-rcplugs-11111-00100', 'room': 'Plugs', 'name': 'rcPlug C'}]
# config components control room name (if wanted) here
mqtt_controls_arr = [
{'device': '123456-energy', 'control': 'Current Power', 'room': 'Home'},
{'device': '123456-solar', 'control': 'Kollektortemperatur', 'room': 'Home'},
{'device': '123456-vito', 'control': 'Aussentemperatur', 'room': 'Home'},
{'device': '123456-windsensor', 'control': 'Wind speed', 'room': 'Home'},
{'device': '123456-gas-meter', 'control': 'Count', 'room': 'Home'}]
# {'device': '123456-rcplugs-11111-10000', 'control': 'Power 1', 'room': 'Home'},
# {'device': '123456-rcplugs-11111-01000', 'control': 'Power 2', 'room': 'Home'},
# {'device': '123456-rcplugs-11111-00100', 'control': 'Power 3', 'room': 'Home'}]
def get_topic(device, t1 = None, t2 = None, t3 = None):
"Create topic string."
topic = "/devices/%s" % (device)
if t1:
topic += "/"+ t1
if t2:
topic += "/"+ t2
if t3:
topic += "/"+ t3
#if debug: print("get_topic(): '"+ topic+ "'")
return topic
def homa_init(mqttc):
"Publish HomA setup messages to MQTT broker."
print("Publishing HomA setup data to "+mqtt_config.host+" ...")
for mqtt_dict in mqtt_devices_arr:
mqttc.publish(get_topic(mqtt_dict['device'], "meta/room"), mqtt_dict['room'], retain=True)
mqttc.publish(get_topic(mqtt_dict['device'], "meta/name"), mqtt_dict['name'], retain=True)
for dict in mqtt_controls_arr:
mqttc.publish(get_topic(dict['device'], "controls", dict['control'], "meta/room"), dict['room'], retain=True)
#mqttc.publish(get_topic(mqtt_dict['device'], "controls/Power/meta/room"), "switch", retain=True)
return
# The callback for when the client receives a CONNACK response from the broker.
def on_connect(client, userdata, flags, rc):
if debug: print("on_connect(): Connected with result code "+ str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
return
# The callback for when a PUBLISH message is received from the broker.
def on_message(client, userdata, msg):
if debug: print("on_message(): "+ msg.topic+ ":"+ str(msg.payload))
return
# The callback for when a message is published to the broker.
def on_publish(client, userdata, mid):
if debug: print("on_publish(): message send "+ str(mid))
return
def main():
# connect to MQTT broker
mqttc = mqtt.Client()
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.on_publish = on_publish
if mqtt_config.ca_certs != "":
# avoid "CertificateError: hostname" error by monkey patching ssl
#ssl.match_hostname = lambda cert, hostname: True
#mqttc.tls_insecure_set(True) # Do not use this "True" in production!
mqttc.tls_set(mqtt_config.ca_certs, certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
mqttc.username_pw_set(mqtt_config.user, password=mqtt_config.pwd)
mqttc.connect(mqtt_config.host, port=mqtt_config.port)
mqttc.loop_start()
homa_init(mqttc) # setup HomA MQTT device and control settings
# wait until all queued topics are published
mqttc.loop_stop()
mqttc.disconnect()
return
if __name__ == "__main__":
main()
exec(open("min_max_saver/setup.py").read())
exec(open("rcplugs-esp/setup.py").read())