Skip to content

Commit

Permalink
Detect disconnections during poll() calls
Browse files Browse the repository at this point in the history
The Paho-MQTT client may not invoke the 'on_disconnect'
callback when used with an external loop, so check for connection
loss during calls to loop_misc() and handle it appropriately.

Closes #222.
  • Loading branch information
kpfleming committed Nov 17, 2020
1 parent 774f348 commit 292db9d
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions insteon_mqtt/network/Mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ def poll(self, t):
passed in so that all clients receive the same "current" time
instead of each calling time.time() and getting a different value.
"""
# This is required to handle keepalive messages.
self.client.loop_misc()
# This is required to handle keepalive messages and detect disconnections.
rc = self.client.loop_misc()
if rc == paho.MQTT_ERR_NO_CONN:
self._on_disconnect(self.client, None, rc)

#-----------------------------------------------------------------------
def retry_connect_dt(self):
Expand Down

0 comments on commit 292db9d

Please sign in to comment.