updated api
This commit is contained in:
1 parent
4cfc429980
commit
3526fbd9ee
8 files changed
+137
-507
No files matched your search
@@ -1,49 +0,0 @@
|
||||
|
||||
|
||||
|
||||
from typing import Iterable, Union
|
||||
|
||||
from src.automations.types import AutomationActionNode, AutomationConditionNode
|
||||
|
||||
|
||||
def _deparse(nodes: Iterable[Union[AutomationActionNode, AutomationConditionNode]]) -> Iterable[dict]:
|
||||
for node in nodes:
|
||||
if isinstance(node, AutomationActionNode):
|
||||
yield from _deparse_action(node)
|
||||
elif isinstance(node, AutomationConditionNode):
|
||||
yield from _deparse_condition(node)
|
||||
else:
|
||||
raise NotImplementedError(type(node))
|
||||
|
||||
def _deparse_action(action: AutomationActionNode) -> Iterable[dict]:
|
||||
if action.action == 'repeat':
|
||||
raw_data = action.action_data
|
||||
repeat_data = raw_data.pop("repeat")
|
||||
yield {
|
||||
**raw_data,
|
||||
"repeat": {
|
||||
**repeat_data,
|
||||
"sequence": list(_deparse(repeat_data['sequence']))
|
||||
}
|
||||
}
|
||||
elif action.action == 'choose':
|
||||
raw_data = action.action_data
|
||||
choose_data = raw_data.pop("choose")
|
||||
default_data = raw_data.pop("default", [])
|
||||
yield {
|
||||
**raw_data,
|
||||
"choose": [
|
||||
{
|
||||
"conditions": list(map(_deparse_condition, option.get("conditions", []))),
|
||||
"sequence": list(_deparse(option.get("sequence", []))),
|
||||
}
|
||||
for option in choose_data
|
||||
],
|
||||
"default": list(_deparse(default_data)),
|
||||
}
|
||||
else:
|
||||
yield action.action_data
|
||||
|
||||
|
||||
def _deparse_condition(condition: AutomationConditionNode) -> Iterable[dict]:
|
||||
raise NotImplementedError
|
||||
@@ -1,10 +1,7 @@
|
||||
from itertools import chain
|
||||
from typing import Iterable, Iterator
|
||||
from typing import Iterator, Union
|
||||
from pydantic.error_wrappers import ValidationError
|
||||
from src.automations.tags import TagManager
|
||||
from src.json_serializer import normalize_obj
|
||||
from .types import AutomationData, AutomationMetdata, ExtenededAutomationData
|
||||
from .parsers import _parse_actions
|
||||
from .types import AutomationMetdata, ExtenededAutomationData
|
||||
from .errors import InvalidAutomationFile
|
||||
|
||||
# Single loader
|
||||
@@ -25,16 +22,30 @@ def load_automation(
|
||||
try:
|
||||
yield ExtenededAutomationData(
|
||||
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", [])],
|
||||
condition=load_conditions(auto_raw.get("condition", [])),
|
||||
sequence=load_conditions(auto_raw.get("action", [])),
|
||||
trigger=normalize_obj(auto_raw.get("trigger", [])),
|
||||
tags=tags,
|
||||
)
|
||||
except ValidationError as err:
|
||||
raise InvalidAutomationFile from err
|
||||
|
||||
|
||||
def load_conditions(conditions: Union[dict, list, str]):
|
||||
if isinstance(conditions, list):
|
||||
return list(map(ensure_condition_dictionary, conditions))
|
||||
else:
|
||||
return [
|
||||
ensure_condition_dictionary(conditions)
|
||||
]
|
||||
|
||||
def ensure_condition_dictionary(c: Union[str, dict]):
|
||||
c = normalize_obj(c)
|
||||
if isinstance(c, str):
|
||||
return {
|
||||
"condition": "template",
|
||||
"value_template": c
|
||||
}
|
||||
elif isinstance(c, dict):
|
||||
return c
|
||||
raise InvalidAutomationFile(f"Invalid type for condition object {c}")
|
||||
@@ -1,88 +0,0 @@
|
||||
from typing import Iterable, Union
|
||||
from src.json_serializer import json_dumps, normalize_obj
|
||||
from src.yaml_serializer.types import SecretValue, NOT_IMPLEMENTED_SV_MSG
|
||||
from .types import AutomationActionNode, AutomationConditionNode
|
||||
|
||||
|
||||
def _parse_actions(
|
||||
raw_actions: Iterable[Union[dict, str]]
|
||||
) -> Iterable[Union[AutomationActionNode, AutomationConditionNode]]:
|
||||
for raw_c in raw_actions:
|
||||
action_type = "unknown"
|
||||
if isinstance(raw_c, str):
|
||||
yield from _parse_conditions([raw_c])
|
||||
continue
|
||||
elif isinstance(raw_c, dict):
|
||||
if raw_c.get("condition"):
|
||||
yield from _parse_conditions([raw_c])
|
||||
continue
|
||||
elif raw_c.get("service"):
|
||||
action_type = "service"
|
||||
action_data = normalize_obj(raw_c)
|
||||
elif raw_c.get("repeat"):
|
||||
action_type = "repeat"
|
||||
repeat_data = raw_c.pop("repeat")
|
||||
action_data = {
|
||||
**raw_c,
|
||||
"repeat": {
|
||||
"count": repeat_data.get("count", 0),
|
||||
"sequence": list(_parse_actions(repeat_data.get("sequence", []))),
|
||||
},
|
||||
}
|
||||
elif raw_c.get("wait_template"):
|
||||
action_type = "wait"
|
||||
action_data = normalize_obj(raw_c)
|
||||
elif raw_c.get("event"):
|
||||
action_type = "event"
|
||||
action_data = normalize_obj(raw_c)
|
||||
elif raw_c.get("type") and raw_c.get("device_id"):
|
||||
action_type = "device"
|
||||
action_data = normalize_obj(raw_c)
|
||||
elif raw_c.get("choose"):
|
||||
action_type = "choose"
|
||||
choose_data = raw_c.pop("choose")
|
||||
default_data = raw_c.pop("default", [])
|
||||
action_data = {
|
||||
**raw_c,
|
||||
"choose": [
|
||||
{
|
||||
"conditions": list(_parse_conditions(option.get("conditions", []))),
|
||||
"sequence": list(_parse_actions(option.get("sequence", []))),
|
||||
}
|
||||
for option in choose_data
|
||||
],
|
||||
"default": list(_parse_actions(default_data)),
|
||||
}
|
||||
else:
|
||||
action_data = normalize_obj(raw_c)
|
||||
yield AutomationActionNode(
|
||||
action=action_type,
|
||||
action_data=action_data,
|
||||
)
|
||||
|
||||
|
||||
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": NOT_IMPLEMENTED_SV_MSG},
|
||||
)
|
||||
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),
|
||||
)
|
||||
@@ -2,28 +2,6 @@ from typing import Dict, List, Literal, Optional, Union
|
||||
from pydantic.fields import Field
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class AutomationConditionNode(BaseModel):
|
||||
condition: str
|
||||
condition_data: dict
|
||||
node_type: Literal["condition"] = Field(alias="$smType", default="condition")
|
||||
|
||||
def to_primitive(self):
|
||||
return {
|
||||
**self.condition_data,
|
||||
"condition": self.condition,
|
||||
}
|
||||
|
||||
|
||||
class AutomationActionNode(BaseModel):
|
||||
action: str
|
||||
action_data: dict
|
||||
node_type: Literal["action"] = Field(alias="$smType", default="action")
|
||||
|
||||
def to_primitive(self):
|
||||
return self.action_data
|
||||
|
||||
|
||||
class AutomationMetdata(BaseModel):
|
||||
id: str
|
||||
alias: Optional[str] = None
|
||||
@@ -35,14 +13,15 @@ class AutomationMetdata(BaseModel):
|
||||
class AutomationData(BaseModel):
|
||||
metadata: AutomationMetdata
|
||||
trigger: List[dict] = []
|
||||
sequence: List[Union[AutomationConditionNode, AutomationActionNode]] = []
|
||||
condition: List[dict] = []
|
||||
sequence: List[dict] = []
|
||||
|
||||
def to_primitive(self):
|
||||
return {
|
||||
**self.metadata.dict(exclude_unset=True, exclude_none=True),
|
||||
"trigger": self.trigger,
|
||||
"condition": [],
|
||||
"action": [action.to_primitive() for action in self.sequence],
|
||||
"condition": self.condition,
|
||||
"action": self.sequence,
|
||||
}
|
||||
|
||||
class ExtenededAutomationData(AutomationData):
|
||||
|
||||
Reference in new issue
Block a user