mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 07:20:24 +00:00
[text_sensor] Use register_apply_condition for text_sensor.state (#19541)
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -22,16 +22,4 @@ class TextSensorStateRawTrigger final : public Trigger<std::string> {
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Ts> class TextSensorStateCondition final : public Condition<Ts...> {
|
||||
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
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user