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

+8
View File
@@ -1,2 +1,10 @@
class AutomationLoaderException(Exception):
pass
class InvalidAutomationFile(AutomationLoaderException):
pass
class FailedDeletion(AutomationLoaderException):
pass
+16 -11
View File
@@ -1,8 +1,10 @@
from itertools import chain
from typing import Iterable, Iterator
from pydantic.error_wrappers import ValidationError
from src.json_serializer import normalize_obj
from .types import AutomationData, AutomationMetdata
from .parsers import _parse_actions
from .errors import InvalidAutomationFile
# Single loader
def load_automation(
@@ -19,15 +21,18 @@ def load_automation(
"""
for auto_raw in automations_raw_data:
yield AutomationData(
metadata=AutomationMetdata.parse_obj(auto_raw),
sequence=list(
_parse_actions(
chain(
auto_raw.get("condition", []),
auto_raw.get("action", []),
try:
yield AutomationData(
metadata=AutomationMetdata.parse_obj(auto_raw),
sequence=list(
_parse_actions(
chain(
auto_raw.get("condition", []),
auto_raw.get("action", []),
)
)
)
),
trigger=[normalize_obj(obj) for obj in auto_raw.get("trigger", [])],
)
),
trigger=[normalize_obj(obj) for obj in auto_raw.get("trigger", [])],
)
except ValidationError as err:
raise InvalidAutomationFile from err
+34 -1
View File
@@ -1,4 +1,5 @@
from typing import Iterator, Optional
from typing import Iterator, List, Optional
from src.automations.errors import FailedDeletion
from src.automations.types import AutomationData
from src.hass_config.loader import HassConfig
from .loader import load_automation
@@ -26,3 +27,35 @@ class AutomationManager:
if i == index:
return next(load_automation(([auto])))
return None
def update(self, index: int, automation: AutomationData):
automations: List[dict] = []
updated = False
for i, orig_auto in enumerate(self.hass_config.automations):
if i == index:
automations.append(automation.to_primitive())
updated = True
else:
automations.append(orig_auto)
if not updated:
automations.append(automation.to_primitive())
self.hass_config.save_automations(automations)
def delete(self, index: int):
automations: List[dict] = []
deleted = False
for i, orig_auto in enumerate(self.hass_config.automations):
if i == index:
deleted = True
else:
automations.append(orig_auto)
if not deleted:
raise FailedDeletion("No such automation found")
self.hass_config.save_automations(automations)
# TODO: improve on this without keeping things in memory
def get_total_items(self) -> int:
count = 0
for i, _ in enumerate(self.hass_config.automations):
count = i
return count + 1
+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})
)
+81 -12
View File
@@ -1,23 +1,92 @@
from unittest import TestCase
from tests.utils import HA_CONFIG_EXAMPLE, create_dummy_config_folder
from src.hass_config.loader import HassConfig
from src.automations.manager import AutomationManager
example_hass_config = HassConfig(HA_CONFIG_EXAMPLE)
example_automation_loader = AutomationManager(example_hass_config)
from src.automations.errors import FailedDeletion, InvalidAutomationFile
from src.automations.types import AutomationData, AutomationMetdata
from tests.utils import (
get_dummy_automation_loader,
get_example_automation_loader,
)
class manager_tests(TestCase):
def test_find_limit_and_get(self):
first_auto_with_find = list(example_automation_loader.find(limit=1))
first_auto_with_get = example_automation_loader.get(0)
_, _, loader = get_example_automation_loader()
first_auto_with_find = list(loader.find(limit=1))
first_auto_with_get = loader.get(0)
self.assertEqual(first_auto_with_find[0], first_auto_with_get)
def test_find_at_most_with_offset(self):
autos = list(example_automation_loader.find(limit=10, offset=10))
_, _, loader = get_example_automation_loader()
autos = list(loader.find(limit=10, offset=10))
self.assertTrue(len(autos), 10)
def test_find_invalid(self):
self.assertIsNone(example_automation_loader.get(-1))
self.assertIsNone(example_automation_loader.get(1_000))
self.assertIsNone(example_automation_loader.get(31))
_, _, loader = get_example_automation_loader()
self.assertIsNone(loader.get(-1))
self.assertIsNone(loader.get(1_000))
self.assertIsNone(loader.get(31))
def test_length(self):
_, _, loader = get_example_automation_loader()
self.assertEqual(loader.get_total_items(), 31)
def test_bad_automations_file(self):
folder, _, manager = get_dummy_automation_loader(
[],
automation_in_conifguration_mode="none",
)
(folder / "automations.yaml").write_text("bob is an uncle")
with self.assertRaises(InvalidAutomationFile):
manager.get(0)
def test_create_automation(self):
_, _, manager = get_dummy_automation_loader(
[],
automation_in_conifguration_mode="none",
)
manager.update(1, AutomationData(metadata=AutomationMetdata(id="test1")))
auto = manager.get(0)
self.assertIsNotNone(auto)
self.assertEqual(auto.metadata.id, "test1") # type: ignore
def test_update_automation(self):
_, _, manager = get_dummy_automation_loader(
[
AutomationData(metadata=AutomationMetdata(id="test1")),
AutomationData(metadata=AutomationMetdata(id="test2")),
AutomationData(metadata=AutomationMetdata(id="test3")),
],
automation_in_conifguration_mode="none",
)
auto = manager.get(0)
self.assertEqual(auto.metadata.id, "test1") # type: ignore
manager.update(0, AutomationData(metadata=AutomationMetdata(id="toaster-7")))
auto = manager.get(0)
self.assertEqual(auto.metadata.id, "toaster-7") # type: ignore
def test_delete_automation(self):
_, _, manager = get_dummy_automation_loader(
[
AutomationData(metadata=AutomationMetdata(id="test1")),
AutomationData(metadata=AutomationMetdata(id="test2")),
AutomationData(metadata=AutomationMetdata(id="test3")),
],
automation_in_conifguration_mode="none",
)
self.assertEqual(manager.get_total_items(), 3)
manager.delete(1)
self.assertEqual(manager.get_total_items(), 2)
for auto in manager.find(0, 1000):
self.assertNotEqual(auto.metadata.id, "test2")
def test_failed_deletion_automation(self):
_, _, manager = get_dummy_automation_loader(
[
AutomationData(metadata=AutomationMetdata(id="test1")),
AutomationData(metadata=AutomationMetdata(id="test2")),
AutomationData(metadata=AutomationMetdata(id="test3")),
],
automation_in_conifguration_mode="none",
)
self.assertEqual(manager.get_total_items(), 3)
with self.assertRaises(FailedDeletion):
manager.delete(10)
@@ -221,27 +221,6 @@ class parser_tests(TestCase):
for expected, parsed in zip(expected_process, _parse_actions(raw_conditions)):
self.assertEqual(parsed, expected)
# def test_invalid_automations(self):
# with self.assertRaises(AutomationLoaderException):
# AutomationLoader(
# HassConfig(
# secrets={},
# config={
# "automation": "bob",
# },
# root_config_path=Path("."),
# )
# )
# with self.assertRaises(AutomationLoaderException):
# AutomationLoader(
# HassConfig(
# secrets={},
# config={},
# root_config_path=Path("."),
# )
# )
# def test_overwriting_one_auto_in_separate_file(self):
# # create dummy data
# config_path = create_dummy_config_folder(
@@ -1,5 +1,6 @@
from unittest import TestCase
from src.automations.types import AutomationData, AutomationMetdata
from src.hass_config.errors import MissingFile
from src.hass_config.loader import HassConfig
from tests.utils import create_dummy_config_folder
@@ -34,3 +35,14 @@ class loader_tests(TestCase):
self.assertIsInstance(hass_config.configurations, dict)
self.assertFalse("automation" in hass_config.configurations)
self.assertEqual(hass_config.automations[0]["id"], "test")
def test_missing_automations(self):
folder = create_dummy_config_folder(
[AutomationData(metadata=AutomationMetdata(id="test"))],
automation_in_conifguration_mode="none",
)
(folder / "automations.yaml").unlink()
hass_config = HassConfig(folder)
self.assertIsInstance(hass_config.configurations, dict)
with self.assertRaises(MissingFile):
list(hass_config.automations)
+56 -1
View File
@@ -1,7 +1,10 @@
from shutil import copytree
from tempfile import mkdtemp
from pathlib import Path
from typing import List, Literal, Optional
from typing import List, Literal, Optional, Tuple
from src.automations.manager import AutomationManager
from src.automations.types import AutomationData
from src.hass_config.loader import HassConfig
from src.yaml_serializer import dump_yaml
from src.yaml_serializer.types import IncludedYaml
@@ -11,6 +14,58 @@ SAMPLES_FOLDER = THIS_FOLDER / "samples"
HA_CONFIG_EXAMPLE = SAMPLES_FOLDER / "example_config_folder"
def get_example_automation_loader() -> Tuple[Path, HassConfig, AutomationManager]:
"""Creates an example automation loader from the samples folder
Returns:
Tuple[Path, HassConfig, AutomationManager]
"""
root_folder = create_copy_of_example_config()
hass_config = HassConfig(root_folder)
automation_loader = AutomationManager(hass_config)
return root_folder, hass_config, automation_loader
def create_copy_of_example_config() -> Path:
"""Creates a copy of the example config folder
Returns:
Path
"""
root_folder = Path(mkdtemp())
root_folder.rmdir()
copytree(HA_CONFIG_EXAMPLE, root_folder)
return root_folder
def get_dummy_automation_loader(
auto: List[AutomationData],
secrets: Optional[dict] = None,
other_config: Optional[dict] = None,
automation_in_conifguration_mode: Literal["include", "inline", "none"] = "include",
) -> Tuple[Path, HassConfig, AutomationManager]:
"""Creates a dummy /config structure for testing and return the loader and config
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.
automation_in_conifguration_mode (Literal['include', 'inline', 'none'], optional): whether to !include automation as a separete file or in the configuration yaml. Defaults to inline.
Returns:
Path: [description]
"""
root_folder = create_dummy_config_folder(
auto,
secrets,
other_config,
automation_in_conifguration_mode,
)
hass_config = HassConfig(root_folder)
automation_loader = AutomationManager(hass_config)
return root_folder, hass_config, automation_loader
def create_dummy_config_folder(
auto: List[AutomationData],
secrets: Optional[dict] = None,