[climate] Fix ControlAction 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.
This commit is contained in:
J. Nick Koston
2026-05-03 16:08:39 -05:00
parent 8046ff7e1e
commit 31239ac950
3 changed files with 22 additions and 3 deletions
+3 -2
View File
@@ -526,10 +526,11 @@ 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.
# Match ControlAction::ApplyFn signature: trigger args forwarded
# by-value as Ts...
apply_args = [
(ClimateCall.operator("ref"), "call"),
*((t.operator("const").operator("ref"), n) for t, n in args),
*args,
]
apply_lambda = LambdaExpression(
["\n".join(body_lines)],
+4 -1
View File
@@ -10,9 +10,12 @@ 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.
//
// Ts... must be forwarded by-value: `const T &` is ill-formed when T is
// already a reference type (some triggers pass `std::string &`).
template<typename... Ts> class ControlAction : public Action<Ts...> {
public:
using ApplyFn = void (*)(ClimateCall &, const Ts &...);
using ApplyFn = void (*)(ClimateCall &, Ts...);
ControlAction(Climate *climate, ApplyFn apply) : climate_(climate), apply_(apply) {}
void play(const Ts &...x) override {
+15
View File
@@ -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;"