]> the.earth.li Git - energenie-attiny.git/commitdiff
Resubscribe to MQTT prefix when reconnecting main
authorJonathan McDowell <noodles@earth.li>
Mon, 21 Sep 2020 20:18:57 +0000 (21:18 +0100)
committerJonathan McDowell <noodles@earth.li>
Mon, 21 Sep 2020 20:18:57 +0000 (21:18 +0100)
mqtt-power

index d2723cba9a6f58e48a1d93fff537494429cddaa8..f74a3374c6be4794d9b02592d073b5f21389b9a2 100755 (executable)
@@ -62,6 +62,15 @@ def mqtt_message(client, device, message):
     control_relay(device, relay, state)
 
 
+def mqtt_connect(client, userdata, flags, rc):
+    if debug:
+        print("Connected to MQTT server")
+
+    client.publish("relay/{}/LWT".format(serno_str), "Online", retain=True)
+    print("Subscribing to %s" % prefix.format(serno_str))
+    client.subscribe(prefix.format(serno_str))
+
+
 def uptime():
     with open('/proc/uptime', 'r') as f:
         seconds = int(float(f.readline().split()[0]))
@@ -94,10 +103,10 @@ client.tls_set(ca_certs='/etc/ssl/certs/ca-certificates.crt')
 client.username_pw_set(auth['username'], auth['password'])
 client.user_data_set(relay)
 client.on_message = mqtt_message
+client.on_connect = mqtt_connect
 client.connect(Broker, port=8883)
-print("Subscribing to %s" % prefix.format(serno_str))
-client.subscribe(prefix.format(serno_str))
 client.loop_start()
+client.will_set("relay/{}/LWT".format(serno_str), "Offline", retain=True)
 
 while True:
     state = {