Skip to content

Commit

Permalink
refactor(config.py): correctly parse add-on config for MQTT, better e…
Browse files Browse the repository at this point in the history
…rror handling
  • Loading branch information
mion00 committed Jul 1, 2023
1 parent 33fdeb2 commit 89251f7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion hikvision-doorbell/src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions hikvision-doorbell/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)

0 comments on commit 89251f7

Please sign in to comment.