From 6168323b858a1eeeafdf9a4e2528df3205d6c2da Mon Sep 17 00:00:00 2001 From: Botond Hende Date: Wed, 4 Dec 2024 14:35:19 +0100 Subject: response handler class --- modules/responses/response_handler.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 modules/responses/response_handler.py (limited to 'modules/responses/response_handler.py') diff --git a/modules/responses/response_handler.py b/modules/responses/response_handler.py new file mode 100644 index 0000000..ee0acca --- /dev/null +++ b/modules/responses/response_handler.py @@ -0,0 +1,31 @@ +import yaml + +from pathlib import Path + +from ..config import Config +from ..responses.responses import Responses +from ..hassil.util import merge_dict +from .libnotify import respond as respond_libnotify + +class ResponseHandler: + def __init__(self, config: Config): + response_dict = {"responses": {}} + + response_yaml_path = Path(config.responses_dir) + response_yaml_file_paths = response_yaml_path.glob("*.yaml") + for yaml_file_path in response_yaml_file_paths: + with open(yaml_file_path, "r", encoding="utf-8") as yaml_file: + merge_dict(response_dict, yaml.safe_load(yaml_file)) + + self.responses = Responses.from_dict(response_dict) + self.respond_methods = [respond_libnotify] + + def respond(self, response: str, intent_name: str): + response_key = intent_name if response == "default" else response + if response_key not in self.responses.responses: + print(f"No response found for: {response_key}") + return + + response_text = self.responses.responses[response_key].sentence_texts[0] + for respond_method in self.respond_methods: + respond_method(response_text) -- cgit v1.2.3-70-g09d2