inline automation support

This commit is contained in:
ari committed 2022-08-27 13:58:41 -04:00
1 parent fbbc5af12d
commit ac82a4c96c
7 files changed
+132 -48

No files matched your search

+2 -2
View File
@@ -1,6 +1,6 @@
from fastapi import APIRouter from fastapi import APIRouter
from src.automations.loader import extract_automation_paths from src.automations.loader import extract_automation_refs
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
@@ -18,7 +18,7 @@ def make_details_router(hass_config: HassConfig) -> APIRouter:
"tags": hass_config.get_automation_tags_path(), "tags": hass_config.get_automation_tags_path(),
"autos": { "autos": {
config_key: auto_file config_key: auto_file
for config_key, auto_file in extract_automation_paths(hass_config) for config_key, auto_file in extract_automation_refs(hass_config)
}, },
} }
+56 -25
View File
@@ -15,7 +15,13 @@ from src.yaml_serializer import (
from ..hass_config.loader import HassConfig from ..hass_config.loader import HassConfig
from .errors import InvalidAutomationFile from .errors import InvalidAutomationFile
from .tags import TagManager from .tags import TagManager
from .types import AutomationPathIter, ConfigurationKey, ExtenededAutomation from .types import (
AutomationExtractedIter,
ConfigurationKey,
ExtenededAutomation,
IncludedAutoamtion,
InlineAutomation,
)
logger = get_logger(__file__) logger = get_logger(__file__)
@@ -25,47 +31,54 @@ AUTOMATION_CONFIGURATION_KEY = "automation shortumation"
def load_and_iter_automations( def load_and_iter_automations(
hass_config: HassConfig, tag_manager: TagManager hass_config: HassConfig, tag_manager: TagManager
) -> Iterator[ExtenededAutomation]: ) -> Iterator[ExtenededAutomation]:
for config_key, p in extract_automation_paths(hass_config): for ref in extract_automation_refs(hass_config):
try: try:
yield from load_automation_path( if isinstance(ref, IncludedAutoamtion):
hass_config.root_path, yield from load_automation_path(
hass_config.root_path / p, hass_config.root_path,
config_key, hass_config.root_path / ref.ref.path,
tag_manager, ref.configuration_key,
) tag_manager,
)
else:
yield from load_automation_inline_ref(hass_config.root_path, tag_manager, ref)
except InvalidAutomationFile as e: except InvalidAutomationFile as e:
logger.error(e) logger.error(e)
def extract_automation_paths( def extract_automation_refs(
hass_config: HassConfig, hass_config: HassConfig,
) -> AutomationPathIter: ) -> AutomationExtractedIter:
found = False found = False
for config_key, automation_path in chain( for ref in chain(
extract_automation_root_paths(hass_config), extract_package_automation_paths(hass_config) extract_automation_root_refs(hass_config), extract_automation_package_refs(hass_config)
): ):
found = True found = True
yield config_key, automation_path yield ref
if not found: if not found:
yield [""], "automations.yaml" yield IncludedAutoamtion([""], IncludedYaml(hass_config.get_default_automation_path()))
def extract_automation_root_paths( def extract_automation_root_refs(
hass_config: HassConfig, hass_config: HassConfig,
) -> AutomationPathIter: ) -> AutomationExtractedIter:
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}'")
if isinstance(value, IncludedYamlDir): # type: ignore if isinstance(value, IncludedYamlDir): # type: ignore
yield [key], str(value.path.relative_to(hass_config.root_path)) yield IncludedAutoamtion([key], value)
elif isinstance(value, list): elif isinstance(value, list):
logger.warning(f"Failed to load from '{key}' as no support for inline automations yet.") yield InlineAutomation(
configuration_key=[key],
automations=value,
source_file=hass_config.get_configuration_path(),
)
else: else:
logger.warning(f"found an invalid type in {key}!") logger.warning(f"found an invalid type in {key}!")
def extract_package_automation_paths(hass_config: HassConfig) -> AutomationPathIter: def extract_automation_package_refs(hass_config: HassConfig) -> AutomationExtractedIter:
for package_name, package_data in hass_config.pacakges.items(): for package_name, package_data in hass_config.pacakges.items():
if not isinstance(package_data, dict): if not isinstance(package_data, dict):
logger.warning(f"The package {package_name} is incorrectly configured!") logger.warning(f"The package {package_name} is incorrectly configured!")
@@ -73,7 +86,7 @@ def extract_package_automation_paths(hass_config: HassConfig) -> AutomationPathI
if automations := package_data.get("automation", None): if automations := package_data.get("automation", None):
logger.info(f"Loading automations from package '{package_name}'") logger.info(f"Loading automations from package '{package_name}'")
if isinstance(automations, IncludedYamlDir): # type: ignore if isinstance(automations, IncludedYamlDir): # type: ignore
yield [package_name], str(automations.path.relative_to(hass_config.root_path)) yield IncludedAutoamtion([package_name], automations)
elif isinstance(automations, list): elif isinstance(automations, list):
logger.warning( logger.warning(
f"Failed to load from '{package_name}' as no support for inline automations yet." f"Failed to load from '{package_name}' as no support for inline automations yet."
@@ -104,19 +117,37 @@ def get_base_automation_key(hass_config: HassConfig) -> Tuple[ConfigurationKey,
def extract_automation_keys( def extract_automation_keys(
hass_config: HassConfig, hass_config: HassConfig,
) -> Iterator[Tuple[str, IncludedYamlDir]]: ) -> Iterator[Tuple[str, IncludedYamlDir | list]]:
for key, value in hass_config.configurations.items(): for key, value in hass_config.configurations.items():
if str(key).startswith("automation"): if str(key).startswith("automation"):
if isinstance(value, dict) or isinstance(value, list): if isinstance(value, list):
raise NotImplementedError( yield key, value
"No support for automations baked into configuration.yaml! Please extract these into a seperate file"
)
elif isinstance(value, IncludedYamlDir): # type: ignore elif isinstance(value, IncludedYamlDir): # type: ignore
yield key, value yield key, value
else: else:
logger.warning(f"found an invalid type in {key}!") logger.warning(f"found an invalid type in {key}!")
def load_automation_inline_ref(
root_path: Path, tag_manager: TagManager, inline_automation: InlineAutomation
) -> Iterator[ExtenededAutomation]:
for auto in inline_automation.automations:
try:
yield ExtenededAutomation(
**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_type="inline",
)
except Exception as err:
raise InvalidAutomationFile(
when="reading file contents",
automation_path=inline_automation.source_file,
error=err,
) from err
def load_automation_path( def load_automation_path(
root_path: Path, root_path: Path,
automation_path: Path, automation_path: Path,
+33 -14
View File
@@ -109,7 +109,7 @@ class AutomationManager:
) )
raw_auto = automation.to_primitive(include_tags=False) raw_auto = automation.to_primitive(include_tags=False)
automation_path.write_text(dump_yaml(raw_auto, self.hass_config.root_path)) automation_path.write_text(dump_yaml(raw_auto, self.hass_config.root_path))
else: elif automation.source_file_type == "list":
if isinstance(objs, dict): if isinstance(objs, dict):
raise AttemptingToOverwriteAnIncompatibleFileError( raise AttemptingToOverwriteAnIncompatibleFileError(
f"{automation_path} is a list, but expected a obj for {automation}" f"{automation_path} is a list, but expected a obj for {automation}"
@@ -120,20 +120,23 @@ class AutomationManager:
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}"
) )
found = False update_or_append_to_automation_list(
for i, obj in enumerate(objs): objs=objs,
if obj["id"] == automation.id: automation=automation,
objs[i] = automation.to_primitive(include_tags=False) create_if_not_found=create_if_not_found,
found = True )
break
if not found:
if create_if_not_found:
objs.append(automation.to_primitive(include_tags=False))
else:
raise InvalidAutomationFile(
"Could not update automations, not found in source file."
)
automation_path.write_text(dump_yaml(objs, self.hass_config.root_path)) automation_path.write_text(dump_yaml(objs, self.hass_config.root_path))
elif automation.source_file_type == "inline":
update_obj = objs
for key in automation.configuration_key:
update_obj = update_obj[key]
update_or_append_to_automation_list(
objs=update_obj,
automation=automation,
create_if_not_found=create_if_not_found,
)
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]):
@@ -164,3 +167,19 @@ class AutomationManager:
kept.append(obj) kept.append(obj)
automation_path.write_text(dump_yaml(kept, self.hass_config.root_path)) automation_path.write_text(dump_yaml(kept, self.hass_config.root_path))
self.delete_tags(automation.id) self.delete_tags(automation.id)
def update_or_append_to_automation_list(
objs: list[dict],
automation: ExtenededAutomation,
create_if_not_found: bool = False,
):
for i, obj in enumerate(objs):
if obj["id"] == automation.id:
objs[i] = automation.to_primitive(include_tags=False)
return True
if create_if_not_found:
objs.append(automation.to_primitive(include_tags=False))
else:
raise InvalidAutomationFile("Could not update automations, not found in source file.")
return False
+19 -2
View File
@@ -1,11 +1,28 @@
import json import json
from typing import Iterator, Literal, Optional, Union from pathlib import Path
from typing import Iterator, Literal, NamedTuple, Optional, Union
from pydantic import BaseModel from pydantic import BaseModel
from pydantic.fields import Field from pydantic.fields import Field
from src.yaml_serializer import IncludedYamlDir
ConfigurationKey = list[str] ConfigurationKey = list[str]
AutomationPathIter = Iterator[tuple[ConfigurationKey, str]]
class InlineAutomation(NamedTuple):
configuration_key: ConfigurationKey
source_file: Path
automations: list[dict]
class IncludedAutoamtion(NamedTuple):
configuration_key: ConfigurationKey
ref: IncludedYamlDir
ExtractedAutomation = InlineAutomation | IncludedAutoamtion
AutomationExtractedIter = Iterator[ExtractedAutomation]
class BaseAutomation(BaseModel): class BaseAutomation(BaseModel):
@@ -7,6 +7,7 @@ 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 ( from tests.utils import (
HA_CONFIG10_EXAMPLE,
HA_CONFIG2_EXAMPLE, HA_CONFIG2_EXAMPLE,
HA_CONFIG6_EXAMPLE, HA_CONFIG6_EXAMPLE,
get_example_automation_loader, get_example_automation_loader,
@@ -386,6 +387,22 @@ class automation_update_split_config_tests(automation_update_tests):
self.client = TestClient(make_app(self.automation_loader)) self.client = TestClient(make_app(self.automation_loader))
class automation_update_split_config_and_inline_config_tests(automation_update_tests):
test_params = {
"total": 3,
"modify_item": 0,
"consitency_tests": {},
}
def setUp(self) -> None:
(
self.config_folder,
self.hass_config,
self.automation_loader,
) = get_example_automation_loader(HA_CONFIG10_EXAMPLE)
self.client = TestClient(make_app(self.automation_loader))
class automation_delete_tests(BaseTestCase): class automation_delete_tests(BaseTestCase):
def test_delete_some(self): def test_delete_some(self):
resp = self.client.post("/automations/list", json={"limit": 1, "offset": 0}) resp = self.client.post("/automations/list", json={"limit": 1, "offset": 0})
@@ -3,7 +3,7 @@ from tempfile import mktemp
from unittest import TestCase from unittest import TestCase
from src.automations.loader import ( from src.automations.loader import (
extract_automation_paths, extract_automation_refs,
load_automation_path, load_automation_path,
) )
from src.automations.tags import TagManager from src.automations.tags import TagManager
@@ -155,9 +155,9 @@ class loader_tests(TestCase):
(HA_CONFIG7_EXAMPLE, 1), (HA_CONFIG7_EXAMPLE, 1),
(HA_CONFIG8_EXAMPLE, 1), (HA_CONFIG8_EXAMPLE, 1),
(HA_CONFIG9_EXAMPLE, 1), (HA_CONFIG9_EXAMPLE, 1),
(HA_CONFIG10_EXAMPLE, 1), (HA_CONFIG10_EXAMPLE, 2),
]: ]:
with self.subTest(config_name=ha_path.name, expected=expected): with self.subTest(config_name=ha_path.name, expected=expected):
hass_config = HassConfig(ha_path) hass_config = HassConfig(ha_path)
paths = list(extract_automation_paths(hass_config)) refs = list(extract_automation_refs(hass_config))
self.assertEqual(len(paths), expected) self.assertEqual(len(refs), expected)
@@ -334,5 +334,5 @@ class manager_tests(TestWithDB):
_, automation_manager = self.get_manager(HA_CONFIG10_EXAMPLE) _, automation_manager = self.get_manager(HA_CONFIG10_EXAMPLE)
automation_manager.reload() automation_manager.reload()
auto = automation_manager.get("inline-auto") auto = automation_manager.get("inline-auto")
self.assertEqual(auto.source_file_type, ["inline"]) self.assertEqual(auto.source_file_type, "inline")
self.assertEqual(auto.configuration_key, ["automation"]) self.assertEqual(auto.configuration_key, ["automation"])