mirror of
https://github.com/esphome/esphome.git
synced 2026-05-26 03:07:04 +08:00
[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:
@@ -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)],
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;"
|
||||
|
||||
Reference in New Issue
Block a user