diff --git a/esphome/components/valve/__init__.py b/esphome/components/valve/__init__.py index f39eb4dc8d7..d82a9fdec28 100644 --- a/esphome/components/valve/__init__.py +++ b/esphome/components/valve/__init__.py @@ -240,23 +240,29 @@ async def valve_control_to_code(config, action_id, template_arg, args): (CONF_POSITION, "set_position", cg.float_), ) + # Normalize trigger args to `const std::remove_cvref_t &` so the + # apply lambda and any inner field lambdas (generated below via + # `process_lambda`) share one parameter spelling that's well-formed for + # any T (value, ref, or const-ref). Matches ControlAction::ApplyFn. + normalized_args = [ + (cg.RawExpression(f"const std::remove_cvref_t<{cg.safe_exp(t)}> &"), n) + for t, n in args + ] + 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_) + inner = await cg.process_lambda(value, normalized_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: forward trigger args as Ts... - # so the apply lambda's parameter types match both ApplyFn and the - # inner field lambdas (which are generated from `args` directly). apply_args = [ (ValveCall.operator("ref"), "call"), - *args, + *normalized_args, ] apply_lambda = LambdaExpression( ["\n".join(body_lines)], diff --git a/esphome/components/valve/automation.h b/esphome/components/valve/automation.h index 88c4ed760df..27c0e329f08 100644 --- a/esphome/components/valve/automation.h +++ b/esphome/components/valve/automation.h @@ -53,16 +53,15 @@ template class ToggleAction : public Action { // Trigger args are forwarded to the apply function so user lambdas // (e.g. `position: !lambda "return x;"`) keep working. // -// Trigger args are forwarded as `Ts...`. The previous `const Ts &...` -// form caused codegen to emit `const T &` for each arg in the apply -// lambda's parameter list, which is invalid C++ source text when T is -// already a reference (e.g. `const std::string & &` for triggers that -// pass `std::string &`). Forwarding `Ts...` lets the codegen reuse the -// trigger's `args` types unchanged for both the apply lambda and any -// inner field lambdas, so they always type-match. +// Trigger args are normalized to `const std::remove_cvref_t &...` so +// the codegen can emit a matching parameter list for both the apply lambda +// and any inner field lambdas without producing invalid C++ source text +// (e.g. `const T & &` if Ts already carries a reference, or `const const +// T &` if Ts already carries a const). This keeps trigger args no-copy +// regardless of whether the trigger supplies `T`, `T &`, or `const T &`. template class ControlAction : public Action { public: - using ApplyFn = void (*)(ValveCall &, Ts...); + using ApplyFn = void (*)(ValveCall &, const std::remove_cvref_t &...); ControlAction(Valve *valve, ApplyFn apply) : valve_(valve), apply_(apply) {} void play(const Ts &...x) override {