From d61085c2f166ef0ca5715e6f4c0b1fffaf8a0d2f Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Wed, 10 Nov 2021 16:03:46 -0500 Subject: [PATCH] added more processng --- api/src/config/AutomationLoader.py | 35 ++++++- api/tests/unittests/test_config.py | 153 ++++++++++++++++++++++++++++- 2 files changed, 186 insertions(+), 2 deletions(-) diff --git a/api/src/config/AutomationLoader.py b/api/src/config/AutomationLoader.py index afab595..c1c4998 100644 --- a/api/src/config/AutomationLoader.py +++ b/api/src/config/AutomationLoader.py @@ -74,17 +74,50 @@ def _parse_actions( 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" - 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( diff --git a/api/tests/unittests/test_config.py b/api/tests/unittests/test_config.py index 693b12d..7fa8311 100644 --- a/api/tests/unittests/test_config.py +++ b/api/tests/unittests/test_config.py @@ -57,6 +57,59 @@ class config_finder_tests(TestCase): {"condition": "template", "value_template": "silly logic here"}, "silly logic here", {"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 = [ AutomationConditionNode( @@ -71,9 +124,107 @@ class config_finder_tests(TestCase): condition="time", 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) def test_automation_loader(self):