initial conversion of automation
This commit is contained in:
1 parent
12b4ff6615
commit
47af2fccff
7 files changed
+310
-11
No files matched your search
@@ -10,6 +10,7 @@ setuptools.setup(
|
|||||||
"pydantic==1.8.2",
|
"pydantic==1.8.2",
|
||||||
"uvicorn[standard]==0.15.0",
|
"uvicorn[standard]==0.15.0",
|
||||||
"python-decouple==3.5",
|
"python-decouple==3.5",
|
||||||
|
"orjson==3.6.4",
|
||||||
],
|
],
|
||||||
extras_require={
|
extras_require={
|
||||||
"dev": [
|
"dev": [
|
||||||
|
|||||||
@@ -0,0 +1,132 @@
|
|||||||
|
from typing import Dict, Iterable, List, Literal, Optional, Tuple, Union
|
||||||
|
from pydantic.fields import Field
|
||||||
|
from src.json_serializer import json_dumps, normalize_obj
|
||||||
|
from src.config.HassSafeConstructor import HassConfig, IncludedYaml, SecretValue
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
# errors
|
||||||
|
class AutomationLoaderException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# types
|
||||||
|
class AutomationConditionNode(BaseModel):
|
||||||
|
condition: str
|
||||||
|
condition_data: dict
|
||||||
|
node_type: Literal["condition"] = Field(alias="$smType", default="condition")
|
||||||
|
|
||||||
|
|
||||||
|
class AutomationActionNode(BaseModel):
|
||||||
|
action: str
|
||||||
|
action_data: dict
|
||||||
|
node_type: Literal["action"] = Field(alias="$smType", default="action")
|
||||||
|
|
||||||
|
|
||||||
|
class AutomationMetdata(BaseModel):
|
||||||
|
id: str
|
||||||
|
alias: Optional[str] = None
|
||||||
|
description: Optional[str] = None
|
||||||
|
trigger_variables: Optional[Dict[str, Union[str, int, bool, float]]] = None
|
||||||
|
mode: Literal["single", "restart", "queued", "parallel"] = "single"
|
||||||
|
|
||||||
|
|
||||||
|
class AutomationData(BaseModel):
|
||||||
|
metadata: AutomationMetdata
|
||||||
|
trigger: List[dict] = []
|
||||||
|
condition: List[AutomationConditionNode] = []
|
||||||
|
action: List[Union[AutomationConditionNode, AutomationActionNode]] = []
|
||||||
|
|
||||||
|
|
||||||
|
# Single loader
|
||||||
|
def load_automation(
|
||||||
|
automations_raw_data: List[dict],
|
||||||
|
) -> Iterable[AutomationData]:
|
||||||
|
"""Converts an automation dictionary loaded directly from the config into
|
||||||
|
several python typed objects
|
||||||
|
|
||||||
|
Args:
|
||||||
|
automations_raw_data (List[dict])
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Iterable[AutomationData]
|
||||||
|
|
||||||
|
"""
|
||||||
|
for auto_raw in automations_raw_data:
|
||||||
|
yield AutomationData(
|
||||||
|
metadata=AutomationMetdata.parse_obj(auto_raw),
|
||||||
|
condition=list(_parse_conditions(auto_raw.get("condition", []))),
|
||||||
|
action=list(_parse_actions(auto_raw.get("action", []))),
|
||||||
|
trigger=[normalize_obj(obj) for obj in auto_raw.get("trigger", [])],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_actions(
|
||||||
|
raw_actions: Iterable[Union[dict, str]]
|
||||||
|
) -> Iterable[Union[AutomationActionNode, AutomationConditionNode]]:
|
||||||
|
for raw_c in raw_actions:
|
||||||
|
if isinstance(raw_c, str):
|
||||||
|
yield from _parse_conditions([raw_c])
|
||||||
|
elif isinstance(raw_c, dict):
|
||||||
|
if raw_c.get("condition"):
|
||||||
|
yield from _parse_conditions([raw_c])
|
||||||
|
elif raw_c.get("service"):
|
||||||
|
action_type = "service"
|
||||||
|
elif raw_c.get("repeat"):
|
||||||
|
action_type = "repeat"
|
||||||
|
elif raw_c.get("wait_template"):
|
||||||
|
action_type = "wait"
|
||||||
|
elif raw_c.get("event"):
|
||||||
|
action_type = "event"
|
||||||
|
elif raw_c.get("type") and raw_c.get("device_id"):
|
||||||
|
action_type = "device"
|
||||||
|
elif raw_c.get("choose"):
|
||||||
|
action_type = "choose"
|
||||||
|
else:
|
||||||
|
action_type = "unknown"
|
||||||
|
else:
|
||||||
|
action_type = "unknown"
|
||||||
|
yield AutomationActionNode(action=action_type, action_data=normalize_obj(raw_c))
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_conditions(
|
||||||
|
raw_conditions: Iterable[Union[dict, str]]
|
||||||
|
) -> Iterable[AutomationConditionNode]:
|
||||||
|
for raw_c in raw_conditions:
|
||||||
|
if isinstance(raw_c, str):
|
||||||
|
yield AutomationConditionNode(
|
||||||
|
condition="template",
|
||||||
|
condition_data={"value_template": raw_c},
|
||||||
|
)
|
||||||
|
elif isinstance(raw_c, SecretValue):
|
||||||
|
yield AutomationConditionNode(
|
||||||
|
condition="template",
|
||||||
|
condition_data={"value_template": "SECRETS NOT SUPPORTED"},
|
||||||
|
)
|
||||||
|
elif isinstance(raw_c, dict):
|
||||||
|
condition = raw_c.pop("condition")
|
||||||
|
yield AutomationConditionNode(
|
||||||
|
condition=condition,
|
||||||
|
condition_data=normalize_obj(raw_c),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
yield AutomationConditionNode(
|
||||||
|
condition="template",
|
||||||
|
condition_data=json_dumps(raw_c),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# Global Loader
|
||||||
|
class AutomationLoader:
|
||||||
|
def __init__(self, loaded_yaml: HassConfig) -> None:
|
||||||
|
self.automation_ref = loaded_yaml["automation"]
|
||||||
|
|
||||||
|
if isinstance(self.automation_ref, IncludedYaml):
|
||||||
|
self.automation_raw_data: list = self.automation_ref.data
|
||||||
|
else:
|
||||||
|
self.automation_raw_data: list = self.automation_ref
|
||||||
|
self.automation_ref = None
|
||||||
|
|
||||||
|
if not isinstance(self.automation_raw_data, list):
|
||||||
|
raise AutomationLoaderException(
|
||||||
|
f"Invalid automation file, expected it to be a 'list' instead it's a '{type(self.automation_data)}'"
|
||||||
|
)
|
||||||
@@ -1,19 +1,25 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Literal, NamedTuple, Optional, Union
|
from typing import Any, Literal, NamedTuple, Optional, Union
|
||||||
from ruamel.yaml import YAML
|
from ruamel.yaml import YAML
|
||||||
from ruamel.yaml.constructor import SafeConstructor
|
from ruamel.yaml.constructor import SafeConstructor
|
||||||
from ruamel.yaml.nodes import Node
|
from ruamel.yaml.nodes import Node
|
||||||
from src.logger import logger
|
from src.logger import logger
|
||||||
|
|
||||||
|
|
||||||
class LoadedYaml(NamedTuple):
|
class HassConfig(NamedTuple):
|
||||||
secrets: dict
|
"""The final output for load_hass_config"""
|
||||||
config: dict
|
|
||||||
|
secrets: dict # the secrets file content
|
||||||
|
config: dict # the complete configration.yaml tree
|
||||||
|
|
||||||
|
|
||||||
class IncludedYaml(NamedTuple):
|
class IncludedYaml(NamedTuple):
|
||||||
original_path: Path
|
"""Any time something in the yaml is specified as `include !filepath`
|
||||||
data: dict
|
this data type will represent it.
|
||||||
|
"""
|
||||||
|
|
||||||
|
original_path: Path # path to location of original file
|
||||||
|
data: Any # the parsed data
|
||||||
|
|
||||||
|
|
||||||
class SecretValue(NamedTuple):
|
class SecretValue(NamedTuple):
|
||||||
@@ -21,10 +27,10 @@ class SecretValue(NamedTuple):
|
|||||||
value: str
|
value: str
|
||||||
|
|
||||||
|
|
||||||
def load_hass_config(file_path: Path) -> LoadedYaml:
|
def load_hass_config(file_path: Path) -> HassConfig:
|
||||||
HAY = HassYaml()
|
HAY = HassYaml()
|
||||||
data = HAY.load(file_path / "configuration.yaml")
|
data = HAY.load(file_path / "configuration.yaml")
|
||||||
return LoadedYaml(HAY.secrets, data)
|
return HassConfig(HAY.secrets, data)
|
||||||
|
|
||||||
|
|
||||||
class HassYaml:
|
class HassYaml:
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import orjson
|
||||||
|
from typing import Any, Union
|
||||||
|
from src.config.HassSafeConstructor import SecretValue
|
||||||
|
|
||||||
|
# TODO: add support for these
|
||||||
|
NOT_IMPLEMENTED_SV_MSG = "SECRET VALUE NOT IMPLEMENTED"
|
||||||
|
|
||||||
|
|
||||||
|
def __default(obj: Any) -> str:
|
||||||
|
if isinstance(obj, SecretValue):
|
||||||
|
return NOT_IMPLEMENTED_SV_MSG
|
||||||
|
raise TypeError
|
||||||
|
|
||||||
|
|
||||||
|
def json_dumps(obj: Any) -> str:
|
||||||
|
"""Serializes a python object into a json string
|
||||||
|
|
||||||
|
Args:
|
||||||
|
obj (Any)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str
|
||||||
|
"""
|
||||||
|
return orjson.dumps(obj, default=__default).decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_obj(obj: Any) -> Union[list, dict]:
|
||||||
|
"""Converts any python object to primitive python types
|
||||||
|
|
||||||
|
Args:
|
||||||
|
obj (Any)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Union[list, dict]
|
||||||
|
"""
|
||||||
|
return orjson.loads(json_dumps(obj))
|
||||||
@@ -1,5 +1,13 @@
|
|||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
from src.config.AutomationLoader import (
|
||||||
|
AutomationActionNode,
|
||||||
|
AutomationConditionNode,
|
||||||
|
AutomationData,
|
||||||
|
AutomationMetdata,
|
||||||
|
load_automation,
|
||||||
|
_parse_conditions,
|
||||||
|
)
|
||||||
|
from src.json_serializer import NOT_IMPLEMENTED_SV_MSG
|
||||||
from src.config.HassSafeConstructor import SecretValue, load_hass_config, IncludedYaml
|
from src.config.HassSafeConstructor import SecretValue, load_hass_config, IncludedYaml
|
||||||
from tests.utils import SAMPLE_HA_PATH
|
from tests.utils import SAMPLE_HA_PATH
|
||||||
|
|
||||||
@@ -16,3 +24,86 @@ class config_finder_tests(TestCase):
|
|||||||
config["automation"].data[32]["trigger"][0]["device_id"],
|
config["automation"].data[32]["trigger"][0]["device_id"],
|
||||||
SecretValue("baby_btn_device_id", "347492ffc6b909a55ebe08745fca1bf6"),
|
SecretValue("baby_btn_device_id", "347492ffc6b909a55ebe08745fca1bf6"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_various_condition_types(self):
|
||||||
|
raw_conditions = [
|
||||||
|
{"condition": "template", "value_template": "silly logic here"},
|
||||||
|
"silly logic here",
|
||||||
|
{"condition": "time", "after": "10:00:00", "weekday": ["mon", "tue"]},
|
||||||
|
]
|
||||||
|
expected_process = [
|
||||||
|
AutomationConditionNode(
|
||||||
|
condition="template",
|
||||||
|
condition_data={"value_template": "silly logic here"},
|
||||||
|
),
|
||||||
|
AutomationConditionNode(
|
||||||
|
condition="template",
|
||||||
|
condition_data={"value_template": "silly logic here"},
|
||||||
|
),
|
||||||
|
AutomationConditionNode(
|
||||||
|
condition="time",
|
||||||
|
condition_data={"after": "10:00:00", "weekday": ["mon", "tue"]},
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
for parsed, expected in zip(
|
||||||
|
expected_process, _parse_conditions(raw_conditions)
|
||||||
|
):
|
||||||
|
self.assertEqual(parsed, expected)
|
||||||
|
|
||||||
|
def test_automation_loader(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...",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)
|
||||||
@@ -71,8 +71,10 @@ class hass_db_tests(TestCase):
|
|||||||
"sensor.battery_wleak_kitchen",
|
"sensor.battery_wleak_kitchen",
|
||||||
]
|
]
|
||||||
for entity in entities.search(
|
for entity in entities.search(
|
||||||
db, search_text="kitchen", attributes_filter={"state_class": "measurement"}
|
db,
|
||||||
|
search_text="kitchen",
|
||||||
|
attributes_filter={"state_class": "sureme"},
|
||||||
):
|
):
|
||||||
self.assertTrue(expected.count(entity.entity_id) > 0)
|
self.assertTrue(expected.count(entity.entity_id) > 0)
|
||||||
self.assertEqual(entity.domain, "sensor")
|
self.assertEqual(entity.domain, "sensor")
|
||||||
self.assertEqual(entity.attributes["state_class"], "sureme")
|
self.assertEqual(entity.attributes["state_class"], "measurement")
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import json
|
||||||
|
from unittest import TestCase
|
||||||
|
from src.config.HassSafeConstructor import SecretValue
|
||||||
|
from src.json_serializer import json_dumps, NOT_IMPLEMENTED_SV_MSG
|
||||||
|
|
||||||
|
|
||||||
|
class json_serializer_tests(TestCase):
|
||||||
|
def test_convert_secret_value(self):
|
||||||
|
self.assertEqual(
|
||||||
|
json_dumps(
|
||||||
|
{
|
||||||
|
"int": 1,
|
||||||
|
"float": 1.3,
|
||||||
|
"bool": False,
|
||||||
|
"list": [1, "w"],
|
||||||
|
"tup": (1, "w"),
|
||||||
|
"some": {"deep": {"obj": SecretValue("super", "sec")}},
|
||||||
|
}
|
||||||
|
),
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"int": 1,
|
||||||
|
"float": 1.3,
|
||||||
|
"bool": False,
|
||||||
|
"list": [1, "w"],
|
||||||
|
"tup": (1, "w"),
|
||||||
|
"some": {"deep": {"obj": NOT_IMPLEMENTED_SV_MSG}},
|
||||||
|
},
|
||||||
|
separators=(",", ":"),
|
||||||
|
),
|
||||||
|
)
|
||||||
Reference in new issue
Block a user