refactor of how automations are sorted
This commit is contained in:
1 parent
80bfbdab80
commit
fbbc5af12d
15 files changed
+155
-93
No files matched your search
@@ -3,7 +3,7 @@ import json
|
||||
import sqlite3
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
from typing import Iterable, Iterator, List, Optional
|
||||
from typing import Iterable, Iterator
|
||||
|
||||
from src.logger import get_logger
|
||||
|
||||
@@ -26,17 +26,25 @@ def encode_auto(auto: ExtenededAutomation) -> str:
|
||||
).decode("utf-8")
|
||||
|
||||
|
||||
def encode_obj(auto: dict | list) -> str:
|
||||
return base64.b64encode(json.dumps(auto).encode("utf-8")).decode("utf-8")
|
||||
|
||||
|
||||
def decode_auto(
|
||||
auto_str: str, source_file: str, source_file_type: str, configuration_key: str
|
||||
) -> ExtenededAutomation:
|
||||
return ExtenededAutomation(
|
||||
source_file=source_file,
|
||||
source_file_type=source_file_type,
|
||||
configuration_key=configuration_key,
|
||||
**json.loads(base64.b64decode(auto_str.encode("utf-8")).decode("utf-8")),
|
||||
configuration_key=decode_obj(configuration_key), # type: ignore
|
||||
**decode_obj(auto_str),
|
||||
)
|
||||
|
||||
|
||||
def decode_obj(obj_str: str) -> dict | list:
|
||||
return json.loads(base64.b64decode(obj_str.encode("utf-8")).decode("utf-8"))
|
||||
|
||||
|
||||
class AutomationDBConnection:
|
||||
def __init__(self, db_file: Path) -> None:
|
||||
self.db_file = db_file
|
||||
@@ -69,13 +77,13 @@ class AutomationDBConnection:
|
||||
mode TEXT NOT NULL,
|
||||
source_file TEXT NOT NULL,
|
||||
source_file_type TEXT NOT NULL,
|
||||
configuration_key TEXT NOT NULL,
|
||||
configuration_key BLOB NOT NULL,
|
||||
rest BLOB NOT NULL
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
||||
def insert_automations(self, automations: List[ExtenededAutomation]):
|
||||
def insert_automations(self, automations: list[ExtenededAutomation]):
|
||||
with self.get_cur() as cur:
|
||||
for automation in automations:
|
||||
try:
|
||||
@@ -88,7 +96,7 @@ class AutomationDBConnection:
|
||||
"{automation.mode}",
|
||||
"{automation.source_file}",
|
||||
"{automation.source_file_type}",
|
||||
"{automation.configuration_key}",
|
||||
"{encode_obj(automation.configuration_key)}",
|
||||
"{encode_auto(automation)}"
|
||||
);
|
||||
""",
|
||||
|
||||
@@ -15,16 +15,31 @@ from src.yaml_serializer import (
|
||||
from ..hass_config.loader import HassConfig
|
||||
from .errors import InvalidAutomationFile
|
||||
from .tags import TagManager
|
||||
from .types import ExtenededAutomation
|
||||
from .types import AutomationPathIter, ConfigurationKey, ExtenededAutomation
|
||||
|
||||
logger = get_logger(__file__)
|
||||
|
||||
AUTOMATION_CONFIGURATION_KEY = "automation shortumation"
|
||||
|
||||
|
||||
def load_and_iter_automations(
|
||||
hass_config: HassConfig, tag_manager: TagManager
|
||||
) -> Iterator[ExtenededAutomation]:
|
||||
for config_key, p in extract_automation_paths(hass_config):
|
||||
try:
|
||||
yield from load_automation_path(
|
||||
hass_config.root_path,
|
||||
hass_config.root_path / p,
|
||||
config_key,
|
||||
tag_manager,
|
||||
)
|
||||
except InvalidAutomationFile as e:
|
||||
logger.error(e)
|
||||
|
||||
|
||||
def extract_automation_paths(
|
||||
hass_config: HassConfig,
|
||||
) -> Iterator[Tuple[str, str]]:
|
||||
) -> AutomationPathIter:
|
||||
found = False
|
||||
|
||||
for config_key, automation_path in chain(
|
||||
@@ -34,23 +49,23 @@ def extract_automation_paths(
|
||||
yield config_key, automation_path
|
||||
|
||||
if not found:
|
||||
yield "", "automations.yaml"
|
||||
yield [""], "automations.yaml"
|
||||
|
||||
|
||||
def extract_automation_root_paths(
|
||||
hass_config: HassConfig,
|
||||
) -> Iterator[Tuple[str, str]]:
|
||||
) -> AutomationPathIter:
|
||||
for key, value in extract_automation_keys(hass_config):
|
||||
logger.info(f"Loading automations from '{key}'")
|
||||
if isinstance(value, IncludedYamlDir): # type: ignore
|
||||
yield key, str(value.path.relative_to(hass_config.root_path))
|
||||
yield [key], str(value.path.relative_to(hass_config.root_path))
|
||||
elif isinstance(value, list):
|
||||
logger.warning(f"Failed to load from '{key}' as no support for inline automations yet.")
|
||||
else:
|
||||
logger.warning(f"found an invalid type in {key}!")
|
||||
|
||||
|
||||
def extract_package_automation_paths(hass_config: HassConfig) -> Iterator[Tuple[str, str]]:
|
||||
def extract_package_automation_paths(hass_config: HassConfig) -> AutomationPathIter:
|
||||
for package_name, package_data in hass_config.pacakges.items():
|
||||
if not isinstance(package_data, dict):
|
||||
logger.warning(f"The package {package_name} is incorrectly configured!")
|
||||
@@ -58,7 +73,7 @@ def extract_package_automation_paths(hass_config: HassConfig) -> Iterator[Tuple[
|
||||
if automations := package_data.get("automation", None):
|
||||
logger.info(f"Loading automations from package '{package_name}'")
|
||||
if isinstance(automations, IncludedYamlDir): # type: ignore
|
||||
yield package_name, str(automations.path.relative_to(hass_config.root_path))
|
||||
yield [package_name], str(automations.path.relative_to(hass_config.root_path))
|
||||
elif isinstance(automations, list):
|
||||
logger.warning(
|
||||
f"Failed to load from '{package_name}' as no support for inline automations yet."
|
||||
@@ -67,12 +82,12 @@ def extract_package_automation_paths(hass_config: HassConfig) -> Iterator[Tuple[
|
||||
logger.warning(f"found an invalid type in {package_name}!")
|
||||
|
||||
|
||||
def get_base_automation_key(hass_config: HassConfig):
|
||||
def get_base_automation_key(hass_config: HassConfig) -> Tuple[ConfigurationKey, str]:
|
||||
found_any = False
|
||||
for key, value in extract_automation_keys(hass_config):
|
||||
found_any = True
|
||||
if isinstance(value, IncludedYaml):
|
||||
return key, str(value.path.relative_to(hass_config.root_path))
|
||||
return [key], str(value.path.relative_to(hass_config.root_path))
|
||||
configurations = hass_config.configurations
|
||||
if found_any:
|
||||
out_path = hass_config.get_backup_automation_file_path()
|
||||
@@ -84,7 +99,7 @@ def get_base_automation_key(hass_config: HassConfig):
|
||||
out_path.touch(exist_ok=True)
|
||||
with hass_config.get_configuration_path().open("w") as fp:
|
||||
fp.write(dump_yaml(configurations, root_path=hass_config.root_path))
|
||||
return AUTOMATION_CONFIGURATION_KEY, out_file
|
||||
return [AUTOMATION_CONFIGURATION_KEY], out_file
|
||||
|
||||
|
||||
def extract_automation_keys(
|
||||
@@ -105,14 +120,14 @@ def extract_automation_keys(
|
||||
def load_automation_path(
|
||||
root_path: Path,
|
||||
automation_path: Path,
|
||||
configuration_key: str,
|
||||
configuration_key: ConfigurationKey,
|
||||
tag_manager: TagManager,
|
||||
) -> Iterator[ExtenededAutomation]:
|
||||
"""
|
||||
|
||||
Args:
|
||||
automation_path (Path)
|
||||
configuration_key (str)
|
||||
configuration_key (list[str])
|
||||
|
||||
Returns:
|
||||
Iterator[ExtenededAutomation]
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
from pathlib import Path
|
||||
from tempfile import mktemp
|
||||
from typing import Dict, Optional, Union
|
||||
from typing import Optional, Union
|
||||
|
||||
from src.errors import ErrorSet
|
||||
from src.hass_config.loader import HassConfig
|
||||
from src.logger import get_logger
|
||||
from src.yaml_serializer import dump_yaml, load_yaml
|
||||
|
||||
from ..errors import ErrorSet
|
||||
from ..logger import get_logger
|
||||
from .db import AutomationDBConnection
|
||||
from .errors import (
|
||||
AttemptingToOverwriteAnIncompatibleFileError,
|
||||
@@ -14,8 +14,8 @@ from .errors import (
|
||||
InvalidAutomationFile,
|
||||
)
|
||||
from .loader import (
|
||||
extract_automation_paths,
|
||||
get_base_automation_key,
|
||||
load_and_iter_automations,
|
||||
load_automation_path,
|
||||
)
|
||||
from .tags import TagManager
|
||||
@@ -42,27 +42,32 @@ class AutomationManager:
|
||||
if self.tag_path.exists():
|
||||
self.tag_manager = TagManager.load(self.tag_path)
|
||||
|
||||
def reload(self):
|
||||
def reload(self, batch_size: int = 100):
|
||||
self.db.reset()
|
||||
self.reload_tags()
|
||||
errors = []
|
||||
for config_key, p in extract_automation_paths(self.hass_config):
|
||||
auto_it = load_and_iter_automations(self.hass_config, self.tag_manager)
|
||||
batch: list[ExtenededAutomation] = []
|
||||
stop = False
|
||||
while not stop:
|
||||
try:
|
||||
automations = list(
|
||||
load_automation_path(
|
||||
self.hass_config.root_path,
|
||||
self.hass_config.root_path / p,
|
||||
config_key,
|
||||
self.tag_manager,
|
||||
)
|
||||
)
|
||||
self.db.insert_automations(automations)
|
||||
except InvalidAutomationFile as e:
|
||||
logger.warning(e)
|
||||
errors.append(e)
|
||||
batch.append(next(auto_it))
|
||||
if len(batch) >= batch_size:
|
||||
self.db.insert_automations(batch)
|
||||
batch = []
|
||||
except StopIteration:
|
||||
if stop:
|
||||
break
|
||||
stop = True
|
||||
except DBError as e:
|
||||
logger.warning(e)
|
||||
errors.append(e)
|
||||
try:
|
||||
if len(batch) >= 0:
|
||||
self.db.insert_automations(batch)
|
||||
except DBError as e:
|
||||
logger.warning(e)
|
||||
errors.append(e)
|
||||
if len(errors) > 0:
|
||||
raise ErrorSet(*errors)
|
||||
|
||||
@@ -131,7 +136,7 @@ class AutomationManager:
|
||||
automation_path.write_text(dump_yaml(objs, self.hass_config.root_path))
|
||||
self.update_tags(automation.id, automation.tags)
|
||||
|
||||
def update_tags(self, automation_id: str, tags: Dict[str, str]):
|
||||
def update_tags(self, automation_id: str, tags: dict[str, str]):
|
||||
self.reload_tags()
|
||||
self.tag_manager[automation_id] = tags
|
||||
self.tag_manager.save(self.tag_path)
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
from pathlib import Path
|
||||
from typing import Dict
|
||||
|
||||
from src.yaml_serializer import dump_yaml, load_yaml
|
||||
|
||||
|
||||
class TagManager(Dict[str, Dict[str, str]]):
|
||||
class TagManager(dict[str, dict[str, str]]):
|
||||
def save(self, path: Path):
|
||||
if not path.parent.exists():
|
||||
path.parent.mkdir(parents=True)
|
||||
|
||||
@@ -1,27 +1,29 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Literal, Optional, Union
|
||||
from typing import Iterator, Literal, Optional, Union
|
||||
|
||||
from pydantic import BaseModel
|
||||
from pydantic.fields import Field
|
||||
|
||||
ConfigurationKey = list[str]
|
||||
AutomationPathIter = Iterator[tuple[ConfigurationKey, str]]
|
||||
|
||||
|
||||
class BaseAutomation(BaseModel):
|
||||
id: str
|
||||
alias: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
trigger_variables: Optional[Dict[str, Union[str, int, bool, float]]] = None
|
||||
trigger_variables: Optional[dict[str, Union[str, int, bool, float]]] = None
|
||||
mode: str = "single"
|
||||
trigger: List[dict] = []
|
||||
condition: List[dict] = []
|
||||
action: List[dict] = []
|
||||
trigger: list[dict] = []
|
||||
condition: list[dict] = []
|
||||
action: list[dict] = []
|
||||
|
||||
def to_primitive(self):
|
||||
return self.dict(exclude_unset=True, exclude_none=True)
|
||||
|
||||
|
||||
class Automation(BaseAutomation):
|
||||
tags: Dict[str, str] = Field(default_factory=dict)
|
||||
tags: dict[str, str] = Field(default_factory=dict)
|
||||
|
||||
def to_primitive(self, include_tags: bool = False):
|
||||
out = super().to_primitive()
|
||||
@@ -32,8 +34,8 @@ class Automation(BaseAutomation):
|
||||
|
||||
class ExtenededAutomation(Automation):
|
||||
source_file: str
|
||||
source_file_type: Literal["list", "obj"]
|
||||
configuration_key: str
|
||||
source_file_type: Literal["list", "obj", "inline"]
|
||||
configuration_key: ConfigurationKey
|
||||
|
||||
def to_primitive(self, include_tags: bool = False):
|
||||
out = super().to_primitive(include_tags=include_tags)
|
||||
|
||||
Reference in new issue
Block a user