added more processng
This commit is contained in:
1 parent
862ec58193
commit
d61085c2f1
2 files changed
+186
-2
No files matched your search
@@ -74,17 +74,50 @@ def _parse_actions(
|
|||||||
continue
|
continue
|
||||||
elif raw_c.get("service"):
|
elif raw_c.get("service"):
|
||||||
action_type = "service"
|
action_type = "service"
|
||||||
|
action_data = normalize_obj(raw_c)
|
||||||
elif raw_c.get("repeat"):
|
elif raw_c.get("repeat"):
|
||||||
action_type = "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"):
|
elif raw_c.get("wait_template"):
|
||||||
action_type = "wait"
|
action_type = "wait"
|
||||||
|
action_data = normalize_obj(raw_c)
|
||||||
elif raw_c.get("event"):
|
elif raw_c.get("event"):
|
||||||
action_type = "event"
|
action_type = "event"
|
||||||
|
action_data = normalize_obj(raw_c)
|
||||||
elif raw_c.get("type") and raw_c.get("device_id"):
|
elif raw_c.get("type") and raw_c.get("device_id"):
|
||||||
action_type = "device"
|
action_type = "device"
|
||||||
|
action_data = normalize_obj(raw_c)
|
||||||
elif raw_c.get("choose"):
|
elif raw_c.get("choose"):
|
||||||
action_type = "choose"
|
action_type = "choose"
|
||||||
yield AutomationActionNode(action=action_type, action_data=normalize_obj(raw_c))
|
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(
|
def _parse_conditions(
|
||||||
|
|||||||
@@ -57,6 +57,59 @@ class config_finder_tests(TestCase):
|
|||||||
{"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"]},
|
||||||
|
{
|
||||||
|
"service": "light.turn_on",
|
||||||
|
"target": ["light.kitchen"],
|
||||||
|
"data": {"brightness": 10},
|
||||||
|
},
|
||||||
|
{"something": "is wrong"},
|
||||||
|
{
|
||||||
|
"alias": "Doing amazing things!",
|
||||||
|
"repeat": {
|
||||||
|
"count": 1000,
|
||||||
|
"sequence": [
|
||||||
|
"conditionA",
|
||||||
|
{
|
||||||
|
"wait_template": "states('light.kitchen') == 'off",
|
||||||
|
"timeout": 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "toggle",
|
||||||
|
"domain": "light",
|
||||||
|
"device_id": "1231412",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{"event": "custom-event", "event_data": "wow"},
|
||||||
|
{
|
||||||
|
"type": "toggle",
|
||||||
|
"domain": "light",
|
||||||
|
"device_id": "1231412",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"alias": "Doing amazing things!",
|
||||||
|
"choose": [
|
||||||
|
{
|
||||||
|
"conditions": [
|
||||||
|
r"{{ states('switch.plug_out_bug_lamp') == 'unavailable'}}",
|
||||||
|
],
|
||||||
|
"sequence": [
|
||||||
|
"conditionA",
|
||||||
|
{
|
||||||
|
"wait_template": "states('light.kitchen') == 'off",
|
||||||
|
"timeout": 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "toggle",
|
||||||
|
"domain": "light",
|
||||||
|
"device_id": "1231412",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"default": [{"event": "nope", "event_data": {}}],
|
||||||
|
},
|
||||||
]
|
]
|
||||||
expected_process = [
|
expected_process = [
|
||||||
AutomationConditionNode(
|
AutomationConditionNode(
|
||||||
@@ -71,9 +124,107 @@ 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"]},
|
||||||
),
|
),
|
||||||
|
AutomationActionNode(
|
||||||
|
action="service",
|
||||||
|
action_data={
|
||||||
|
"service": "light.turn_on",
|
||||||
|
"target": ["light.kitchen"],
|
||||||
|
"data": {"brightness": 10},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
AutomationActionNode(
|
||||||
|
action="unknown", action_data={"something": "is wrong"}
|
||||||
|
),
|
||||||
|
AutomationActionNode(
|
||||||
|
action="repeat",
|
||||||
|
action_data={
|
||||||
|
"alias": "Doing amazing things!",
|
||||||
|
"repeat": {
|
||||||
|
"count": 1000,
|
||||||
|
"sequence": [
|
||||||
|
AutomationConditionNode(
|
||||||
|
condition="template",
|
||||||
|
condition_data={"value_template": "conditionA"},
|
||||||
|
),
|
||||||
|
AutomationActionNode(
|
||||||
|
action="wait",
|
||||||
|
action_data={
|
||||||
|
"wait_template": "states('light.kitchen') == 'off",
|
||||||
|
"timeout": 10,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
AutomationActionNode(
|
||||||
|
action="device",
|
||||||
|
action_data={
|
||||||
|
"type": "toggle",
|
||||||
|
"domain": "light",
|
||||||
|
"device_id": "1231412",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
AutomationActionNode(
|
||||||
|
action="event",
|
||||||
|
action_data={"event": "custom-event", "event_data": "wow"},
|
||||||
|
),
|
||||||
|
AutomationActionNode(
|
||||||
|
action="device",
|
||||||
|
action_data={
|
||||||
|
"type": "toggle",
|
||||||
|
"domain": "light",
|
||||||
|
"device_id": "1231412",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
AutomationActionNode(
|
||||||
|
action="choose",
|
||||||
|
action_data={
|
||||||
|
"alias": "Doing amazing things!",
|
||||||
|
"choose": [
|
||||||
|
{
|
||||||
|
"conditions": [
|
||||||
|
AutomationConditionNode(
|
||||||
|
condition="template",
|
||||||
|
condition_data={
|
||||||
|
"value_template": r"{{ states('switch.plug_out_bug_lamp') == 'unavailable'}}"
|
||||||
|
},
|
||||||
|
)
|
||||||
|
],
|
||||||
|
"sequence": [
|
||||||
|
AutomationConditionNode(
|
||||||
|
condition="template",
|
||||||
|
condition_data={"value_template": "conditionA"},
|
||||||
|
),
|
||||||
|
AutomationActionNode(
|
||||||
|
action="wait",
|
||||||
|
action_data={
|
||||||
|
"wait_template": "states('light.kitchen') == 'off",
|
||||||
|
"timeout": 10,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
AutomationActionNode(
|
||||||
|
action="device",
|
||||||
|
action_data={
|
||||||
|
"type": "toggle",
|
||||||
|
"domain": "light",
|
||||||
|
"device_id": "1231412",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"default": [
|
||||||
|
AutomationActionNode(
|
||||||
|
action="event",
|
||||||
|
action_data={"event": "nope", "event_data": {}},
|
||||||
|
)
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
for parsed, expected in zip(expected_process, _parse_actions(raw_conditions)):
|
for expected, parsed in zip(expected_process, _parse_actions(raw_conditions)):
|
||||||
self.assertEqual(parsed, expected)
|
self.assertEqual(parsed, expected)
|
||||||
|
|
||||||
def test_automation_loader(self):
|
def test_automation_loader(self):
|
||||||
|
|||||||
Reference in new issue
Block a user