additional api refactor
This commit is contained in:
1 parent
8792ed6ed8
commit
7cb1a5d907
11 files changed
+415
-271
No files matched your search
@@ -14,9 +14,9 @@ from .errors import (
|
||||
DBError,
|
||||
InvalidAutomationFile,
|
||||
)
|
||||
from .loader import extract_automation_paths, load_automation_path
|
||||
from .loader import extract_automation_paths, get_base_automation_key, load_automation_path
|
||||
from .tags import TagManager
|
||||
from .types import ExtenededAutomation
|
||||
from .types import Automation, ExtenededAutomation
|
||||
|
||||
logger = get_logger(__file__)
|
||||
|
||||
@@ -45,7 +45,14 @@ class AutomationManager:
|
||||
errors = []
|
||||
for config_key, p in extract_automation_paths(self.hass_config):
|
||||
try:
|
||||
automations = list(load_automation_path(p, config_key, self.tag_manager))
|
||||
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)
|
||||
@@ -65,32 +72,45 @@ class AutomationManager:
|
||||
def get(self, auto_id: str):
|
||||
return self.db.get_automation(auto_id)
|
||||
|
||||
def upsert(self, automation: ExtenededAutomation):
|
||||
if not automation.source_file.parent.exists():
|
||||
automation.source_file.parent.mkdir(parents=True)
|
||||
def create(self, automation: Automation):
|
||||
config_key, config_path = get_base_automation_key(self.hass_config)
|
||||
return self.update(
|
||||
ExtenededAutomation(
|
||||
**automation.dict(),
|
||||
configuration_key=config_key,
|
||||
source_file=config_path,
|
||||
source_file_type="list",
|
||||
),
|
||||
create_if_not_found=True,
|
||||
)
|
||||
|
||||
def update(self, automation: ExtenededAutomation, create_if_not_found: bool = False):
|
||||
automation_path = self.hass_config.root_path / automation.source_file
|
||||
if not automation_path.parent.exists():
|
||||
automation_path.parent.mkdir(parents=True)
|
||||
|
||||
objs: Optional[Union[list, dict]] = None
|
||||
if automation.source_file.exists():
|
||||
with automation.source_file.open("r") as fp:
|
||||
if automation_path.exists():
|
||||
with automation_path.open("r") as fp:
|
||||
objs = load_yaml(fp)
|
||||
|
||||
if automation.source_file_type == "obj":
|
||||
if isinstance(objs, list):
|
||||
raise AttemptingToOverwriteAnIncompatibleFileError(
|
||||
f"{automation.source_file} is a list, but expected a obj for {automation}"
|
||||
f"{automation_path} is a list, but expected a obj for {automation}"
|
||||
)
|
||||
raw_auto = automation.to_primitive(include_tags=False)
|
||||
automation.source_file.write_text(dump_yaml(raw_auto))
|
||||
automation_path.write_text(dump_yaml(raw_auto))
|
||||
else:
|
||||
if isinstance(objs, dict):
|
||||
raise AttemptingToOverwriteAnIncompatibleFileError(
|
||||
f"{automation.source_file} is a list, but expected a obj for {automation}"
|
||||
f"{automation_path} is a list, but expected a obj for {automation}"
|
||||
)
|
||||
elif objs is None:
|
||||
objs = []
|
||||
if not isinstance(objs, list):
|
||||
raise AssertionError(
|
||||
f"Attemption to save automation to {automation.source_file} but encountering an invalid list yaml file. Did you modify this file prior to saving?\n{automation}"
|
||||
f"Attemption to save automation to {automation_path} but encountering an invalid list yaml file. Did you modify this file prior to saving?\n{automation}"
|
||||
)
|
||||
found = False
|
||||
for i, obj in enumerate(objs):
|
||||
@@ -99,8 +119,13 @@ class AutomationManager:
|
||||
found = True
|
||||
break
|
||||
if not found:
|
||||
objs.append(automation.to_primitive(include_tags=False))
|
||||
automation.source_file.write_text(dump_yaml(objs))
|
||||
if create_if_not_found:
|
||||
objs.append(automation.to_primitive(include_tags=False))
|
||||
else:
|
||||
raise InvalidAutomationFile(
|
||||
"Could not update automations, not found in source file."
|
||||
)
|
||||
automation_path.write_text(dump_yaml(objs))
|
||||
self.update_tags(automation.id, automation.tags)
|
||||
|
||||
def update_tags(self, automation_id: str, tags: Dict[str, str]):
|
||||
@@ -115,18 +140,19 @@ class AutomationManager:
|
||||
self.tag_manager.save(self.tag_path)
|
||||
|
||||
def delete(self, automation: ExtenededAutomation):
|
||||
automation_path = self.hass_config.root_path / automation.source_file
|
||||
if automation.source_file_type == "obj":
|
||||
automation.source_file.unlink(missing_ok=True)
|
||||
automation_path.unlink(missing_ok=True)
|
||||
else:
|
||||
with automation.source_file.open("r") as fp:
|
||||
with automation_path.open("r") as fp:
|
||||
objs = load_yaml(fp)
|
||||
if not isinstance(objs, list):
|
||||
raise AssertionError(
|
||||
f"Attemption to save automation to {automation.source_file} but encountering an invalid list yaml file. Did you modify this file prior to saving?\n{automation}"
|
||||
f"Attemption to save automation to {automation_path} but encountering an invalid list yaml file. Did you modify this file prior to saving?\n{automation}"
|
||||
)
|
||||
kept = []
|
||||
for i, obj in enumerate(objs):
|
||||
if obj["id"] != automation.id:
|
||||
kept.append(obj)
|
||||
automation.source_file.write_text(dump_yaml(kept))
|
||||
automation_path.write_text(dump_yaml(kept))
|
||||
self.delete_tags(automation.id)
|
||||
Reference in new issue
Block a user