mirror of
https://github.com/esphome/esphome.git
synced 2026-05-28 13:37:24 +08:00
[valve] 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:
@@ -251,10 +251,11 @@ async def valve_control_to_code(config, action_id, template_arg, args):
|
|||||||
else:
|
else:
|
||||||
body_lines.append(f"call.{setter}({cg.safe_exp(value)});")
|
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 = [
|
apply_args = [
|
||||||
(ValveCall.operator("ref"), "call"),
|
(ValveCall.operator("ref"), "call"),
|
||||||
*((t.operator("const").operator("ref"), n) for t, n in args),
|
*args,
|
||||||
]
|
]
|
||||||
apply_lambda = LambdaExpression(
|
apply_lambda = LambdaExpression(
|
||||||
["\n".join(body_lines)],
|
["\n".join(body_lines)],
|
||||||
|
|||||||
@@ -52,9 +52,12 @@ template<typename... Ts> class ToggleAction : public Action<Ts...> {
|
|||||||
// plus one parent pointer, regardless of how many fields the user set.
|
// plus one parent pointer, regardless of how many fields the user set.
|
||||||
// Trigger args are forwarded to the apply function so user lambdas
|
// Trigger args are forwarded to the apply function so user lambdas
|
||||||
// (e.g. `position: !lambda "return x;"`) keep working.
|
// (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<typename... Ts> class ControlAction : public Action<Ts...> {
|
template<typename... Ts> class ControlAction : public Action<Ts...> {
|
||||||
public:
|
public:
|
||||||
using ApplyFn = void (*)(ValveCall &, const Ts &...);
|
using ApplyFn = void (*)(ValveCall &, Ts...);
|
||||||
ControlAction(Valve *valve, ApplyFn apply) : valve_(valve), apply_(apply) {}
|
ControlAction(Valve *valve, ApplyFn apply) : valve_(valve), apply_(apply) {}
|
||||||
|
|
||||||
void play(const Ts &...x) override {
|
void play(const Ts &...x) override {
|
||||||
|
|||||||
@@ -356,6 +356,19 @@ number:
|
|||||||
min_value: 0
|
min_value: 0
|
||||||
max_value: 100
|
max_value: 100
|
||||||
step: 1
|
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:
|
select:
|
||||||
- platform: template
|
- platform: template
|
||||||
|
|||||||
Reference in New Issue
Block a user