[valve] Fold ControlAction fields into a single stateless lambda (#16123)

This commit is contained in:
J. Nick Koston
2026-04-29 14:20:15 -05:00
committed by GitHub
parent 61a41402df
commit 5a146ab6b7
2 changed files with 46 additions and 21 deletions

View File

@@ -21,14 +21,14 @@ from esphome.const import (
DEVICE_CLASS_GAS,
DEVICE_CLASS_WATER,
)
from esphome.core import CORE, CoroPriority, coroutine_with_priority
from esphome.core import CORE, CoroPriority, Lambda, coroutine_with_priority
from esphome.core.entity_helpers import (
entity_duplicate_validator,
queue_entity_register,
setup_device_class,
setup_entity,
)
from esphome.cpp_generator import MockObjClass
from esphome.cpp_generator import LambdaExpression, MockObjClass
IS_PLATFORM_COMPONENT = True
@@ -43,6 +43,7 @@ DEVICE_CLASSES = [
valve_ns = cg.esphome_ns.namespace("valve")
Valve = valve_ns.class_("Valve", cg.EntityBase)
ValveCall = valve_ns.class_("ValveCall")
VALVE_OPEN = valve_ns.VALVE_OPEN
VALVE_CLOSED = valve_ns.VALVE_CLOSED
@@ -228,17 +229,40 @@ VALVE_CONTROL_ACTION_SCHEMA = cv.Schema(
)
async def valve_control_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
if stop_config := config.get(CONF_STOP):
template_ = await cg.templatable(stop_config, args, cg.bool_)
cg.add(var.set_stop(template_))
if state_config := config.get(CONF_STATE):
template_ = await cg.templatable(state_config, args, cg.float_)
cg.add(var.set_position(template_))
if (position_config := config.get(CONF_POSITION)) is not None:
template_ = await cg.templatable(position_config, args, cg.float_)
cg.add(var.set_position(template_))
return var
# All configured fields are folded into a single stateless lambda whose
# constants live in flash; the action stores only a function pointer.
# CONF_STATE and CONF_POSITION are cv.Exclusive in the schema, so at most
# one is present and both dispatch to set_position.
FIELDS = (
(CONF_STOP, "set_stop", cg.bool_),
(CONF_STATE, "set_position", cg.float_),
(CONF_POSITION, "set_position", cg.float_),
)
fwd_args = ", ".join(name for _, name in args)
body_lines: list[str] = []
for conf_key, setter, type_ in FIELDS:
if (value := config.get(conf_key)) is None:
continue
if isinstance(value, Lambda):
inner = await cg.process_lambda(value, args, return_type=type_)
body_lines.append(f"call.{setter}(({inner})({fwd_args}));")
else:
body_lines.append(f"call.{setter}({cg.safe_exp(value)});")
# Match ControlAction::ApplyFn signature: const Ts &... for trigger args.
apply_args = [
(ValveCall.operator("ref"), "call"),
*((t.operator("const").operator("ref"), n) for t, n in args),
]
apply_lambda = LambdaExpression(
["\n".join(body_lines)],
apply_args,
capture="",
return_type=cg.void,
)
return cg.new_Pvariable(action_id, template_arg, paren, apply_lambda)
@coroutine_with_priority(CoroPriority.CORE)

View File

@@ -47,24 +47,25 @@ template<typename... Ts> class ToggleAction : public Action<Ts...> {
Valve *valve_;
};
// All configured fields are baked into a single stateless lambda whose
// constants live in flash. The action only stores one function pointer
// plus one parent pointer, regardless of how many fields the user set.
// Trigger args are forwarded to the apply function so user lambdas
// (e.g. `position: !lambda "return x;"`) keep working.
template<typename... Ts> class ControlAction : public Action<Ts...> {
public:
explicit ControlAction(Valve *valve) : valve_(valve) {}
TEMPLATABLE_VALUE(bool, stop)
TEMPLATABLE_VALUE(float, position)
using ApplyFn = void (*)(ValveCall &, const Ts &...);
ControlAction(Valve *valve, ApplyFn apply) : valve_(valve), apply_(apply) {}
void play(const Ts &...x) override {
auto call = this->valve_->make_call();
if (this->stop_.has_value())
call.set_stop(this->stop_.value(x...));
if (this->position_.has_value())
call.set_position(this->position_.value(x...));
this->apply_(call, x...);
call.perform();
}
protected:
Valve *valve_;
ApplyFn apply_;
};
template<typename... Ts> class ValveIsOpenCondition : public Condition<Ts...> {