automation path loader
This commit is contained in:
1 parent
a746c4db3a
commit
579017d9ec
5 files changed
+79
-5
No files matched your search
@@ -1,16 +1,53 @@
|
||||
from pathlib import Path
|
||||
from typing import Any, Iterator, Union
|
||||
from typing import Any, Iterator, Union, Dict
|
||||
from ..hass_config.loader import HassConfig
|
||||
|
||||
from src.json_serializer import normalize_obj
|
||||
from ..logger import get_logger
|
||||
from src.logger import get_logger
|
||||
from src.yaml_serializer import load_yaml
|
||||
from src.yaml_serializer.types import IncludedYaml, IncludedYamlDir
|
||||
|
||||
from .errors import InvalidAutomationFile
|
||||
from .types import ExtenededAutomationData
|
||||
|
||||
logger = get_logger(__file__)
|
||||
|
||||
# Single loader
|
||||
|
||||
def extract_files(p: Path) -> Iterator[Path]:
|
||||
if not p.exists():
|
||||
logger.warning(
|
||||
f"the specified path '{p}' does not point to a valid location! -- {p.absolute()}"
|
||||
)
|
||||
return
|
||||
if p.is_dir():
|
||||
for c in p.iterdir():
|
||||
yield from extract_files(c)
|
||||
else:
|
||||
yield p
|
||||
|
||||
|
||||
def extract_automation_paths(
|
||||
hass_config: HassConfig,
|
||||
) -> Iterator[Path]:
|
||||
found = False
|
||||
for key, value in hass_config.configurations.items():
|
||||
if str(key).startswith("automation"):
|
||||
logger.info(f"Loading automations from '{key}'")
|
||||
found = True
|
||||
if isinstance(value, dict):
|
||||
raise NotImplementedError(
|
||||
"No support for automations baked into configuration.yaml! Please extract these into a seperate file"
|
||||
)
|
||||
elif isinstance(value, IncludedYaml):
|
||||
yield hass_config.root_path / value.path_str
|
||||
elif isinstance(value, IncludedYamlDir):
|
||||
yield from extract_files(hass_config.root_path / value.name)
|
||||
else:
|
||||
logger.warning(f"found an invalid type in {key}!")
|
||||
if not found:
|
||||
yield hass_config.root_path / "automations.yaml"
|
||||
|
||||
|
||||
def load_automation_path(automation_path: Path) -> Iterator[ExtenededAutomationData]:
|
||||
"""
|
||||
|
||||
|
||||
Whitespace-only changes.
@@ -0,0 +1,13 @@
|
||||
# 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
|
||||
@@ -2,9 +2,17 @@ from pathlib import Path
|
||||
from tempfile import mktemp
|
||||
from unittest import TestCase
|
||||
|
||||
from src.automations_v2.loader import load_automation_path
|
||||
from src.automations_v2.loader import extract_automation_paths, load_automation_path
|
||||
from src.automations_v2.types import ExtenededAutomationData
|
||||
from tests.utils import HA_CONFIG2_EXAMPLE, HA_CONFIG4_EXAMPLE
|
||||
from src.hass_config.loader import HassConfig
|
||||
from tests.utils import (
|
||||
HA_CONFIG2_EXAMPLE,
|
||||
HA_CONFIG3_EXAMPLE,
|
||||
HA_CONFIG4_EXAMPLE,
|
||||
HA_CONFIG5_EXAMPLE,
|
||||
HA_CONFIG6_EXAMPLE,
|
||||
HA_CONFIG_EXAMPLE,
|
||||
)
|
||||
|
||||
|
||||
class loader_tests(TestCase):
|
||||
@@ -85,3 +93,17 @@ class loader_tests(TestCase):
|
||||
file_path.unlink(missing_ok=True)
|
||||
automations = list(load_automation_path(file_path))
|
||||
self.assertEqual(len(automations), 0)
|
||||
|
||||
def test_extract_all_automation_files(self):
|
||||
for ha_path, expected in [
|
||||
(HA_CONFIG_EXAMPLE, 1),
|
||||
(HA_CONFIG2_EXAMPLE, 1),
|
||||
(HA_CONFIG3_EXAMPLE, 1),
|
||||
(HA_CONFIG4_EXAMPLE, 3),
|
||||
(HA_CONFIG5_EXAMPLE, 7),
|
||||
(HA_CONFIG6_EXAMPLE, 8),
|
||||
]:
|
||||
with self.subTest(ha_path=ha_path, expected=expected):
|
||||
hass_config = HassConfig(ha_path)
|
||||
paths = list(extract_automation_paths(hass_config))
|
||||
self.assertEqual(len(paths), expected)
|
||||
@@ -15,6 +15,8 @@ HA_CONFIG_EXAMPLE = SAMPLES_FOLDER / "config"
|
||||
HA_CONFIG2_EXAMPLE = SAMPLES_FOLDER / "config-2"
|
||||
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"
|
||||
|
||||
|
||||
def get_example_automation_loader(
|
||||
|
||||
Reference in new issue
Block a user