From 037a9fbaf283ec515a5d0a89d9a0f9225270424a Mon Sep 17 00:00:00 2001
From: Sean Rees <sean@erifax.org>
Date: Tue, 30 Mar 2021 09:17:49 +0100
Subject: [PATCH] Fix a startup ordering bug that prevented
 --create_device_cache from working

---
 main.py | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/main.py b/main.py
index faf76ad..2e1e80d 100755
--- a/main.py
+++ b/main.py
@@ -54,7 +54,8 @@ class DeviceWrapper:
         return self.libdyson.is_connected
 
     def connect(self, host: str):
-        """Connect to the device and start the environmental monitoring timer."""
+        """Connect to the device and start the environmental monitoring
+        timer."""
         self.libdyson.connect(host)
         self._refresh_timer()
 
@@ -208,18 +209,23 @@ def main(argv):
         logging.exception('Could not load configuration: %s', args.config)
         sys.exit(-1)
 
-    devices = cfg.devices
-    if len(devices) == 0:
-        logging.fatal(
-            'No devices configured; please re-run this program with --create_device_cache.')
-        sys.exit(-2)
-
     if args.create_device_cache:
+        if not cfg.dyson_credentials:
+            logging.error('DysonLink credentials not found in %s, cannot generate device cache',
+                          args.config)
+            sys.exit(-1)
+
         logging.info(
             '--create_device_cache supplied; breaking out to perform this.')
         account.generate_device_cache(cfg.dyson_credentials, args.config)
         sys.exit(0)
 
+    devices = cfg.devices
+    if len(devices) == 0:
+        logging.fatal(
+            'No devices configured; please re-run this program with --create_device_cache.')
+        sys.exit(-2)
+
     prometheus_client.start_http_server(args.port)
 
     ConnectionManager(metrics.Metrics().update, devices, cfg.hosts)
-- 
GitLab