tags
This commit is contained in:
1 parent
02cbacabe0
commit
f8b59c6b17
7 files changed
+71
-56
No files matched your search
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
Reference in new issue
Block a user