added upesert for autos
This commit is contained in:
1 parent
3c082608f8
commit
9e23885b28
3 files changed
+166
-16
No files matched your search
@@ -15,12 +15,21 @@ class AutomationConditionNode(BaseModel):
|
||||
condition_data: dict
|
||||
node_type: Literal["condition"] = Field(alias="$smType", default="condition")
|
||||
|
||||
def to_primitive(self):
|
||||
return {
|
||||
**self.condition_data,
|
||||
"condition": self.condition,
|
||||
}
|
||||
|
||||
|
||||
class AutomationActionNode(BaseModel):
|
||||
action: str
|
||||
action_data: dict
|
||||
node_type: Literal["action"] = Field(alias="$smType", default="action")
|
||||
|
||||
def to_primitive(self):
|
||||
return self.action_data
|
||||
|
||||
|
||||
class AutomationMetdata(BaseModel):
|
||||
id: str
|
||||
@@ -36,6 +45,14 @@ class AutomationData(BaseModel):
|
||||
condition: List[AutomationConditionNode] = []
|
||||
action: List[Union[AutomationConditionNode, AutomationActionNode]] = []
|
||||
|
||||
def to_primitive(self):
|
||||
return {
|
||||
**self.metadata.dict(),
|
||||
"trigger": self.trigger,
|
||||
"condition": [condition.to_primitive() for condition in self.condition],
|
||||
"action": [action.to_primitive() for action in self.action],
|
||||
}
|
||||
|
||||
|
||||
# Single loader
|
||||
def load_automation(
|
||||
@@ -143,8 +160,9 @@ def _parse_conditions(
|
||||
|
||||
# Global Loader
|
||||
class AutomationLoader:
|
||||
def __init__(self, loaded_yaml: HassConfig) -> None:
|
||||
self.automation_ref = loaded_yaml.config.get("automation", None)
|
||||
def __init__(self, hass_config: HassConfig) -> None:
|
||||
self.hass_config = hass_config
|
||||
self.automation_ref = hass_config.config.get("automation", None)
|
||||
|
||||
if isinstance(self.automation_ref, IncludedYaml):
|
||||
self.automation_raw_data: list = self.automation_ref.data
|
||||
@@ -188,15 +206,48 @@ class AutomationLoader:
|
||||
return len(self.automations_data)
|
||||
|
||||
def save(self, index: int, auto: AutomationData):
|
||||
if isinstance(self.automation_ref, IncludedYaml):
|
||||
"""Save or create automation on disk and in-memory
|
||||
|
||||
Args:
|
||||
index (int): index of the automation
|
||||
auto (AutomationData): automation data
|
||||
|
||||
"""
|
||||
# Update in-memory store
|
||||
if len(self) <= index:
|
||||
self.automations_data.append(auto)
|
||||
else:
|
||||
self.automations_data[index] = auto
|
||||
# Load a simple version of the latest data on disk
|
||||
if not isinstance(self.automation_ref, IncludedYaml):
|
||||
with self.hass_config.root_config_path.open("r") as f:
|
||||
hass_config_yaml = simple_load_yaml(f)
|
||||
original_yaml_path = f"{self.hass_config.root_config_path}>automation"
|
||||
if "automation" not in hass_config_yaml:
|
||||
raise AutomationLoaderException(
|
||||
f'missing "automation" key in {self.hass_config.root_config_path}'
|
||||
)
|
||||
original_yaml = hass_config_yaml["automation"]
|
||||
else:
|
||||
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
|
||||
original_yaml_path = f"{self.automation_ref.original_path}"
|
||||
|
||||
if not isinstance(original_yaml, list):
|
||||
raise AutomationLoaderException(
|
||||
f"Invalid data received from {original_yaml_path} expected a list"
|
||||
)
|
||||
# Upsert automation
|
||||
if len(original_yaml) <= index:
|
||||
original_yaml.append(auto.to_primitive())
|
||||
else:
|
||||
original_yaml[index] = auto.to_primitive()
|
||||
|
||||
# Save back to disk
|
||||
if isinstance(self.automation_ref, IncludedYaml):
|
||||
with self.automation_ref.original_path.open("w") as f:
|
||||
f.write(dump_yaml(original_yaml))
|
||||
else:
|
||||
raise NotImplementedError
|
||||
hass_config_yaml["automation"] = original_yaml
|
||||
with self.hass_config.root_config_path.open("w") as f:
|
||||
f.write(dump_yaml(hass_config_yaml))
|
||||
@@ -18,7 +18,7 @@ from src.yaml_serializer import (
|
||||
load_hass_config,
|
||||
IncludedYaml,
|
||||
)
|
||||
from tests.utils import SAMPLE_HA_PATH
|
||||
from tests.utils import SAMPLE_HA_PATH, create_dummy_config_folder
|
||||
from src.yaml_serializer.types import HassConfig
|
||||
|
||||
|
||||
@@ -338,3 +338,80 @@ class AutomationLoader_tests(TestCase):
|
||||
root_config_path=Path("."),
|
||||
)
|
||||
)
|
||||
|
||||
def test_overwriting_one_auto_in_separate_file(self):
|
||||
# create dummy data
|
||||
config_path = create_dummy_config_folder(
|
||||
auto=[
|
||||
AutomationData(
|
||||
metadata=AutomationMetdata(
|
||||
id="01",
|
||||
)
|
||||
),
|
||||
AutomationData(
|
||||
metadata=AutomationMetdata(
|
||||
id="02",
|
||||
)
|
||||
),
|
||||
AutomationData(
|
||||
metadata=AutomationMetdata(
|
||||
id="03",
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
# check the data is loaded well
|
||||
automation_loader = AutomationLoader(load_hass_config(config_path))
|
||||
self.assertEqual(automation_loader.get(0).metadata.id, "01")
|
||||
self.assertEqual(automation_loader.get(1).metadata.id, "02")
|
||||
self.assertEqual(automation_loader.get(2).metadata.id, "03")
|
||||
self.assertIsNone(automation_loader.get(3))
|
||||
self.assertEqual(len(automation_loader), 3)
|
||||
# update auto #1
|
||||
automation_loader.save(
|
||||
1,
|
||||
AutomationData(
|
||||
metadata=AutomationMetdata(id="02", alias="flush toilet!"),
|
||||
trigger=[{"event": "toilet.flushed"}],
|
||||
),
|
||||
)
|
||||
# check that in-memory data is correctly updated
|
||||
self.assertEqual(automation_loader.get(1).metadata.id, "02")
|
||||
self.assertEqual(automation_loader.get(1).metadata.alias, "flush toilet!")
|
||||
self.assertListEqual(automation_loader.get(1).trigger, [{"event": "toilet.flushed"}])
|
||||
|
||||
# check that disk data is correctly updated
|
||||
reloaded_automation_loader = AutomationLoader(load_hass_config(config_path))
|
||||
self.assertEqual(len(automation_loader), 3)
|
||||
self.assertEqual(reloaded_automation_loader.get(1).metadata.id, "02")
|
||||
self.assertEqual(reloaded_automation_loader.get(1).metadata.alias, "flush toilet!")
|
||||
self.assertListEqual(
|
||||
reloaded_automation_loader.get(1).trigger, [{"event": "toilet.flushed"}]
|
||||
)
|
||||
# add new auto
|
||||
automation_loader.save(
|
||||
3,
|
||||
AutomationData(
|
||||
metadata=AutomationMetdata(id="newest", alias="testing autos"),
|
||||
condition=[
|
||||
AutomationConditionNode(condition="time", condition_data={"at": "10:00:00"})
|
||||
],
|
||||
),
|
||||
)
|
||||
# check that in-memory data is correctly updated
|
||||
self.assertEqual(len(automation_loader), 4)
|
||||
self.assertEqual(automation_loader.get(3).metadata.id, "newest")
|
||||
self.assertEqual(automation_loader.get(3).metadata.alias, "testing autos")
|
||||
self.assertListEqual(
|
||||
automation_loader.get(3).condition,
|
||||
[AutomationConditionNode(condition="time", condition_data={"at": "10:00:00"})],
|
||||
)
|
||||
# check that disk data is correctly updated
|
||||
reloaded_automation_loader = AutomationLoader(load_hass_config(config_path))
|
||||
self.assertEqual(len(reloaded_automation_loader), 4)
|
||||
self.assertEqual(reloaded_automation_loader.get(3).metadata.id, "newest")
|
||||
self.assertEqual(reloaded_automation_loader.get(3).metadata.alias, "testing autos")
|
||||
self.assertListEqual(
|
||||
reloaded_automation_loader.get(3).condition,
|
||||
[AutomationConditionNode(condition="time", condition_data={"at": "10:00:00"})],
|
||||
)
|
||||
+28
-6
@@ -1,22 +1,44 @@
|
||||
from tempfile import mkdtemp
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from typing import List, Optional
|
||||
from src.config.AutomationLoader import AutomationData
|
||||
|
||||
from src.yaml_serializer import dump_yaml
|
||||
from src.yaml_serializer.types import IncludedYaml
|
||||
|
||||
THIS_FOLDER = Path(__file__).parent
|
||||
SAMPLE_HA_PATH = THIS_FOLDER / "samples"
|
||||
SAMPLE_DB_PATH = SAMPLE_HA_PATH / "home-assistant_v2.db"
|
||||
|
||||
|
||||
def create_dummy_auto_folder(
|
||||
auto: dict,
|
||||
def create_dummy_config_folder(
|
||||
auto: List[AutomationData],
|
||||
secrets: Optional[dict] = None,
|
||||
other_config: Optional[dict] = None,
|
||||
include_auto_in_configuration: bool = False,
|
||||
) -> Path:
|
||||
"""Creates a dummy /config structure for testing
|
||||
|
||||
Args:
|
||||
auto (List[AutomationData]): automations
|
||||
secrets (Optional[dict], optional): dictionary of secrets
|
||||
other_config (Optional[dict], optional): some config stuff to place into configuration.yaml. Defaults to None.
|
||||
include_auto_in_configuration (bool, optional): whether to !include automation as a separete file or in the configuration yaml. Defaults to False.
|
||||
|
||||
Returns:
|
||||
Path: [description]
|
||||
"""
|
||||
root_folder = Path(mkdtemp())
|
||||
auto_prims = [a.to_primitive() for a in auto]
|
||||
configuration_yaml = {**other_config} if other_config is not None else {}
|
||||
if include_auto_in_configuration:
|
||||
configuration_yaml["automation"] = auto
|
||||
configuration_yaml["automation"] = auto_prims
|
||||
else:
|
||||
configuration_yaml["automation"] = "!include automations.yaml"
|
||||
# (root_folder / 'automations.yaml').write_text(yaml)
|
||||
configuration_yaml["automation"] = IncludedYaml(
|
||||
"automations.yaml", root_folder / "automations.yaml", auto
|
||||
)
|
||||
(root_folder / "automations.yaml").write_text(dump_yaml(auto_prims))
|
||||
if secrets is not None:
|
||||
(root_folder / "secrets.yaml").write_text(dump_yaml(secrets))
|
||||
(root_folder / "configuration.yaml").write_text(dump_yaml(configuration_yaml))
|
||||
return root_folder
|
||||
Reference in new issue
Block a user