diff --git a/esphome/components/climate/__init__.py b/esphome/components/climate/__init__.py index 7c9002d6dc3..a55c5b0f5a5 100644 --- a/esphome/components/climate/__init__.py +++ b/esphome/components/climate/__init__.py @@ -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)], diff --git a/esphome/components/climate/automation.h b/esphome/components/climate/automation.h index 71d23fd6b6d..c5562069269 100644 --- a/esphome/components/climate/automation.h +++ b/esphome/components/climate/automation.h @@ -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 class ControlAction : public Action { 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 { diff --git a/tests/components/climate/common.yaml b/tests/components/climate/common.yaml index 2d35438afd2..c28fde8eeb4 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;"