noop yamlsupport for all directives
This commit is contained in:
1 parent
97521097dc
commit
f4fe036480
37 files changed
+632
-23
No files matched your search
@@ -28,7 +28,7 @@ def extract_automation_paths(
|
||||
for key, value in extract_automation_keys(hass_config):
|
||||
logger.info(f"Loading automations from '{key}'")
|
||||
found = True
|
||||
if isinstance(value, (IncludedYaml, IncludedYamlDir)):
|
||||
if isinstance(value, IncludedYamlDir):
|
||||
yield key, str(value.path.relative_to(hass_config.root_path))
|
||||
else:
|
||||
logger.warning(f"found an invalid type in {key}!")
|
||||
@@ -58,14 +58,14 @@ def get_base_automation_key(hass_config: HassConfig):
|
||||
|
||||
def extract_automation_keys(
|
||||
hass_config: HassConfig,
|
||||
) -> Iterator[Tuple[str, Union[IncludedYaml, IncludedYamlDir]]]:
|
||||
) -> Iterator[Tuple[str, IncludedYamlDir]]:
|
||||
for key, value in hass_config.configurations.items():
|
||||
if str(key).startswith("automation"):
|
||||
if isinstance(value, dict) or isinstance(value, list):
|
||||
raise NotImplementedError(
|
||||
"No support for automations baked into configuration.yaml! Please extract these into a seperate file"
|
||||
)
|
||||
elif isinstance(value, IncludedYaml) or isinstance(value, IncludedYamlDir):
|
||||
elif isinstance(value, IncludedYamlDir):
|
||||
yield key, value
|
||||
else:
|
||||
logger.warning(f"found an invalid type in {key}!")
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from pathlib import Path
|
||||
from typing import Iterator, NamedTuple, Tuple, Union
|
||||
|
||||
from yaml.nodes import Node
|
||||
|
||||
@@ -7,7 +8,14 @@ from .manager import YamlManager
|
||||
dump_yaml = YamlManager.dump_yaml
|
||||
load_yaml = YamlManager.load_yaml
|
||||
|
||||
from typing import NamedTuple
|
||||
|
||||
def load_dir_yaml(folder_or_file: Path) -> Iterator[Tuple[Path, dict | list]]:
|
||||
if folder_or_file.is_dir():
|
||||
for file in folder_or_file.iterdir():
|
||||
yield from load_dir_yaml(file)
|
||||
else:
|
||||
with folder_or_file.open("r") as fp:
|
||||
yield folder_or_file, load_yaml(fp, root_path=folder_or_file.parent)
|
||||
|
||||
|
||||
@YamlManager.as_constructor("!include")
|
||||
@@ -55,24 +63,121 @@ class SecretValue(NamedTuple):
|
||||
|
||||
|
||||
@YamlManager.as_constructor(
|
||||
"!include_dir_list",
|
||||
"!include_dir_merge_list",
|
||||
"!include_dir_named",
|
||||
"!include_dir_merge_named",
|
||||
)
|
||||
class IncludedYamlDir(NamedTuple):
|
||||
original_tag: str # original node name
|
||||
path: Path # original node name
|
||||
class IncludedYamlDirMergedNamed(NamedTuple):
|
||||
path: Path
|
||||
|
||||
@classmethod
|
||||
def to_yaml(cls, representer, node: "IncludedYamlDir"):
|
||||
def to_yaml(cls, representer, node: "IncludedYamlDirMergedNamed"):
|
||||
return representer.represent_scalar(
|
||||
f"!{node.original_tag}", str(node.path.relative_to(representer.root_path))
|
||||
f"!include_dir_merge_named", str(node.path.relative_to(representer.root_path))
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, constructor, node: Node):
|
||||
return cls(node.tag[1:], constructor.root_path / node.value)
|
||||
return cls(constructor.root_path / node.value)
|
||||
|
||||
def to_normalized_json(self):
|
||||
out = {}
|
||||
for path, data in load_dir_yaml(self.path):
|
||||
overlap = set(data.key()).intersection(set(out.keys()))
|
||||
if len(overlap) > 0:
|
||||
raise AssertionError(
|
||||
f"The keys {overlap} were found in more than 1 file, cannot handle overlapping keys due to undefined behavior expectation. See {path}"
|
||||
)
|
||||
|
||||
out = {
|
||||
**out,
|
||||
**data,
|
||||
}
|
||||
|
||||
return out
|
||||
|
||||
|
||||
@YamlManager.as_constructor(
|
||||
"!include_dir_named",
|
||||
)
|
||||
class IncludedYamlDirNamed(NamedTuple):
|
||||
path: Path
|
||||
|
||||
@classmethod
|
||||
def to_yaml(cls, representer, node: "IncludedYamlDirNamed"):
|
||||
return representer.represent_scalar(
|
||||
f"!include_dir_named", str(node.path.relative_to(representer.root_path))
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, constructor, node: Node):
|
||||
return cls(constructor.root_path / node.value)
|
||||
|
||||
def to_normalized_json(self):
|
||||
out = {}
|
||||
for path, data in load_dir_yaml(self.path):
|
||||
if out.get(path.name, None):
|
||||
out[path.name] = data
|
||||
else:
|
||||
raise AssertionError(
|
||||
f"expected {path.name} to be a unique file name but found a second file name in {path}"
|
||||
)
|
||||
|
||||
return out
|
||||
|
||||
|
||||
@YamlManager.as_constructor(
|
||||
"!include_dir_merge_list",
|
||||
)
|
||||
class IncludedYamlMergeListDir(NamedTuple):
|
||||
path: Path
|
||||
|
||||
@classmethod
|
||||
def to_yaml(cls, representer, node: "IncludedYamlMergeListDir"):
|
||||
return representer.represent_scalar(
|
||||
f"!include_dir_merge_list", str(node.path.relative_to(representer.root_path))
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, constructor, node: Node):
|
||||
return cls(constructor.root_path / node.value)
|
||||
|
||||
def to_normalized_json(self):
|
||||
out = []
|
||||
for path, data in load_dir_yaml(self.path):
|
||||
if isinstance(data, list):
|
||||
out.extend(data)
|
||||
else:
|
||||
raise AssertionError(f"expected {path} to be a list but got an object")
|
||||
|
||||
return out
|
||||
|
||||
|
||||
@YamlManager.as_constructor(
|
||||
"!include_dir_list",
|
||||
)
|
||||
class IncludedYamlDirList(NamedTuple):
|
||||
path: Path
|
||||
|
||||
@classmethod
|
||||
def to_yaml(cls, representer, node: "IncludedYamlDirList"):
|
||||
return representer.represent_scalar(
|
||||
f"!include_dir_list", str(node.path.relative_to(representer.root_path))
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, constructor, node: Node):
|
||||
return cls(constructor.root_path / node.value)
|
||||
|
||||
def to_normalized_json(self):
|
||||
return [data for _, data in load_dir_yaml(self.path)]
|
||||
|
||||
|
||||
IncludedYamlDir = (
|
||||
IncludedYamlDirMergedNamed
|
||||
| IncludedYamlDirNamed
|
||||
| IncludedYamlMergeListDir
|
||||
| IncludedYamlDirList
|
||||
| IncludedYaml
|
||||
)
|
||||
|
||||
|
||||
@YamlManager.as_constructor(
|
||||
|
||||
Reference in new issue
Block a user