From 99243bd33a63c076d16bd08914bd1808d4a4112c Mon Sep 17 00:00:00 2001 From: Botond Hende Date: Wed, 4 Dec 2024 15:22:10 +0100 Subject: response manager and libnotify response handler --- modules/config.py | 6 ++++++ modules/responses/libnotify.py | 15 +++++++++++++-- modules/responses/response_handler.py | 35 +++++------------------------------ modules/responses/response_manager.py | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 32 deletions(-) create mode 100644 modules/responses/response_manager.py (limited to 'modules') 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 index 2e83101..237b9ac 100644 --- a/modules/responses/libnotify.py +++ b/modules/responses/libnotify.py @@ -1,4 +1,15 @@ import os -def respond(text: str) -> None: - os.popen(f'notify-send Hestia "{text}" --app-name=hestia') \ No newline at end of file +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 index ee0acca..9cc0cf0 100644 --- a/modules/responses/response_handler.py +++ b/modules/responses/response_handler.py @@ -1,31 +1,6 @@ -import yaml +from abc import ABC, abstractmethod -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) +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) -- cgit v1.2.3-70-g09d2