From e11f4deab54c00da380c0f083dfb1d4ad64477ce Mon Sep 17 00:00:00 2001 From: Ari Date: Sun, 14 Aug 2022 01:07:19 -0400 Subject: [PATCH] watching folders --- .../{watcher.py => automation_watcher.py} | 0 api/src/automations_v2/loader.py | 55 ++++++++++--------- ..._watcher.py => test_automation_watcher.py} | 13 ++++- 3 files changed, 41 insertions(+), 27 deletions(-) rename api/src/automations_v2/{watcher.py => automation_watcher.py} (100%) rename api/tests/unittests/automations_v2/{test_watcher.py => test_automation_watcher.py} (77%) diff --git a/api/src/automations_v2/watcher.py b/api/src/automations_v2/automation_watcher.py similarity index 100% rename from api/src/automations_v2/watcher.py rename to api/src/automations_v2/automation_watcher.py diff --git a/api/src/automations_v2/loader.py b/api/src/automations_v2/loader.py index 45ea6cd..db2497d 100644 --- a/api/src/automations_v2/loader.py +++ b/api/src/automations_v2/loader.py @@ -1,3 +1,4 @@ +from curses.ascii import isdigit from pathlib import Path from typing import Any, Iterator, Union, Dict from ..hass_config.loader import HassConfig @@ -61,47 +62,51 @@ def load_automation_path(automation_path: Path) -> Iterator[ExtenededAutomationD if not automation_path.exists(): logger.warning(f"the file {automation_path} does not exists") return - try: - with automation_path.open("r") as fp: - automations = load_yaml(fp) - except Exception as err: - raise InvalidAutomationFile( - when="loading yaml", - automation_path=automation_path, - error=err, - ) from err - - if isinstance(automations, dict): + if automation_path.is_dir(): + for f in extract_files(automation_path): + yield from load_automation_path(f) + else: try: - yield ExtenededAutomationData( - **clean_automation(automations), - source_file=str(automation_path), - source_file_type="obj", - ) + with automation_path.open("r") as fp: + automations = load_yaml(fp) except Exception as err: raise InvalidAutomationFile( - when="reading file contents", + when="loading yaml", automation_path=automation_path, error=err, ) from err - else: - if automations is None: - logger.warning(f"the file {automation_path} is empty") - return - for automation in automations: + + if isinstance(automations, dict): try: yield ExtenededAutomationData( - **clean_automation(automation), + **clean_automation(automations), source_file=str(automation_path), - source_file_type="list", + source_file_type="obj", ) except Exception as err: raise InvalidAutomationFile( when="reading file contents", automation_path=automation_path, error=err, - automation=automation, ) from err + else: + if automations is None: + logger.warning(f"the file {automation_path} is empty") + return + for automation in automations: + try: + yield ExtenededAutomationData( + **clean_automation(automation), + source_file=str(automation_path), + source_file_type="list", + ) + except Exception as err: + raise InvalidAutomationFile( + when="reading file contents", + automation_path=automation_path, + error=err, + automation=automation, + ) from err def clean_automation(auto_raw: dict): diff --git a/api/tests/unittests/automations_v2/test_watcher.py b/api/tests/unittests/automations_v2/test_automation_watcher.py similarity index 77% rename from api/tests/unittests/automations_v2/test_watcher.py rename to api/tests/unittests/automations_v2/test_automation_watcher.py index 0c48e6a..52c2338 100644 --- a/api/tests/unittests/automations_v2/test_watcher.py +++ b/api/tests/unittests/automations_v2/test_automation_watcher.py @@ -1,5 +1,5 @@ -from src.automations_v2.watcher import AutomationFileWatcher -from tests.utils import create_copy_of_example_config +from src.automations_v2.automation_watcher import AutomationFileWatcher +from tests.utils import HA_CONFIG6_EXAMPLE, create_copy_of_example_config from .utils import TestWithDB @@ -30,6 +30,15 @@ class watcher_tests(TestWithDB): watcher.start() watcher.wait_until_next_reload(True) self.assertEqual(self.db.count_automations(), 0) + watcher.join() + + def test_watch_folder(self): + example_folder = create_copy_of_example_config(HA_CONFIG6_EXAMPLE) + watcher = AutomationFileWatcher(example_folder / "automations", self.db_file) + watcher.start() + watcher.wait_until_next_reload(True) + self.assertEqual(self.db.count_automations(), 12) + watcher.join() # def test_valid_changes(self): # example_folder = create_copy_of_example_config()