diff --git a/api/src/automations/loader.py b/api/src/automations/loader.py index 9fec95b..5836f47 100644 --- a/api/src/automations/loader.py +++ b/api/src/automations/loader.py @@ -1,5 +1,5 @@ from itertools import chain -from typing import Iterable +from typing import Iterable, Iterator from src.json_serializer import normalize_obj from .types import AutomationData, AutomationMetdata from .parsers import _parse_actions @@ -7,7 +7,7 @@ from .parsers import _parse_actions # Single loader def load_automation( automations_raw_data: Iterable[dict], -) -> Iterable[AutomationData]: +) -> Iterator[AutomationData]: """Converts an automation dictionary loaded directly from the config into several python typed objects @@ -15,7 +15,7 @@ def load_automation( automations_raw_data (Iterable[dict]) Returns: - Iterable[AutomationData] + Iterator[AutomationData] """ for auto_raw in automations_raw_data: diff --git a/api/src/automations/manager.py b/api/src/automations/manager.py index d22dd83..0d4620d 100644 --- a/api/src/automations/manager.py +++ b/api/src/automations/manager.py @@ -1,3 +1,28 @@ +from typing import Iterator, Optional +from src.automations.types import AutomationData +from src.hass_config.loader import HassConfig +from .loader import load_automation + + class AutomationManager: - def __init__(self) -> None: - pass + def __init__(self, hass_config: HassConfig) -> None: + self.hass_config = hass_config + + def find( + self, + offset: int = 0, + limit: int = 10, + ) -> Iterator[AutomationData]: + for i, auto in enumerate(self.hass_config.automations): + if i < offset: + continue + if i > offset + limit: + break + yield from load_automation([auto]) + + def get(self, index: int) -> Optional[AutomationData]: + if index >= 0: + for i, auto in enumerate(self.hass_config.automations): + if i == index: + return next(load_automation(([auto]))) + return None diff --git a/api/src/hass_config/loader.py b/api/src/hass_config/loader.py index af6d4b4..22802fa 100644 --- a/api/src/hass_config/loader.py +++ b/api/src/hass_config/loader.py @@ -21,8 +21,8 @@ class HassConfig: if "automation" in config: automation_ref = config["automation"] if isinstance(automation_ref, IncludedYaml): - automation_path = automation_ref.path + automation_path = self.root_path / automation_ref.path_str else: - return list(config["automation"]) + return config["automation"] with automation_path.open("r") as f: - return list(load_yaml(f)) # type: ignore + return load_yaml(f) # type: ignore diff --git a/api/src/yaml_serializer/types.py b/api/src/yaml_serializer/types.py index 1ce7975..8391e7f 100644 --- a/api/src/yaml_serializer/types.py +++ b/api/src/yaml_serializer/types.py @@ -9,15 +9,15 @@ class IncludedYaml(NamedTuple): this data type will represent it. """ - path: Path # original node path + path_str: str # original node path @classmethod def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYaml"): - return representer.represent_scalar("!include", str(node.path.absolute())) + return representer.represent_scalar("!include", node.path_str) @classmethod def from_yaml(cls, _constructor, node: Node): - return cls(Path(node.value).absolute()) + return cls(node.value) class SecretValue(NamedTuple): diff --git a/api/tests/unittests/automations/test_manager.py b/api/tests/unittests/automations/test_manager.py new file mode 100644 index 0000000..f4f002c --- /dev/null +++ b/api/tests/unittests/automations/test_manager.py @@ -0,0 +1,23 @@ +from unittest import TestCase +from tests.utils import HA_CONFIG_EXAMPLE, create_dummy_config_folder +from src.hass_config.loader import HassConfig +from src.automations.manager import AutomationManager + +example_hass_config = HassConfig(HA_CONFIG_EXAMPLE) +example_automation_loader = AutomationManager(example_hass_config) + + +class manager_tests(TestCase): + def test_find_limit_and_get(self): + first_auto_with_find = list(example_automation_loader.find(limit=1)) + first_auto_with_get = example_automation_loader.get(0) + self.assertEqual(first_auto_with_find[0], first_auto_with_get) + + def test_find_at_most_with_offset(self): + autos = list(example_automation_loader.find(limit=10, offset=10)) + self.assertTrue(len(autos), 10) + + def test_find_invalid(self): + self.assertIsNone(example_automation_loader.get(-1)) + self.assertIsNone(example_automation_loader.get(1_000)) + self.assertIsNone(example_automation_loader.get(31)) diff --git a/api/tests/unittests/automations/test_parsers.py b/api/tests/unittests/automations/test_parsers.py index eb28fe1..2eb3b05 100644 --- a/api/tests/unittests/automations/test_parsers.py +++ b/api/tests/unittests/automations/test_parsers.py @@ -221,21 +221,6 @@ class parser_tests(TestCase): for expected, parsed in zip(expected_process, _parse_actions(raw_conditions)): self.assertEqual(parsed, expected) - # def test_finding_with_AutomationLoader(self): - # automation_loader = AutomationLoader(load_hass_config(SAMPLE_HA_PATH)) - - # first_auto_with_find = list(automation_loader.find(limit=1)) - # first_auto_with_get = automation_loader.get(0) - # self.assertEqual(first_auto_with_find[0], first_auto_with_get) - - # kitchen_autos = list(automation_loader.find(alias="kitchen")) - # self.assertTrue(len(kitchen_autos), 3) - - # self.assertIsNone(automation_loader.get(-1)) - # self.assertIsNone(automation_loader.get(1_000)) - # self.assertIsNone(automation_loader.get(31)) - # self.assertEqual(len(automation_loader), 31) - # def test_invalid_automations(self): # with self.assertRaises(AutomationLoaderException): # AutomationLoader( diff --git a/api/tests/unittests/hass_config/test_loader.py b/api/tests/unittests/hass_config/test_loader.py index 7304255..dae3a64 100644 --- a/api/tests/unittests/hass_config/test_loader.py +++ b/api/tests/unittests/hass_config/test_loader.py @@ -1,9 +1,7 @@ from unittest import TestCase from src.automations.types import AutomationData, AutomationMetdata from src.hass_config.loader import HassConfig -from tests.utils import HA_CONFIG_EXAMPLE, create_dummy_config_folder - -example_hass_config = HassConfig(HA_CONFIG_EXAMPLE) +from tests.utils import create_dummy_config_folder class loader_tests(TestCase): @@ -17,7 +15,7 @@ class loader_tests(TestCase): self.assertTrue("automation" in hass_config.configurations) self.assertEqual(hass_config.automations[0]["id"], "test") - def test_load_automation_from_config_yaml_inclue(self): + def test_load_automation_from_config_yaml_include(self): folder = create_dummy_config_folder( [AutomationData(metadata=AutomationMetdata(id="test"))], automation_in_conifguration_mode="include", diff --git a/api/tests/unittests/test_yaml_serializer.py b/api/tests/unittests/test_yaml_serializer.py index fa8b0a9..a1921d4 100644 --- a/api/tests/unittests/test_yaml_serializer.py +++ b/api/tests/unittests/test_yaml_serializer.py @@ -17,11 +17,11 @@ class dumping_yamls_tests(TestCase): yaml = dump_yaml( { "automations": IncludedYaml( - Path("automations.yaml"), + "automations.yaml", ) } ) - self.assertEqual(yaml, f"automations: !include {Path('automations.yaml').absolute()}\n") + self.assertEqual(yaml, "automations: !include automations.yaml\n") def test_dumping_secrets(self): yaml = dump_yaml({"google_password": SecretValue("gpass", "supersec")}) @@ -48,7 +48,7 @@ class dumping_yamls_tests(TestCase): "name": "cool", "google_password": SecretValue("gpass", "n/a"), "sensor": IncludedYamlDir("include_dir_list", "sensors/*yaml"), - "automations": IncludedYaml(Path("automations.yaml").absolute()), + "automations": IncludedYaml("automations.yaml"), }, ) self.assertEqual( @@ -56,5 +56,5 @@ class dumping_yamls_tests(TestCase): "name: cool\n" "google_password: !secret gpass\n" "sensor: !include_dir_list sensors/*yaml\n" - f"automations: !include {Path('automations.yaml').absolute()}\n", + f"automations: !include automations.yaml\n", ) diff --git a/api/tests/utils.py b/api/tests/utils.py index 0caf959..a1c336b 100644 --- a/api/tests/utils.py +++ b/api/tests/utils.py @@ -35,7 +35,7 @@ def create_dummy_config_folder( configuration_yaml["automation"] = auto_prims elif automation_in_conifguration_mode == "include": configuration_yaml["automation"] = IncludedYaml( - Path(root_folder / "automations.yaml"), + "automations.yaml", ) if automation_in_conifguration_mode != "inline": (root_folder / "automations.yaml").write_text(dump_yaml(auto_prims))