upped coverage
This commit is contained in:
1 parent
2768975f9d
commit
d6adbb897d
6 files changed
+82
-25
No files matched your search
@@ -0,0 +1,31 @@
|
|||||||
|
[project]
|
||||||
|
[build-system]
|
||||||
|
requires = ["setuptools >= 40.9.0", "wheel"]
|
||||||
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
|
[tool.isort]
|
||||||
|
multi_line_output = 3
|
||||||
|
|
||||||
|
[tool.black]
|
||||||
|
line-length = 100
|
||||||
|
target_version = ['py39']
|
||||||
|
include = '\.pyi?$'
|
||||||
|
exclude = '''
|
||||||
|
(
|
||||||
|
/(
|
||||||
|
\.eggs
|
||||||
|
| \.git
|
||||||
|
| \.hg
|
||||||
|
| \.mypy_cache
|
||||||
|
| \.tox
|
||||||
|
| \.venv
|
||||||
|
| _build
|
||||||
|
| buck-out
|
||||||
|
| build
|
||||||
|
| dist
|
||||||
|
)/
|
||||||
|
)
|
||||||
|
'''
|
||||||
|
|
||||||
|
[tool.coverage.run]
|
||||||
|
omit = ["./venv/*", "tests/*"]
|
||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
import setuptools # type: ignore
|
import setuptools # type: ignore
|
||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
python_requires=">=3.7",
|
python_requires=">=3.9",
|
||||||
install_requires=[
|
install_requires=[
|
||||||
"ruamel.yaml==0.17.17",
|
"ruamel.yaml==0.17.17",
|
||||||
"SQLAlchemy==1.4.26",
|
"SQLAlchemy==1.4.26",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from typing import Dict, Iterable, List, Literal, Optional, Union
|
from typing import Dict, Iterable, List, Literal, Optional, Union
|
||||||
from pydantic.fields import Field
|
from pydantic.fields import Field
|
||||||
from src.json_serializer import json_dumps, normalize_obj
|
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
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
@@ -82,9 +82,7 @@ def _parse_actions(
|
|||||||
**raw_c,
|
**raw_c,
|
||||||
"repeat": {
|
"repeat": {
|
||||||
"count": repeat_data.get("count", 0),
|
"count": repeat_data.get("count", 0),
|
||||||
"sequence": list(
|
"sequence": list(_parse_actions(repeat_data.get("sequence", []))),
|
||||||
_parse_actions(repeat_data.get("sequence", []))
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
elif raw_c.get("wait_template"):
|
elif raw_c.get("wait_template"):
|
||||||
@@ -104,12 +102,8 @@ def _parse_actions(
|
|||||||
**raw_c,
|
**raw_c,
|
||||||
"choose": [
|
"choose": [
|
||||||
{
|
{
|
||||||
"conditions": list(
|
"conditions": list(_parse_conditions(option.get("conditions", []))),
|
||||||
_parse_conditions(option.get("conditions", []))
|
"sequence": list(_parse_actions(option.get("sequence", []))),
|
||||||
),
|
|
||||||
"sequence": list(
|
|
||||||
_parse_actions(option.get("sequence", []))
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
for option in choose_data
|
for option in choose_data
|
||||||
],
|
],
|
||||||
@@ -132,7 +126,7 @@ def _parse_conditions(
|
|||||||
elif isinstance(raw_c, SecretValue):
|
elif isinstance(raw_c, SecretValue):
|
||||||
yield AutomationConditionNode(
|
yield AutomationConditionNode(
|
||||||
condition="template",
|
condition="template",
|
||||||
condition_data={"value_template": "SECRETS NOT SUPPORTED"},
|
condition_data={"value_template": NOT_IMPLEMENTED_SV_MSG},
|
||||||
)
|
)
|
||||||
elif isinstance(raw_c, dict):
|
elif isinstance(raw_c, dict):
|
||||||
condition = raw_c.pop("condition")
|
condition = raw_c.pop("condition")
|
||||||
@@ -150,7 +144,7 @@ def _parse_conditions(
|
|||||||
# Global Loader
|
# Global Loader
|
||||||
class AutomationLoader:
|
class AutomationLoader:
|
||||||
def __init__(self, loaded_yaml: HassConfig) -> None:
|
def __init__(self, loaded_yaml: HassConfig) -> None:
|
||||||
self.automation_ref = loaded_yaml.config["automation"]
|
self.automation_ref = loaded_yaml.config.get("automation", None)
|
||||||
|
|
||||||
if isinstance(self.automation_ref, IncludedYaml):
|
if isinstance(self.automation_ref, IncludedYaml):
|
||||||
self.automation_raw_data: list = self.automation_ref.data
|
self.automation_raw_data: list = self.automation_ref.data
|
||||||
@@ -160,7 +154,7 @@ class AutomationLoader:
|
|||||||
|
|
||||||
if not isinstance(self.automation_raw_data, list):
|
if not isinstance(self.automation_raw_data, list):
|
||||||
raise AutomationLoaderException(
|
raise AutomationLoaderException(
|
||||||
f"Invalid automation file, expected it to be a 'list' instead it's a '{type(self.automation_data)}'"
|
f"Invalid automation file, expected it to be a 'list' instead it's a '{type(self.automation_raw_data)}' --> `automation: {self.automation_raw_data}`"
|
||||||
)
|
)
|
||||||
|
|
||||||
self.automations_data = list(load_automation(self.automation_raw_data))
|
self.automations_data = list(load_automation(self.automation_raw_data))
|
||||||
|
|||||||
File renamed without changes.
@@ -2,7 +2,7 @@ import io
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, List
|
from typing import Any, List
|
||||||
from src.yaml_serializer.base_yaml import yaml
|
from src.yaml_serializer.base_yaml import yaml
|
||||||
from src.yaml_serializer.HassSafeConstructor import HassYaml
|
from src.yaml_serializer.HassYaml import HassYaml
|
||||||
from src.yaml_serializer.types import HassConfig, SecretValue, IncludedYaml
|
from src.yaml_serializer.types import HassConfig, SecretValue, IncludedYaml
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from src.config.AutomationLoader import (
|
|||||||
AutomationConditionNode,
|
AutomationConditionNode,
|
||||||
AutomationData,
|
AutomationData,
|
||||||
AutomationLoader,
|
AutomationLoader,
|
||||||
|
AutomationLoaderException,
|
||||||
AutomationMetdata,
|
AutomationMetdata,
|
||||||
load_automation,
|
load_automation,
|
||||||
_parse_conditions,
|
_parse_conditions,
|
||||||
@@ -17,12 +18,13 @@ from src.yaml_serializer import (
|
|||||||
IncludedYaml,
|
IncludedYaml,
|
||||||
)
|
)
|
||||||
from tests.utils import SAMPLE_HA_PATH
|
from tests.utils import SAMPLE_HA_PATH
|
||||||
|
from src.yaml_serializer.types import HassConfig
|
||||||
|
|
||||||
|
|
||||||
class config_finder_tests(TestCase):
|
class config_finder_tests(TestCase):
|
||||||
def test_simple_made_up_config(self):
|
def test_simple_made_up_config(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_find_sample_config(self):
|
def test_find_sample_config(self):
|
||||||
secrets, config = load_hass_config(SAMPLE_HA_PATH)
|
secrets, config = load_hass_config(SAMPLE_HA_PATH)
|
||||||
self.assertIsInstance(config["template"], IncludedYaml)
|
self.assertIsInstance(config["template"], IncludedYaml)
|
||||||
@@ -35,11 +37,15 @@ class config_finder_tests(TestCase):
|
|||||||
SecretValue("baby_btn_device_id", "347492ffc6b909a55ebe08745fca1bf6"),
|
SecretValue("baby_btn_device_id", "347492ffc6b909a55ebe08745fca1bf6"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class AutomationLoader_tests(TestCase):
|
||||||
def test_various_condition_types(self):
|
def test_various_condition_types(self):
|
||||||
raw_conditions = [
|
raw_conditions = [
|
||||||
{"condition": "template", "value_template": "silly logic here"},
|
{"condition": "template", "value_template": "silly logic here"},
|
||||||
"silly logic here",
|
"silly logic here",
|
||||||
{"condition": "time", "after": "10:00:00", "weekday": ["mon", "tue"]},
|
{"condition": "time", "after": "10:00:00", "weekday": ["mon", "tue"]},
|
||||||
|
{"condition": "what is this?", "wuba": "dub"},
|
||||||
|
SecretValue("supersecretcondition", "rm -rf ./*"),
|
||||||
]
|
]
|
||||||
expected_process = [
|
expected_process = [
|
||||||
AutomationConditionNode(
|
AutomationConditionNode(
|
||||||
@@ -54,11 +60,17 @@ class config_finder_tests(TestCase):
|
|||||||
condition="time",
|
condition="time",
|
||||||
condition_data={"after": "10:00:00", "weekday": ["mon", "tue"]},
|
condition_data={"after": "10:00:00", "weekday": ["mon", "tue"]},
|
||||||
),
|
),
|
||||||
|
AutomationConditionNode(
|
||||||
|
condition="what is this?",
|
||||||
|
condition_data={"wuba": "dub"},
|
||||||
|
),
|
||||||
|
AutomationConditionNode(
|
||||||
|
condition="template",
|
||||||
|
condition_data={"value_template": NOT_IMPLEMENTED_SV_MSG},
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
for parsed, expected in zip(
|
for expected, parsed in zip(expected_process, _parse_conditions(raw_conditions)):
|
||||||
expected_process, _parse_conditions(raw_conditions)
|
|
||||||
):
|
|
||||||
self.assertEqual(parsed, expected)
|
self.assertEqual(parsed, expected)
|
||||||
|
|
||||||
def test_various_action_types(self):
|
def test_various_action_types(self):
|
||||||
@@ -141,9 +153,7 @@ class config_finder_tests(TestCase):
|
|||||||
"data": {"brightness": 10},
|
"data": {"brightness": 10},
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
AutomationActionNode(
|
AutomationActionNode(action="unknown", action_data={"something": "is wrong"}),
|
||||||
action="unknown", action_data={"something": "is wrong"}
|
|
||||||
),
|
|
||||||
AutomationActionNode(
|
AutomationActionNode(
|
||||||
action="repeat",
|
action="repeat",
|
||||||
action_data={
|
action_data={
|
||||||
@@ -273,9 +283,7 @@ class config_finder_tests(TestCase):
|
|||||||
condition=[
|
condition=[
|
||||||
AutomationConditionNode(
|
AutomationConditionNode(
|
||||||
condition="template",
|
condition="template",
|
||||||
condition_data={
|
condition_data={"value_template": "states(time.time) >= '10:00:00'"},
|
||||||
"value_template": "states(time.time) >= '10:00:00'"
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
action=[
|
action=[
|
||||||
@@ -302,3 +310,27 @@ class config_finder_tests(TestCase):
|
|||||||
|
|
||||||
kitchen_autos = list(automation_loader.find(alias="kitchen"))
|
kitchen_autos = list(automation_loader.find(alias="kitchen"))
|
||||||
self.assertTrue(len(kitchen_autos), 3)
|
self.assertTrue(len(kitchen_autos), 3)
|
||||||
|
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
with self.assertRaises(AutomationLoaderException):
|
||||||
|
AutomationLoader(
|
||||||
|
HassConfig(
|
||||||
|
secrets={},
|
||||||
|
config={},
|
||||||
|
)
|
||||||
|
)
|
||||||
Reference in new issue
Block a user