esphome / esphome/feature-requests

Add way to abort an action list

Open
#1,769 7 comments 4 reactions 0 assignees View on GitHub
enhancement
Dominant language
No language data
Stars
450
Forks
29
PR merge metrics
No merged PRs in 30d

Description

**Describe the problem you have/What new integration you would like**

I want to have the ability to "break out of" or abort an action list. This will aid in flattening conditional code by un-nesting, i.e. it will lower the _cognitive complexity_ of the code.

**Please describe your use case for this integration and alternatives you've tried:**

I have YAML like this:

```yaml
esphome:
on_boot:
priority: 100
then:
- if:
condition:
lambda: "return esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_TOUCHPAD;"
then:
- logger.log: "Did not wake up because of touchpad, ignoring this boot"
else:
- logger.log: "Woke up from touch, going to wait for API & send event"
- more code here to actually do stuff on wake-up-on-touch.
```

This approach works, but has the downside that all the subsequent code would be nested under the `else`. As a result, a consistent application of this pattern would make the execution order "read" from top left to bottom right, nesting ever deeper.

Flipping the condition would nest it under the inner `then`, which is at the same nesting level, so doesn't solve the actual issue.

The goal of this feature request is to have something like this:

```yaml
# initial lines same as above.
then:
- if:
condition:
lambda: "return esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_TOUCHPAD;"
then:
- logger.log: "Did not wake up because of touchpad, ignoring this boot"
- abort:
- logger.log: "Woke up from touch, going to wait for API & send event"
- more code here to actually do stuff on wake-up-on-touch.
```
As you can see, the code flow now is directly under the top-level `then:` instead of nested.

**Additional context**
Translated to C++, the above two blocks of code would loosely map to:

```c++
void currently_possible() {
if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_TOUCHPAD) {
// All of this function's behaviour inside this condition.
}
}
void requested() {
if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_TOUCHPAD) return;
// All of this function's behaviour outside the condition.
}
```
Repeating the `currently_possible()` approach will cause deeper and deeper nesting, and typically makes it harder to understand the main flow as each condition needs to be inspected carefully. Repeating the `requested()` approach will just push the main behaviour down, and the main flow will remain top-to-bottom.

Contributor guide

No contributing guide indexed for this repository

Research direction

The request provides YAML action-list examples and a C++ analogy but names no implementation files, entry points, or tests. Start by locating action-list execution and existing conditional actions, then define and test abort behavior so later top-level actions are skipped when the abort action runs.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
embedded-iot
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.