From f46dd914e630b5bd27f91b81ee2d17dd48b1d652 Mon Sep 17 00:00:00 2001
From: Ellis Percival <flyte@failcode.co.uk>
Date: Tue, 13 Nov 2018 13:42:06 +0000
Subject: [PATCH] Wait for the client to disconnect cleanly so that we don't
 accidentally trigger the LWT.

---
 pi_mqtt_gpio/server.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/pi_mqtt_gpio/server.py b/pi_mqtt_gpio/server.py
index d194b4b..7070501 100644
--- a/pi_mqtt_gpio/server.py
+++ b/pi_mqtt_gpio/server.py
@@ -504,8 +504,11 @@ if __name__ == "__main__":
         client.publish(
             "%s/%s" % (topic_prefix, config["mqtt"]["status_topic"]),
             config["mqtt"]["status_payload_stopped"], qos=1, retain=True)
-        # This should also quit the mqtt loop thread.
+
+        client.loop_stop()
         client.disconnect()
+        client.loop_forever()
+
         for name, gpio in GPIO_MODULES.items():
             if not GPIO_CONFIGS[name]["cleanup"]:
                 _LOG.info("Cleanup disabled for module %r.", name)
-- 
GitLab