refactor + cleanup
This commit is contained in:
1 parent
4a08956e1a
commit
381835c642
34 files changed
+724
-933
No files matched your search
Submodule
+1
Submodule api/tests/samples/example_config_folder added at 2f2c69d6db.
Whitespace-only changes.
@@ -0,0 +1,67 @@
|
||||
from unittest import TestCase
|
||||
from src.automations.types import (
|
||||
AutomationActionNode,
|
||||
AutomationConditionNode,
|
||||
AutomationData,
|
||||
AutomationMetdata,
|
||||
)
|
||||
from src.automations.loader import load_automation
|
||||
from src.json_serializer import NOT_IMPLEMENTED_SV_MSG
|
||||
from src.yaml_serializer.types import (
|
||||
SecretValue,
|
||||
)
|
||||
|
||||
|
||||
class loader_tests(TestCase):
|
||||
def test_load_automation_runs(self):
|
||||
auto = list(
|
||||
load_automation(
|
||||
[
|
||||
{
|
||||
"id": "1616887975473",
|
||||
"alias": "Home Assistant Off",
|
||||
"description": "",
|
||||
"trigger": [{"platform": "homeassistant", "event": "shutdown"}],
|
||||
"condition": ["states(time.time) >= '10:00:00'"],
|
||||
"action": [
|
||||
{
|
||||
"device_id": SecretValue("my_mobile_phone", "..."),
|
||||
"domain": "mobile_app",
|
||||
"type": "notify",
|
||||
"title": "Hassio Status",
|
||||
"message": "Hassio Is turning off...",
|
||||
}
|
||||
],
|
||||
"mode": "single",
|
||||
}
|
||||
]
|
||||
)
|
||||
)
|
||||
self.assertEqual(
|
||||
auto[0],
|
||||
AutomationData(
|
||||
metadata=AutomationMetdata(
|
||||
id="1616887975473",
|
||||
alias="Home Assistant Off",
|
||||
description="",
|
||||
mode="single",
|
||||
),
|
||||
trigger=[{"platform": "homeassistant", "event": "shutdown"}],
|
||||
sequence=[
|
||||
AutomationConditionNode(
|
||||
condition="template",
|
||||
condition_data={"value_template": "states(time.time) >= '10:00:00'"},
|
||||
),
|
||||
AutomationActionNode(
|
||||
action="device",
|
||||
action_data={
|
||||
"device_id": NOT_IMPLEMENTED_SV_MSG,
|
||||
"domain": "mobile_app",
|
||||
"type": "notify",
|
||||
"title": "Hassio Status",
|
||||
"message": "Hassio Is turning off...",
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
+110
-192
@@ -1,46 +1,19 @@
|
||||
from pathlib import Path
|
||||
from tempfile import mkdtemp
|
||||
from unittest import TestCase
|
||||
from src.config.AutomationLoader import (
|
||||
from src.automations.types import (
|
||||
AutomationActionNode,
|
||||
AutomationConditionNode,
|
||||
AutomationData,
|
||||
AutomationLoader,
|
||||
AutomationLoaderException,
|
||||
AutomationMetdata,
|
||||
load_automation,
|
||||
)
|
||||
from src.automations.parsers import (
|
||||
_parse_conditions,
|
||||
_parse_actions,
|
||||
)
|
||||
from src.json_serializer import NOT_IMPLEMENTED_SV_MSG
|
||||
from src.yaml_serializer import (
|
||||
from src.yaml_serializer.types import (
|
||||
SecretValue,
|
||||
load_hass_config,
|
||||
IncludedYaml,
|
||||
)
|
||||
from tests.utils import SAMPLE_HA_PATH, create_dummy_config_folder
|
||||
from src.yaml_serializer.types import HassConfig
|
||||
|
||||
|
||||
class config_finder_tests(TestCase):
|
||||
def test_simple_made_up_config(self):
|
||||
pass
|
||||
|
||||
def test_find_sample_config(self):
|
||||
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)
|
||||
self.assertIsInstance(config["switch"], IncludedYaml)
|
||||
self.assertEqual(secrets["myamazingpassword"], "holymoly")
|
||||
self.assertEqual(
|
||||
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):
|
||||
class parser_tests(TestCase):
|
||||
def test_various_condition_types(self):
|
||||
raw_conditions = [
|
||||
{"condition": "template", "value_template": "silly logic here"},
|
||||
@@ -248,170 +221,115 @@ class AutomationLoader_tests(TestCase):
|
||||
for expected, parsed in zip(expected_process, _parse_actions(raw_conditions)):
|
||||
self.assertEqual(parsed, expected)
|
||||
|
||||
def test_load_automation_runs(self):
|
||||
auto = list(
|
||||
load_automation(
|
||||
[
|
||||
{
|
||||
"id": "1616887975473",
|
||||
"alias": "Home Assistant Off",
|
||||
"description": "",
|
||||
"trigger": [{"platform": "homeassistant", "event": "shutdown"}],
|
||||
"condition": ["states(time.time) >= '10:00:00'"],
|
||||
"action": [
|
||||
{
|
||||
"device_id": SecretValue("my_mobile_phone", "..."),
|
||||
"domain": "mobile_app",
|
||||
"type": "notify",
|
||||
"title": "Hassio Status",
|
||||
"message": "Hassio Is turning off...",
|
||||
}
|
||||
],
|
||||
"mode": "single",
|
||||
}
|
||||
]
|
||||
)
|
||||
)
|
||||
self.assertEqual(
|
||||
auto[0],
|
||||
AutomationData(
|
||||
metadata=AutomationMetdata(
|
||||
id="1616887975473",
|
||||
alias="Home Assistant Off",
|
||||
description="",
|
||||
mode="single",
|
||||
),
|
||||
trigger=[{"platform": "homeassistant", "event": "shutdown"}],
|
||||
condition=[
|
||||
AutomationConditionNode(
|
||||
condition="template",
|
||||
condition_data={"value_template": "states(time.time) >= '10:00:00'"},
|
||||
)
|
||||
],
|
||||
action=[
|
||||
AutomationActionNode(
|
||||
action="device",
|
||||
action_data={
|
||||
"device_id": NOT_IMPLEMENTED_SV_MSG,
|
||||
"domain": "mobile_app",
|
||||
"type": "notify",
|
||||
"title": "Hassio Status",
|
||||
"message": "Hassio Is turning off...",
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
)
|
||||
# def test_finding_with_AutomationLoader(self):
|
||||
# automation_loader = AutomationLoader(load_hass_config(SAMPLE_HA_PATH))
|
||||
|
||||
def test_finding_with_AutomationLoader(self):
|
||||
automation_loader = AutomationLoader(load_hass_config(SAMPLE_HA_PATH))
|
||||
# first_auto_with_find = list(automation_loader.find(limit=1))
|
||||
# first_auto_with_get = automation_loader.get(0)
|
||||
# self.assertEqual(first_auto_with_find[0], first_auto_with_get)
|
||||
|
||||
first_auto_with_find = list(automation_loader.find(limit=1))
|
||||
first_auto_with_get = automation_loader.get(0)
|
||||
self.assertEqual(first_auto_with_find[0], first_auto_with_get)
|
||||
# kitchen_autos = list(automation_loader.find(alias="kitchen"))
|
||||
# self.assertTrue(len(kitchen_autos), 3)
|
||||
|
||||
kitchen_autos = list(automation_loader.find(alias="kitchen"))
|
||||
self.assertTrue(len(kitchen_autos), 3)
|
||||
# self.assertIsNone(automation_loader.get(-1))
|
||||
# self.assertIsNone(automation_loader.get(1_000))
|
||||
# self.assertIsNone(automation_loader.get(31))
|
||||
# self.assertEqual(len(automation_loader), 31)
|
||||
|
||||
self.assertIsNone(automation_loader.get(-1))
|
||||
self.assertIsNone(automation_loader.get(1_000))
|
||||
self.assertIsNone(automation_loader.get(35))
|
||||
self.assertEqual(len(automation_loader), 34)
|
||||
# def test_invalid_automations(self):
|
||||
# with self.assertRaises(AutomationLoaderException):
|
||||
# AutomationLoader(
|
||||
# HassConfig(
|
||||
# secrets={},
|
||||
# config={
|
||||
# "automation": "bob",
|
||||
# },
|
||||
# root_config_path=Path("."),
|
||||
# )
|
||||
# )
|
||||
|
||||
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("."),
|
||||
# )
|
||||
# )
|
||||
|
||||
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(
|
||||
# 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"}])
|
||||
|
||||
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"})],
|
||||
)
|
||||
# # 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"),
|
||||
# sequence=[
|
||||
# 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).sequence,
|
||||
# [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).sequence,
|
||||
# [AutomationConditionNode(condition="time", condition_data={"at": "10:00:00"})],
|
||||
# )
|
||||
Whitespace-only changes.
@@ -0,0 +1,38 @@
|
||||
from unittest import TestCase
|
||||
from src.automations.types import AutomationData, AutomationMetdata
|
||||
from src.hass_config.loader import HassConfig
|
||||
from tests.utils import HA_CONFIG_EXAMPLE, create_dummy_config_folder
|
||||
|
||||
example_hass_config = HassConfig(HA_CONFIG_EXAMPLE)
|
||||
|
||||
|
||||
class loader_tests(TestCase):
|
||||
def test_load_automation_from_config_yaml(self):
|
||||
folder = create_dummy_config_folder(
|
||||
[AutomationData(metadata=AutomationMetdata(id="test"))],
|
||||
automation_in_conifguration_mode="inline",
|
||||
)
|
||||
hass_config = HassConfig(folder)
|
||||
self.assertIsInstance(hass_config.configurations, dict)
|
||||
self.assertTrue("automation" in hass_config.configurations)
|
||||
self.assertEqual(hass_config.automations[0]["id"], "test")
|
||||
|
||||
def test_load_automation_from_config_yaml_inclue(self):
|
||||
folder = create_dummy_config_folder(
|
||||
[AutomationData(metadata=AutomationMetdata(id="test"))],
|
||||
automation_in_conifguration_mode="include",
|
||||
)
|
||||
hass_config = HassConfig(folder)
|
||||
self.assertIsInstance(hass_config.configurations, dict)
|
||||
self.assertTrue("automation" in hass_config.configurations)
|
||||
self.assertEqual(hass_config.automations[0]["id"], "test")
|
||||
|
||||
def test_load_automation_from_infer(self):
|
||||
folder = create_dummy_config_folder(
|
||||
[AutomationData(metadata=AutomationMetdata(id="test"))],
|
||||
automation_in_conifguration_mode="none",
|
||||
)
|
||||
hass_config = HassConfig(folder)
|
||||
self.assertIsInstance(hass_config.configurations, dict)
|
||||
self.assertFalse("automation" in hass_config.configurations)
|
||||
self.assertEqual(hass_config.automations[0]["id"], "test")
|
||||
@@ -1,80 +0,0 @@
|
||||
from unittest import TestCase
|
||||
from src.db.connection import HassDatabase
|
||||
from src.db import entities
|
||||
from tests.utils import SAMPLE_DB_PATH
|
||||
|
||||
db = HassDatabase(f"sqlite:///{SAMPLE_DB_PATH.absolute()}")
|
||||
|
||||
|
||||
class hass_db_tests(TestCase):
|
||||
def test_it_loads_local_db(self):
|
||||
self.assertEqual(db.execute_sql("SELECT count(*) FROM states")[0][0], 333_878)
|
||||
self.assertEqual(db.db_type, "sqlite")
|
||||
|
||||
def test_search_entity_find_by_text(self):
|
||||
expected = [
|
||||
"sensor.temperature_entrance_closet",
|
||||
"sensor.temperature_office",
|
||||
"sensor.temperature_kitchen",
|
||||
"sensor.temperature_mbathroom",
|
||||
"sensor.temperature_baby_room",
|
||||
"sensor.temperature_storage_unit",
|
||||
"sensor.temperature_powder_room",
|
||||
"sensor.temperature_laundry",
|
||||
"sensor.temperature_living_room",
|
||||
"sensor.temperature_ewelink",
|
||||
"sensor.temperature_main_bedroom",
|
||||
]
|
||||
for entity in entities.search(
|
||||
db,
|
||||
search_text="sensor.temperature",
|
||||
):
|
||||
self.assertTrue(expected.count(entity.entity_id) > 0)
|
||||
self.assertEqual(entity.domain, "sensor")
|
||||
self.assertEqual(entity.attributes["state_class"], "measurement")
|
||||
|
||||
def test_search_entity_find_by_text_and_domain(self):
|
||||
expected = [
|
||||
"automation.kitchen_island_buttom_press",
|
||||
"automation.kitchen_button_long_press",
|
||||
]
|
||||
for entity in entities.search(
|
||||
db, search_text="kitchen", domain_filter="automation"
|
||||
):
|
||||
self.assertTrue(expected.count(entity.entity_id) > 0)
|
||||
self.assertEqual(entity.domain, "automation")
|
||||
self.assertEqual(entity.attributes["mode"], "single")
|
||||
|
||||
def test_search_entity_find_by_text_and_attr(self):
|
||||
expected = [
|
||||
"sensor.humidity_kitchen",
|
||||
"sensor.pressure_kitchen",
|
||||
"sensor.temperature_kitchen",
|
||||
"sensor.battery_kitchen_btn",
|
||||
"sensor.battery_weather_kitchen",
|
||||
"sensor.battery_wleak_kitchen",
|
||||
]
|
||||
for entity in entities.search(
|
||||
db, search_text="kitchen", attributes_filter={"state_class": "measurement"}
|
||||
):
|
||||
self.assertTrue(expected.count(entity.entity_id) > 0)
|
||||
self.assertEqual(entity.domain, "sensor")
|
||||
self.assertEqual(entity.attributes["state_class"], "measurement")
|
||||
|
||||
def test_search_entity_find_by_text_and_partial_attr(self):
|
||||
expected = [
|
||||
"sensor.humidity_kitchen",
|
||||
"sensor.pressure_kitchen",
|
||||
"sensor.temperature_kitchen",
|
||||
"sensor.battery_kitchen_btn",
|
||||
"sensor.battery_weather_kitchen",
|
||||
"sensor.battery_wleak_kitchen",
|
||||
]
|
||||
for entity in entities.search(
|
||||
db,
|
||||
search_text="kitchen",
|
||||
attributes_filter={"state_class": "sureme"},
|
||||
):
|
||||
self.assertTrue(expected.count(entity.entity_id) > 0)
|
||||
self.assertEqual(entity.domain, "sensor")
|
||||
self.assertEqual(entity.attributes["state_class"], "measurement")
|
||||
@@ -1,6 +1,6 @@
|
||||
import json
|
||||
from unittest import TestCase
|
||||
from src.yaml_serializer import SecretValue
|
||||
from src.yaml_serializer.types import SecretValue
|
||||
from src.json_serializer import json_dumps, NOT_IMPLEMENTED_SV_MSG
|
||||
|
||||
|
||||
@@ -3,10 +3,12 @@ from pathlib import Path
|
||||
from unittest import TestCase
|
||||
from src.yaml_serializer import (
|
||||
dump_yaml,
|
||||
load_yaml,
|
||||
)
|
||||
from src.yaml_serializer.types import (
|
||||
IncludedYaml,
|
||||
SecretValue,
|
||||
IncludedYamlDir,
|
||||
simple_load_yaml,
|
||||
)
|
||||
|
||||
|
||||
@@ -15,15 +17,11 @@ class dumping_yamls_tests(TestCase):
|
||||
yaml = dump_yaml(
|
||||
{
|
||||
"automations": IncludedYaml(
|
||||
"automations.yaml",
|
||||
Path("/config/automations.yaml"),
|
||||
[
|
||||
{"id": "wow", "alias": "NO!"},
|
||||
],
|
||||
Path("automations.yaml"),
|
||||
)
|
||||
}
|
||||
)
|
||||
self.assertEqual(yaml, "automations: !include automations.yaml\n")
|
||||
self.assertEqual(yaml, f"automations: !include {Path('automations.yaml').absolute()}\n")
|
||||
|
||||
def test_dumping_secrets(self):
|
||||
yaml = dump_yaml({"google_password": SecretValue("gpass", "supersec")})
|
||||
@@ -34,7 +32,7 @@ class dumping_yamls_tests(TestCase):
|
||||
self.assertEqual(yaml, "sensor: !include_dir_list sensors/*yaml\n")
|
||||
|
||||
def test_load_stub(self):
|
||||
yaml_obj = simple_load_yaml(
|
||||
yaml_obj = load_yaml(
|
||||
io.StringIO(
|
||||
"""
|
||||
name: cool
|
||||
@@ -50,7 +48,7 @@ class dumping_yamls_tests(TestCase):
|
||||
"name": "cool",
|
||||
"google_password": SecretValue("gpass", "n/a"),
|
||||
"sensor": IncludedYamlDir("include_dir_list", "sensors/*yaml"),
|
||||
"automations": IncludedYaml("automations.yaml", Path("/"), "n/a"),
|
||||
"automations": IncludedYaml(Path("automations.yaml").absolute()),
|
||||
},
|
||||
)
|
||||
self.assertEqual(
|
||||
@@ -58,5 +56,5 @@ class dumping_yamls_tests(TestCase):
|
||||
"name: cool\n"
|
||||
"google_password: !secret gpass\n"
|
||||
"sensor: !include_dir_list sensors/*yaml\n"
|
||||
"automations: !include automations.yaml\n",
|
||||
f"automations: !include {Path('automations.yaml').absolute()}\n",
|
||||
)
|
||||
+10
-9
@@ -1,21 +1,21 @@
|
||||
from tempfile import mkdtemp
|
||||
from pathlib import Path
|
||||
from typing import List, Optional
|
||||
from src.config.AutomationLoader import AutomationData
|
||||
from typing import List, Literal, Optional
|
||||
from src.automations.types 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"
|
||||
SAMPLES_FOLDER = THIS_FOLDER / "samples"
|
||||
HA_CONFIG_EXAMPLE = SAMPLES_FOLDER / "example_config_folder"
|
||||
|
||||
|
||||
def create_dummy_config_folder(
|
||||
auto: List[AutomationData],
|
||||
secrets: Optional[dict] = None,
|
||||
other_config: Optional[dict] = None,
|
||||
include_auto_in_configuration: bool = False,
|
||||
automation_in_conifguration_mode: Literal["include", "inline", "none"] = "include",
|
||||
) -> Path:
|
||||
"""Creates a dummy /config structure for testing
|
||||
|
||||
@@ -23,7 +23,7 @@ def create_dummy_config_folder(
|
||||
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.
|
||||
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]
|
||||
@@ -31,12 +31,13 @@ def create_dummy_config_folder(
|
||||
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:
|
||||
if automation_in_conifguration_mode == "inline":
|
||||
configuration_yaml["automation"] = auto_prims
|
||||
else:
|
||||
elif automation_in_conifguration_mode == "include":
|
||||
configuration_yaml["automation"] = IncludedYaml(
|
||||
"automations.yaml", root_folder / "automations.yaml", auto
|
||||
Path(root_folder / "automations.yaml"),
|
||||
)
|
||||
if automation_in_conifguration_mode != "inline":
|
||||
(root_folder / "automations.yaml").write_text(dump_yaml(auto_prims))
|
||||
if secrets is not None:
|
||||
(root_folder / "secrets.yaml").write_text(dump_yaml(secrets))
|
||||
|
||||
Reference in new issue
Block a user