From cf223674e51cff65ed9c71b46b434c9c48dd8b72 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 3 May 2026 19:43:49 -0500 Subject: [PATCH] [climate] Fix ControlAction trigger args with reference types (#16221) --- esphome/components/climate/__init__.py | 14 +++++++++++--- esphome/components/climate/automation.h | 9 ++++++++- tests/components/climate/common.yaml | 15 +++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/esphome/components/climate/__init__.py b/esphome/components/climate/__init__.py index 7c9002d6dc..fc1b0f368e 100644 --- a/esphome/components/climate/__init__.py +++ b/esphome/components/climate/__init__.py @@ -506,6 +506,15 @@ async def climate_control_to_code(config, action_id, template_arg, args): (CONF_SWING_MODE, "set_swing_mode", ClimateSwingMode), ) + # 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] = [] @@ -513,7 +522,7 @@ async def climate_control_to_code(config, action_id, template_arg, args): 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}));") elif type_ is cg.std_string: # Static custom strings: emit a flash literal and pass the @@ -526,10 +535,9 @@ async def climate_control_to_code(config, action_id, template_arg, args): else: body_lines.append(f"call.{setter}({cg.safe_exp(value)});") - # Match ControlAction::ApplyFn signature: const Ts &... for trigger args. apply_args = [ (ClimateCall.operator("ref"), "call"), - *((t.operator("const").operator("ref"), n) for t, n in args), + *normalized_args, ] apply_lambda = LambdaExpression( ["\n".join(body_lines)], diff --git a/esphome/components/climate/automation.h b/esphome/components/climate/automation.h index 71d23fd6b6..6ac9bd8bae 100644 --- a/esphome/components/climate/automation.h +++ b/esphome/components/climate/automation.h @@ -10,9 +10,16 @@ namespace esphome::climate { // 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. `target_temperature: !lambda "return x;"`) keep working. +// +// 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 (*)(ClimateCall &, const Ts &...); + using ApplyFn = void (*)(ClimateCall &, const std::remove_cvref_t &...); ControlAction(Climate *climate, ApplyFn apply) : climate_(climate), apply_(apply) {} void play(const Ts &...x) override { diff --git a/tests/components/climate/common.yaml b/tests/components/climate/common.yaml index 2d35438afd..c28fde8eeb 100644 --- a/tests/components/climate/common.yaml +++ b/tests/components/climate/common.yaml @@ -85,3 +85,18 @@ button: - climate.control: id: climate_test_thermostat mode: "OFF" + +# Exercise climate.control inside a trigger with non-empty Ts (number on_value +# passes float). +number: + - platform: template + id: climate_target_temp_number + optimistic: true + min_value: 16 + max_value: 28 + step: 0.5 + on_value: + then: + - climate.control: + id: climate_test_thermostat + target_temperature_high: !lambda "return x;"