From 19b97b3897448e0af0c7c4defd63da37e5b1c014 Mon Sep 17 00:00:00 2001 From: Mikhail Diatchenko Date: Sat, 13 Nov 2021 20:40:46 +1300 Subject: [PATCH] Moved transition/input actions to before on_leave actions --- README.md | 6 ++-- components/state_machine/__init__.py | 44 ++++++++++++++-------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index e08e675..e3e0d8d 100644 --- a/README.md +++ b/README.md @@ -69,10 +69,10 @@ binary_sensor: * **transitions** (**Required**, list): The list of allowed transitions. Short form is `FROM_STATE -> TO_STATE`, or advanced configuration: * **from** (**Required**, string): Source state that this input is allowed on. * **to** (**Required**, string): Target state that this input transitions to. - * **action** (*Optional*, [Automation](https://esphome.io/guides/automations.html#automation)): An automation to perform when transition is performed. - * **action** (*Optional*, [Automation](https://esphome.io/guides/automations.html#automation)): An automation to perform when transition is done by this input. This action is performed after transition-specific action. + * **action** (*Optional*, [Automation](https://esphome.io/guides/automations.html#automation)): An automation to perform when transition is performed. This action is performed before state's `on_leave` action is called. + * **action** (*Optional*, [Automation](https://esphome.io/guides/automations.html#automation)): An automation to perform when transition is done by this input. This action is performed after transition-specific action and before state's `on_leave` action is called. -* **diagram** (*Optional*, boolean): If true, then a diagram of the state machine will be ouput to the console during validation/compilation of YAML. See **Diagrams** section below for more details. Defaults to `false`. +* **diagram** (*Optional*, boolean): If true, then a diagram of the state machine will be output to the console during validation/compilation of YAML. See **Diagrams** section below for more details. Defaults to `false`. > ### Note: > diff --git a/components/state_machine/__init__.py b/components/state_machine/__init__.py index b82ab5e..8efcb9f 100644 --- a/components/state_machine/__init__.py +++ b/components/state_machine/__init__.py @@ -236,7 +236,7 @@ async def to_code(config): if CONF_NAME in config: cg.add(var.set_name(config[CONF_NAME])) - # setup on_set automations first + # setup on_set automations for state in config[CONF_STATES_KEY]: if CONF_STATE_ON_LEAVE_KEY in state: @@ -246,26 +246,7 @@ async def to_code(config): ) await automation.build_automation(trigger, [], action) - # then setup on_leave automations (to ensure they are executed before on_enter) - for state in config[CONF_STATES_KEY]: - - if CONF_STATE_ON_LEAVE_KEY in state: - for action in state.get(CONF_STATE_ON_LEAVE_KEY, []): - trigger = cg.new_Pvariable( - action[CONF_TRIGGER_ID], var, state[CONF_NAME] - ) - await automation.build_automation(trigger, [], action) - - # setup on_enter automations after on_leave - for state in config[CONF_STATES_KEY]: - - if CONF_STATE_ON_ENTER_KEY in state: - for action in state.get(CONF_STATE_ON_ENTER_KEY, []): - trigger = cg.new_Pvariable( - action[CONF_TRIGGER_ID], var, state[CONF_NAME] - ) - await automation.build_automation(trigger, [], action) - + # 1. setup transition/input automations (they should run first) for input in config[CONF_INPUTS_KEY]: if CONF_INPUT_TRANSITIONS_KEY in input: for transition in input[CONF_INPUT_TRANSITIONS_KEY]: @@ -289,7 +270,26 @@ async def to_code(config): action[CONF_TRIGGER_ID], var, input[CONF_NAME] ) await automation.build_automation(trigger, [], action) - + + # 2. setup on_leave automations (to ensure they are executed before on_enter) + for state in config[CONF_STATES_KEY]: + + if CONF_STATE_ON_LEAVE_KEY in state: + for action in state.get(CONF_STATE_ON_LEAVE_KEY, []): + trigger = cg.new_Pvariable( + action[CONF_TRIGGER_ID], var, state[CONF_NAME] + ) + await automation.build_automation(trigger, [], action) + + # 3. setup on_enter automations after on_leave + for state in config[CONF_STATES_KEY]: + + if CONF_STATE_ON_ENTER_KEY in state: + for action in state.get(CONF_STATE_ON_ENTER_KEY, []): + trigger = cg.new_Pvariable( + action[CONF_TRIGGER_ID], var, state[CONF_NAME] + ) + await automation.build_automation(trigger, [], action) await cg.register_component(var, config)