From 3c2383e26119c3c05341786aa335ea7653c0d120 Mon Sep 17 00:00:00 2001 From: mknjc Date: Thu, 21 Dec 2023 01:08:13 +0100 Subject: [PATCH] Add default substitutions for package includes (#5752) --- esphome/const.py | 1 + esphome/yaml_util.py | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/esphome/const.py b/esphome/const.py index 5de34b86cd..58c6c47e3a 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -185,6 +185,7 @@ CONF_DEFAULT_MODE = "default_mode" CONF_DEFAULT_TARGET_TEMPERATURE_HIGH = "default_target_temperature_high" CONF_DEFAULT_TARGET_TEMPERATURE_LOW = "default_target_temperature_low" CONF_DEFAULT_TRANSITION_LENGTH = "default_transition_length" +CONF_DEFAULTS = "defaults" CONF_DELAY = "delay" CONF_DELIMITER = "delimiter" CONF_DELTA = "delta" diff --git a/esphome/yaml_util.py b/esphome/yaml_util.py index f0f755dd61..aa9fe45ebb 100644 --- a/esphome/yaml_util.py +++ b/esphome/yaml_util.py @@ -282,7 +282,7 @@ class ESPHomeLoader(FastestAvailableSafeLoader): return file, vars def substitute_vars(config, vars): - from esphome.const import CONF_SUBSTITUTIONS + from esphome.const import CONF_SUBSTITUTIONS, CONF_DEFAULTS from esphome.components import substitutions org_subs = None @@ -294,7 +294,15 @@ class ESPHomeLoader(FastestAvailableSafeLoader): elif CONF_SUBSTITUTIONS in result: org_subs = result.pop(CONF_SUBSTITUTIONS) + defaults = {} + if CONF_DEFAULTS in result: + defaults = result.pop(CONF_DEFAULTS) + result[CONF_SUBSTITUTIONS] = vars + for k, v in defaults.items(): + if k not in result[CONF_SUBSTITUTIONS]: + result[CONF_SUBSTITUTIONS][k] = v + # Ignore missing vars that refer to the top level substitutions substitutions.do_substitution_pass(result, None, ignore_missing=True) result.pop(CONF_SUBSTITUTIONS)