From 2d91e6b97760974ae6e5d3a02018b83a67ffd3f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Mon, 6 Sep 2021 22:00:08 +0200 Subject: [PATCH] template: select: fix initial_value cannot be used with lambda (#2244) --- .../components/template/select/__init__.py | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/esphome/components/template/select/__init__.py b/esphome/components/template/select/__init__.py index 6562beb7f8..4eba77119d 100644 --- a/esphome/components/template/select/__init__.py +++ b/esphome/components/template/select/__init__.py @@ -19,17 +19,6 @@ TemplateSelect = template_ns.class_( CONF_SET_ACTION = "set_action" -def validate_initial_value_in_options(config): - if CONF_INITIAL_OPTION in config: - if config[CONF_INITIAL_OPTION] not in config[CONF_OPTIONS]: - raise cv.Invalid( - f"initial_option '{config[CONF_INITIAL_OPTION]}' is not a valid option [{', '.join(config[CONF_OPTIONS])}]" - ) - else: - config[CONF_INITIAL_OPTION] = config[CONF_OPTIONS][0] - return config - - def validate(config): if CONF_LAMBDA in config: if config[CONF_OPTIMISTIC]: @@ -38,6 +27,14 @@ def validate(config): raise cv.Invalid("initial_value cannot be used with lambda") if CONF_RESTORE_VALUE in config: raise cv.Invalid("restore_value cannot be used with lambda") + elif CONF_INITIAL_OPTION in config: + if config[CONF_INITIAL_OPTION] not in config[CONF_OPTIONS]: + raise cv.Invalid( + f"initial_option '{config[CONF_INITIAL_OPTION]}' is not a valid option [{', '.join(config[CONF_OPTIONS])}]" + ) + else: + config[CONF_INITIAL_OPTION] = config[CONF_OPTIONS][0] + if not config[CONF_OPTIMISTIC] and CONF_SET_ACTION not in config: raise cv.Invalid( "Either optimistic mode must be enabled, or set_action must be set, to handle the option being set." @@ -59,7 +56,6 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_RESTORE_VALUE): cv.boolean, } ).extend(cv.polling_component_schema("60s")), - validate_initial_value_in_options, validate, )