From d481d0cb4a5dff5e0b2ad64cd98f2ce86c2852db Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 3 May 2026 16:10:03 -0500 Subject: [PATCH 1/4] [fan] Fix TurnOnAction trigger args with reference types `const Ts &...` is ill-formed when Ts is already a reference (e.g. a trigger that passes `std::string &`). Forward Ts by-value so the generated lambda matches ApplyFn for any valid trigger arg type. --- esphome/components/fan/__init__.py | 5 +++-- esphome/components/fan/automation.h | 5 ++++- tests/components/fan/common.yaml | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/esphome/components/fan/__init__.py b/esphome/components/fan/__init__.py index f47fc06b3d..bc006a6602 100644 --- a/esphome/components/fan/__init__.py +++ b/esphome/components/fan/__init__.py @@ -369,10 +369,11 @@ 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: const Ts &... for trigger args. + # Match TurnOnAction::ApplyFn signature: trigger args forwarded + # by-value as Ts... apply_args = [ (FanCall.operator("ref"), "call"), - *((t.operator("const").operator("ref"), n) for t, n in args), + *args, ] apply_lambda = LambdaExpression( ["\n".join(body_lines)], diff --git a/esphome/components/fan/automation.h b/esphome/components/fan/automation.h index d8eda41b27..a5ed80aec6 100644 --- a/esphome/components/fan/automation.h +++ b/esphome/components/fan/automation.h @@ -12,9 +12,12 @@ namespace fan { // 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. `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 &`). template class TurnOnAction : public Action { public: - using ApplyFn = void (*)(FanCall &, const Ts &...); + using ApplyFn = void (*)(FanCall &, Ts...); TurnOnAction(Fan *state, ApplyFn apply) : state_(state), apply_(apply) {} void play(const Ts &...x) override { diff --git a/tests/components/fan/common.yaml b/tests/components/fan/common.yaml index 6cabbd24f8..91f799432c 100644 --- a/tests/components/fan/common.yaml +++ b/tests/components/fan/common.yaml @@ -88,3 +88,18 @@ button: - fan.turn_on: id: test_fan speed: !lambda 'return 1;' + +# Exercise fan.turn_on inside a trigger with non-empty Ts (number on_value +# passes float). +number: + - platform: template + id: fan_speed_number + optimistic: true + min_value: 1 + max_value: 3 + step: 1 + on_value: + then: + - fan.turn_on: + id: test_fan + speed: !lambda "return (int) x;" From 1509db2a2c9c9de3ea80f265dd8e49a07daf3348 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 3 May 2026 16:55:09 -0500 Subject: [PATCH 2/4] [fan] Use const remove_reference_t & to avoid copies of non-ref Ts --- esphome/components/fan/__init__.py | 10 +++++++--- esphome/components/fan/automation.h | 8 +++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/esphome/components/fan/__init__.py b/esphome/components/fan/__init__.py index bc006a6602..23483adde7 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 a5ed80aec6..9c14eab3da 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 { From e4de502bd4882c50ba26ee98ac9c05357f6adade Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 3 May 2026 17:30:01 -0500 Subject: [PATCH 3/4] [fan] Normalize trigger args to const remove_cvref_t & --- esphome/components/fan/__init__.py | 19 +++++++++++-------- esphome/components/fan/automation.h | 12 +++++++----- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/esphome/components/fan/__init__.py b/esphome/components/fan/__init__.py index 23483adde7..3949f16d2e 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 9c14eab3da..577c9ce600 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 { From 59de35a7b8d46dae6bf1fff8be24844df08fefe8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 3 May 2026 17:52:58 -0500 Subject: [PATCH 4/4] [fan] Cover fan.turn_on inside on_preset_set (StringRef Ts) in tests --- tests/components/fan/common.yaml | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/components/fan/common.yaml b/tests/components/fan/common.yaml index 91f799432c..76508f391e 100644 --- a/tests/components/fan/common.yaml +++ b/tests/components/fan/common.yaml @@ -9,6 +9,14 @@ fan: has_oscillating: true has_direction: true speed_count: 3 + # Exercise fan.turn_on inside a trigger whose Ts pack is non-empty + # (StringRef from on_preset_set) so the apply-lambda + inner-lambda + # codegen runs through the cvref-normalized path. + on_preset_set: + then: + - fan.turn_on: + id: test_fan + speed: !lambda "return x.empty() ? 1 : 3;" # Test lambdas using get_preset_mode() which returns StringRef # These examples match the migration guide in the PR description @@ -89,8 +97,11 @@ button: id: test_fan speed: !lambda 'return 1;' -# Exercise fan.turn_on inside a trigger with non-empty Ts (number on_value -# passes float). +# Exercise fan.turn_on inside triggers with non-empty Ts: +# - number.on_value: Ts = float (Python value type; previously raised +# AttributeError on .operator("const")) +# - fan.on_preset_set: Ts = StringRef (already a value-type wrapper around +# a const char * + size; tests the cvref-normalized inner-lambda path) number: - platform: template id: fan_speed_number