diff --git a/api/src/automations_v2/loader.py b/api/src/automations_v2/loader.py index 86baf57..45ea6cd 100644 --- a/api/src/automations_v2/loader.py +++ b/api/src/automations_v2/loader.py @@ -1,16 +1,53 @@ from pathlib import Path -from typing import Any, Iterator, Union +from typing import Any, Iterator, Union, Dict +from ..hass_config.loader import HassConfig from src.json_serializer import normalize_obj -from ..logger import get_logger +from src.logger import get_logger from src.yaml_serializer import load_yaml +from src.yaml_serializer.types import IncludedYaml, IncludedYamlDir from .errors import InvalidAutomationFile from .types import ExtenededAutomationData logger = get_logger(__file__) -# Single loader + +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]: + found = False + for key, value in hass_config.configurations.items(): + if str(key).startswith("automation"): + logger.info(f"Loading automations from '{key}'") + found = True + if isinstance(value, dict): + raise NotImplementedError( + "No support for automations baked into configuration.yaml! Please extract these into a seperate file" + ) + elif isinstance(value, IncludedYaml): + yield hass_config.root_path / value.path_str + elif isinstance(value, IncludedYamlDir): + yield from extract_files(hass_config.root_path / value.name) + else: + logger.warning(f"found an invalid type in {key}!") + if not found: + yield hass_config.root_path / "automations.yaml" + + def load_automation_path(automation_path: Path) -> Iterator[ExtenededAutomationData]: """ diff --git a/api/src/automations_v2/manager.py b/api/src/automations_v2/manager.py new file mode 100644 index 0000000..e69de29 diff --git a/api/tests/samples/config-6/configuration.yaml b/api/tests/samples/config-6/configuration.yaml new file mode 100644 index 0000000..ffdac96 --- /dev/null +++ b/api/tests/samples/config-6/configuration.yaml @@ -0,0 +1,13 @@ +# Loads default set of integrations. Do not remove. +default_config: + +# Text to speech +tts: + - platform: google_translate + +automation manual: !include_dir_list ./automations/include_dir_list +automation ui: !include automations/ui.yaml +automation: !include automations/base.yaml +automation cools: !include_dir_merge_list automations/include_dir_merge_list +script: !include scripts.yaml +scene: !include scenes.yaml diff --git a/api/tests/unittests/automations_v2/test_loader.py b/api/tests/unittests/automations_v2/test_loader.py index 71bf9b6..8f008d7 100644 --- a/api/tests/unittests/automations_v2/test_loader.py +++ b/api/tests/unittests/automations_v2/test_loader.py @@ -2,9 +2,17 @@ from pathlib import Path from tempfile import mktemp from unittest import TestCase -from src.automations_v2.loader import load_automation_path +from src.automations_v2.loader import extract_automation_paths, load_automation_path from src.automations_v2.types import ExtenededAutomationData -from tests.utils import HA_CONFIG2_EXAMPLE, HA_CONFIG4_EXAMPLE +from src.hass_config.loader import HassConfig +from tests.utils import ( + HA_CONFIG2_EXAMPLE, + HA_CONFIG3_EXAMPLE, + HA_CONFIG4_EXAMPLE, + HA_CONFIG5_EXAMPLE, + HA_CONFIG6_EXAMPLE, + HA_CONFIG_EXAMPLE, +) class loader_tests(TestCase): @@ -85,3 +93,17 @@ class loader_tests(TestCase): file_path.unlink(missing_ok=True) automations = list(load_automation_path(file_path)) self.assertEqual(len(automations), 0) + + def test_extract_all_automation_files(self): + for ha_path, expected in [ + (HA_CONFIG_EXAMPLE, 1), + (HA_CONFIG2_EXAMPLE, 1), + (HA_CONFIG3_EXAMPLE, 1), + (HA_CONFIG4_EXAMPLE, 3), + (HA_CONFIG5_EXAMPLE, 7), + (HA_CONFIG6_EXAMPLE, 8), + ]: + with self.subTest(ha_path=ha_path, expected=expected): + hass_config = HassConfig(ha_path) + paths = list(extract_automation_paths(hass_config)) + self.assertEqual(len(paths), expected) diff --git a/api/tests/utils.py b/api/tests/utils.py index ce2d6e8..2925200 100644 --- a/api/tests/utils.py +++ b/api/tests/utils.py @@ -15,6 +15,8 @@ HA_CONFIG_EXAMPLE = SAMPLES_FOLDER / "config" HA_CONFIG2_EXAMPLE = SAMPLES_FOLDER / "config-2" HA_CONFIG3_EXAMPLE = SAMPLES_FOLDER / "config-3" HA_CONFIG4_EXAMPLE = SAMPLES_FOLDER / "config-4" +HA_CONFIG5_EXAMPLE = SAMPLES_FOLDER / "config-5" +HA_CONFIG6_EXAMPLE = SAMPLES_FOLDER / "config-6" def get_example_automation_loader(