diff options
author | Botond Hende <nettingman@gmail.com> | 2024-12-04 14:35:19 +0100 |
---|---|---|
committer | Botond Hende <nettingman@gmail.com> | 2024-12-04 14:35:19 +0100 |
commit | 6168323b858a1eeeafdf9a4e2528df3205d6c2da (patch) | |
tree | 6d815b48ceb9b3259edf2d25a999e0913dab5333 | |
parent | cb40a2ff74a8165cb59357c423f83219a01631bc (diff) |
response handler class
-rw-r--r-- | __main__.py | 22 | ||||
-rw-r--r-- | modules/responses/response_handler.py | 31 |
2 files changed, 38 insertions, 15 deletions
diff --git a/__main__.py b/__main__.py index 0e8a1ec..5196760 100644 --- a/__main__.py +++ b/__main__.py @@ -3,13 +3,13 @@ import yaml from pathlib import Path -from modules.config import Config +from .modules.config import Config from .modules.config import load_config from .modules.hassil.recognize import recognize from .modules.hassil.util import merge_dict from .modules.hassil.intents import Intents, TextSlotList -from modules.responses.responses import Responses +from .modules.responses.response_handler import ResponseHandler from .modules.input_handlers.stdin_input import StdinInput from .modules.input_handlers.pipewire_record import PipeWireRecord @@ -21,7 +21,6 @@ from .modules.intents import * def main(): config = load_config() input_dict = {"intents": {}} - response_dict = {"responses": {}} intent_yaml_path = Path(config.intents_dir) intent_yaml_file_paths = intent_yaml_path.glob("*.yaml") @@ -31,14 +30,6 @@ def main(): intents = Intents.from_dict(input_dict) - 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)) - - responses = Responses.from_dict(response_dict) - processes = [] for file in os.listdir(config.applications_dir): if file == "[": @@ -53,9 +44,9 @@ def main(): } input_handler = PipeWireRecord() if config.input_mode == Config.INPUT_PW else StdinInput() + response_handler = ResponseHandler(config) try: - # TODO select input type from config for input_text in input_handler.get_input(): result = recognize(input_text, intents, slot_lists=slot_lists) if result is not None: @@ -64,12 +55,13 @@ def main(): **{e.name: e.value for e in result.entities_list}, } print(result_dict) + + response_handler.respond(result.response, result.intent.name) + handler = getattr(globals()[result_dict["domain"]], result_dict["intent"]) handler(result_dict, config) - if result.response != "default": - respond(responses.responses[result.intent.name].sentence_texts[0]) else: - print("<no match>") + print("<no intent match>") finally: input_handler.cleanup() 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) |