error handling for invalid config file

This commit is contained in:
joBr99
2023-11-23 19:12:15 +01:00
parent a542ba39aa
commit 9cc31b6e5b

View File

@@ -69,8 +69,22 @@ def get_config_file():
def get_config(file): def get_config(file):
global settings global settings
with open(file, 'r', encoding="utf8") as file: try:
settings = yaml.safe_load(file) with open(file, 'r', encoding="utf8") as file:
settings = yaml.safe_load(file)
except yaml.YAMLError as exc:
print ("Error while parsing YAML file:")
if hasattr(exc, 'problem_mark'):
if exc.context != None:
print (' parser says\n' + str(exc.problem_mark) + '\n ' +
str(exc.problem) + ' ' + str(exc.context) +
'\nPlease correct data and retry.')
else:
print (' parser says\n' + str(exc.problem_mark) + '\n ' +
str(exc.problem) + '\nPlease correct data and retry.')
else:
print ("Something went wrong while parsing yaml file")
return False
if not settings.get("mqtt_username"): if not settings.get("mqtt_username"):
settings["mqtt_username"] = os.getenv('MQTT_USER') settings["mqtt_username"] = os.getenv('MQTT_USER')
@@ -90,10 +104,7 @@ def get_config(file):
settings["home_assistant_token"] = st settings["home_assistant_token"] = st
settings["home_assistant_address"] = "http://supervisor" settings["home_assistant_address"] = "http://supervisor"
settings["is_addon"] = True settings["is_addon"] = True
return True
#print(settings["home_assistant_token"])
#print(settings["home_assistant_address"])
#print(settings["is_addon"])
def connect(): def connect():
global settings, home_assistant, client global settings, home_assistant, client
@@ -179,8 +190,11 @@ def signal_handler(signum, frame):
os.execl(python, python, *sys.argv) os.execl(python, python, *sys.argv)
if __name__ == '__main__': if __name__ == '__main__':
get_config(get_config_file())
signal.signal(signal.SIGTERM, signal_handler) signal.signal(signal.SIGTERM, signal_handler)
threading.Thread(target=config_watch).start() threading.Thread(target=config_watch).start()
connect() if (get_config(get_config_file())):
loop() connect()
loop()
else:
while True:
time.sleep(100)