From 431385ebc1ea01cb194e7ae4ac066212a46ac6ad Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 3 May 2026 16:56:14 -0500 Subject: [PATCH] [valve] Use const remove_reference_t & to avoid copies of non-ref Ts --- esphome/components/valve/__init__.py | 10 +++++++--- esphome/components/valve/automation.h | 8 +++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/esphome/components/valve/__init__.py b/esphome/components/valve/__init__.py index bc7e2ba48d..e072f70d1b 100644 --- a/esphome/components/valve/__init__.py +++ b/esphome/components/valve/__init__.py @@ -251,11 +251,15 @@ async def valve_control_to_code(config, action_id, template_arg, args): else: body_lines.append(f"call.{setter}({cg.safe_exp(value)});") - # Match ControlAction::ApplyFn signature: trigger args forwarded - # by-value as Ts... + # Match ControlAction::ApplyFn signature: `const std::remove_reference_t &` + # for each trigger arg so non-reference Ts stay no-copy (`const T &`) and + # reference Ts collapse correctly without producing `const T & &`. apply_args = [ (ValveCall.operator("ref"), "call"), - *args, + *( + (cg.RawExpression(f"const std::remove_reference_t<{cg.safe_exp(t)}> &"), n) + for t, n in args + ), ] apply_lambda = LambdaExpression( ["\n".join(body_lines)], diff --git a/esphome/components/valve/automation.h b/esphome/components/valve/automation.h index 81fcd12200..cc71999c35 100644 --- a/esphome/components/valve/automation.h +++ b/esphome/components/valve/automation.h @@ -53,11 +53,13 @@ template class ToggleAction : public Action { // Trigger args are forwarded to the apply function so user lambdas // (e.g. `position: !lambda "return x;"`) keep working. // -// Ts... must be forwarded by-value: `const T &` is ill-formed when T is -// already a reference type (some triggers pass `std::string &`). +// Trigger args are forwarded as `const std::remove_reference_t &...` +// (instead of `const Ts &...`) so codegen can emit the same form in the +// apply lambda's parameter list without producing `const T & &` for +// triggers whose Ts already carries a reference (e.g. `std::string &`). template class ControlAction : public Action { public: - using ApplyFn = void (*)(ValveCall &, Ts...); + using ApplyFn = void (*)(ValveCall &, const std::remove_reference_t &...); ControlAction(Valve *valve, ApplyFn apply) : valve_(valve), apply_(apply) {} void play(const Ts &...x) override {