mirror of
https://github.com/esphome/esphome.git
synced 2026-09-02 11:06:04 +00:00
[light] Fix LightControlAction 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:
@@ -36,9 +36,12 @@ template<bool HasTransitionLength, typename... Ts> class ToggleAction : public A
|
||||
// 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. `brightness: !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 LightControlAction : public Action<Ts...> {
|
||||
public:
|
||||
using ApplyFn = void (*)(LightState *, LightCall &, const Ts &...);
|
||||
using ApplyFn = void (*)(LightState *, LightCall &, Ts...);
|
||||
LightControlAction(LightState *parent, ApplyFn apply) : parent_(parent), apply_(apply) {}
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
|
||||
@@ -230,11 +230,12 @@ async def light_control_to_code(config, action_id, template_arg, args):
|
||||
f"call.set_effect(static_cast<uint32_t>({_resolve_effect_index(config)}));"
|
||||
)
|
||||
|
||||
# Match LightControlAction::ApplyFn signature: const Ts &... for trigger args.
|
||||
# Match LightControlAction::ApplyFn signature: trigger args forwarded
|
||||
# by-value as Ts...
|
||||
apply_args = [
|
||||
(LightState.operator("ptr"), "parent"),
|
||||
(LightCall.operator("ref"), "call"),
|
||||
*((t.operator("const").operator("ref"), n) for t, n in args),
|
||||
*args,
|
||||
]
|
||||
apply_lambda = LambdaExpression(
|
||||
["\n".join(body_lines)],
|
||||
|
||||
@@ -127,6 +127,21 @@ esphome:
|
||||
blue: 0%
|
||||
transition_length: 1s
|
||||
|
||||
# Exercise light actions inside a trigger with non-empty Ts (number on_value
|
||||
# passes float).
|
||||
number:
|
||||
- platform: template
|
||||
id: test_number_brightness
|
||||
optimistic: true
|
||||
min_value: 0
|
||||
max_value: 100
|
||||
step: 1
|
||||
on_value:
|
||||
then:
|
||||
- light.turn_on:
|
||||
id: test_monochromatic_light
|
||||
brightness: !lambda "return x / 100.0;"
|
||||
|
||||
light:
|
||||
- platform: binary
|
||||
id: test_binary_light
|
||||
|
||||
Reference in New Issue
Block a user