This commit is contained in:
ari committed 2022-08-15 19:50:24 -04:00
1 parent 02cbacabe0
commit f8b59c6b17
7 files changed
+71 -56

No files matched your search

+5 -1
View File
@@ -1,5 +1,6 @@
from pathlib import Path
from typing import Any, Iterator, Tuple, Union
from .tags import TagManager
from src.json_serializer import normalize_obj
from src.logger import get_logger
@@ -39,6 +40,7 @@ def extract_automation_paths(
def load_automation_path(
automation_path: Path,
configuration_key: str,
tag_manager: TagManager,
) -> Iterator[ExtenededAutomation]:
"""
@@ -55,7 +57,7 @@ def load_automation_path(
return
if automation_path.is_dir():
for f in extract_files(automation_path):
yield from load_automation_path(f, configuration_key)
yield from load_automation_path(f, configuration_key, tag_manager)
else:
try:
with automation_path.open("r") as fp:
@@ -71,6 +73,7 @@ def load_automation_path(
try:
yield ExtenededAutomation(
**clean_automation(automations),
tags=tag_manager.get(automations["id"], {}),
configuration_key=configuration_key,
source_file=str(automation_path),
source_file_type="obj",
@@ -89,6 +92,7 @@ def load_automation_path(
try:
yield ExtenededAutomation(
**clean_automation(automation),
tags=tag_manager.get(automation["id"], {}),
configuration_key=configuration_key,
source_file=str(automation_path),
source_file_type="list",
+12 -2
View File
@@ -1,6 +1,7 @@
from pathlib import Path
from tempfile import mktemp
from typing import Optional, Union
from .tags import TagManager
from src.hass_config.loader import HassConfig
from src.yaml_serializer import dump_yaml, load_yaml
@@ -20,18 +21,27 @@ logger = get_logger(__file__)
class AutomationManager:
def __init__(self, hass_config: HassConfig, db_path: Optional[Path] = None) -> None:
def __init__(
self,
hass_config: HassConfig,
tag_path: Path,
db_path: Optional[Path] = None,
) -> None:
if db_path is None:
db_path = Path(mktemp())
self.hass_config = hass_config
self.db = AutomationDBConnection(db_path)
self.tag_path = tag_path
self.tag_manager = TagManager()
if tag_path.exists():
self.tag_manager.load(tag_path)
def reload(self):
self.db.reset()
errors = []
for config_key, p in extract_automation_paths(self.hass_config):
try:
automations = list(load_automation_path(p, config_key))
automations = list(load_automation_path(p, config_key, self.tag_manager))
self.db.upsert_automations(automations)
except InvalidAutomationFile as e:
logger.warning(e)
+14
View File
@@ -0,0 +1,14 @@
from pathlib import Path
from typing import Dict
from src.yaml_serializer import dump_yaml, load_yaml
class TagManager(Dict[str, Dict[str, str]]):
def save(self, path: Path):
path.write_text(dump_yaml(dict(self)))
@classmethod
def load(cls, path: Path):
with path.open("r") as fp:
return cls(load_yaml(fp))
@@ -64,53 +64,3 @@ class db_tests(TestWithDB):
autos = list(self.db.list_automations(0, 10))
self.assertEqual(len(autos), 1)
self.assertTrue(autos[0] == originals[1])
def test_delete_automations(self):
originals = [
ExtenededAutomation(
id="test",
alias="test",
source_file="automation.yaml",
source_file_type="obj",
configuration_key="automation",
),
ExtenededAutomation(
id="test2",
alias="test",
source_file="automation.yaml",
source_file_type="obj",
action=[{"event": {"nice": "one"}, "event_type": "custom"}],
configuration_key="automation",
),
]
self.assertEqual(self.db.count_automations(), 0)
self.db.upsert_automations(originals)
self.assertEqual(self.db.count_automations(), 2)
self.db.delete_automations(originals)
self.assertEqual(self.db.count_automations(), 0)
def test_delete_automations_by_source_type(self):
originals = [
ExtenededAutomation(
id="test",
alias="test",
source_file="automation2.yaml",
source_file_type="obj",
configuration_key="automation",
),
ExtenededAutomation(
id="test2",
alias="test",
source_file="automation.yaml",
source_file_type="obj",
action=[{"event": {"nice": "one"}, "event_type": "custom"}],
configuration_key="automation",
),
]
self.assertEqual(self.db.count_automations(), 0)
self.db.upsert_automations(originals)
self.assertEqual(self.db.count_automations(), 2)
self.db.delete_automations_in_source_file(Path("automation2.yaml"))
self.assertEqual(self.db.count_automations(), 1)
[auto] = self.db.list_automations(0, 10)
self.assertEqual(auto.id, "test2")
@@ -6,6 +6,7 @@ from src.automations_v2.loader import (
extract_automation_paths,
load_automation_path,
)
from src.automations_v2.tags import TagManager
from src.automations_v2.types import ExtenededAutomation
from src.hass_config.loader import HassConfig
from tests.utils import (
@@ -22,7 +23,9 @@ class loader_tests(TestCase):
def test_load_automation_list_file(self):
automations = list(
load_automation_path(
HA_CONFIG2_EXAMPLE / "automations.yaml", configuration_key="automation"
HA_CONFIG2_EXAMPLE / "automations.yaml",
configuration_key="automation",
tag_manager=TagManager({"type": "routine"}),
)
)
self.assertEqual(len(automations), 1)
@@ -36,6 +39,7 @@ class loader_tests(TestCase):
alias="Climate - Pref temperature ",
description="",
mode="single",
tags={"type": "routine"},
trigger=[
{
"platform": "state",
@@ -64,6 +68,7 @@ class loader_tests(TestCase):
load_automation_path(
HA_CONFIG4_EXAMPLE / "automations" / "notify_washer.yaml",
configuration_key="automation base",
tag_manager=TagManager({"1659114647067": {"room": "laundry"}}),
)
)
self.assertEqual(len(automations), 1)
@@ -77,6 +82,7 @@ class loader_tests(TestCase):
alias="Notify Washer",
description="Example",
mode="single",
tags={"room": "laundry"},
trigger=[
{
"platform": "homeassistant",
@@ -97,13 +103,25 @@ class loader_tests(TestCase):
def test_load_automation_empty_file(self):
file_path = Path(mktemp())
file_path.touch()
automations = list(load_automation_path(file_path, configuration_key="automation ui"))
automations = list(
load_automation_path(
file_path,
configuration_key="automation ui",
tag_manager=TagManager(),
)
)
self.assertEqual(len(automations), 0)
def test_load_automation_none_existing_file(self):
file_path = Path(mktemp())
file_path.unlink(missing_ok=True)
automations = list(load_automation_path(file_path, configuration_key="automation manual"))
automations = list(
load_automation_path(
file_path,
configuration_key="automation manual",
tag_manager=TagManager(),
)
)
self.assertEqual(len(automations), 0)
def test_extract_all_automation_files(self):
@@ -1,4 +1,5 @@
from pathlib import Path
from tempfile import mktemp
from src import yaml_serializer
from src.automations_v2.errors import (
@@ -24,6 +25,7 @@ class manager_tests(TestWithDB):
self.config_path = create_copy_of_example_config(ha_config_example)
self.automation_manager = AutomationManager(
HassConfig(self.config_path),
Path(mktemp()),
self.db_file,
)
return self.config_path, self.automation_manager
@@ -0,0 +1,17 @@
from pathlib import Path
from tempfile import mktemp
from unittest import TestCase
from src.automations_v2.tags import TagManager
class tag_tests(TestCase):
def test_load_and_save(self):
tmp_file = Path(mktemp()).with_suffix(".yml")
tags = TagManager()
tags[0] = {"Room": "Kitchen", "Type": "Lights"}
tags[1] = {}
tags.save(tmp_file)
loaded_tags = TagManager.load(tmp_file)
assert loaded_tags.keys() == tags.keys()