noop yaml loader refactor
This commit is contained in:
1 parent
0cf3f2706a
commit
97521097dc
17 files changed
+147
-93
No files matched your search
@@ -1,8 +1,9 @@
|
|||||||
get_index # unused function (src/api/app.py:43)
|
get_index # unused function (src/api/app.py:43)
|
||||||
list_autos # unused function (src/api/routes/automations.py:12)
|
list_autos # unused function (src/api/routes/automations.py:13)
|
||||||
upsert_auto # unused function (src/api/routes/automations.py:24)
|
insert_auto # unused function (src/api/routes/automations.py:28)
|
||||||
delete_auto # unused function (src/api/routes/automations.py:32)
|
update_auto # unused function (src/api/routes/automations.py:32)
|
||||||
read_details # unused function (src/api/routes/details.py:10)
|
delete_auto # unused function (src/api/routes/automations.py:40)
|
||||||
|
read_details # unused function (src/api/routes/details.py:11)
|
||||||
read_ping # unused function (src/api/routes/ping.py:10)
|
read_ping # unused function (src/api/routes/ping.py:10)
|
||||||
websocket_endpoint # unused function (src/api/routes/socket.py:13)
|
websocket_endpoint # unused function (src/api/routes/socket.py:13)
|
||||||
params # unused variable (src/api/types.py:16)
|
params # unused variable (src/api/types.py:16)
|
||||||
@@ -12,8 +13,8 @@ trigger_variables # unused variable (src/automations/types.py:13)
|
|||||||
trigger # unused variable (src/automations/types.py:15)
|
trigger # unused variable (src/automations/types.py:15)
|
||||||
condition # unused variable (src/automations/types.py:16)
|
condition # unused variable (src/automations/types.py:16)
|
||||||
action # unused variable (src/automations/types.py:17)
|
action # unused variable (src/automations/types.py:17)
|
||||||
_.to_json # unused method (src/automations/types.py:38)
|
_.to_json # unused method (src/automations/types.py:45)
|
||||||
_.automation_tags # unused property (src/hass_config/loader.py:18)
|
_.automation_tags # unused property (src/hass_config/loader.py:17)
|
||||||
_.save_automations # unused method (src/hass_config/loader.py:48)
|
|
||||||
_.save_tags # unused method (src/hass_config/loader.py:56)
|
|
||||||
json_dumps # unused function (src/json_serializer.py:5)
|
json_dumps # unused function (src/json_serializer.py:5)
|
||||||
|
SecretValue # unused class (src/yaml_serializer/__init__.py:40)
|
||||||
|
EnvVar # unused class (src/yaml_serializer/__init__.py:78)
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
|
|
||||||
|
from src.automations.loader import extract_automation_paths
|
||||||
from src.env import BUILD_VERSION, HASSIO_TOKEN, HASSIO_WS
|
from src.env import BUILD_VERSION, HASSIO_TOKEN, HASSIO_WS
|
||||||
from src.hass_config import HassConfig
|
from src.hass_config import HassConfig
|
||||||
from src.automations.loader import extract_automation_paths
|
|
||||||
|
|
||||||
|
|
||||||
def make_details_router(hass_config: HassConfig) -> APIRouter:
|
def make_details_router(hass_config: HassConfig) -> APIRouter:
|
||||||
|
|||||||
@@ -7,7 +7,12 @@ from typing import Iterable, Iterator, List, Optional
|
|||||||
|
|
||||||
from src.logger import get_logger
|
from src.logger import get_logger
|
||||||
|
|
||||||
from .errors import DBDataError, DBError, DBNoAutomationFound, RepeatedAutomationId
|
from .errors import (
|
||||||
|
DBDataError,
|
||||||
|
DBError,
|
||||||
|
DBNoAutomationFound,
|
||||||
|
RepeatedAutomationId,
|
||||||
|
)
|
||||||
from .types import ExtenededAutomation
|
from .types import ExtenededAutomation
|
||||||
|
|
||||||
logger = get_logger(__file__)
|
logger = get_logger(__file__)
|
||||||
|
|||||||
@@ -4,7 +4,12 @@ from typing import Any, Iterator, Tuple, Union
|
|||||||
from src.json_serializer import normalize_obj
|
from src.json_serializer import normalize_obj
|
||||||
from src.logger import get_logger
|
from src.logger import get_logger
|
||||||
from src.utils import extract_files
|
from src.utils import extract_files
|
||||||
from src.yaml_serializer import dump_yaml, load_yaml, IncludedYaml, IncludedYamlDir
|
from src.yaml_serializer import (
|
||||||
|
IncludedYaml,
|
||||||
|
IncludedYamlDir,
|
||||||
|
dump_yaml,
|
||||||
|
load_yaml,
|
||||||
|
)
|
||||||
|
|
||||||
from ..hass_config.loader import HassConfig
|
from ..hass_config.loader import HassConfig
|
||||||
from .errors import InvalidAutomationFile
|
from .errors import InvalidAutomationFile
|
||||||
@@ -23,10 +28,8 @@ def extract_automation_paths(
|
|||||||
for key, value in extract_automation_keys(hass_config):
|
for key, value in extract_automation_keys(hass_config):
|
||||||
logger.info(f"Loading automations from '{key}'")
|
logger.info(f"Loading automations from '{key}'")
|
||||||
found = True
|
found = True
|
||||||
if isinstance(value, IncludedYaml):
|
if isinstance(value, (IncludedYaml, IncludedYamlDir)):
|
||||||
yield key, value.path_str
|
yield key, str(value.path.relative_to(hass_config.root_path))
|
||||||
elif isinstance(value, IncludedYamlDir):
|
|
||||||
yield key, value.name
|
|
||||||
else:
|
else:
|
||||||
logger.warning(f"found an invalid type in {key}!")
|
logger.warning(f"found an invalid type in {key}!")
|
||||||
if not found:
|
if not found:
|
||||||
@@ -38,17 +41,18 @@ def get_base_automation_key(hass_config: HassConfig):
|
|||||||
for key, value in extract_automation_keys(hass_config):
|
for key, value in extract_automation_keys(hass_config):
|
||||||
found_any = True
|
found_any = True
|
||||||
if isinstance(value, IncludedYaml):
|
if isinstance(value, IncludedYaml):
|
||||||
return key, value.path_str
|
return key, str(value.path.relative_to(hass_config.root_path))
|
||||||
configurations = hass_config.configurations
|
configurations = hass_config.configurations
|
||||||
if found_any:
|
if found_any:
|
||||||
out_path = hass_config.get_backup_automation_file_path()
|
out_path = hass_config.get_backup_automation_file_path()
|
||||||
else:
|
else:
|
||||||
out_path = hass_config.get_default_automation_path()
|
if not (out_path := hass_config.get_default_automation_path()): # type: ignore
|
||||||
|
raise AssertionError("Failed to find automation files...")
|
||||||
out_file = str(out_path.relative_to(hass_config.root_path))
|
out_file = str(out_path.relative_to(hass_config.root_path))
|
||||||
configurations[AUTOMATION_CONFIGURATION_KEY] = IncludedYaml(out_file)
|
configurations[AUTOMATION_CONFIGURATION_KEY] = IncludedYaml(out_path)
|
||||||
out_path.touch(exist_ok=True)
|
out_path.touch(exist_ok=True)
|
||||||
with hass_config.get_configuration_path().open("w") as fp:
|
with hass_config.get_configuration_path().open("w") as fp:
|
||||||
fp.write(dump_yaml(configurations))
|
fp.write(dump_yaml(configurations, root_path=hass_config.root_path))
|
||||||
return AUTOMATION_CONFIGURATION_KEY, out_file
|
return AUTOMATION_CONFIGURATION_KEY, out_file
|
||||||
|
|
||||||
|
|
||||||
@@ -92,7 +96,7 @@ def load_automation_path(
|
|||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
with automation_path.open("r") as fp:
|
with automation_path.open("r") as fp:
|
||||||
automations = load_yaml(fp)
|
automations = load_yaml(fp, root_path=root_path)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
raise InvalidAutomationFile(
|
raise InvalidAutomationFile(
|
||||||
when="loading yaml",
|
when="loading yaml",
|
||||||
|
|||||||
@@ -14,7 +14,11 @@ from .errors import (
|
|||||||
DBError,
|
DBError,
|
||||||
InvalidAutomationFile,
|
InvalidAutomationFile,
|
||||||
)
|
)
|
||||||
from .loader import extract_automation_paths, get_base_automation_key, load_automation_path
|
from .loader import (
|
||||||
|
extract_automation_paths,
|
||||||
|
get_base_automation_key,
|
||||||
|
load_automation_path,
|
||||||
|
)
|
||||||
from .tags import TagManager
|
from .tags import TagManager
|
||||||
from .types import Automation, ExtenededAutomation
|
from .types import Automation, ExtenededAutomation
|
||||||
|
|
||||||
@@ -92,7 +96,7 @@ class AutomationManager:
|
|||||||
objs: Optional[Union[list, dict]] = None
|
objs: Optional[Union[list, dict]] = None
|
||||||
if automation_path.exists():
|
if automation_path.exists():
|
||||||
with automation_path.open("r") as fp:
|
with automation_path.open("r") as fp:
|
||||||
objs = load_yaml(fp)
|
objs = load_yaml(fp, self.hass_config.root_path)
|
||||||
|
|
||||||
if automation.source_file_type == "obj":
|
if automation.source_file_type == "obj":
|
||||||
if isinstance(objs, list):
|
if isinstance(objs, list):
|
||||||
@@ -100,7 +104,7 @@ class AutomationManager:
|
|||||||
f"{automation_path} is a list, but expected a obj for {automation}"
|
f"{automation_path} is a list, but expected a obj for {automation}"
|
||||||
)
|
)
|
||||||
raw_auto = automation.to_primitive(include_tags=False)
|
raw_auto = automation.to_primitive(include_tags=False)
|
||||||
automation_path.write_text(dump_yaml(raw_auto))
|
automation_path.write_text(dump_yaml(raw_auto, self.hass_config.root_path))
|
||||||
else:
|
else:
|
||||||
if isinstance(objs, dict):
|
if isinstance(objs, dict):
|
||||||
raise AttemptingToOverwriteAnIncompatibleFileError(
|
raise AttemptingToOverwriteAnIncompatibleFileError(
|
||||||
@@ -125,7 +129,7 @@ class AutomationManager:
|
|||||||
raise InvalidAutomationFile(
|
raise InvalidAutomationFile(
|
||||||
"Could not update automations, not found in source file."
|
"Could not update automations, not found in source file."
|
||||||
)
|
)
|
||||||
automation_path.write_text(dump_yaml(objs))
|
automation_path.write_text(dump_yaml(objs, self.hass_config.root_path))
|
||||||
self.update_tags(automation.id, automation.tags)
|
self.update_tags(automation.id, automation.tags)
|
||||||
|
|
||||||
def update_tags(self, automation_id: str, tags: Dict[str, str]):
|
def update_tags(self, automation_id: str, tags: Dict[str, str]):
|
||||||
@@ -145,7 +149,7 @@ class AutomationManager:
|
|||||||
automation_path.unlink(missing_ok=True)
|
automation_path.unlink(missing_ok=True)
|
||||||
else:
|
else:
|
||||||
with automation_path.open("r") as fp:
|
with automation_path.open("r") as fp:
|
||||||
objs = load_yaml(fp)
|
objs = load_yaml(fp, self.hass_config.root_path)
|
||||||
if not isinstance(objs, list):
|
if not isinstance(objs, list):
|
||||||
raise AssertionError(
|
raise AssertionError(
|
||||||
f"Attemption to save automation to {automation_path} but encountering an invalid list yaml file. Did you modify this file prior to saving?\n{automation}"
|
f"Attemption to save automation to {automation_path} but encountering an invalid list yaml file. Did you modify this file prior to saving?\n{automation}"
|
||||||
@@ -154,5 +158,5 @@ class AutomationManager:
|
|||||||
for i, obj in enumerate(objs):
|
for i, obj in enumerate(objs):
|
||||||
if obj["id"] != automation.id:
|
if obj["id"] != automation.id:
|
||||||
kept.append(obj)
|
kept.append(obj)
|
||||||
automation_path.write_text(dump_yaml(kept))
|
automation_path.write_text(dump_yaml(kept, self.hass_config.root_path))
|
||||||
self.delete_tags(automation.id)
|
self.delete_tags(automation.id)
|
||||||
@@ -8,9 +8,9 @@ class TagManager(Dict[str, Dict[str, str]]):
|
|||||||
def save(self, path: Path):
|
def save(self, path: Path):
|
||||||
if not path.parent.exists():
|
if not path.parent.exists():
|
||||||
path.parent.mkdir(parents=True)
|
path.parent.mkdir(parents=True)
|
||||||
path.write_text(dump_yaml(dict(self)))
|
path.write_text(dump_yaml(dict(self), root_path=path.parent))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load(cls, path: Path):
|
def load(cls, path: Path):
|
||||||
with path.open("r") as fp:
|
with path.open("r") as fp:
|
||||||
return cls(load_yaml(fp))
|
return cls(load_yaml(fp, root_path=path.parent))
|
||||||
@@ -2,7 +2,7 @@ from pathlib import Path
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from src.automations.tags import TagManager
|
from src.automations.tags import TagManager
|
||||||
from src.yaml_serializer import load_yaml, IncludedYaml
|
from src.yaml_serializer import IncludedYaml, load_yaml
|
||||||
|
|
||||||
|
|
||||||
class HassConfig:
|
class HassConfig:
|
||||||
@@ -12,7 +12,7 @@ class HassConfig:
|
|||||||
@property
|
@property
|
||||||
def configurations(self) -> dict:
|
def configurations(self) -> dict:
|
||||||
with self.get_configuration_path().open("r") as f:
|
with self.get_configuration_path().open("r") as f:
|
||||||
return dict(load_yaml(f)) # type: ignore
|
return dict(load_yaml(f, root_path=self.root_path)) # type: ignore
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def automation_tags(self) -> TagManager:
|
def automation_tags(self) -> TagManager:
|
||||||
@@ -35,7 +35,7 @@ class HassConfig:
|
|||||||
if "automation" in config:
|
if "automation" in config:
|
||||||
automation_ref = config["automation"]
|
automation_ref = config["automation"]
|
||||||
if isinstance(automation_ref, IncludedYaml):
|
if isinstance(automation_ref, IncludedYaml):
|
||||||
return self.root_path / automation_ref.path_str
|
return automation_ref.path
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
return self.root_path / "automations.yaml"
|
return self.root_path / "automations.yaml"
|
||||||
@@ -51,6 +51,3 @@ class HassConfig:
|
|||||||
|
|
||||||
def get_configuration_path(self) -> Path:
|
def get_configuration_path(self) -> Path:
|
||||||
return self.root_path / "configuration.yaml"
|
return self.root_path / "configuration.yaml"
|
||||||
|
|
||||||
def save_tags(self, tags: TagManager):
|
|
||||||
tags.save(self.get_automation_tags_path())
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from yaml.nodes import Node
|
from yaml.nodes import Node
|
||||||
from yaml.representer import BaseRepresenter
|
|
||||||
|
|
||||||
from .manager import YamlManager
|
from .manager import YamlManager
|
||||||
|
|
||||||
@@ -15,15 +16,21 @@ class IncludedYaml(NamedTuple):
|
|||||||
this data type will represent it.
|
this data type will represent it.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
path_str: str # original node path
|
path: Path # original node path
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYaml"):
|
def to_yaml(cls, representer, node: "IncludedYaml"):
|
||||||
return representer.represent_scalar("!include", node.path_str)
|
return representer.represent_scalar(
|
||||||
|
"!include", str(node.path.relative_to(representer.root_path))
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_yaml(cls, constructor: YamlManager.Loader, node: Node):
|
def from_yaml(cls, constructor, node: Node):
|
||||||
return cls(node.value)
|
return cls(constructor.root_path / node.value)
|
||||||
|
|
||||||
|
def to_normalized_json(self):
|
||||||
|
with self.path.open("r") as fp:
|
||||||
|
return load_yaml(fp, root_path=self.path.parent)
|
||||||
|
|
||||||
|
|
||||||
# TODO: add support for these
|
# TODO: add support for these
|
||||||
@@ -36,11 +43,11 @@ class SecretValue(NamedTuple):
|
|||||||
value: str
|
value: str
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def to_yaml(cls, representer: BaseRepresenter, node: "SecretValue"):
|
def to_yaml(cls, representer, node: "SecretValue"):
|
||||||
return representer.represent_scalar("!secret", node.name)
|
return representer.represent_scalar("!secret", node.name)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_yaml(cls, _constructor, node: Node):
|
def from_yaml(cls, constructor, node: Node):
|
||||||
return cls(node.value, "n/a")
|
return cls(node.value, "n/a")
|
||||||
|
|
||||||
def to_normalized_json(self):
|
def to_normalized_json(self):
|
||||||
@@ -52,16 +59,32 @@ class SecretValue(NamedTuple):
|
|||||||
"!include_dir_merge_list",
|
"!include_dir_merge_list",
|
||||||
"!include_dir_named",
|
"!include_dir_named",
|
||||||
"!include_dir_merge_named",
|
"!include_dir_merge_named",
|
||||||
"!env_var",
|
|
||||||
)
|
)
|
||||||
class IncludedYamlDir(NamedTuple):
|
class IncludedYamlDir(NamedTuple):
|
||||||
original_tag: str # original node name
|
original_tag: str # original node name
|
||||||
name: str # original node name
|
path: Path # original node name
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYamlDir"):
|
def to_yaml(cls, representer, node: "IncludedYamlDir"):
|
||||||
return representer.represent_scalar(f"!{node.original_tag}", node.name)
|
return representer.represent_scalar(
|
||||||
|
f"!{node.original_tag}", str(node.path.relative_to(representer.root_path))
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_yaml(cls, _constructor, node: Node):
|
def from_yaml(cls, constructor, node: Node):
|
||||||
return cls(node.tag[1:], node.value)
|
return cls(node.tag[1:], constructor.root_path / node.value)
|
||||||
|
|
||||||
|
|
||||||
|
@YamlManager.as_constructor(
|
||||||
|
"!env_var",
|
||||||
|
)
|
||||||
|
class EnvVar(NamedTuple):
|
||||||
|
value: str # original node name
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def to_yaml(cls, representer, node: "EnvVar"):
|
||||||
|
return representer.represent_scalar("!env_var", node.value)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_yaml(cls, constructor, node: Node):
|
||||||
|
return cls(node.value)
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
import yaml
|
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional, TextIO
|
from typing import Optional, TextIO
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
|
||||||
class YamlSafeDumper(yaml.SafeDumper):
|
class YamlSafeDumper(yaml.SafeDumper):
|
||||||
def __init__(self, stream: TextIO, root_path: Optional[Path] = None) -> None:
|
def __init__(self, stream: TextIO, root_path: Optional[Path] = None) -> None:
|
||||||
|
|||||||
@@ -1,15 +1,13 @@
|
|||||||
import yaml
|
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional, Union, TextIO
|
from typing import Optional, TextIO
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
|
||||||
class YamlSafeLoader(yaml.SafeLoader):
|
class YamlSafeLoader(yaml.SafeLoader):
|
||||||
def __init__(self, stream: Union[TextIO, Path], root_path: Optional[Path] = None) -> None:
|
def __init__(self, stream: TextIO, root_path: Optional[Path] = None) -> None:
|
||||||
super().__init__(stream)
|
super().__init__(stream)
|
||||||
if isinstance(stream, Path) and root_path is None:
|
if root_path is None:
|
||||||
self.root_path = stream.parent
|
|
||||||
elif root_path is None:
|
|
||||||
self.root_path = Path.cwd()
|
self.root_path = Path.cwd()
|
||||||
else:
|
else:
|
||||||
self.root_path = root_path
|
self.root_path = root_path
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
import io
|
import io
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Optional, TextIO, TypeVar, Union
|
from typing import Any, TextIO, Type, TypeVar, Union
|
||||||
|
|
||||||
from .dumper import YamlSafeDumper
|
from .dumper import YamlSafeDumper
|
||||||
from .loader import YamlSafeLoader
|
from .loader import YamlSafeLoader
|
||||||
from .types import Constructor
|
from .types import Constructor
|
||||||
|
|
||||||
ConstructorT = TypeVar("ConstructorT", bound=Constructor)
|
ConstructorT = TypeVar("ConstructorT", bound=Type[Constructor])
|
||||||
|
|
||||||
|
|
||||||
class YamlManager:
|
class YamlManager:
|
||||||
@@ -23,7 +24,7 @@ class YamlManager:
|
|||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load_yaml(cls, yaml_stream: TextIO, root_path: Optional[Path] = None) -> Union[dict, list]:
|
def load_yaml(cls, yaml_stream: TextIO, root_path: Path) -> Union[dict, list]:
|
||||||
"""Load yaml from string using some hass-compliant yaml strings
|
"""Load yaml from string using some hass-compliant yaml strings
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -39,7 +40,7 @@ class YamlManager:
|
|||||||
loader.dispose()
|
loader.dispose()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def dump_yaml(cls, obj: Any, root_path: Optional[Path] = None) -> str:
|
def dump_yaml(cls, obj: Any, root_path: Path) -> str:
|
||||||
"""Convert a python object to a hass-compliant yaml string
|
"""Convert a python object to a hass-compliant yaml string
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|||||||
@@ -1,14 +1,16 @@
|
|||||||
from typing import Protocol, TypeVar
|
from typing import Protocol
|
||||||
|
|
||||||
from yaml.nodes import Node
|
from yaml.nodes import Node
|
||||||
from yaml.representer import BaseRepresenter
|
|
||||||
|
from .dumper import YamlSafeDumper
|
||||||
|
from .loader import YamlSafeLoader
|
||||||
|
|
||||||
|
|
||||||
class Constructor(Protocol):
|
class Constructor(Protocol):
|
||||||
@classmethod
|
@classmethod
|
||||||
def to_yaml(cls, representer: BaseRepresenter, node: "Constructor"):
|
def to_yaml(cls, representer: YamlSafeDumper, node: "Constructor"):
|
||||||
...
|
...
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_yaml(cls, _constructor, node: Node):
|
def from_yaml(cls, constructor: YamlSafeLoader, node: Node):
|
||||||
...
|
...
|
||||||
@@ -6,7 +6,11 @@ from fastapi.testclient import TestClient
|
|||||||
|
|
||||||
from src.api.app import make_app
|
from src.api.app import make_app
|
||||||
from src.automations.types import Automation, ExtenededAutomation
|
from src.automations.types import Automation, ExtenededAutomation
|
||||||
from tests.utils import HA_CONFIG2_EXAMPLE, HA_CONFIG6_EXAMPLE, get_example_automation_loader
|
from tests.utils import (
|
||||||
|
HA_CONFIG2_EXAMPLE,
|
||||||
|
HA_CONFIG6_EXAMPLE,
|
||||||
|
get_example_automation_loader,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class BaseTestCase(TestCase):
|
class BaseTestCase(TestCase):
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from src.automations.types import ExtenededAutomation
|
|
||||||
from src.automations.errors import DBDataError
|
from src.automations.errors import DBDataError
|
||||||
|
from src.automations.types import ExtenededAutomation
|
||||||
|
|
||||||
from .utils import TestWithDB
|
from .utils import TestWithDB
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -229,7 +229,8 @@ class manager_tests(TestWithDB):
|
|||||||
automation_manager.get("newlymade")
|
automation_manager.get("newlymade")
|
||||||
|
|
||||||
data = yaml_serializer.load_yaml(
|
data = yaml_serializer.load_yaml(
|
||||||
(config_path / "automations/include_dir_merge_list/sub/list2.yaml").open("r")
|
(config_path / "automations/include_dir_merge_list/sub/list2.yaml").open("r"),
|
||||||
|
root_path=config_path,
|
||||||
)
|
)
|
||||||
self.assertIsInstance(data, list)
|
self.assertIsInstance(data, list)
|
||||||
|
|
||||||
@@ -254,7 +255,8 @@ class manager_tests(TestWithDB):
|
|||||||
automation_manager.get("newlymade")
|
automation_manager.get("newlymade")
|
||||||
|
|
||||||
data = yaml_serializer.load_yaml(
|
data = yaml_serializer.load_yaml(
|
||||||
(config_path / "automations/include_dir_list/home/down.yaml").open("r")
|
(config_path / "automations/include_dir_list/home/down.yaml").open("r"),
|
||||||
|
config_path,
|
||||||
)
|
)
|
||||||
self.assertIsInstance(data, dict)
|
self.assertIsInstance(data, dict)
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import io
|
import io
|
||||||
|
from pathlib import Path
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
|
||||||
from src.yaml_serializer import dump_yaml, load_yaml
|
|
||||||
from src.yaml_serializer import (
|
from src.yaml_serializer import (
|
||||||
IncludedYaml,
|
IncludedYaml,
|
||||||
IncludedYamlDir,
|
IncludedYamlDir,
|
||||||
SecretValue,
|
SecretValue,
|
||||||
|
dump_yaml,
|
||||||
|
load_yaml,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -14,23 +16,29 @@ class dumping_yamls_tests(TestCase):
|
|||||||
yaml = dump_yaml(
|
yaml = dump_yaml(
|
||||||
{
|
{
|
||||||
"automations": IncludedYaml(
|
"automations": IncludedYaml(
|
||||||
"automations.yaml",
|
Path.cwd() / "automations.yaml",
|
||||||
)
|
)
|
||||||
}
|
},
|
||||||
|
Path.cwd(),
|
||||||
)
|
)
|
||||||
self.assertEqual(yaml, "automations: !include 'automations.yaml'\n")
|
self.assertEqual(yaml, "automations: !include 'automations.yaml'\n")
|
||||||
|
|
||||||
def test_dumping_secrets(self):
|
def test_dumping_secrets(self):
|
||||||
yaml = dump_yaml({"google_password": SecretValue("gpass", "supersec")})
|
yaml = dump_yaml({"google_password": SecretValue("gpass", "supersec")}, Path.cwd())
|
||||||
self.assertEqual(yaml, "google_password: !secret 'gpass'\n")
|
self.assertEqual(yaml, "google_password: !secret 'gpass'\n")
|
||||||
|
|
||||||
def test_dumping_include_dir_list(self):
|
def test_dumping_include_dir_list(self):
|
||||||
yaml = dump_yaml({"sensor": IncludedYamlDir("include_dir_list", "sensors/*yaml")})
|
yaml = dump_yaml(
|
||||||
self.assertEqual(yaml, "sensor: !include_dir_list 'sensors/*yaml'\n")
|
{"sensor": IncludedYamlDir("include_dir_list", Path.cwd() / "sensors")}, Path.cwd()
|
||||||
|
)
|
||||||
|
self.assertEqual(yaml, "sensor: !include_dir_list 'sensors'\n")
|
||||||
|
|
||||||
def test_dumping_include_dir_merge_list(self):
|
def test_dumping_include_dir_merge_list(self):
|
||||||
yaml = dump_yaml({"sensor": IncludedYamlDir("include_dir_merge_list", "sensors/*yaml")})
|
yaml = dump_yaml(
|
||||||
self.assertEqual(yaml, "sensor: !include_dir_merge_list 'sensors/*yaml'\n")
|
{"sensor": IncludedYamlDir("include_dir_merge_list", Path.cwd() / "sensors")},
|
||||||
|
Path.cwd(),
|
||||||
|
)
|
||||||
|
self.assertEqual(yaml, "sensor: !include_dir_merge_list 'sensors'\n")
|
||||||
|
|
||||||
def test_load_complex_automation_setup(self):
|
def test_load_complex_automation_setup(self):
|
||||||
yaml_obj = load_yaml(
|
yaml_obj = load_yaml(
|
||||||
@@ -41,19 +49,20 @@ class dumping_yamls_tests(TestCase):
|
|||||||
automation: !include automations/base.yaml
|
automation: !include automations/base.yaml
|
||||||
automation cools: !include_dir_merge_list automations/include_dir_merge_list
|
automation cools: !include_dir_merge_list automations/include_dir_merge_list
|
||||||
"""
|
"""
|
||||||
)
|
),
|
||||||
|
Path.cwd(),
|
||||||
)
|
)
|
||||||
self.assertDictEqual(
|
self.assertDictEqual(
|
||||||
dict(yaml_obj),
|
dict(yaml_obj),
|
||||||
{
|
{
|
||||||
"automation manual": IncludedYamlDir(
|
"automation manual": IncludedYamlDir(
|
||||||
"include_dir_list", "./automations/include_dir_list"
|
"include_dir_list", Path.cwd() / "automations/include_dir_list"
|
||||||
),
|
),
|
||||||
"automation ui": IncludedYaml("automations/ui.yaml"),
|
"automation ui": IncludedYaml(Path.cwd() / "automations/ui.yaml"),
|
||||||
"automation cools": IncludedYamlDir(
|
"automation cools": IncludedYamlDir(
|
||||||
"include_dir_merge_list", "automations/include_dir_merge_list"
|
"include_dir_merge_list", Path.cwd() / "automations/include_dir_merge_list"
|
||||||
),
|
),
|
||||||
"automation": IncludedYaml("automations/base.yaml"),
|
"automation": IncludedYaml(Path.cwd() / "automations/base.yaml"),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -71,10 +80,11 @@ class dumping_yamls_tests(TestCase):
|
|||||||
bobIsNull:
|
bobIsNull:
|
||||||
bobIsNull2: null
|
bobIsNull2: null
|
||||||
google_password: !secret gpass
|
google_password: !secret gpass
|
||||||
sensor: !include_dir_list sensors/*yaml
|
sensor: !include_dir_list sensors
|
||||||
automations: !include automations.yaml
|
automations: !include automations.yaml
|
||||||
"""
|
"""
|
||||||
)
|
),
|
||||||
|
Path.cwd(),
|
||||||
)
|
)
|
||||||
self.assertDictEqual(
|
self.assertDictEqual(
|
||||||
dict(yaml_obj),
|
dict(yaml_obj),
|
||||||
@@ -85,12 +95,12 @@ class dumping_yamls_tests(TestCase):
|
|||||||
"bobIsNull": None,
|
"bobIsNull": None,
|
||||||
"bobIsNull2": None,
|
"bobIsNull2": None,
|
||||||
"google_password": SecretValue("gpass", "n/a"),
|
"google_password": SecretValue("gpass", "n/a"),
|
||||||
"sensor": IncludedYamlDir("include_dir_list", "sensors/*yaml"),
|
"sensor": IncludedYamlDir("include_dir_list", Path.cwd() / "sensors"),
|
||||||
"automations": IncludedYaml("automations.yaml"),
|
"automations": IncludedYaml(Path.cwd() / "automations.yaml"),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
dump_yaml(yaml_obj),
|
dump_yaml(yaml_obj, Path.cwd()),
|
||||||
"""name: cool
|
"""name: cool
|
||||||
date: '10:00'
|
date: '10:00'
|
||||||
bools:
|
bools:
|
||||||
@@ -101,7 +111,7 @@ bools:
|
|||||||
bobIsNull: null
|
bobIsNull: null
|
||||||
bobIsNull2: null
|
bobIsNull2: null
|
||||||
google_password: !secret 'gpass'
|
google_password: !secret 'gpass'
|
||||||
sensor: !include_dir_list 'sensors/*yaml'
|
sensor: !include_dir_list 'sensors'
|
||||||
automations: !include 'automations.yaml'
|
automations: !include 'automations.yaml'
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
+7
-5
@@ -6,7 +6,7 @@ from typing import List, Literal, Optional, Tuple
|
|||||||
from src.automations.manager import AutomationManager
|
from src.automations.manager import AutomationManager
|
||||||
from src.automations.types import BaseAutomation
|
from src.automations.types import BaseAutomation
|
||||||
from src.hass_config.loader import HassConfig
|
from src.hass_config.loader import HassConfig
|
||||||
from src.yaml_serializer import dump_yaml, IncludedYaml
|
from src.yaml_serializer import IncludedYaml, dump_yaml
|
||||||
|
|
||||||
THIS_FOLDER = Path(__file__).parent
|
THIS_FOLDER = Path(__file__).parent
|
||||||
SAMPLES_FOLDER = THIS_FOLDER / "samples"
|
SAMPLES_FOLDER = THIS_FOLDER / "samples"
|
||||||
@@ -96,11 +96,13 @@ def create_dummy_config_folder(
|
|||||||
configuration_yaml["automation"] = auto_prims
|
configuration_yaml["automation"] = auto_prims
|
||||||
elif automation_in_conifguration_mode == "include":
|
elif automation_in_conifguration_mode == "include":
|
||||||
configuration_yaml["automation"] = IncludedYaml(
|
configuration_yaml["automation"] = IncludedYaml(
|
||||||
"automations.yaml",
|
root_folder / "automations.yaml",
|
||||||
)
|
)
|
||||||
if automation_in_conifguration_mode != "inline":
|
if automation_in_conifguration_mode != "inline":
|
||||||
(root_folder / "automations.yaml").write_text(dump_yaml(auto_prims))
|
(root_folder / "automations.yaml").write_text(dump_yaml(auto_prims, root_path=root_folder))
|
||||||
if secrets is not None:
|
if secrets is not None:
|
||||||
(root_folder / "secrets.yaml").write_text(dump_yaml(secrets))
|
(root_folder / "secrets.yaml").write_text(dump_yaml(secrets, root_path=root_folder))
|
||||||
(root_folder / "configuration.yaml").write_text(dump_yaml(configuration_yaml))
|
(root_folder / "configuration.yaml").write_text(
|
||||||
|
dump_yaml(configuration_yaml, root_path=root_folder)
|
||||||
|
)
|
||||||
return root_folder
|
return root_folder
|
||||||
Reference in new issue
Block a user