esphome / esphome/feature-requests

Add more controllability for !extends merging when it comes to lists

Open
#2,879 0 comments 0 reactions 0 assignees View on GitHub
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**

When I have a package with a component in it that includes a list of actions - i.e. a binary sensor for a switch or something similar like so:

```yaml
- platform: gpio
id: the_switch
name: "Power Switch"
disabled_by_default: true
pin:
number: GPIO14
mode: INPUT_PULLUP
on_state:
- light.toggle: mini_relay
- light.toggle: led
filters:
- delayed_on_off: ${switch_delayed_on_off}
```

then if I want to extend it to add new functionality then the logical thing to is use the `!extend` command. However, the extend command currently works by only appending lists - so a change which is intended to modify control flow doesn't work - i.e. this:

```yaml
- id: !extend the_switch
on_state:
- if:
condition:
lambda: 'return id(sw_interlock) != true;'
then:
- light.toggle: mini_relay
- light.toggle: led
else:
- logger.log: "Physical switch locked out"
filters:
- delayed_on_off: ${switch_delayed_on_off}
```

results in this effective configuration:

```yaml
- platform: gpio
id: the_switch
name: "Power Switch"
disabled_by_default: true
pin:
number: GPIO14
mode: INPUT_PULLUP
on_state:
- light.toggle: mini_relay
- light.toggle: led
- if:
condition:
lambda: 'return id(sw_interlock) != true;'
then:
- light.toggle: mini_relay
- light.toggle: led
else:
- logger.log: "Physical switch locked out"
filters:
- delayed_on_off: ${switch_delayed_on_off}
```

Note that the list has simple had the actions appended. I understand this is intended behavior and serves a sensible use case under most conditions.

What I would _like_ to be able to do is something like this:

```yaml
- id: !extend the_switch
on_state: !replace
- if:
condition:
lambda: 'return id(sw_interlock) != true;'
then:
- light.toggle: mini_relay
- light.toggle: led
else:
- logger.log: "Physical switch locked out"
filters:
- delayed_on_off: ${switch_delayed_on_off}
```

And have it accomplish the original intent - replace rather then extend the `on_state` list of actions.

**Please describe your use case for this integration and alternatives you've tried:**
The use case is replacing control flow in existing packages where I am not simply extending the action lists.

The current alternative is to the use the `!remove` syntax and copy and paste the entire component into the derived file. This is fine, but it does limit the usefulness of the `!extend` syntax.

**Additional context**

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.