noop yamlsupport for all directives

This commit is contained in:
ari committed 2022-08-25 10:15:26 -04:00
1 parent 97521097dc
commit f4fe036480
37 files changed
+632 -23

No files matched your search

+3 -3
View File
@@ -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}!")
+115 -10
View File
@@ -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(
@@ -0,0 +1,23 @@
- id: "ui1"
alias: UI 1
description: Example
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
- id: "ui2"
alias: UI 2
description: ""
trigger:
- platform: sun
event: sunrise
offset: 0
condition: []
action: []
mode: single
@@ -0,0 +1,18 @@
# Loads default set of integrations. Do not remove.
default_config:
# Text to speech
tts:
- platform: google_translate
# automation manual: !include_dir_list ./automations/include_dir_list
# automation ui: !include automations/ui.yaml
# automation: !include automations/base.yaml
# automation cools: !include_dir_merge_list automations/include_dir_merge_list
script: !include scripts.yaml
scene: !include scenes.yaml
homeassistant:
packages:
ha:
automation: !include automations/ui.yaml
@@ -0,0 +1,23 @@
- 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
@@ -0,0 +1,14 @@
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: {}
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,7 @@
- id: "mlist1"
alias: Merged List 1
description: ""
trigger: []
condition: []
action: []
mode: single
@@ -0,0 +1,14 @@
- id: "mlist2"
alias: Merged List 2
description: ""
trigger: []
condition: []
action: []
mode: single
- id: "mlist3"
alias: Merged List 3
description: ""
trigger: []
condition: []
action: []
mode: single
@@ -0,0 +1,14 @@
- 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
@@ -0,0 +1,23 @@
- id: "ui1"
alias: UI 1
description: Example
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
- id: "ui2"
alias: UI 2
description: ""
trigger:
- platform: sun
event: sunrise
offset: 0
condition: []
action: []
mode: single
@@ -0,0 +1,23 @@
- 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
@@ -0,0 +1,14 @@
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: {}
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,7 @@
- id: "mlist1"
alias: Merged List 1
description: ""
trigger: []
condition: []
action: []
mode: single
@@ -0,0 +1,14 @@
- id: "mlist2"
alias: Merged List 2
description: ""
trigger: []
condition: []
action: []
mode: single
- id: "mlist3"
alias: Merged List 3
description: ""
trigger: []
condition: []
action: []
mode: single
@@ -0,0 +1,14 @@
- 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
@@ -0,0 +1,23 @@
- id: "ui1"
alias: UI 1
description: Example
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
- id: "ui2"
alias: UI 2
description: ""
trigger:
- platform: sun
event: sunrise
offset: 0
condition: []
action: []
mode: single
@@ -0,0 +1,23 @@
- 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
@@ -0,0 +1,14 @@
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: {}
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,7 @@
- id: "mlist1"
alias: Merged List 1
description: ""
trigger: []
condition: []
action: []
mode: single
@@ -0,0 +1,14 @@
- id: "mlist2"
alias: Merged List 2
description: ""
trigger: []
condition: []
action: []
mode: single
- id: "mlist3"
alias: Merged List 3
description: ""
trigger: []
condition: []
action: []
mode: single
@@ -0,0 +1,14 @@
- 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
@@ -0,0 +1,23 @@
- id: "ui1"
alias: UI 1
description: Example
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
- id: "ui2"
alias: UI 2
description: ""
trigger:
- platform: sun
event: sunrise
offset: 0
condition: []
action: []
mode: single
@@ -0,0 +1,18 @@
# Loads default set of integrations. Do not remove.
default_config:
# Text to speech
tts:
- platform: google_translate
# automation manual: !include_dir_list ./automations/include_dir_list
# automation ui: !include automations/ui.yaml
# automation: !include automations/base.yaml
# automation cools: !include_dir_merge_list automations/include_dir_merge_list
script: !include scripts.yaml
scene: !include scenes.yaml
homeassistant:
packages:
ha:
automation: !include automations/ui.yaml
@@ -0,0 +1,18 @@
# Loads default set of integrations. Do not remove.
default_config:
# Text to speech
tts:
- platform: google_translate
# automation manual: !include_dir_list ./automations/include_dir_list
# automation ui: !include automations/ui.yaml
# automation: !include automations/base.yaml
# automation cools: !include_dir_merge_list automations/include_dir_merge_list
script: !include scripts.yaml
scene: !include scenes.yaml
homeassistant:
packages:
ha:
automation: !include automations/ui.yaml
@@ -0,0 +1,19 @@
# Loads default set of integrations. Do not remove.
default_config:
# Text to speech
tts:
- platform: google_translate
# automation manual: !include_dir_list ./automations/include_dir_list
# automation ui: !include automations/ui.yaml
# automation: !include automations/base.yaml
# automation cools: !include_dir_merge_list automations/include_dir_merge_list
script: !include scripts.yaml
scene: !include scenes.yaml
homeassistant:
packages: !include_dir_merge_named packages
packages base:
ha:
automation: !include automations/ui.yaml
@@ -0,0 +1,20 @@
- id: "1616887975473"
alias: Home Assistant Off
description: ""
trigger:
- platform: homeassistant
event: shutdown
condition: []
action:
- !include parts.yaml
mode: single
- id: "1616888026264"
alias: Home Assistant On
description: ""
trigger:
- platform: homeassistant
event: start
condition: []
action:
- !include parts.yaml
mode: single
@@ -0,0 +1,4 @@
# empty configuration file
cloud:
automation base: !include automations.yaml
+5
View File
@@ -0,0 +1,5 @@
device_id: das2qdasdasdasadaasd
domain: mobile_app
type: notify
title: Hassio Status
message: Hassio Is turning off...
@@ -15,6 +15,7 @@ from tests.utils import (
HA_CONFIG4_EXAMPLE,
HA_CONFIG5_EXAMPLE,
HA_CONFIG6_EXAMPLE,
HA_CONFIG9_EXAMPLE,
HA_CONFIG_EXAMPLE,
)
@@ -102,6 +103,18 @@ class loader_tests(TestCase):
),
)
def test_include_in_automation(self):
automations = list(
load_automation_path(
HA_CONFIG9_EXAMPLE,
HA_CONFIG9_EXAMPLE / "automations.yaml",
configuration_key="automation base",
tag_manager=TagManager({}),
)
)
self.assertEqual(len(automations), 2)
self.assertEqual(automations[1].action, automations[0].action)
def test_load_automation_empty_file(self):
file_path = Path(mktemp())
file_path.touch()
@@ -136,6 +149,7 @@ class loader_tests(TestCase):
(HA_CONFIG4_EXAMPLE, 1),
(HA_CONFIG5_EXAMPLE, 3),
(HA_CONFIG6_EXAMPLE, 4),
(HA_CONFIG9_EXAMPLE, 1),
]:
with self.subTest(ha_path=ha_path, expected=expected):
hass_config = HassConfig(ha_path)
+9 -10
View File
@@ -4,7 +4,8 @@ from unittest import TestCase
from src.yaml_serializer import (
IncludedYaml,
IncludedYamlDir,
IncludedYamlDirList,
IncludedYamlMergeListDir,
SecretValue,
dump_yaml,
load_yaml,
@@ -28,14 +29,12 @@ class dumping_yamls_tests(TestCase):
self.assertEqual(yaml, "google_password: !secret 'gpass'\n")
def test_dumping_include_dir_list(self):
yaml = dump_yaml(
{"sensor": IncludedYamlDir("include_dir_list", Path.cwd() / "sensors")}, Path.cwd()
)
yaml = dump_yaml({"sensor": IncludedYamlDirList(Path.cwd() / "sensors")}, Path.cwd())
self.assertEqual(yaml, "sensor: !include_dir_list 'sensors'\n")
def test_dumping_include_dir_merge_list(self):
yaml = dump_yaml(
{"sensor": IncludedYamlDir("include_dir_merge_list", Path.cwd() / "sensors")},
{"sensor": IncludedYamlMergeListDir(Path.cwd() / "sensors")},
Path.cwd(),
)
self.assertEqual(yaml, "sensor: !include_dir_merge_list 'sensors'\n")
@@ -55,12 +54,12 @@ class dumping_yamls_tests(TestCase):
self.assertDictEqual(
dict(yaml_obj),
{
"automation manual": IncludedYamlDir(
"include_dir_list", Path.cwd() / "automations/include_dir_list"
"automation manual": IncludedYamlDirList(
Path.cwd() / "automations/include_dir_list"
),
"automation ui": IncludedYaml(Path.cwd() / "automations/ui.yaml"),
"automation cools": IncludedYamlDir(
"include_dir_merge_list", Path.cwd() / "automations/include_dir_merge_list"
"automation cools": IncludedYamlMergeListDir(
Path.cwd() / "automations/include_dir_merge_list"
),
"automation": IncludedYaml(Path.cwd() / "automations/base.yaml"),
},
@@ -95,7 +94,7 @@ class dumping_yamls_tests(TestCase):
"bobIsNull": None,
"bobIsNull2": None,
"google_password": SecretValue("gpass", "n/a"),
"sensor": IncludedYamlDir("include_dir_list", Path.cwd() / "sensors"),
"sensor": IncludedYamlDirList(Path.cwd() / "sensors"),
"automations": IncludedYaml(Path.cwd() / "automations.yaml"),
},
)
+3
View File
@@ -16,6 +16,9 @@ HA_CONFIG3_EXAMPLE = SAMPLES_FOLDER / "config-3"
HA_CONFIG4_EXAMPLE = SAMPLES_FOLDER / "config-4"
HA_CONFIG5_EXAMPLE = SAMPLES_FOLDER / "config-5"
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"
def get_example_automation_loader(