functioning manager

This commit is contained in:
ari committed 2022-01-03 21:25:00 -05:00
1 parent ef5715ea6c
commit 00505c0faa
8 files changed
+239 -54

No files matched your search

+32 -8
View File
@@ -1,8 +1,9 @@
from pathlib import Path
from typing import List
from typing import List, Optional
from src.yaml_serializer import load_yaml
from src.yaml_serializer import load_yaml, dump_yaml
from src.yaml_serializer.types import IncludedYaml
from .errors import MissingFile
class HassConfig:
@@ -11,18 +12,41 @@ class HassConfig:
@property
def configurations(self) -> dict:
with (self.root_path / "configuration.yaml").open("r") as f:
with self.get_configuration_path().open("r") as f:
return dict(load_yaml(f)) # type: ignore
@property
def automations(self) -> List[dict]:
automation_path = self.root_path / "automations.yaml"
if automation_path := self.get_automation_path():
try:
with automation_path.open("r") as f:
return load_yaml(f) # type: ignore
except FileNotFoundError as err:
raise MissingFile("automations.yaml") from err
return self.configurations["automation"]
def get_configuration_path(self) -> Path:
return self.root_path / "configuration.yaml"
def get_automation_path(self) -> Optional[Path]:
"""Get Automation Path (if not included in configuration.yaml, in this case the return value is None)
Returns:
Optional[Path]
"""
config = self.configurations
if "automation" in config:
automation_ref = config["automation"]
if isinstance(automation_ref, IncludedYaml):
automation_path = self.root_path / automation_ref.path_str
return self.root_path / automation_ref.path_str
else:
return config["automation"]
with automation_path.open("r") as f:
return load_yaml(f) # type: ignore
return None
return self.root_path / "automations.yaml"
def save_automations(self, automations: List[dict]):
if automation_path := self.get_automation_path():
automation_path.write_text(dump_yaml(automations))
else:
self.get_configuration_path().write_text(
dump_yaml({**self.configurations, "automation": automations})
)