diff --git a/esphome/components/fan/__init__.py b/esphome/components/fan/__init__.py index 23483adde74..3949f16d2e1 100644 --- a/esphome/components/fan/__init__.py +++ b/esphome/components/fan/__init__.py @@ -358,26 +358,29 @@ async def fan_turn_on_to_code(config, action_id, template_arg, args): (CONF_DIRECTION, "set_direction", FanDirection), ) + # 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 TurnOnAction::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 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"), - *( - (cg.RawExpression(f"const std::remove_reference_t<{cg.safe_exp(t)}> &"), n) - for t, n in args - ), + *normalized_args, ] apply_lambda = LambdaExpression( ["\n".join(body_lines)], diff --git a/esphome/components/fan/automation.h b/esphome/components/fan/automation.h index 9c14eab3da9..577c9ce600e 100644 --- a/esphome/components/fan/automation.h +++ b/esphome/components/fan/automation.h @@ -13,13 +13,15 @@ namespace fan { // Trigger args are forwarded to the apply function so user lambdas // (e.g. `speed: !lambda "return x;"`) keep working. // -// 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 &`). +// 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 TurnOnAction : public Action { public: - using ApplyFn = void (*)(FanCall &, const std::remove_reference_t &...); + using ApplyFn = void (*)(FanCall &, const std::remove_cvref_t &...); TurnOnAction(Fan *state, ApplyFn apply) : state_(state), apply_(apply) {} void play(const Ts &...x) override {