From 98e1e970c103807472dff1c3098daad5212907c7 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" <git@edgarpierre.fr> Date: Sun, 9 Mar 2025 11:40:40 +0100 Subject: [PATCH] Refactor configuration file loading to check multiple paths and raise error if not found --- hasspy/__main__.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/hasspy/__main__.py b/hasspy/__main__.py index 688d500..92744e9 100644 --- a/hasspy/__main__.py +++ b/hasspy/__main__.py @@ -1,5 +1,6 @@ import logging import tomllib +from pathlib import Path from hasspy.mqtt import HassClient @@ -7,7 +8,18 @@ from hasspy.mqtt import HassClient def main() -> None: logging.basicConfig(level=logging.INFO) - with open("/etc/hasspy/config.toml", "rb") as file: + config_file = next( + ( + x + for x in (Path("/etc/hasspy.toml"), Path("/etc/hasspy/config.toml")) + if x.exists() + ), + None, + ) + if not config_file: + raise FileNotFoundError("No configuration file found") + + with open(config_file, "rb") as file: config = tomllib.load(file) ha = HassClient(