mirror of
https://github.com/esphome/esphome.git
synced 2026-10-02 14:22:58 +08:00
[select] Build the on_value trigger with build_trigger_callback
This commit is contained in:
@@ -15,7 +15,6 @@ from esphome.const import (
|
||||
CONF_OPERATION,
|
||||
CONF_OPTION,
|
||||
CONF_OPTIONS,
|
||||
CONF_TRIGGER_ID,
|
||||
CONF_WEB_SERVER,
|
||||
)
|
||||
from esphome.core import CORE, ID, CoroPriority, coroutine_with_priority
|
||||
@@ -36,12 +35,6 @@ select_ns = cg.esphome_ns.namespace("select")
|
||||
Select = select_ns.class_("Select", cg.EntityBase)
|
||||
SelectPtr = Select.operator("ptr")
|
||||
|
||||
# Triggers
|
||||
SelectStateTrigger = select_ns.class_(
|
||||
"SelectStateTrigger",
|
||||
automation.Trigger.template(cg.StringRef, cg.size_t),
|
||||
)
|
||||
|
||||
# Conditions
|
||||
SelectIsCondition = select_ns.class_("SelectIsCondition", automation.Condition)
|
||||
|
||||
@@ -62,11 +55,7 @@ _SELECT_SCHEMA = (
|
||||
{
|
||||
cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(mqtt.MQTTSelectComponent),
|
||||
cv.GenerateID(): cv.declare_id(Select),
|
||||
cv.Optional(CONF_ON_VALUE): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SelectStateTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_VALUE): automation.validate_automation(),
|
||||
}
|
||||
)
|
||||
)
|
||||
@@ -98,9 +87,16 @@ async def setup_select_core_(var, config, *, options: list[str]):
|
||||
cg.add(var.traits.set_options(options))
|
||||
|
||||
for conf in config.get(CONF_ON_VALUE, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(
|
||||
trigger, [(cg.StringRef, "x"), (cg.size_t, "i")], conf
|
||||
# The callback carries the index; the automation gets the option text as well.
|
||||
parent = automation.parent_ref(var)
|
||||
index = cg.RawExpression("index")
|
||||
await automation.build_callback_automation(
|
||||
var,
|
||||
"add_on_state_callback",
|
||||
[(cg.StringRef, "x"), (cg.size_t, "i")],
|
||||
conf,
|
||||
params=[(cg.size_t, "index")],
|
||||
forward=[cg.StringRef(parent.option_at(index)), index],
|
||||
)
|
||||
|
||||
if (mqtt_id := config.get(CONF_MQTT_ID)) is not None:
|
||||
|
||||
@@ -6,17 +6,6 @@
|
||||
|
||||
namespace esphome::select {
|
||||
|
||||
class SelectStateTrigger final : public Trigger<StringRef, size_t> {
|
||||
public:
|
||||
explicit SelectStateTrigger(Select *parent) : parent_(parent) {
|
||||
parent->add_on_state_callback(
|
||||
[this](size_t index) { this->trigger(StringRef(this->parent_->option_at(index)), index); });
|
||||
}
|
||||
|
||||
protected:
|
||||
Select *parent_;
|
||||
};
|
||||
|
||||
template<size_t N, typename... Ts> class SelectIsCondition final : public Condition<Ts...> {
|
||||
public:
|
||||
SelectIsCondition(Select *parent, const char *const *option_list) : parent_(parent), option_list_(option_list) {}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
# on_value with both trigger arguments.
|
||||
select:
|
||||
- platform: template
|
||||
name: Select Triggers
|
||||
id: select_triggers
|
||||
optimistic: true
|
||||
options:
|
||||
- one
|
||||
- two
|
||||
- three
|
||||
on_value:
|
||||
- logger.log:
|
||||
format: "Selected %s at %zu"
|
||||
args: [x.c_str(), i]
|
||||
@@ -0,0 +1,3 @@
|
||||
packages:
|
||||
select: !include common.yaml
|
||||
triggers: !include common-triggers.yaml
|
||||
Reference in New Issue
Block a user