diff options
-rw-r--r-- | __main__.py | 19 | ||||
-rw-r--r-- | config.yaml | 1 | ||||
-rw-r--r-- | modules/config.py | 6 | ||||
-rw-r--r-- | modules/responses/libnotify.py | 15 | ||||
-rw-r--r-- | modules/responses/response_handler.py | 6 | ||||
-rw-r--r-- | modules/responses/response_manager.py | 32 | ||||
-rw-r--r-- | modules/responses/responses.py | 16 | ||||
-rw-r--r-- | resources/hestia_images/hestia.jpg | bin | 0 -> 43277 bytes | |||
-rw-r--r-- | responses/process_HesExecuteProcess.yaml | 5 |
9 files changed, 73 insertions, 27 deletions
diff --git a/__main__.py b/__main__.py index ab4f59c..7d6f84b 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_manager import ResponseManager from .modules.input_handlers.stdin_input import StdinInput from .modules.input_handlers.pipewire_record import PipeWireRecord @@ -28,14 +28,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(input_dict, yaml.safe_load(yaml_file)) - - responses = Responses.from_dict(input_dict) - processes = [] for file in os.listdir(config.applications_dir): if file == "[": @@ -50,9 +42,9 @@ def main(): } input_handler = PipeWireRecord() if config.input_mode == Config.INPUT_PW else StdinInput() + response_manager = ResponseManager(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: @@ -61,10 +53,13 @@ def main(): **{e.name: e.value for e in result.entities_list}, } print(result_dict) + + response_manager.respond(result.response, result.intent.name) + handler = getattr(globals()[result_dict["domain"]], result_dict["intent"]) handler(result_dict, config) else: - print("<no match>") + print("<no intent match>") finally: input_handler.cleanup() diff --git a/config.yaml b/config.yaml index cc3c816..ac3069c 100644 --- a/config.yaml +++ b/config.yaml @@ -1,5 +1,6 @@ intents_dir: sentences responses_dir: responses +resources_dir: resources applications_dir: /usr/bin input_mode: pw-record # other option: stdin diff --git a/modules/config.py b/modules/config.py index b0fc624..1a5865b 100644 --- a/modules/config.py +++ b/modules/config.py @@ -15,15 +15,21 @@ class Config: self.intents_dir = "" self.responses_dir = "" + self.resources_dir = "" self.applications_dir = "" self.lock = "" + self.hestia_images = "" + def update(self, **entries) -> None: self.__dict__.update(entries) self.intents_dir = Config.__convert_to_absolute_path(self.intents_dir) self.responses_dir = Config.__convert_to_absolute_path(self.responses_dir) + self.resources_dir = Config.__convert_to_absolute_path(self.resources_dir) + + self.hestia_images = os.path.join(self.resources_dir, "hestia_images") @staticmethod def __convert_to_absolute_path(path: str) -> str: diff --git a/modules/responses/libnotify.py b/modules/responses/libnotify.py new file mode 100644 index 0000000..0bc774f --- /dev/null +++ b/modules/responses/libnotify.py @@ -0,0 +1,15 @@ +import os + +from .response_handler import ResponseHandler +from ..config import Config + + +class LibNotify(ResponseHandler): + def __init__(self, config: Config): + self.config = config + self.hestia_images = {} + for path in os.listdir(self.config.hestia_images): + self.hestia_images[os.path.splitext(os.path.basename(path))[0]] = os.path.join(self.config.hestia_images, path) + + def respond(self, text: str) -> None: + os.popen(f'notify-send "Hestia " "{text}" -i "{self.hestia_images["hestia"]}" --app-name=hestia')
\ No newline at end of file diff --git a/modules/responses/response_handler.py b/modules/responses/response_handler.py new file mode 100644 index 0000000..9cc0cf0 --- /dev/null +++ b/modules/responses/response_handler.py @@ -0,0 +1,6 @@ +from abc import ABC, abstractmethod + +class ResponseHandler(ABC): + @abstractmethod + def respond(self, response: str): + pass
\ No newline at end of file diff --git a/modules/responses/response_manager.py b/modules/responses/response_manager.py new file mode 100644 index 0000000..34c0a41 --- /dev/null +++ b/modules/responses/response_manager.py @@ -0,0 +1,32 @@ +import yaml + +from pathlib import Path + +from ..config import Config +from ..responses.responses import Responses +from ..hassil.util import merge_dict +from .response_handler import ResponseHandler +from .libnotify import LibNotify + +class ResponseManager: + 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_handlers: list[ResponseHandler] = [LibNotify(config)] + + 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 handler in self.respond_handlers: + handler.respond(response_text) diff --git a/modules/responses/responses.py b/modules/responses/responses.py index 1836deb..f89a1fe 100644 --- a/modules/responses/responses.py +++ b/modules/responses/responses.py @@ -2,13 +2,9 @@ from dataclasses import dataclass, field from typing import List, Dict, Any @dataclass(frozen=True) -class ResponseData: - sentence_texts: List[str] - -@dataclass(frozen=True) class Response: name: str - data: List[ResponseData] = field(default_factory=list) + sentence_texts: List[str] @dataclass(frozen=True) class Responses: @@ -25,13 +21,9 @@ class Responses: responses={ response_name: Response( name=response_name, - data=[ - ResponseData( - sentence_texts=data_dict["sentences"], - ) - for data_dict in response_dict["data"] - ], + sentence_texts=response_dict["sentences"] ) for response_name, response_dict in input_dict["responses"].items() }, - )
\ No newline at end of file + ) + diff --git a/resources/hestia_images/hestia.jpg b/resources/hestia_images/hestia.jpg Binary files differnew file mode 100644 index 0000000..3091af0 --- /dev/null +++ b/resources/hestia_images/hestia.jpg diff --git a/responses/process_HesExecuteProcess.yaml b/responses/process_HesExecuteProcess.yaml index 66653c2..31646ff 100644 --- a/responses/process_HesExecuteProcess.yaml +++ b/responses/process_HesExecuteProcess.yaml @@ -1,5 +1,4 @@ responses: HesExecuteProcess: - data: - - sentences: - "Right away."
\ No newline at end of file + sentences: + - "Starting it now."
\ No newline at end of file |