diff --git a/esphome/components/fan/__init__.py b/esphome/components/fan/__init__.py index bc006a6602d..23483adde74 100644 --- a/esphome/components/fan/__init__.py +++ b/esphome/components/fan/__init__.py @@ -369,11 +369,15 @@ async def fan_turn_on_to_code(config, action_id, template_arg, args): else: body_lines.append(f"call.{setter}({cg.safe_exp(value)});") - # Match TurnOnAction::ApplyFn signature: trigger args forwarded - # by-value as Ts... + # Match TurnOnAction::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 = [ (FanCall.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/fan/automation.h b/esphome/components/fan/automation.h index a5ed80aec6d..9c14eab3da9 100644 --- a/esphome/components/fan/automation.h +++ b/esphome/components/fan/automation.h @@ -13,11 +13,13 @@ namespace fan { // Trigger args are forwarded to the apply function so user lambdas // (e.g. `speed: !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 TurnOnAction : public Action { public: - using ApplyFn = void (*)(FanCall &, Ts...); + using ApplyFn = void (*)(FanCall &, const std::remove_reference_t &...); TurnOnAction(Fan *state, ApplyFn apply) : state_(state), apply_(apply) {} void play(const Ts &...x) override {