added complete support for packages?

This commit is contained in:
ari committed 2022-08-27 13:58:41 -04:00
1 parent ac82a4c96c
commit f95dd75198
19 files changed
+365 -47

No files matched your search

+122 -19
View File
@@ -8,6 +8,9 @@ from src.utils import extract_files
from src.yaml_serializer import (
IncludedYaml,
IncludedYamlDir,
IncludedYamlDirList,
IncludedYamlDirMergedNamed,
IncludedYamlDirNamed,
dump_yaml,
load_yaml,
)
@@ -40,8 +43,10 @@ def load_and_iter_automations(
ref.configuration_key,
tag_manager,
)
else:
elif isinstance(ref, InlineAutomation):
yield from load_automation_inline_ref(hass_config.root_path, tag_manager, ref)
else:
raise AssertionError(f"ref has an invalid type of {type(ref)} = {ref}")
except InvalidAutomationFile as e:
logger.error(e)
@@ -58,7 +63,7 @@ def extract_automation_refs(
yield ref
if not found:
yield IncludedAutoamtion([""], IncludedYaml(hass_config.get_default_automation_path()))
yield IncludedAutoamtion([""], IncludedYaml(hass_config.root_path / "automations.yaml"))
def extract_automation_root_refs(
@@ -67,7 +72,7 @@ def extract_automation_root_refs(
for key, value in extract_automation_keys(hass_config):
logger.info(f"Loading automations from '{key}'")
if isinstance(value, IncludedYamlDir): # type: ignore
yield IncludedAutoamtion([key], value)
yield IncludedAutoamtion([key], value) # type: ignore
elif isinstance(value, list):
yield InlineAutomation(
configuration_key=[key],
@@ -79,20 +84,118 @@ def extract_automation_root_refs(
def extract_automation_package_refs(hass_config: HassConfig) -> AutomationExtractedIter:
for package_name, package_data in hass_config.pacakges.items():
if not isinstance(package_data, dict):
logger.warning(f"The package {package_name} is incorrectly configured!")
else:
if automations := package_data.get("automation", None):
logger.info(f"Loading automations from package '{package_name}'")
if isinstance(automations, IncludedYamlDir): # type: ignore
yield IncludedAutoamtion([package_name], automations)
elif isinstance(automations, list):
logger.warning(
f"Failed to load from '{package_name}' as no support for inline automations yet."
if packages_config := hass_config.homeassistant.get("packages", None):
if isinstance(packages_config, IncludedYaml):
packages = packages_config.to_normalized_json()
yield from extract_automation_inline_package_refs(
packages_config.path,
["homeassistant", "packages"],
packages,
)
elif isinstance(packages_config, IncludedYamlDirList):
raise AssertionError("cannot configure packages with 'included_dir_list' directive")
elif isinstance(packages_config, IncludedYamlDirNamed):
packages = packages_config.to_normalized_json(use_path_as_keys=True)
for package_path, package in packages.items():
yield from extract_automation_inline_package_refs(
package_path,
[],
{package_path.stem: package},
ignore_package_name=True,
)
elif isinstance(packages_config, IncludedYamlDirMergedNamed):
for (
pacakge_set_path,
package_set,
) in iter(packages_config):
if not isinstance(package_set, dict):
raise AssertionError(
f"expected {pacakge_set_path} to be a dictionary but got {type(package_set)}"
)
else:
logger.warning(f"found an invalid type in {package_name}!")
yield from extract_automation_inline_package_refs(
source_file=pacakge_set_path,
prefix_config_keys=[],
packages_config=package_set,
)
elif isinstance(packages_config, dict):
yield from extract_automation_inline_package_refs(
hass_config.root_path,
["homeassistant", "packages"],
packages_config,
)
else:
raise AssertionError("configurations.homeassistant.packages must be a dictionary!")
def extract_automation_inline_package_refs(
source_file: Path,
prefix_config_keys: ConfigurationKey,
packages_config: dict,
ignore_package_name=False,
) -> AutomationExtractedIter:
for package_name, package_data in packages_config.items():
if isinstance(package_data, IncludedYaml):
package_mapping = package_data.to_normalized_json()
yield from extract_automation_from_package(
package_data=package_mapping,
package_name=package_name,
source_file=source_file,
prefix_config_keys=prefix_config_keys,
ignore_package_name=ignore_package_name,
)
elif isinstance(package_data, IncludedYamlDirList):
raise NotImplementedError(package_data)
elif isinstance(package_data, IncludedYamlDirNamed):
for package_key_path in package_data.to_normalized_json(True).keys():
if package_key_path.stem == "automation":
yield IncludedAutoamtion(
configuration_key=[package_name], ref=IncludedYaml(package_key_path)
)
elif isinstance(package_data, IncludedYamlDirMergedNamed):
for data_path, data_set in iter(package_data):
yield from extract_automation_from_package(
package_name=package_name,
package_data=data_set,
source_file=data_path,
prefix_config_keys=[],
ignore_package_name=True,
)
elif isinstance(package_data, dict):
yield from extract_automation_from_package(
package_data=package_data,
package_name=package_name,
source_file=source_file,
prefix_config_keys=prefix_config_keys,
ignore_package_name=ignore_package_name,
)
else:
raise AssertionError(
f"configurations.homeassistant.packages[{package_name}] must be a dictionary!"
)
def extract_automation_from_package(
package_name: str,
package_data: dict,
source_file: Path,
prefix_config_keys: ConfigurationKey,
ignore_package_name=False,
) -> AutomationExtractedIter:
if automations := package_data.get("automation", None):
logger.info(f"Loading automations from package '{package_name}'")
suffix_keys = [package_name] if not ignore_package_name else []
if isinstance(automations, IncludedYamlDir): # type: ignore
yield IncludedAutoamtion(suffix_keys, automations)
elif isinstance(automations, list):
yield InlineAutomation(
source_file=source_file,
configuration_key=[*prefix_config_keys, *suffix_keys, "automation"],
automations=automations,
)
else:
logger.warning(
f"found an invalid type in {package_name}! automations must be included or inlined as a list"
)
def get_base_automation_key(hass_config: HassConfig) -> Tuple[ConfigurationKey, str]:
@@ -137,7 +240,7 @@ def load_automation_inline_ref(
**clean_automation(auto),
tags=tag_manager.get(auto["id"], {}),
configuration_key=inline_automation.configuration_key,
source_file=str(inline_automation.source_file.relative_to(root_path)),
source_file=str(inline_automation.source_file.resolve().relative_to(root_path)),
source_file_type="inline",
)
except Exception as err:
@@ -187,7 +290,7 @@ def load_automation_path(
**clean_automation(automations),
tags=tag_manager.get(automations["id"], {}),
configuration_key=configuration_key,
source_file=str(automation_path.relative_to(root_path)),
source_file=str(automation_path.resolve().relative_to(root_path)),
source_file_type="obj",
)
except Exception as err:
@@ -206,7 +309,7 @@ def load_automation_path(
**clean_automation(automation),
tags=tag_manager.get(automation["id"], {}),
configuration_key=configuration_key,
source_file=str(automation_path.relative_to(root_path)),
source_file=str(automation_path.resolve().relative_to(root_path)),
source_file_type="list",
)
except Exception as err:
+8
View File
@@ -127,9 +127,17 @@ class AutomationManager:
)
automation_path.write_text(dump_yaml(objs, self.hass_config.root_path))
elif automation.source_file_type == "inline":
if not isinstance(objs, dict):
raise AssertionError(
f"expected {automation.source_file} to be a dict instead found {type(objs)}"
)
update_obj = objs
for key in automation.configuration_key:
update_obj = update_obj[key]
if not isinstance(update_obj, list):
raise AssertionError(
f"expected {automation.source_file}@{automation.configuration_key} to be a list instead found {type(update_obj)}"
)
update_or_append_to_automation_list(
objs=update_obj,
automation=automation,
-12
View File
@@ -25,18 +25,6 @@ class HassConfig:
raise AssertionError("configurations.homeassistant must be a dictionary!")
return {}
@property
def pacakges(self) -> dict:
if packages_config := self.homeassistant.get("packages", None):
if isinstance(packages_config, IncludedYamlDir): # type: ignore
packages_config = packages_config.to_normalized_json()
if isinstance(packages_config, dict):
return packages_config
else:
raise AssertionError("configurations.homeassistant.packages must be a dictionary!")
return {}
@property
def automation_tags(self) -> TagManager:
tag_path = self.get_automation_tags_path()
+6 -3
View File
@@ -78,9 +78,12 @@ class IncludedYamlDirMergedNamed(NamedTuple):
def from_yaml(cls, constructor, node: Node):
return cls(constructor.root_path / node.value)
def __iter__(self):
return load_dir_yaml(self.path)
def to_normalized_json(self):
out = {}
for path, data in load_dir_yaml(self.path):
for path, data in self:
overlap = set(data.keys()).intersection(set(out.keys()))
if len(overlap) > 0:
raise AssertionError(
@@ -111,11 +114,11 @@ class IncludedYamlDirNamed(NamedTuple):
def from_yaml(cls, constructor, node: Node):
return cls(constructor.root_path / node.value)
def to_normalized_json(self):
def to_normalized_json(self, use_path_as_keys=False):
out = {}
for path, data in load_dir_yaml(self.path):
if out.get(path.name, None) is None:
out[path.stem] = data
out[path.stem if not use_path_as_keys else path] = data
else:
raise AssertionError(
f"expected {path.name} to be a unique file name but found a second file name in {path}"
@@ -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,31 @@
# 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
automation:
- id: "inline-auto"
alias: inline-auto
description: Example package auto
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
homeassistant:
packages: !include packages.yaml
@@ -0,0 +1,2 @@
ha: !include packages/ha.yaml
sub: !include_dir_named packages/sub
@@ -0,0 +1 @@
automation: !include ../automations/ui.yaml
@@ -0,0 +1,13 @@
- id: "package2"
alias: package ha
description: Example package auto
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
@@ -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,31 @@
# 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
automation:
- id: "inline-auto"
alias: inline-auto
description: Example package auto
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
homeassistant:
packages: !include packages.yaml
+17
View File
@@ -0,0 +1,17 @@
ha: !include packages/ha.yaml
sub: !include_dir_merge_named packages/sub
secret_ones:
automation:
- id: "inline-package-auto"
alias: package ha
description: Example package auto
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
@@ -0,0 +1 @@
automation: !include ../automations/ui.yaml
@@ -0,0 +1,14 @@
automation:
- id: "package2"
alias: package ha
description: Example package auto
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: counter.increment
data: {}
target:
entity_id: counter.up_times
+2 -2
View File
@@ -7,9 +7,9 @@ from fastapi.testclient import TestClient
from src.api.app import make_app
from src.automations.types import Automation, ExtenededAutomation
from tests.utils import (
HA_CONFIG10_EXAMPLE,
HA_CONFIG2_EXAMPLE,
HA_CONFIG6_EXAMPLE,
HA_CONFIG10_EXAMPLE,
get_example_automation_loader,
)
@@ -389,7 +389,7 @@ class automation_update_split_config_tests(automation_update_tests):
class automation_update_split_config_and_inline_config_tests(automation_update_tests):
test_params = {
"total": 3,
"total": 4,
"modify_item": 0,
"consitency_tests": {},
}
@@ -19,6 +19,8 @@ from tests.utils import (
HA_CONFIG8_EXAMPLE,
HA_CONFIG9_EXAMPLE,
HA_CONFIG10_EXAMPLE,
HA_CONFIG11_EXAMPLE,
HA_CONFIG12_EXAMPLE,
HA_CONFIG_EXAMPLE,
)
@@ -153,9 +155,11 @@ class loader_tests(TestCase):
(HA_CONFIG5_EXAMPLE, 3),
(HA_CONFIG6_EXAMPLE, 4),
(HA_CONFIG7_EXAMPLE, 1),
(HA_CONFIG8_EXAMPLE, 1),
(HA_CONFIG8_EXAMPLE, 2),
(HA_CONFIG9_EXAMPLE, 1),
(HA_CONFIG10_EXAMPLE, 2),
(HA_CONFIG10_EXAMPLE, 3),
(HA_CONFIG11_EXAMPLE, 3),
(HA_CONFIG12_EXAMPLE, 4),
]:
with self.subTest(config_name=ha_path.name, expected=expected):
hass_config = HassConfig(ha_path)
@@ -14,6 +14,8 @@ from tests.utils import (
HA_CONFIG6_EXAMPLE,
HA_CONFIG8_EXAMPLE,
HA_CONFIG10_EXAMPLE,
HA_CONFIG11_EXAMPLE,
HA_CONFIG12_EXAMPLE,
HA_CONFIG_EXAMPLE,
create_copy_of_example_config,
)
@@ -324,11 +326,53 @@ class manager_tests(TestWithDB):
{},
)
def test_load_pacakges(self):
def test_load_pacakges_include_dir_named(self):
_, automation_manager = self.get_manager(HA_CONFIG8_EXAMPLE)
automation_manager.reload()
auto = automation_manager.get("package-sub")
self.assertEqual(auto.configuration_key, ["pacakage2"])
with self.subTest(name="inline"):
auto = automation_manager.get("package-sub")
self.assertEqual(auto.source_file_type, "inline")
self.assertEqual(auto.source_file, "packages/sub/package2.yaml")
self.assertEqual(auto.configuration_key, ["automation"])
with self.subTest(name="sub !inlcude"):
auto = automation_manager.get("ui1")
self.assertEqual(auto.source_file, "automations/ui.yaml")
self.assertEqual(auto.source_file_type, "list")
self.assertEqual(auto.configuration_key, [])
def test_load_pacakges_include_dir_merge_named(self):
_, automation_manager = self.get_manager(HA_CONFIG10_EXAMPLE)
automation_manager.reload()
with self.subTest(name="inline"):
auto = automation_manager.get("package2")
self.assertEqual(auto.source_file_type, "inline")
self.assertEqual(auto.source_file, "packages/sub/package2.yaml")
self.assertEqual(auto.configuration_key, ["package2", "automation"])
with self.subTest(name="sub !inlcude"):
auto = automation_manager.get("ui1")
self.assertEqual(auto.source_file, "automations/ui.yaml")
self.assertEqual(auto.source_file_type, "list")
self.assertEqual(auto.configuration_key, ["ha"])
def test_load_pacakges_include(self):
_, automation_manager = self.get_manager(HA_CONFIG11_EXAMPLE)
automation_manager.reload()
with self.subTest(name="inline"):
auto = automation_manager.get("package2")
self.assertEqual(auto.source_file_type, "list")
self.assertEqual(auto.source_file, "packages/sub/automation.yaml")
self.assertEqual(auto.configuration_key, ["sub"])
with self.subTest(name="sub !inlcude"):
auto = automation_manager.get("ui1")
self.assertEqual(auto.source_file, "automations/ui.yaml")
self.assertEqual(auto.source_file_type, "list")
self.assertEqual(auto.configuration_key, ["ha"])
def test_load_inline_auto(self):
_, automation_manager = self.get_manager(HA_CONFIG10_EXAMPLE)
@@ -336,3 +380,19 @@ class manager_tests(TestWithDB):
auto = automation_manager.get("inline-auto")
self.assertEqual(auto.source_file_type, "inline")
self.assertEqual(auto.configuration_key, ["automation"])
def test_load_pacakges_nested_include_dir_merge_named(self):
_, automation_manager = self.get_manager(HA_CONFIG12_EXAMPLE)
automation_manager.reload()
with self.subTest(name="inline"):
auto = automation_manager.get("package2")
self.assertEqual(auto.source_file_type, "inline")
self.assertEqual(auto.source_file, "packages/sub/automation.yaml")
self.assertEqual(auto.configuration_key, ["automation"])
with self.subTest(name="sub !inlcude"):
auto = automation_manager.get("ui1")
self.assertEqual(auto.source_file, "automations/ui.yaml")
self.assertEqual(auto.source_file_type, "list")
self.assertEqual(auto.configuration_key, ["ha"])
@@ -51,9 +51,3 @@ class loader_tests(TestCase):
with self.subTest(name=folder.name):
hass_config = HassConfig(folder)
assert isinstance(hass_config.homeassistant, dict)
def test_load_packages(self):
for folder in SAMPLES_FOLDER.iterdir():
with self.subTest(name=folder.name):
hass_config = HassConfig(folder)
assert isinstance(hass_config.pacakges, dict)
+2
View File
@@ -20,6 +20,8 @@ HA_CONFIG7_EXAMPLE = SAMPLES_FOLDER / "config-7"
HA_CONFIG8_EXAMPLE = SAMPLES_FOLDER / "config-8"
HA_CONFIG9_EXAMPLE = SAMPLES_FOLDER / "config-9"
HA_CONFIG10_EXAMPLE = SAMPLES_FOLDER / "config-10"
HA_CONFIG11_EXAMPLE = SAMPLES_FOLDER / "config-11"
HA_CONFIG12_EXAMPLE = SAMPLES_FOLDER / "config-12"
def get_example_automation_loader(