diff --git a/api/src/automations_v2/automation_watcher.py b/api/src/automations_v2/automation_watcher.py index 13cd7f7..82a9b85 100644 --- a/api/src/automations_v2/automation_watcher.py +++ b/api/src/automations_v2/automation_watcher.py @@ -1,4 +1,5 @@ -from threading import Thread, Condition, Event +import asyncio +from threading import Thread, Event from pathlib import Path from time import sleep from typing import List, Optional @@ -8,6 +9,7 @@ from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler from src.logger import get_logger +from ..utils import throttle from .db import AutomationDBConnection, DBError from .loader import load_automation_path from .types import ExtenededAutomationData @@ -23,8 +25,9 @@ class AutomationFileEventHandler(FileSystemEventHandler): self.__automation_db = AutomationDBConnection(automation_db_path) self.file_loaded = file_loaded self.__loaded_automations: Optional[List[ExtenededAutomationData]] = None - self.reload_file() + asyncio.run(self.reload_file()) + @throttle(seconds=60) def reload_file(self): try: automations = list(load_automation_path(self.__automation_file)) @@ -48,17 +51,17 @@ class AutomationFileEventHandler(FileSystemEventHandler): def on_modified(self, event: FileSystemEvent): logger.info(f"on_modified: {type(event)} {event.event_type} path : {event.src_path}") if Path(event.src_path).absolute() == self.__automation_file: - self.reload_file() + asyncio.run(self.reload_file()) def on_created(self, event: FileSystemEvent): logger.info(f"on_created: {type(event)} {event.event_type} path : {event.src_path}") if Path(event.src_path).absolute() == self.__automation_file: - self.reload_file() + asyncio.run(self.reload_file()) def on_deleted(self, event: FileSystemEvent): logger.info(f"on_deleted: {type(event)} {event.event_type} path : {event.src_path}") if Path(event.src_path).absolute() == self.__automation_file: - self.reload_file() + asyncio.run(self.reload_file()) class AutomationFileWatcher(Thread): diff --git a/api/src/automations_v2/loader.py b/api/src/automations_v2/loader.py index db2497d..bd82722 100644 --- a/api/src/automations_v2/loader.py +++ b/api/src/automations_v2/loader.py @@ -1,12 +1,12 @@ -from curses.ascii import isdigit from pathlib import Path -from typing import Any, Iterator, Union, Dict +from typing import Any, Iterator, Union from ..hass_config.loader import HassConfig from src.json_serializer import normalize_obj from src.logger import get_logger from src.yaml_serializer import load_yaml from src.yaml_serializer.types import IncludedYaml, IncludedYamlDir +from src.utils import extract_files from .errors import InvalidAutomationFile from .types import ExtenededAutomationData @@ -14,19 +14,6 @@ from .types import ExtenededAutomationData logger = get_logger(__file__) -def extract_files(p: Path) -> Iterator[Path]: - if not p.exists(): - logger.warning( - f"the specified path '{p}' does not point to a valid location! -- {p.absolute()}" - ) - return - if p.is_dir(): - for c in p.iterdir(): - yield from extract_files(c) - else: - yield p - - def extract_automation_paths( hass_config: HassConfig, ) -> Iterator[Path]: diff --git a/api/src/utils.py b/api/src/utils.py new file mode 100644 index 0000000..60a226a --- /dev/null +++ b/api/src/utils.py @@ -0,0 +1,57 @@ +import asyncio +from datetime import datetime, timedelta +from functools import wraps +from pathlib import Path +from typing import Any, Callable, Coroutine, Iterator, Optional, Tuple, TypeVar + +FnOutT = TypeVar("FnOutT") + + +class throttle(object): + """source: https://gist.github.com/ChrisTM/5834503 + Decorator that prevents a function from being called more than once every + time period. + To create a function that cannot be called more than once a minute: + @throttle(minutes=1) + def my_fun(): + pass + """ + + def __init__(self, seconds=0, minutes=0, hours=0): + self.throttle_period = timedelta(seconds=seconds, minutes=minutes, hours=hours) + self.time_of_last_call = datetime.min + self.current_id: Optional[datetime] = None + + def __call__(self, fn: Callable[..., FnOutT]) -> Callable[..., Coroutine[Any, Any, FnOutT]]: + @wraps(fn) + async def wrapper(*args, **kwargs): + self.current_id = now = datetime.now() + time_since_last_call = now - self.time_of_last_call + + if time_since_last_call > self.throttle_period: + self.time_of_last_call = now + return fn(*args, **kwargs) + else: + asyncio.sleep(self.throttle_period.total_seconds()) + if self.current_id == now: + return fn(*args, **kwargs) + + return wrapper + + +def extract_files(p: Path) -> Iterator[Path]: + """Extracts all files in folder and it's subfolders + + Args: + p (Path): + + Yields: + Iterator[Path] + """ + if not p.exists(): + return + if p.is_dir(): + for c in p.iterdir(): + yield from extract_files(c) + else: + yield p diff --git a/api/tests/unittests/automations_v2/test_automation_watcher.py b/api/tests/unittests/automations_v2/test_automation_watcher.py index 52c2338..e416ff7 100644 --- a/api/tests/unittests/automations_v2/test_automation_watcher.py +++ b/api/tests/unittests/automations_v2/test_automation_watcher.py @@ -1,5 +1,5 @@ from src.automations_v2.automation_watcher import AutomationFileWatcher -from tests.utils import HA_CONFIG6_EXAMPLE, create_copy_of_example_config +from tests.utils import HA_CONFIG6_EXAMPLE, HA_CONFIG_EXAMPLE, create_copy_of_example_config from .utils import TestWithDB @@ -40,6 +40,28 @@ class watcher_tests(TestWithDB): self.assertEqual(self.db.count_automations(), 12) watcher.join() + def test_rapid_changes(self): + example_folder = create_copy_of_example_config() + watcher = AutomationFileWatcher(example_folder / "automations.yaml", self.db_file) + watcher.start() + watcher.wait_until_next_reload(True) + self.assertEqual(self.db.count_automations(), 32) + (example_folder / "automations.yaml").write_text("") + (example_folder / "automations.yaml").write_text( + (HA_CONFIG_EXAMPLE / "automations.yaml").read_text() + ) + (example_folder / "automations.yaml").write_text("") + (example_folder / "automations.yaml").write_text( + (HA_CONFIG_EXAMPLE / "automations.yaml").read_text() + ) + (example_folder / "automations.yaml").write_text("") + (example_folder / "automations.yaml").write_text( + (HA_CONFIG_EXAMPLE / "automations.yaml").read_text() + ) + watcher.wait_until_next_reload(True) + self.assertEqual(self.db.count_automations(), 32) + watcher.join() + # def test_valid_changes(self): # example_folder = create_copy_of_example_config() # watcher = AutomationFileWatcher(example_folder / "automations.yaml", self.db_file)