diff --git a/api/src/automations/loader.py b/api/src/automations/loader.py index 1440bc7..ac468ca 100644 --- a/api/src/automations/loader.py +++ b/api/src/automations/loader.py @@ -1,3 +1,4 @@ +from itertools import chain from pathlib import Path from typing import Any, Iterator, Tuple, Union @@ -25,25 +26,45 @@ def extract_automation_paths( hass_config: HassConfig, ) -> Iterator[Tuple[str, str]]: found = False - for key, value in extract_automation_keys(hass_config): - logger.info(f"Loading automations from '{key}'") + + for config_key, automation_path in chain( + extract_automation_root_paths(hass_config), extract_package_automation_paths(hass_config) + ): found = True - if isinstance(value, IncludedYamlDir): # type: ignore - yield key, str(value.path.relative_to(hass_config.root_path)) - else: - logger.warning(f"found an invalid type in {key}!") + yield config_key, automation_path + if not found: yield "", "automations.yaml" -def extract_package_automation_paths(hass_config: HassConfig): - if homeassistant_config := hass_config.configurations.get("homeassistant", None): - if isinstance(homeassistant_config, IncludedYamlDir): # type: ignore - homeassistant_config_d = homeassistant_config.to_normalized_json() - elif isinstance(homeassistant_config, dict): - homeassistant_config_d = homeassistant_config +def extract_automation_root_paths( + hass_config: HassConfig, +) -> Iterator[Tuple[str, str]]: + for key, value in extract_automation_keys(hass_config): + logger.info(f"Loading automations from '{key}'") + if isinstance(value, IncludedYamlDir): # type: ignore + yield key, str(value.path.relative_to(hass_config.root_path)) + elif isinstance(value, list): + logger.warning(f"Failed to load from '{key}' as no support for inline automations yet.") else: - raise AssertionError("configurations.homeassistant must be a dictionary!") + logger.warning(f"found an invalid type in {key}!") + + +def extract_package_automation_paths(hass_config: HassConfig) -> Iterator[Tuple[str, str]]: + for package_name, package_data in hass_config.pacakges.items(): + if not isinstance(package_data, dict): + logger.warning(f"The package {package_name} is incorrectly configured!") + else: + if automations := package_data.get("automation", None): + logger.info(f"Loading automations from package '{package_name}'") + if isinstance(automations, IncludedYamlDir): # type: ignore + yield package_name, str(automations.path.relative_to(hass_config.root_path)) + elif isinstance(automations, list): + logger.warning( + f"Failed to load from '{package_name}' as no support for inline automations yet." + ) + else: + logger.warning(f"found an invalid type in {package_name}!") def get_base_automation_key(hass_config: HassConfig): diff --git a/api/src/yaml_serializer/__init__.py b/api/src/yaml_serializer/__init__.py index 3bd600c..e16a337 100644 --- a/api/src/yaml_serializer/__init__.py +++ b/api/src/yaml_serializer/__init__.py @@ -115,7 +115,7 @@ class IncludedYamlDirNamed(NamedTuple): out = {} for path, data in load_dir_yaml(self.path): if out.get(path.name, None) is None: - out[path.name] = data + out[path.stem] = data else: raise AssertionError( f"expected {path.name} to be a unique file name but found a second file name in {path}" diff --git a/api/tests/samples/config-10/automations/base.yaml b/api/tests/samples/config-10/automations/base.yaml deleted file mode 100644 index b933716..0000000 --- a/api/tests/samples/config-10/automations/base.yaml +++ /dev/null @@ -1,23 +0,0 @@ -- id: "base1" - alias: BASE - description: Example - mode: single - trigger: - - platform: homeassistant - event: start - condition: [] - action: - - service: counter.increment - data: {} - target: - entity_id: counter.up_times -- id: "base2" - alias: base 2 - description: "" - trigger: - - platform: sun - event: sunrise - offset: 0 - condition: [] - action: [] - mode: single diff --git a/api/tests/samples/config-10/automations/include_dir_list/home/down.yaml b/api/tests/samples/config-10/automations/include_dir_list/home/down.yaml deleted file mode 100644 index bbe9f33..0000000 --- a/api/tests/samples/config-10/automations/include_dir_list/home/down.yaml +++ /dev/null @@ -1,14 +0,0 @@ -id: "1659114822642" -alias: On Home Down -description: Example -mode: single -trigger: - - platform: homeassistant - event: shutdown -condition: [] -action: - - service: counter.increment - target: - entity_id: - - counter.up_times - data: {} diff --git a/api/tests/samples/config-10/automations/include_dir_list/home/up.yaml b/api/tests/samples/config-10/automations/include_dir_list/home/up.yaml deleted file mode 100644 index 6f670e3..0000000 --- a/api/tests/samples/config-10/automations/include_dir_list/home/up.yaml +++ /dev/null @@ -1,13 +0,0 @@ -id: "1659114647066" -alias: HA Up -description: Example -mode: single -trigger: - - platform: homeassistant - event: start -condition: [] -action: - - service: counter.increment - data: {} - target: - entity_id: counter.up_times diff --git a/api/tests/samples/config-10/automations/include_dir_list/notify_washer.yaml b/api/tests/samples/config-10/automations/include_dir_list/notify_washer.yaml deleted file mode 100644 index 835a43a..0000000 --- a/api/tests/samples/config-10/automations/include_dir_list/notify_washer.yaml +++ /dev/null @@ -1,13 +0,0 @@ -id: "1659114647067" -alias: Notify Washer -description: Example -mode: single -trigger: - - platform: homeassistant - event: start -condition: [] -action: - - service: counter.increment - data: {} - target: - entity_id: counter.up_times diff --git a/api/tests/samples/config-10/automations/include_dir_merge_list/list1.yaml b/api/tests/samples/config-10/automations/include_dir_merge_list/list1.yaml deleted file mode 100644 index c8db855..0000000 --- a/api/tests/samples/config-10/automations/include_dir_merge_list/list1.yaml +++ /dev/null @@ -1,7 +0,0 @@ -- id: "mlist1" - alias: Merged List 1 - description: "" - trigger: [] - condition: [] - action: [] - mode: single diff --git a/api/tests/samples/config-10/automations/include_dir_merge_list/sub/list2.yaml b/api/tests/samples/config-10/automations/include_dir_merge_list/sub/list2.yaml deleted file mode 100644 index e0374c1..0000000 --- a/api/tests/samples/config-10/automations/include_dir_merge_list/sub/list2.yaml +++ /dev/null @@ -1,14 +0,0 @@ -- id: "mlist2" - alias: Merged List 2 - description: "" - trigger: [] - condition: [] - action: [] - mode: single -- id: "mlist3" - alias: Merged List 3 - description: "" - trigger: [] - condition: [] - action: [] - mode: single diff --git a/api/tests/samples/config-10/automations/include_dir_merge_list/sub2/sub3/list3.yaml b/api/tests/samples/config-10/automations/include_dir_merge_list/sub2/sub3/list3.yaml deleted file mode 100644 index ab55fe7..0000000 --- a/api/tests/samples/config-10/automations/include_dir_merge_list/sub2/sub3/list3.yaml +++ /dev/null @@ -1,14 +0,0 @@ -- id: "sublist3-mlist2" - alias: Sublist 3 - Merged List 2 - description: "" - trigger: [] - condition: [] - action: [] - mode: single -- id: "sublist3-mlist3" - alias: Sublist 3 - Merged List 3 - description: "" - trigger: [] - condition: [] - action: [] - mode: single diff --git a/api/tests/samples/config-10/packages/sub/package2.yaml b/api/tests/samples/config-10/packages/sub/package2.yaml index e257fc3..51c0abc 100644 --- a/api/tests/samples/config-10/packages/sub/package2.yaml +++ b/api/tests/samples/config-10/packages/sub/package2.yaml @@ -1,6 +1,6 @@ package2: automation: - - id: "package-ha" + - id: "package2" alias: package ha description: Example package auto mode: single diff --git a/api/tests/samples/config-8/automations/base.yaml b/api/tests/samples/config-8/automations/base.yaml deleted file mode 100644 index b933716..0000000 --- a/api/tests/samples/config-8/automations/base.yaml +++ /dev/null @@ -1,23 +0,0 @@ -- id: "base1" - alias: BASE - description: Example - mode: single - trigger: - - platform: homeassistant - event: start - condition: [] - action: - - service: counter.increment - data: {} - target: - entity_id: counter.up_times -- id: "base2" - alias: base 2 - description: "" - trigger: - - platform: sun - event: sunrise - offset: 0 - condition: [] - action: [] - mode: single diff --git a/api/tests/samples/config-8/automations/include_dir_list/home/down.yaml b/api/tests/samples/config-8/automations/include_dir_list/home/down.yaml deleted file mode 100644 index bbe9f33..0000000 --- a/api/tests/samples/config-8/automations/include_dir_list/home/down.yaml +++ /dev/null @@ -1,14 +0,0 @@ -id: "1659114822642" -alias: On Home Down -description: Example -mode: single -trigger: - - platform: homeassistant - event: shutdown -condition: [] -action: - - service: counter.increment - target: - entity_id: - - counter.up_times - data: {} diff --git a/api/tests/samples/config-8/automations/include_dir_list/home/up.yaml b/api/tests/samples/config-8/automations/include_dir_list/home/up.yaml deleted file mode 100644 index 6f670e3..0000000 --- a/api/tests/samples/config-8/automations/include_dir_list/home/up.yaml +++ /dev/null @@ -1,13 +0,0 @@ -id: "1659114647066" -alias: HA Up -description: Example -mode: single -trigger: - - platform: homeassistant - event: start -condition: [] -action: - - service: counter.increment - data: {} - target: - entity_id: counter.up_times diff --git a/api/tests/samples/config-8/automations/include_dir_list/notify_washer.yaml b/api/tests/samples/config-8/automations/include_dir_list/notify_washer.yaml deleted file mode 100644 index 835a43a..0000000 --- a/api/tests/samples/config-8/automations/include_dir_list/notify_washer.yaml +++ /dev/null @@ -1,13 +0,0 @@ -id: "1659114647067" -alias: Notify Washer -description: Example -mode: single -trigger: - - platform: homeassistant - event: start -condition: [] -action: - - service: counter.increment - data: {} - target: - entity_id: counter.up_times diff --git a/api/tests/samples/config-8/automations/include_dir_merge_list/list1.yaml b/api/tests/samples/config-8/automations/include_dir_merge_list/list1.yaml deleted file mode 100644 index c8db855..0000000 --- a/api/tests/samples/config-8/automations/include_dir_merge_list/list1.yaml +++ /dev/null @@ -1,7 +0,0 @@ -- id: "mlist1" - alias: Merged List 1 - description: "" - trigger: [] - condition: [] - action: [] - mode: single diff --git a/api/tests/samples/config-8/automations/include_dir_merge_list/sub/list2.yaml b/api/tests/samples/config-8/automations/include_dir_merge_list/sub/list2.yaml deleted file mode 100644 index e0374c1..0000000 --- a/api/tests/samples/config-8/automations/include_dir_merge_list/sub/list2.yaml +++ /dev/null @@ -1,14 +0,0 @@ -- id: "mlist2" - alias: Merged List 2 - description: "" - trigger: [] - condition: [] - action: [] - mode: single -- id: "mlist3" - alias: Merged List 3 - description: "" - trigger: [] - condition: [] - action: [] - mode: single diff --git a/api/tests/samples/config-8/automations/include_dir_merge_list/sub2/sub3/list3.yaml b/api/tests/samples/config-8/automations/include_dir_merge_list/sub2/sub3/list3.yaml deleted file mode 100644 index ab55fe7..0000000 --- a/api/tests/samples/config-8/automations/include_dir_merge_list/sub2/sub3/list3.yaml +++ /dev/null @@ -1,14 +0,0 @@ -- id: "sublist3-mlist2" - alias: Sublist 3 - Merged List 2 - description: "" - trigger: [] - condition: [] - action: [] - mode: single -- id: "sublist3-mlist3" - alias: Sublist 3 - Merged List 3 - description: "" - trigger: [] - condition: [] - action: [] - mode: single diff --git a/api/tests/samples/config-8/packages/sub/package2.yaml b/api/tests/samples/config-8/packages/sub/package2.yaml index 921be97..96d2206 100644 --- a/api/tests/samples/config-8/packages/sub/package2.yaml +++ b/api/tests/samples/config-8/packages/sub/package2.yaml @@ -1,5 +1,5 @@ automation: - - id: "package-ha" + - id: "package-sub" alias: package ha description: Example package auto mode: single diff --git a/api/tests/unittests/automations/test_loader.py b/api/tests/unittests/automations/test_loader.py index 43791e8..1dd2301 100644 --- a/api/tests/unittests/automations/test_loader.py +++ b/api/tests/unittests/automations/test_loader.py @@ -10,11 +10,14 @@ from src.automations.tags import TagManager from src.automations.types import ExtenededAutomation from src.hass_config.loader import HassConfig from tests.utils import ( + HA_CONFIG10_EXAMPLE, HA_CONFIG2_EXAMPLE, HA_CONFIG3_EXAMPLE, HA_CONFIG4_EXAMPLE, HA_CONFIG5_EXAMPLE, HA_CONFIG6_EXAMPLE, + HA_CONFIG7_EXAMPLE, + HA_CONFIG8_EXAMPLE, HA_CONFIG9_EXAMPLE, HA_CONFIG_EXAMPLE, ) @@ -149,9 +152,12 @@ class loader_tests(TestCase): (HA_CONFIG4_EXAMPLE, 1), (HA_CONFIG5_EXAMPLE, 3), (HA_CONFIG6_EXAMPLE, 4), + (HA_CONFIG7_EXAMPLE, 1), + (HA_CONFIG8_EXAMPLE, 1), (HA_CONFIG9_EXAMPLE, 1), + (HA_CONFIG10_EXAMPLE, 1), ]: - with self.subTest(ha_path=ha_path, expected=expected): + with self.subTest(config_name=ha_path.name, expected=expected): hass_config = HassConfig(ha_path) paths = list(extract_automation_paths(hass_config)) self.assertEqual(len(paths), expected) diff --git a/api/tests/unittests/automations/test_manager.py b/api/tests/unittests/automations/test_manager.py index 4ac6a81..deaf6c5 100644 --- a/api/tests/unittests/automations/test_manager.py +++ b/api/tests/unittests/automations/test_manager.py @@ -12,6 +12,7 @@ from src.errors import ErrorSet from src.hass_config.loader import HassConfig from tests.utils import ( HA_CONFIG6_EXAMPLE, + HA_CONFIG8_EXAMPLE, HA_CONFIG_EXAMPLE, create_copy_of_example_config, ) @@ -327,3 +328,9 @@ class manager_tests(TestWithDB): auto.tags, {}, ) + + def test_load_pacakges(self): + _, automation_manager = self.get_manager(HA_CONFIG8_EXAMPLE) + automation_manager.reload() + auto = automation_manager.get("package-sub") + self.assertEqual(auto.configuration_key, "pacakage2") diff --git a/api/tests/utils.py b/api/tests/utils.py index 7222add..7e4ab23 100644 --- a/api/tests/utils.py +++ b/api/tests/utils.py @@ -19,6 +19,7 @@ HA_CONFIG6_EXAMPLE = SAMPLES_FOLDER / "config-6" HA_CONFIG7_EXAMPLE = SAMPLES_FOLDER / "config-7" HA_CONFIG8_EXAMPLE = SAMPLES_FOLDER / "config-8" HA_CONFIG9_EXAMPLE = SAMPLES_FOLDER / "config-9" +HA_CONFIG10_EXAMPLE = SAMPLES_FOLDER / "config-10" def get_example_automation_loader(