esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
290 stars 34 forks source link

Substitutions in `!include`d files do not get updated with the values passed in `vars` #5568

Open fakuivan opened 6 months ago

fakuivan commented 6 months ago

The problem

vars in !include do not get substituted in entries for the substitutions components on the imported file, making the following fail:

---
# main file
...
  !include
    file: my_file.yaml
    vars:
      my_id: some_id
---
# my_file.yaml
substitutions:
  # this fails
  sensor_name: Sensor of id $my_id

binary_sensor:
  name: $sensor_name
  id: $my_id

This is a result of how vars in !include is processed as substitutions in substitute_vars.

https://github.com/esphome/esphome/blob/357ac3b85f6cff3c93ec8a3c29daca1e945a4fda/esphome/yaml_util.py#L286-L316

Specifically this line, where the contents of the substitutions section is swapped by what's in vars, instead of first substituted and then swapped back in. It'll probably be a good idea to specify how substitutions are to be merged in cases like this.

Which version of ESPHome has the issue?

2024.3.0-dev

What type of installation are you using?

pip

Which version of Home Assistant has the issue?

No response

What platform are you using?

ESP8266

Board

No response

Component causing the issue

No response

Example YAML snippet

---
# main file
esphome:
  name: test

packages:
  included: !include
    file: included.yaml
    vars:
      included_sensor_id: included_sensor

host:

---
# included.yaml

substitutions:
  included_sensor_id_nested: $included_sensor_id

sensor:
  - id: $included_sensor_id_nested
    platform: template
    name: Inlcuded sensor

Anything in the logs that might be useful for us?

WARNING Found '$included_sensor_id' (see substitutions->included_sensor_id_nested) which looks like a substitution, but 'included_sensor_id' was not declared
WARNING Found '$included_sensor_id' (see substitutions->included_sensor_id_nested) which looks like a substitution, but 'included_sensor_id' was not declared
WARNING Found '$included_sensor_id' (see sensor->0->id) which looks like a substitution, but 'included_sensor_id' was not declared

Additional information

No response

ssieb commented 6 months ago

What is the point of doing substitutions like that?

fakuivan commented 6 months ago

The same point as using substitutions in the first place, avoiding repetition, for example:

---
# main file
esphome:
  name: test

packages:
  included: !include
    file: included.yaml
    vars:
      value: some value

host:

---
# included.yaml

substitutions:
  frequently_used_value: ${value} and more

...
- $frequently_used_value
- $frequently_used_value
- $frequently_used_value
- $frequently_used_value
- $frequently_used_value

that's the first thing I can come up with, I'm sure there are more uses

github-actions[bot] commented 2 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.