[text] Use register_apply_action for text.set (#19509)

This commit is contained in:
J. Nick Koston
2026-09-23 18:24:25 -04:00
committed by GitHub
parent bd91d3ff23
commit 4efeee245c
3 changed files with 16 additions and 34 deletions
+5 -19
View File
@@ -13,13 +13,13 @@ from esphome.const import (
CONF_VALUE,
CONF_WEB_SERVER,
)
from esphome.core import CORE, ID, CoroPriority, coroutine_with_priority
from esphome.core import CORE, CoroPriority, coroutine_with_priority
from esphome.core.entity_helpers import (
entity_duplicate_validator,
queue_entity_register,
setup_entity,
)
from esphome.cpp_generator import MockObj, MockObjClass, TemplateArgsType
from esphome.cpp_generator import MockObj, MockObjClass
from esphome.types import ConfigType
CODEOWNERS = ["@mauritskorse"]
@@ -34,9 +34,6 @@ TextStateTrigger = text_ns.class_(
"TextStateTrigger", automation.Trigger.template(cg.std_string)
)
# Actions
TextSetAction = text_ns.class_("TextSetAction", automation.Action)
# Conditions
TextMode = text_ns.enum("TextMode")
@@ -160,24 +157,13 @@ OPERATION_BASE_SCHEMA = cv.Schema(
)
@automation.register_action(
automation.register_apply_action(
"text.set",
TextSetAction,
OPERATION_BASE_SCHEMA.extend(
{
cv.Required(CONF_VALUE): cv.templatable(cv.string_strict),
}
),
synchronous=True,
automation.ApplyField(CONF_VALUE, "set_value", cg.std_string),
call="make_call",
)
async def text_set_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_VALUE], args, cg.std_string)
cg.add(var.set_value(template_))
return var
-15
View File
@@ -13,19 +13,4 @@ class TextStateTrigger final : public Trigger<std::string> {
}
};
template<typename... Ts> class TextSetAction final : public Action<Ts...> {
public:
explicit TextSetAction(Text *text) : text_(text) {}
TEMPLATABLE_VALUE(std::string, value)
void play(const Ts &...x) override {
auto call = this->text_->make_call();
call.set_value(this->value_.value(x...));
call.perform();
}
protected:
Text *text_;
};
} // namespace esphome::text
+11
View File
@@ -28,3 +28,14 @@ text_sensor:
- platform: text
name: "Test Text State"
source_id: test_text
button:
- platform: template
name: Set Text
on_press:
- text.set:
id: test_text
value: Hello
- text.set:
id: test_text
value: !lambda return id(test_text).state + "!";