This commit is contained in:
ari committed 2022-08-15 21:23:51 -04:00
1 parent f8b59c6b17
commit d96c1fb953
6 files changed
+95 -10

No files matched your search

+1 -1
View File
@@ -1,6 +1,5 @@
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
@@ -10,6 +9,7 @@ from src.yaml_serializer.types import IncludedYaml, IncludedYamlDir
from ..hass_config.loader import HassConfig
from .errors import InvalidAutomationFile
from .tags import TagManager
from .types import ExtenededAutomation
logger = get_logger(__file__)
+22 -5
View File
@@ -1,7 +1,7 @@
from enum import auto
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
@@ -15,6 +15,7 @@ from .errors import (
InvalidAutomationFile,
)
from .loader import extract_automation_paths, load_automation_path
from .tags import TagManager
from .types import ExtenededAutomation
logger = get_logger(__file__)
@@ -24,20 +25,23 @@ class AutomationManager:
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_path = hass_config.get_automation_tags_path()
self.tag_manager = TagManager()
if tag_path.exists():
self.tag_manager.load(tag_path)
self.reload_tags()
def reload_tags(self):
if self.tag_path.exists():
self.tag_manager = TagManager.load(self.tag_path)
def reload(self):
self.db.reset()
self.reload_tags()
errors = []
for config_key, p in extract_automation_paths(self.hass_config):
try:
@@ -97,6 +101,18 @@ class AutomationManager:
if not found:
objs.append(automation.to_primitive(include_tags=False))
automation.source_file.write_text(dump_yaml(objs))
self.update_tags(automation)
def update_tags(self, automation: ExtenededAutomation):
self.reload_tags()
self.tag_manager[automation.id] = automation.tags
self.tag_manager.save(self.tag_path)
def delete_tags(self, automation_id: str):
self.reload_tags()
if automation_id in self.tag_manager:
del self.tag_manager[automation_id]
self.tag_manager.save(self.tag_path)
def delete(self, automation: ExtenededAutomation):
if automation.source_file_type == "obj":
@@ -113,3 +129,4 @@ class AutomationManager:
if obj["id"] != automation.id:
kept.append(obj)
automation.source_file.write_text(dump_yaml(kept))
self.delete_tags(automation.id)
+2
View File
@@ -6,6 +6,8 @@ from src.yaml_serializer import dump_yaml, load_yaml
class TagManager(Dict[str, Dict[str, str]]):
def save(self, path: Path):
if not path.parent.exists():
path.parent.mkdir(parents=True)
path.write_text(dump_yaml(dict(self)))
@classmethod
+1 -1
View File
@@ -30,6 +30,6 @@ class ExtenededAutomation(BaseAutomation):
del out["source_file"]
del out["source_file_type"]
del out["configuration_key"]
if include_tags and out.get("tags", None) is not None:
if not include_tags and out.get("tags", None) is not None:
del out["tags"]
return out
@@ -25,7 +25,7 @@ class loader_tests(TestCase):
load_automation_path(
HA_CONFIG2_EXAMPLE / "automations.yaml",
configuration_key="automation",
tag_manager=TagManager({"type": "routine"}),
tag_manager=TagManager({"1652069225859": {"type": "routine"}}),
)
)
self.assertEqual(len(automations), 1)
@@ -1,5 +1,4 @@
from pathlib import Path
from tempfile import mktemp
from src import yaml_serializer
from src.automations_v2.errors import (
@@ -25,7 +24,6 @@ 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
@@ -255,3 +253,71 @@ class manager_tests(TestWithDB):
(config_path / "automations/include_dir_list/home/down.yaml").open("r")
)
self.assertIsInstance(data, dict)
def test_update_tags_through_automation_update(self):
_, automation_manager = self.get_manager(HA_CONFIG_EXAMPLE)
automation_manager.reload()
auto = automation_manager.get("1619371298367")
self.assertDictEqual(
auto.tags,
{
"Room": "Living",
"Type": "Button",
},
)
auto.tags["Room"] = "Bathroom"
automation_manager.upsert(auto)
del auto
automation_manager.reload()
auto = automation_manager.get("1619371298367")
self.assertDictEqual(
auto.tags,
{
"Room": "Bathroom",
"Type": "Button",
},
)
def test_update_tags_through_tags(self):
_, automation_manager = self.get_manager(HA_CONFIG_EXAMPLE)
automation_manager.reload()
auto = automation_manager.get("1619371298367")
self.assertDictEqual(
auto.tags,
{
"Room": "Living",
"Type": "Button",
},
)
auto.tags["Room"] = "Bathroom"
automation_manager.update_tags(auto)
del auto
automation_manager.reload()
auto = automation_manager.get("1619371298367")
self.assertDictEqual(
auto.tags,
{
"Room": "Bathroom",
"Type": "Button",
},
)
def test_delete_tags(self):
_, automation_manager = self.get_manager(HA_CONFIG_EXAMPLE)
automation_manager.reload()
auto = automation_manager.get("1619371298367")
self.assertDictEqual(
auto.tags,
{
"Room": "Living",
"Type": "Button",
},
)
automation_manager.delete_tags(auto.id)
del auto
automation_manager.reload()
auto = automation_manager.get("1619371298367")
self.assertDictEqual(
auto.tags,
{},
)