From 73bb4aa4d5428cd4d1034d8dd7ad9800903d632f Mon Sep 17 00:00:00 2001 From: Anton Sergunov Date: Mon, 29 Apr 2024 16:40:03 +0600 Subject: [PATCH] [template/text] Fix lambda config (#6655) Co-authored-by: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com> --- esphome/components/template/text/__init__.py | 2 +- tests/component_tests/text/test_text.py | 14 ++++++++++++++ tests/component_tests/text/test_text.yaml | 12 ++++++++++++ tests/components/template/common.yaml | 11 +++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/esphome/components/template/text/__init__.py b/esphome/components/template/text/__init__.py index 0f228a3c6b..f73b240197 100644 --- a/esphome/components/template/text/__init__.py +++ b/esphome/components/template/text/__init__.py @@ -28,7 +28,7 @@ def validate(config): raise cv.Invalid("optimistic cannot be used with lambda") if CONF_INITIAL_VALUE in config: raise cv.Invalid("initial_value cannot be used with lambda") - if CONF_RESTORE_VALUE in config: + if config[CONF_RESTORE_VALUE]: raise cv.Invalid("restore_value cannot be used with lambda") elif CONF_INITIAL_VALUE not in config: config[CONF_INITIAL_VALUE] = "" diff --git a/tests/component_tests/text/test_text.py b/tests/component_tests/text/test_text.py index 43f4ef2592..51fcb3d382 100644 --- a/tests/component_tests/text/test_text.py +++ b/tests/component_tests/text/test_text.py @@ -54,3 +54,17 @@ def test_text_config_value_mode_set(generate_main): # Then assert "it_1->traits.set_mode(text::TEXT_MODE_TEXT);" in main_cpp assert "it_3->traits.set_mode(text::TEXT_MODE_PASSWORD);" in main_cpp + + +def test_text_config_lamda_is_set(generate_main): + """ + Test if lambda is set for lambda mode + """ + # Given + + # When + main_cpp = generate_main("tests/component_tests/text/test_text.yaml") + + # Then + assert "it_4->set_template([=]() -> optional {" in main_cpp + assert 'return std::string{"Hello"};' in main_cpp diff --git a/tests/component_tests/text/test_text.yaml b/tests/component_tests/text/test_text.yaml index d0fdf5303f..d81c909f9d 100644 --- a/tests/component_tests/text/test_text.yaml +++ b/tests/component_tests/text/test_text.yaml @@ -31,3 +31,15 @@ text: optimistic: true internal: true max_length: 255 + + - platform: template + name: "test 4 key" + id: "it_4" + mode: text + set_action: + - then: + - logger.log: + format: Template text set to %s + args: ["x.c_str()"] + lambda: | + return std::string{"Hello"}; diff --git a/tests/components/template/common.yaml b/tests/components/template/common.yaml index ba7167157b..2b91225b5a 100644 --- a/tests/components/template/common.yaml +++ b/tests/components/template/common.yaml @@ -149,6 +149,17 @@ text: min_length: 0 max_length: 100 mode: text + - platform: template + name: "Template text lambda" + mode: text + update_interval: 1s + lambda: | + return std::string{"Hello!"}; + set_action: + then: + - logger.log: + format: Template Text set to %s + args: ["x.c_str()"] alarm_control_panel: - platform: template