cleaned up multinode stlying
This commit is contained in:
1 parent
8d804add13
commit
bfc95db6f6
14 files changed
+402
-69
No files matched your search
@@ -0,0 +1,49 @@
|
||||
|
||||
|
||||
|
||||
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
|
||||
@@ -39,7 +39,7 @@ class AutomationData(BaseModel):
|
||||
|
||||
def to_primitive(self):
|
||||
return {
|
||||
**self.metadata.dict(),
|
||||
**self.metadata.dict(exclude_unset=True, exclude_none=True),
|
||||
"trigger": self.trigger,
|
||||
"condition": [],
|
||||
"action": [action.to_primitive() for action in self.sequence],
|
||||
|
||||
@@ -10,8 +10,10 @@ def find_hass_config() -> HassConfig:
|
||||
if config("USE_EXAMPLE_REPO", False, cast=bool):
|
||||
print("using USE_EXAMPLE_REPO")
|
||||
from tests.utils import create_copy_of_example_config
|
||||
|
||||
return HassConfig(create_copy_of_example_config())
|
||||
|
||||
path = create_copy_of_example_config()
|
||||
print("|-> ", path)
|
||||
return HassConfig(path)
|
||||
|
||||
if "HASSIO_TOKEN" in os.environ:
|
||||
return HassConfig(Path("/config"))
|
||||
|
||||
Reference in new issue
Block a user