mirror of
https://github.com/esphome/esphome.git
synced 2026-07-10 17:05:36 +00:00
[fan] Fix TurnOnAction 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:
@@ -369,10 +369,11 @@ async def fan_turn_on_to_code(config, action_id, template_arg, args):
|
||||
else:
|
||||
body_lines.append(f"call.{setter}({cg.safe_exp(value)});")
|
||||
|
||||
# Match TurnOnAction::ApplyFn signature: const Ts &... for trigger args.
|
||||
# Match TurnOnAction::ApplyFn signature: trigger args forwarded
|
||||
# by-value as Ts...
|
||||
apply_args = [
|
||||
(FanCall.operator("ref"), "call"),
|
||||
*((t.operator("const").operator("ref"), n) for t, n in args),
|
||||
*args,
|
||||
]
|
||||
apply_lambda = LambdaExpression(
|
||||
["\n".join(body_lines)],
|
||||
|
||||
@@ -12,9 +12,12 @@ namespace fan {
|
||||
// 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. `speed: !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 TurnOnAction : public Action<Ts...> {
|
||||
public:
|
||||
using ApplyFn = void (*)(FanCall &, const Ts &...);
|
||||
using ApplyFn = void (*)(FanCall &, Ts...);
|
||||
TurnOnAction(Fan *state, ApplyFn apply) : state_(state), apply_(apply) {}
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
|
||||
@@ -88,3 +88,18 @@ button:
|
||||
- fan.turn_on:
|
||||
id: test_fan
|
||||
speed: !lambda 'return 1;'
|
||||
|
||||
# Exercise fan.turn_on inside a trigger with non-empty Ts (number on_value
|
||||
# passes float).
|
||||
number:
|
||||
- platform: template
|
||||
id: fan_speed_number
|
||||
optimistic: true
|
||||
min_value: 1
|
||||
max_value: 3
|
||||
step: 1
|
||||
on_value:
|
||||
then:
|
||||
- fan.turn_on:
|
||||
id: test_fan
|
||||
speed: !lambda "return (int) x;"
|
||||
|
||||
Reference in New Issue
Block a user