Compare commits

...

1 Commits

Author SHA1 Message Date
J. Nick Koston
e62a87afe1 [template] Conditionally compile select set_trigger
Only allocate the set_trigger when set_action is configured.
This saves ~20-24 bytes of heap per template select that doesn't
use set_action.
2026-02-01 20:39:52 +01:00
4 changed files with 17 additions and 0 deletions

View File

@@ -87,6 +87,7 @@ async def to_code(config):
cg.add(var.set_restore_value(True))
if CONF_SET_ACTION in config:
cg.add_define("USE_TEMPLATE_SELECT_SET_TRIGGER")
await automation.build_automation(
var.get_set_trigger(), [(cg.StringRef, "x")], config[CONF_SET_ACTION]
)

View File

@@ -41,7 +41,9 @@ void TemplateSelect::update() {
}
void TemplateSelect::control(size_t index) {
#ifdef USE_TEMPLATE_SELECT_SET_TRIGGER
this->set_trigger_->trigger(StringRef(this->option_at(index)));
#endif
if (this->optimistic_)
this->publish_state(index);

View File

@@ -18,7 +18,9 @@ class TemplateSelect final : public select::Select, public PollingComponent {
void dump_config() override;
float get_setup_priority() const override { return setup_priority::HARDWARE; }
#ifdef USE_TEMPLATE_SELECT_SET_TRIGGER
Trigger<StringRef> *get_set_trigger() const { return this->set_trigger_; }
#endif
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
void set_initial_option_index(size_t initial_option_index) { this->initial_option_index_ = initial_option_index; }
void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; }
@@ -28,7 +30,9 @@ class TemplateSelect final : public select::Select, public PollingComponent {
bool optimistic_ = false;
size_t initial_option_index_{0};
bool restore_value_ = false;
#ifdef USE_TEMPLATE_SELECT_SET_TRIGGER
Trigger<StringRef> *set_trigger_ = new Trigger<StringRef>();
#endif
TemplateLambda<std::string> f_;
ESPPreferenceObject pref_;

View File

@@ -296,6 +296,16 @@ select:
// Migration guide: Store in std::string
std::string stored_option(id(template_select).current_option());
ESP_LOGI("test", "Stored: %s", stored_option.c_str());
- platform: template
id: template_select_with_action
name: "Template select with action"
options:
- option_a
- option_b
set_action:
- logger.log:
format: "Selected: %s"
args: ["x.c_str()"]
lock:
- platform: template