diff --git a/esphome/components/valve/__init__.py b/esphome/components/valve/__init__.py index 7377aea1ed2..bc7e2ba48d9 100644 --- a/esphome/components/valve/__init__.py +++ b/esphome/components/valve/__init__.py @@ -251,10 +251,11 @@ async def valve_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 = [ (ValveCall.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/valve/automation.h b/esphome/components/valve/automation.h index ae9ac0db762..81fcd122003 100644 --- a/esphome/components/valve/automation.h +++ b/esphome/components/valve/automation.h @@ -52,9 +52,12 @@ template class ToggleAction : public Action { // 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. `position: !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 (*)(ValveCall &, const Ts &...); + using ApplyFn = void (*)(ValveCall &, Ts...); ControlAction(Valve *valve, ApplyFn apply) : valve_(valve), apply_(apply) {} void play(const Ts &...x) override { diff --git a/tests/components/template/common-base.yaml b/tests/components/template/common-base.yaml index 819eaa8bbfe..984ef129ad4 100644 --- a/tests/components/template/common-base.yaml +++ b/tests/components/template/common-base.yaml @@ -356,6 +356,19 @@ number: min_value: 0 max_value: 100 step: 1 + # Exercise valve.control inside a trigger with non-empty Ts (number on_value + # passes float). + - platform: template + id: template_valve_position_number + optimistic: true + min_value: 0 + max_value: 100 + step: 1 + on_value: + then: + - valve.control: + id: template_valve + position: !lambda "return x / 100.0f;" select: - platform: template