added root path for ha

This commit is contained in:
Ari Sosnovsky committed 2021-11-11 12:41:10 -05:00
1 parent c493c0a3d0
commit 3c082608f8
4 files changed
+19 -4

No files matched your search

+12 -2
View File
@@ -1,7 +1,7 @@
from typing import Dict, Iterable, List, Literal, Optional, Union
from pydantic.fields import Field
from src.json_serializer import NOT_IMPLEMENTED_SV_MSG, json_dumps, normalize_obj
from src.yaml_serializer import HassConfig, IncludedYaml, SecretValue
from src.yaml_serializer import HassConfig, IncludedYaml, SecretValue, dump_yaml, simple_load_yaml
from pydantic import BaseModel
# errors
@@ -189,4 +189,14 @@ class AutomationLoader:
def save(self, index: int, auto: AutomationData):
if isinstance(self.automation_ref, IncludedYaml):
self.automation_ref.original_path
with self.automation_ref.original_path.open("r") as f:
original_yaml = simple_load_yaml(f)
if not isinstance(original_yaml, list):
raise AutomationLoaderException(
f"Invalid data received from {self.automation_ref.original_path} expected a list"
)
original_yaml[index] = auto
with self.automation_ref.original_path.open("w") as f:
f.write(dump_yaml(original_yaml))
else:
raise NotImplementedError
+1 -1
View File
@@ -19,7 +19,7 @@ def load_hass_config(file_path: Path) -> HassConfig:
"""
HAY = HassYaml()
data = HAY.load(file_path / "configuration.yaml")
return HassConfig(HAY.secrets, data)
return HassConfig(HAY.secrets, data, file_path / "configuration.yaml")
def dump_yaml(obj: Any) -> str:
+1
View File
@@ -61,3 +61,4 @@ class HassConfig(NamedTuple):
secrets: dict # the secrets file content
config: dict # the complete configration.yaml tree
root_config_path: Path # where the root 'configuration.yaml' lives
+5 -1
View File
@@ -1,3 +1,4 @@
from pathlib import Path
from tempfile import mkdtemp
from unittest import TestCase
from src.config.AutomationLoader import (
@@ -26,7 +27,7 @@ class config_finder_tests(TestCase):
pass
def test_find_sample_config(self):
secrets, config = load_hass_config(SAMPLE_HA_PATH)
secrets, config, file_path = load_hass_config(SAMPLE_HA_PATH)
self.assertIsInstance(config["template"], IncludedYaml)
self.assertIsInstance(config["binary_sensor"], IncludedYaml)
self.assertIsInstance(config["group"], IncludedYaml)
@@ -36,6 +37,7 @@ class config_finder_tests(TestCase):
config["automation"].data[32]["trigger"][0]["device_id"],
SecretValue("baby_btn_device_id", "347492ffc6b909a55ebe08745fca1bf6"),
)
self.assertEqual(file_path, SAMPLE_HA_PATH / "configuration.yaml")
class AutomationLoader_tests(TestCase):
@@ -324,6 +326,7 @@ class AutomationLoader_tests(TestCase):
config={
"automation": "bob",
},
root_config_path=Path("."),
)
)
@@ -332,5 +335,6 @@ class AutomationLoader_tests(TestCase):
HassConfig(
secrets={},
config={},
root_config_path=Path("."),
)
)