diff --git a/api/src/api/config.py b/api/src/api/config.py index 1f78bb4..6b1ef95 100644 --- a/api/src/api/config.py +++ b/api/src/api/config.py @@ -1,7 +1,7 @@ from src.env import ROOT_FOLDER from src.db.connection import HassDatabase from src.config.AutomationLoader import AutomationLoader -from src.config.HassSafeConstructor import load_hass_config +from src.yaml_serializer import load_hass_config hass_config = load_hass_config(ROOT_FOLDER / "tests/samples") diff --git a/api/src/config/AutomationLoader.py b/api/src/config/AutomationLoader.py index 0536c88..9fce5c9 100644 --- a/api/src/config/AutomationLoader.py +++ b/api/src/config/AutomationLoader.py @@ -1,8 +1,7 @@ from typing import Dict, Iterable, List, Literal, Optional, Union from pydantic.fields import Field -from pydantic.error_wrappers import ValidationError from src.json_serializer import json_dumps, normalize_obj -from src.config.HassSafeConstructor import HassConfig, IncludedYaml, SecretValue +from src.yaml_serializer import HassConfig, IncludedYaml, SecretValue from pydantic import BaseModel # errors diff --git a/api/src/config/__init__.py b/api/src/config/__init__.py index 108a215..b1808a2 100644 --- a/api/src/config/__init__.py +++ b/api/src/config/__init__.py @@ -1,7 +1,7 @@ import os from decouple import config from pathlib import Path -from .HassSafeConstructor import load_hass_config +from src.yaml_serializer import load_hass_config def find_hass_config() -> Path: diff --git a/api/src/json_serializer.py b/api/src/json_serializer.py index e5c5a71..73e5e72 100644 --- a/api/src/json_serializer.py +++ b/api/src/json_serializer.py @@ -1,6 +1,6 @@ import orjson from typing import Any, Union -from src.config.HassSafeConstructor import SecretValue +from src.yaml_serializer import SecretValue # TODO: add support for these NOT_IMPLEMENTED_SV_MSG = "SECRET VALUE NOT IMPLEMENTED" diff --git a/api/src/config/HassSafeConstructor.py b/api/src/yaml_serializer/HassSafeConstructor.py similarity index 87% rename from api/src/config/HassSafeConstructor.py rename to api/src/yaml_serializer/HassSafeConstructor.py index dffa891..a0a85c1 100644 --- a/api/src/config/HassSafeConstructor.py +++ b/api/src/yaml_serializer/HassSafeConstructor.py @@ -1,36 +1,10 @@ from pathlib import Path -from typing import Any, Literal, NamedTuple, Optional, Union +from typing import Any, NamedTuple, Optional from ruamel.yaml import YAML from ruamel.yaml.constructor import SafeConstructor from ruamel.yaml.nodes import Node from src.logger import logger - - -class HassConfig(NamedTuple): - """The final output for load_hass_config""" - - secrets: dict # the secrets file content - config: dict # the complete configration.yaml tree - - -class IncludedYaml(NamedTuple): - """Any time something in the yaml is specified as `include !filepath` - this data type will represent it. - """ - - original_path: Path # path to location of original file - data: Any # the parsed data - - -class SecretValue(NamedTuple): - name: str - value: str - - -def load_hass_config(file_path: Path) -> HassConfig: - HAY = HassYaml() - data = HAY.load(file_path / "configuration.yaml") - return HassConfig(HAY.secrets, data) +from src.yaml_serializer.types import SecretValue, IncludedYaml class HassYaml: @@ -72,7 +46,9 @@ class HassYaml: else: logger.info(f"Found `!include {node.value}` in {file_path.absolute()}!") - return IncludedYaml(file_path, self.__internal_constructor_load(file_path)) + return IncludedYaml( + node.value, file_path, self.__internal_constructor_load(file_path) + ) def _secret_yaml(loader, node: Node): """Load secrets and embed it into the configuration YAML.""" diff --git a/api/src/yaml_serializer/__init__.py b/api/src/yaml_serializer/__init__.py new file mode 100644 index 0000000..4c80ae2 --- /dev/null +++ b/api/src/yaml_serializer/__init__.py @@ -0,0 +1,19 @@ +import io +from pathlib import Path +from typing import Any, List +from src.yaml_serializer.base_yaml import yaml +from src.yaml_serializer.HassSafeConstructor import HassYaml +from src.yaml_serializer.types import HassConfig, SecretValue, IncludedYaml + + +def load_hass_config(file_path: Path) -> HassConfig: + HAY = HassYaml() + data = HAY.load(file_path / "configuration.yaml") + return HassConfig(HAY.secrets, data) + + +def dump_yaml(obj: Any) -> str: + stream = io.StringIO() + yaml.dump(obj, stream) + stream.seek(0) + return stream.read() diff --git a/api/src/yaml_serializer/base_yaml.py b/api/src/yaml_serializer/base_yaml.py new file mode 100644 index 0000000..9e729f5 --- /dev/null +++ b/api/src/yaml_serializer/base_yaml.py @@ -0,0 +1,6 @@ +from ruamel.yaml import YAML +from src.yaml_serializer.types import IncludedYaml, SecretValue + +yaml = YAML() +yaml.register_class(IncludedYaml) +yaml.register_class(SecretValue) diff --git a/api/src/yaml_serializer/types.py b/api/src/yaml_serializer/types.py new file mode 100644 index 0000000..064e68a --- /dev/null +++ b/api/src/yaml_serializer/types.py @@ -0,0 +1,34 @@ +from pathlib import Path +from typing import Any, NamedTuple +from ruamel.yaml.nodes import Node +from ruamel.yaml.representer import BaseRepresenter + + +class IncludedYaml(NamedTuple): + """Any time something in the yaml is specified as `include !filepath` + this data type will represent it. + """ + + name: str # original node name + original_path: Path # path to location of original file + data: Any # the parsed data + + @classmethod + def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYaml"): + return representer.represent_scalar("!include", node.name) + + +class SecretValue(NamedTuple): + name: str + value: str + + @classmethod + def to_yaml(cls, representer: BaseRepresenter, node: Node): + return representer.represent_scalar("!secret", node.name) + + +class HassConfig(NamedTuple): + """The final output for load_hass_config""" + + secrets: dict # the secrets file content + config: dict # the complete configration.yaml tree diff --git a/api/tests/unittests/test_config.py b/api/tests/unittests/test_config.py index 61033d6..b205d2c 100644 --- a/api/tests/unittests/test_config.py +++ b/api/tests/unittests/test_config.py @@ -1,3 +1,4 @@ +from tempfile import mkdtemp from unittest import TestCase from src.config.AutomationLoader import ( AutomationActionNode, @@ -10,8 +11,7 @@ from src.config.AutomationLoader import ( _parse_actions, ) from src.json_serializer import NOT_IMPLEMENTED_SV_MSG -from src.config.HassSafeConstructor import ( - HassConfig, +from src.yaml_serializer import ( SecretValue, load_hass_config, IncludedYaml, @@ -20,6 +20,9 @@ from tests.utils import SAMPLE_HA_PATH class config_finder_tests(TestCase): + def test_simple_made_up_config(self): + pass + def test_find_sample_config(self): secrets, config = load_hass_config(SAMPLE_HA_PATH) self.assertIsInstance(config["template"], IncludedYaml) diff --git a/api/tests/unittests/test_json_s.py b/api/tests/unittests/test_json_s.py index a7a1f3e..774e093 100644 --- a/api/tests/unittests/test_json_s.py +++ b/api/tests/unittests/test_json_s.py @@ -1,6 +1,6 @@ import json from unittest import TestCase -from src.config.HassSafeConstructor import SecretValue +from src.yaml_serializer import SecretValue from src.json_serializer import json_dumps, NOT_IMPLEMENTED_SV_MSG diff --git a/api/tests/unittests/test_yaml.py b/api/tests/unittests/test_yaml.py new file mode 100644 index 0000000..b7760f1 --- /dev/null +++ b/api/tests/unittests/test_yaml.py @@ -0,0 +1,23 @@ +from pathlib import Path +from unittest import TestCase +from src.yaml_serializer import dump_yaml, IncludedYaml, SecretValue + + +class dumping_yamls_tests(TestCase): + def test_dumping_includes(self): + yaml = dump_yaml( + { + "automations": IncludedYaml( + "automations.yaml", + Path("/config/automations.yaml"), + [ + {"id": "wow", "alias": "NO!"}, + ], + ) + } + ) + self.assertEqual(yaml, "automations: !include automations.yaml\n") + + def test_dumping_secrets(self): + yaml = dump_yaml({"google_password": SecretValue("gpass", "supersec")}) + self.assertEqual(yaml, "google_password: !secret gpass\n") diff --git a/api/tests/utils.py b/api/tests/utils.py index 6b41354..cb07cc8 100644 --- a/api/tests/utils.py +++ b/api/tests/utils.py @@ -1,5 +1,22 @@ +from tempfile import mkdtemp from pathlib import Path +from typing import Optional THIS_FOLDER = Path(__file__).parent SAMPLE_HA_PATH = THIS_FOLDER / "samples" SAMPLE_DB_PATH = SAMPLE_HA_PATH / "home-assistant_v2.db" + + +def create_dummy_auto_folder( + auto: dict, + secrets: Optional[dict] = None, + other_config: Optional[dict] = None, + include_auto_in_configuration: bool = False, +) -> Path: + root_folder = Path(mkdtemp()) + configuration_yaml = {**other_config} if other_config is not None else {} + if include_auto_in_configuration: + configuration_yaml["automation"] = auto + else: + configuration_yaml["automation"] = "!include automations.yaml" + # (root_folder / 'automations.yaml').write_text(yaml)