initial manager work

This commit is contained in:
ari committed 2022-08-14 16:50:58 -04:00
1 parent 08af01dd11
commit 7aadfbc175
15 files changed
+217 -80

No files matched your search

@@ -1,9 +1,13 @@
- id: "sublist3-mlist2"
alias: Sublist 3 - Merged List 2
description: ""
description: "That was correct!"
trigger: []
condition: []
action: []
action:
- service: "switch.turn_off"
target:
entity_id: "switch.bathroom"
data:
mode: single
- id: "sublist3-mlist3"
alias: Sublist 3 - Merged List 3
+29 -4
View File
@@ -1,3 +1,4 @@
from pathlib import Path
from src.automations_v2.types import ExtenededAutomationData
from .utils import TestWithDB
@@ -19,13 +20,13 @@ class db_tests(TestWithDB):
ExtenededAutomationData(
id="test",
alias="test",
source_file="automation.yaml",
source_file=Path("automation.yaml").absolute(),
source_file_type="obj",
),
ExtenededAutomationData(
id="test2",
alias="test",
source_file="automation.yaml",
source_file=Path("automation.yaml").absolute(),
source_file_type="obj",
action=[{"event": {"nice": "one"}, "event_type": "custom"}],
),
@@ -42,13 +43,13 @@ class db_tests(TestWithDB):
ExtenededAutomationData(
id="test",
alias="test",
source_file="automation.yaml",
source_file=Path("automation.yaml").absolute(),
source_file_type="obj",
),
ExtenededAutomationData(
id="test",
alias="test",
source_file="automation.yaml",
source_file=Path("automation.yaml").absolute(),
source_file_type="obj",
action=[{"event": {"nice": "one"}, "event_type": "custom"}],
),
@@ -79,3 +80,27 @@ class db_tests(TestWithDB):
self.assertEqual(self.db.count_automations(), 2)
self.db.delete_automations(originals)
self.assertEqual(self.db.count_automations(), 0)
def test_delete_automations_by_source_type(self):
originals = [
ExtenededAutomationData(
id="test",
alias="test",
source_file="automation2.yaml",
source_file_type="obj",
),
ExtenededAutomationData(
id="test2",
alias="test",
source_file="automation.yaml",
source_file_type="obj",
action=[{"event": {"nice": "one"}, "event_type": "custom"}],
),
]
self.assertEqual(self.db.count_automations(), 0)
self.db.upsert_automations(originals)
self.assertEqual(self.db.count_automations(), 2)
self.db.delete_automations_in_source_file(Path("automation2.yaml"))
self.assertEqual(self.db.count_automations(), 1)
[auto] = self.db.list_automations(0, 10)
self.assertEqual(auto.id, "test2")
@@ -0,0 +1,45 @@
from time import sleep
from src.hass_config.loader import HassConfig
from src.automations_v2.manager import AutomationManager
from tests.utils import HA_CONFIG6_EXAMPLE, create_copy_of_example_config
from .utils import TestWithDB
class manager_tests(TestWithDB):
def test_load_complex_read_modify_delete(self):
config_path = create_copy_of_example_config(HA_CONFIG6_EXAMPLE)
automation_manager = AutomationManager(
HassConfig(config_path),
self.db_file,
)
self.assertEqual(automation_manager.config_watcher.wait_until_next_event(True), "loaded")
self.assertEqual(automation_manager.count(), 12)
automation = automation_manager.get("sublist3-mlist2")
self.assertEqual(automation.description, "That was correct!")
self.assertEqual(
automation.action,
[
{
"service": "switch.turn_off",
"target": {"entity_id": "switch.bathroom"},
"data": None,
}
],
)
automation.trigger.append(
{"platform": "zone", "entity_id": "person.thor", "zone": "zone.azguard"}
)
automation_manager.update(automation)
sleep(2)
automation = automation_manager.get("sublist3-mlist2")
self.assertEqual(
automation.trigger,
[
{
"platform": "zone",
"entity_id": "person.thor",
"zone": "zone.azguard",
}
],
)
@@ -1,12 +1,23 @@
from pathlib import Path
from time import sleep
from src.automations_v2.watchers.automation import AutomationFileWatcher
from tests.utils import HA_CONFIG6_EXAMPLE, HA_CONFIG_EXAMPLE, create_copy_of_example_config
from .utils import TestWithDB
class automation_watcher_tests(TestWithDB):
def make_safe_watcher(self, automation_file: Path):
self.watcher = AutomationFileWatcher(automation_file, self.db_file)
return self.watcher
def tearDown(self) -> None:
if hasattr(self, "watcher"):
getattr(self, "watcher").join()
super().tearDown()
def test_bad_file(self):
example_folder = create_copy_of_example_config()
watcher = AutomationFileWatcher(example_folder / "automations.yaml", self.db_file)
watcher = self.make_safe_watcher(example_folder / "automations.yaml")
total = self.db.count_automations()
self.assertEqual(total, 0)
@@ -23,7 +34,7 @@ class automation_watcher_tests(TestWithDB):
def test_load_empty_file(self):
example_folder = create_copy_of_example_config()
(example_folder / "automations.yaml").write_text("")
watcher = AutomationFileWatcher(example_folder / "automations.yaml", self.db_file)
watcher = self.make_safe_watcher(example_folder / "automations.yaml")
total = self.db.count_automations()
self.assertEqual(total, 0)
@@ -34,7 +45,7 @@ class automation_watcher_tests(TestWithDB):
def test_watch_folder(self):
example_folder = create_copy_of_example_config(HA_CONFIG6_EXAMPLE)
watcher = AutomationFileWatcher(example_folder / "automations", self.db_file)
watcher = self.make_safe_watcher(example_folder / "automations")
watcher.start()
self.assertEqual(watcher.wait_until_next_event(True), "loaded")
self.assertEqual(self.db.count_automations(), 12)
@@ -42,7 +53,7 @@ class automation_watcher_tests(TestWithDB):
def test_rapid_changes(self):
example_folder = create_copy_of_example_config()
watcher = AutomationFileWatcher(example_folder / "automations.yaml", self.db_file)
watcher = self.make_safe_watcher(example_folder / "automations.yaml")
watcher.start()
self.assertEqual(watcher.wait_until_next_event(True), "loaded")
self.assertEqual(self.db.count_automations(), 32)
@@ -58,24 +69,6 @@ class automation_watcher_tests(TestWithDB):
(example_folder / "automations.yaml").write_text(
(HA_CONFIG_EXAMPLE / "automations.yaml").read_text()
)
self.assertEqual(watcher.wait_until_next_event(True), "loaded")
sleep(60)
self.assertEqual(self.db.count_automations(), 32)
watcher.join()
# def test_valid_changes(self):
# example_folder = create_copy_of_example_config()
# watcher = AutomationFileWatcher(example_folder / "automations.yaml", self.db_file)
# total = self.db.count_automations()
# self.assertEqual(total, 0)
# watcher.start()
# self.assertEqual(watcher.wait_until_next_event(True), 'loaded')
# self.assertEqual(self.db.count_automations(), 32)
# autos = self.db.list_automations(0, 32)
# (example_folder / "automations.yaml").write_text("haha!")
# watcher.wait_until_next_reload(True, timeout=60 * 10)
# self.assertEqual(self.db.count_automations(), 0)
# watcher.join()
@@ -1,4 +1,5 @@
from src.automations_v2.watchers.config import ConfigFileWatcher
from src.hass_config.loader import HassConfig
from .utils import TestWithDB, make_temp_db
from tests.utils import (
@@ -27,7 +28,7 @@ class config_watcher_tests(TestWithDB):
db.reset()
config_path = create_copy_of_example_config(p)
config_watcher = ConfigFileWatcher(
config_path=config_path,
config_path=HassConfig(config_path).get_configuration_path(),
automation_db_path=db.db_file,
)
self.assertEqual(db.count_automations(), 0)