From f8b59c6b170e39158016e633526ef9fcbae47a24 Mon Sep 17 00:00:00 2001 From: Ari Date: Mon, 15 Aug 2022 19:50:24 -0400 Subject: [PATCH] tags --- api/src/automations_v2/loader.py | 6 ++- api/src/automations_v2/manager.py | 14 +++++- api/src/automations_v2/tags.py | 14 ++++++ api/tests/unittests/automations_v2/test_db.py | 50 ------------------- .../unittests/automations_v2/test_loader.py | 24 +++++++-- .../unittests/automations_v2/test_manager.py | 2 + .../unittests/automations_v2/test_tags.py | 17 +++++++ 7 files changed, 71 insertions(+), 56 deletions(-) create mode 100644 api/src/automations_v2/tags.py create mode 100644 api/tests/unittests/automations_v2/test_tags.py diff --git a/api/src/automations_v2/loader.py b/api/src/automations_v2/loader.py index 6f07d1c..e56670f 100644 --- a/api/src/automations_v2/loader.py +++ b/api/src/automations_v2/loader.py @@ -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", diff --git a/api/src/automations_v2/manager.py b/api/src/automations_v2/manager.py index 54e510c..610c946 100644 --- a/api/src/automations_v2/manager.py +++ b/api/src/automations_v2/manager.py @@ -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) diff --git a/api/src/automations_v2/tags.py b/api/src/automations_v2/tags.py new file mode 100644 index 0000000..c71d53d --- /dev/null +++ b/api/src/automations_v2/tags.py @@ -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)) diff --git a/api/tests/unittests/automations_v2/test_db.py b/api/tests/unittests/automations_v2/test_db.py index 415087e..1fbd5e4 100644 --- a/api/tests/unittests/automations_v2/test_db.py +++ b/api/tests/unittests/automations_v2/test_db.py @@ -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") diff --git a/api/tests/unittests/automations_v2/test_loader.py b/api/tests/unittests/automations_v2/test_loader.py index 2312ef1..e7243fe 100644 --- a/api/tests/unittests/automations_v2/test_loader.py +++ b/api/tests/unittests/automations_v2/test_loader.py @@ -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): diff --git a/api/tests/unittests/automations_v2/test_manager.py b/api/tests/unittests/automations_v2/test_manager.py index 922117a..48c61cb 100644 --- a/api/tests/unittests/automations_v2/test_manager.py +++ b/api/tests/unittests/automations_v2/test_manager.py @@ -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 diff --git a/api/tests/unittests/automations_v2/test_tags.py b/api/tests/unittests/automations_v2/test_tags.py new file mode 100644 index 0000000..24e4626 --- /dev/null +++ b/api/tests/unittests/automations_v2/test_tags.py @@ -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()