1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-31 07:03:55 +00:00

Jinja expressions in configs (Take #3) (#8955)

This commit is contained in:
Javier Peletier
2025-07-01 04:57:00 +02:00
committed by GitHub
parent 27c745d5a1
commit 8c34b72b62
21 changed files with 486 additions and 24 deletions

View File

@@ -0,0 +1 @@
*.received.yaml

View File

@@ -0,0 +1,19 @@
substitutions:
var1: '1'
var2: '2'
var21: '79'
esphome:
name: test
test_list:
- '1'
- '1'
- '1'
- '1'
- 'Values: 1 2'
- 'Value: 79'
- 1 + 2
- 1 * 2
- 'Undefined var: ${undefined_var}'
- ${undefined_var}
- $undefined_var
- ${ undefined_var }

View File

@@ -0,0 +1,21 @@
esphome:
name: test
substitutions:
var1: "1"
var2: "2"
var21: "79"
test_list:
- "$var1"
- "${var1}"
- $var1
- ${var1}
- "Values: $var1 ${var2}"
- "Value: ${var2${var1}}"
- "$var1 + $var2"
- "${ var1 } * ${ var2 }"
- "Undefined var: ${undefined_var}"
- ${undefined_var}
- $undefined_var
- ${ undefined_var }

View File

@@ -0,0 +1,15 @@
substitutions:
var1: '1'
var2: '2'
a: alpha
test_list:
- values:
- var1: '1'
- a: A
- b: B-default
- c: The value of C is C
- values:
- var1: '1'
- a: alpha
- b: beta
- c: The value of C is $c

View File

@@ -0,0 +1,15 @@
substitutions:
var1: "1"
var2: "2"
a: "alpha"
test_list:
- !include
file: inc1.yaml
vars:
a: "A"
c: "C"
- !include
file: inc1.yaml
vars:
b: "beta"

View File

@@ -0,0 +1,24 @@
substitutions:
width: 7
height: 8
enabled: true
pin: &id001
number: 18
inverted: true
area: 25
numberOne: 1
var1: 79
test_list:
- The area is 56
- 56
- 56 + 1
- ENABLED
- list:
- 7
- 8
- width: 7
height: 8
- *id001
- The pin number is 18
- The square root is: 5.0
- The number is 80

View File

@@ -0,0 +1,22 @@
substitutions:
width: 7
height: 8
enabled: true
pin:
number: 18
inverted: true
area: 25
numberOne: 1
var1: 79
test_list:
- "The area is ${width * height}"
- ${width * height}
- ${width * height} + 1
- ${enabled and "ENABLED" or "DISABLED"}
- list: ${ [width, height] }
- "${ {'width': width, 'height': height} }"
- ${pin}
- The pin number is ${pin.number}
- The square root is: ${math.sqrt(area)}
- The number is ${var${numberOne} + 1}

View File

@@ -0,0 +1,17 @@
substitutions:
B: 5
var7: 79
package_result:
- The value of A*B is 35, where A is a package var and B is a substitution in the
root file
- Double substitution also works; the value of var7 is 79, where A is a package
var
local_results:
- The value of B is 5
- 'You will see, however, that
${A} is not substituted here, since
it is out of scope.
'

View File

@@ -0,0 +1,16 @@
substitutions:
B: 5
var7: 79
packages:
closures_package: !include
file: closures_package.yaml
vars:
A: 7
local_results:
- The value of B is ${B}
- |
You will see, however, that
${A} is not substituted here, since
it is out of scope.

View File

@@ -0,0 +1,5 @@
display:
- platform: ili9xxx
dimensions:
width: 960
height: 544

View File

@@ -0,0 +1,7 @@
# main.yaml
packages:
my_display: !include
file: display.yaml
vars:
high_dpi: true
native_height: 272

View File

@@ -0,0 +1,3 @@
package_result:
- The value of A*B is ${A * B}, where A is a package var and B is a substitution in the root file
- Double substitution also works; the value of var7 is ${var$A}, where A is a package var

View File

@@ -0,0 +1,11 @@
# display.yaml
defaults:
native_width: 480
native_height: 480
display:
- platform: ili9xxx
dimensions:
width: ${high_dpi and native_width * 2 or native_width}
height: ${high_dpi and native_height * 2 or native_height}

View File

@@ -0,0 +1,8 @@
defaults:
b: "B-default"
values:
- var1: $var1
- a: $a
- b: ${b}
- c: The value of C is $c