From e1126a152e2fda099f10ec9b99c89ee41ffacc70 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 24 Sep 2026 00:30:54 +0100 Subject: [PATCH] [text_sensor] Use register_apply_condition for text_sensor.state (#19541) --- esphome/components/text_sensor/__init__.py | 14 ++------------ esphome/components/text_sensor/automation.h | 12 ------------ tests/components/text_sensor/common.yaml | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/esphome/components/text_sensor/__init__.py b/esphome/components/text_sensor/__init__.py index 35e33f237c..34c46d5451 100644 --- a/esphome/components/text_sensor/__init__.py +++ b/esphome/components/text_sensor/__init__.py @@ -43,10 +43,6 @@ text_sensor_ns = cg.esphome_ns.namespace("text_sensor") TextSensor = text_sensor_ns.class_("TextSensor", cg.EntityBase) TextSensorPtr = TextSensor.operator("ptr") -TextSensorStateCondition = text_sensor_ns.class_( - "TextSensorStateCondition", automation.Condition -) - FILTER_REGISTRY = Registry() validate_filters = cv.validate_registry("filter", FILTER_REGISTRY) @@ -240,22 +236,16 @@ async def to_code(config): cg.add_global(text_sensor_ns.using) -@automation.register_condition( +automation.register_apply_condition( "text_sensor.state", - TextSensorStateCondition, cv.Schema( { cv.Required(CONF_ID): cv.use_id(TextSensor), cv.Required(CONF_STATE): cv.templatable(cv.string_strict), } ), + automation.ApplyCall("state == {}", ((CONF_STATE, cg.std_string),)), ) -async def text_sensor_state_to_code(config, condition_id, template_arg, args): - paren = await cg.get_variable(config[CONF_ID]) - var = cg.new_Pvariable(condition_id, template_arg, paren) - templ = await cg.templatable(config[CONF_STATE], args, cg.std_string) - cg.add(var.set_state(templ)) - return var FILTER_SOURCE_FILES = filter_source_files_from_defines( diff --git a/esphome/components/text_sensor/automation.h b/esphome/components/text_sensor/automation.h index ac130afa33..ae632753e5 100644 --- a/esphome/components/text_sensor/automation.h +++ b/esphome/components/text_sensor/automation.h @@ -22,16 +22,4 @@ class TextSensorStateRawTrigger final : public Trigger { } }; -template class TextSensorStateCondition final : public Condition { - public: - explicit TextSensorStateCondition(TextSensor *parent) : parent_(parent) {} - - TEMPLATABLE_VALUE(std::string, state) - - bool check(const Ts &...x) override { return this->parent_->state == this->state_.value(x...); } - - protected: - TextSensor *parent_; -}; - } // namespace esphome::text_sensor diff --git a/tests/components/text_sensor/common.yaml b/tests/components/text_sensor/common.yaml index ce1dd3a3f2..efbf4e1dc7 100644 --- a/tests/components/text_sensor/common.yaml +++ b/tests/components/text_sensor/common.yaml @@ -32,6 +32,23 @@ text_sensor: filters: - map: - ON -> Active + # text_sensor.state with a constant and with a lambda, inside a trigger + # whose argument is a std::string. + on_value: + - if: + condition: + text_sensor.state: + id: test_substitute_single + state: Error + then: + - logger.log: substitute reports an error + - if: + condition: + text_sensor.state: + id: test_substitute_single + state: !lambda return x; + then: + - logger.log: substitute matches the mapped value - platform: template name: "Test Map Multiple"