From 89251f792ca4cc16d4629480942b80bb799cbed9 Mon Sep 17 00:00:00 2001 From: Carlo Mion Date: Sat, 1 Jul 2023 09:05:01 +0200 Subject: [PATCH] refactor(config.py): correctly parse add-on config for MQTT, better error handling --- hikvision-doorbell/src/config.py | 2 +- hikvision-doorbell/src/main.py | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/hikvision-doorbell/src/config.py b/hikvision-doorbell/src/config.py index c916af1..67d53c8 100644 --- a/hikvision-doorbell/src/config.py +++ b/hikvision-doorbell/src/config.py @@ -116,7 +116,7 @@ def load_mqtt_config(cls, v): Load the MQTT configuration from the user-supplied values, if provided, or fallback to asking the HA supervisor for the integrated MQTT add-on ''' # If we have no value in input, skip validation - if not v: + if v is None: return # If the user supplied some configuration values, ues them diff --git a/hikvision-doorbell/src/main.py b/hikvision-doorbell/src/main.py index 10bf519..9c175d9 100644 --- a/hikvision-doorbell/src/main.py +++ b/hikvision-doorbell/src/main.py @@ -17,9 +17,13 @@ async def main(): """Main entrypoint of the application""" - # Disable type warnings since the object is populated at runtime using goodconf library - config = AppConfig() # type:ignore - config.load() + try: + # Disable type warnings since the object is populated at runtime using goodconf library + config = AppConfig() # type:ignore + config.load() + except RuntimeError as e: + logger.error("Configuration error: {}", e) + sys.exit(1) # Remove the default handler installed by loguru (it redirects to stderr) logger.remove() @@ -96,7 +100,7 @@ def signal_handler(task: asyncio.Task): user_message, sdk_code, sdk_message = e.args logger.error("{}: {} Error code:{}", user_message, sdk_message, sdk_code) sys.exit(1) - except (ConnectionRefusedError, socket.gaierror) as e: + except (OSError, ConnectionRefusedError) as e: # Connection to MQTT broker failed logger.error("Error while connecting to MQTT broker: {}", e.strerror) sys.exit(1) \ No newline at end of file