not as bad as I was thinking it would be

This commit is contained in:
J. Nick Koston
2026-02-02 02:31:29 +01:00
parent e62a87afe1
commit 9ca394d1e5
3 changed files with 23 additions and 13 deletions

View File

@@ -17,6 +17,9 @@ from .. import template_ns
TemplateSelect = template_ns.class_(
"TemplateSelect", select.Select, cg.PollingComponent
)
TemplateSelectWithSetAction = template_ns.class_(
"TemplateSelectWithSetAction", TemplateSelect
)
def validate(config):
@@ -62,7 +65,9 @@ CONFIG_SCHEMA = cv.All(
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
# Use subclass with trigger only when set_action is configured
cls = TemplateSelectWithSetAction if CONF_SET_ACTION in config else TemplateSelect
var = cg.new_Pvariable(config[CONF_ID], cls)
await cg.register_component(var, config)
await select.register_select(var, config, options=config[CONF_OPTIONS])
@@ -87,7 +92,6 @@ 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,10 +41,6 @@ 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);
@@ -52,6 +48,11 @@ void TemplateSelect::control(size_t index) {
this->pref_.save(&index);
}
void TemplateSelectWithSetAction::control(size_t index) {
this->set_trigger_.trigger(StringRef(this->option_at(index)));
TemplateSelect::control(index);
}
void TemplateSelect::dump_config() {
LOG_SELECT("", "Template Select", this);
LOG_UPDATE_INTERVAL(this);

View File

@@ -9,7 +9,8 @@
namespace esphome::template_ {
class TemplateSelect final : public select::Select, public PollingComponent {
/// Base template select class - used when no set_action is configured
class TemplateSelect : public select::Select, public PollingComponent {
public:
template<typename F> void set_template(F &&f) { this->f_.set(std::forward<F>(f)); }
@@ -18,9 +19,6 @@ 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; }
@@ -30,12 +28,19 @@ 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_;
};
/// Template select with set_action trigger - only instantiated when set_action is configured
class TemplateSelectWithSetAction final : public TemplateSelect {
public:
Trigger<StringRef> *get_set_trigger() { return &this->set_trigger_; }
protected:
void control(size_t index) override;
Trigger<StringRef> set_trigger_;
};
} // namespace esphome::template_