fix substitution losing track of document range (#1547)

This commit is contained in:
Guillermo Ruffino 2021-02-19 21:52:42 -03:00 committed by GitHub
parent 6c8ace0ce8
commit 25924ca4e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 18 deletions

View file

@ -4,6 +4,7 @@ import re
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome import core from esphome import core
from esphome.const import CONF_SUBSTITUTIONS from esphome.const import CONF_SUBSTITUTIONS
from esphome.yaml_util import ESPHomeDataBase, make_data_base
CODEOWNERS = ['@esphome/core'] CODEOWNERS = ['@esphome/core']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -68,6 +69,14 @@ def _expand_substitutions(substitutions, value, path):
value = value[:i] + sub value = value[:i] + sub
i = len(value) i = len(value)
value += tail value += tail
# orig_value can also already be a lambda with esp_range info, and only
# a plain string is sent in orig_value
if isinstance(orig_value, ESPHomeDataBase):
# even though string can get larger or smaller, the range should point
# to original document marks
return make_data_base(value, orig_value)
return value return value

View file

@ -1,5 +1,8 @@
substitutions: substitutions:
devicename: test1 devicename: test1
sensorname: my
textname: template
roomname: living_room
esphome: esphome:
name: test1 name: test1
@ -36,7 +39,7 @@ esphome:
verify_ssl: false verify_ssl: false
json: json:
key: !lambda |- key: !lambda |-
return id(template_text).state; return id(${textname}_text).state;
greeting: 'Hello World' greeting: 'Hello World'
- http_request.send: - http_request.send:
method: PUT method: PUT
@ -121,7 +124,7 @@ mqtt:
int data = x["my_data"]; int data = x["my_data"];
ESP_LOGD("main", "The data is: %d", data); ESP_LOGD("main", "The data is: %d", data);
- light.turn_on: - light.turn_on:
id: living_room_lights id: ${roomname}_lights
brightness: !lambda |- brightness: !lambda |-
float brightness = 1.0; float brightness = 1.0;
if (x.containsKey("brightness")) if (x.containsKey("brightness"))
@ -133,10 +136,10 @@ mqtt:
effect = x["effect"]; effect = x["effect"];
return effect; return effect;
- light.control: - light.control:
id: living_room_lights id: ${roomname}_lights
brightness: !lambda 'return id(living_room_lights).current_values.get_brightness() + 0.5;' brightness: !lambda 'return id(${roomname}_lights).current_values.get_brightness() + 0.5;'
- light.dim_relative: - light.dim_relative:
id: living_room_lights id: ${roomname}_lights
relative_brightness: 5% relative_brightness: 5%
- uart.write: - uart.write:
id: uart0 id: uart0
@ -278,9 +281,9 @@ sensor:
then: then:
- lambda: |- - lambda: |-
ESP_LOGD("main", "Got value %f", x); ESP_LOGD("main", "Got value %f", x);
id(my_sensor).publish_state(42.0); id(${sensorname}_sensor).publish_state(42.0);
ESP_LOGI("main", "Value of my sensor: %f", id(my_sensor).state); ESP_LOGI("main", "Value of my sensor: %f", id(${sensorname}_sensor).state);
ESP_LOGI("main", "Raw Value of my sensor: %f", id(my_sensor).state); ESP_LOGI("main", "Raw Value of my sensor: %f", id(${sensorname}_sensor).state);
on_value_range: on_value_range:
above: 5 above: 5
below: 10 below: 10
@ -307,7 +310,7 @@ sensor:
- platform: ads1115 - platform: ads1115
multiplexer: 'A0_A1' multiplexer: 'A0_A1'
gain: 1.024 gain: 1.024
id: my_sensor id: ${sensorname}_sensor
filters: filters:
state_topic: hi/me state_topic: hi/me
retain: false retain: false
@ -946,12 +949,12 @@ binary_sensor:
name: 'Garage Door Open' name: 'Garage Door Open'
id: garage_door id: garage_door
lambda: |- lambda: |-
if (isnan(id(my_sensor).state)) { if (isnan(id(${sensorname}_sensor).state)) {
// isnan checks if the ultrasonic sensor echo // isnan checks if the ultrasonic sensor echo
// has timed out, resulting in a NaN (not a number) state // has timed out, resulting in a NaN (not a number) state
// in that case, return {} to indicate that we don't know. // in that case, return {} to indicate that we don't know.
return {}; return {};
} else if (id(my_sensor).state > 30) { } else if (id(${sensorname}_sensor).state > 30) {
// Garage Door is open. // Garage Door is open.
return true; return true;
} else { } else {
@ -1244,7 +1247,7 @@ light:
state = 0; state = 0;
- platform: rgb - platform: rgb
name: 'Living Room Lights' name: 'Living Room Lights'
id: living_room_lights id: ${roomname}_lights
red: pca_0 red: pca_0
green: pca_1 green: pca_1
blue: pca_2 blue: pca_2
@ -1400,14 +1403,14 @@ climate:
name: TCL112 Climate With Sensor name: TCL112 Climate With Sensor
supports_heat: True supports_heat: True
supports_cool: True supports_cool: True
sensor: my_sensor sensor: ${sensorname}_sensor
- platform: tcl112 - platform: tcl112
name: TCL112 Climate name: TCL112 Climate
- platform: coolix - platform: coolix
name: Coolix Climate With Sensor name: Coolix Climate With Sensor
supports_heat: True supports_heat: True
supports_cool: True supports_cool: True
sensor: my_sensor sensor: ${sensorname}_sensor
- platform: coolix - platform: coolix
name: Coolix Climate name: Coolix Climate
- platform: fujitsu_general - platform: fujitsu_general
@ -1976,10 +1979,10 @@ text_sensor:
qos: 2 qos: 2
on_value: on_value:
- text_sensor.template.publish: - text_sensor.template.publish:
id: template_text id: ${textname}_text
state: Hello World state: Hello World
- text_sensor.template.publish: - text_sensor.template.publish:
id: template_text id: ${textname}_text
state: |- state: |-
return "Hello World2"; return "Hello World2";
- globals.set: - globals.set:
@ -1990,7 +1993,7 @@ text_sensor:
data: [0x10, 0x20, 0x30] data: [0x10, 0x20, 0x30]
- platform: template - platform: template
name: Template Text Sensor name: Template Text Sensor
id: template_text id: ${textname}_text
- platform: wifi_info - platform: wifi_info
ip_address: ip_address:
name: 'IP Address' name: 'IP Address'
@ -2032,4 +2035,4 @@ canbus:
condition: condition:
lambda: 'return x[0] == 0x11;' lambda: 'return x[0] == 0x11;'
then: then:
light.toggle: living_room_lights light.toggle: ${roomname}_lights