Skip to content

Commit

Permalink
Make reconnects more aggressive
Browse files Browse the repository at this point in the history
Previously: we would retry once. If this failed, it would require the whole
binary to be restarted. Now: we attach a timer to periodically retry and we
also reset discovery to ensure the underlying zeroconf libraries notify us
when the device reappears.
  • Loading branch information
seanrees committed Apr 23, 2021
1 parent 7f45fd1 commit 89cebac
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,23 @@ def is_connected(self) -> bool:
"""True if we're connected to the Dyson device."""
return self.libdyson.is_connected

def connect(self, host: str):
def connect(self, host: str, retry_on_timeout_secs: int=30):
"""Connect to the device and start the environmental monitoring
timer."""
self.libdyson.connect(host)
self._refresh_timer()
timer.
Args:
host: ip or hostname of Dyson device
retry_on_timeout_secs: number of seconds to wait in between retries. this will block the running thread.
"""
if self.is_connected:
logging.info('Already connected to %s (%s); no need to reconnect.', host, self.serial)
else:
try:
self.libdyson.connect(host)
self._refresh_timer()
except libdyson.exceptions.DysonConnectTimeout:
logging.error('Timeout connecting to %s (%s); will retry', host, self.serial)
threading.Timer(retry_on_timeout_secs, self.connect, args=[host]).start()

def disconnect(self):
"""Disconnect from the Dyson device."""
Expand All @@ -75,7 +87,7 @@ def _timer_callback(self):
self.libdyson.request_environmental_data()
self._refresh_timer()
else:
logging.debug('Device %s is disconnected.')
logging.debug('Device %s is disconnected.', self.serial)

def _create_libdyson_device(self):
return libdyson.get_device(self.serial, self._config_device.credentials,
Expand Down Expand Up @@ -143,8 +155,10 @@ def _device_callback(self, device, message):
logging.debug('Received update from %s: %s', device.serial, message)
if not device.is_connected:
logging.info(
'Device %s is now disconnected, clearing it and re-adding.', device.serial)
'Device %s is now disconnected, clearing it and re-adding', device.serial)
device.disconnect()
self._discovery.stop_discovery()
self._discovery.start_discovery()
self._add_device(device, add_listener=False)
return

Expand Down

0 comments on commit 89cebac

Please sign in to comment.