mirror of
https://github.com/esphome/esphome.git
synced 2026-05-27 11:56:11 +08:00
Merge remote-tracking branch 'upstream/fix-fan-action-trigger-args' into integration
This commit is contained in:
@@ -358,21 +358,29 @@ async def fan_turn_on_to_code(config, action_id, template_arg, args):
|
|||||||
(CONF_DIRECTION, "set_direction", FanDirection),
|
(CONF_DIRECTION, "set_direction", FanDirection),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Normalize trigger args to `const std::remove_cvref_t<T> &` so the
|
||||||
|
# apply lambda and any inner field lambdas (generated below via
|
||||||
|
# `process_lambda`) share one parameter spelling that's well-formed for
|
||||||
|
# any T (value, ref, or const-ref). Matches TurnOnAction::ApplyFn.
|
||||||
|
normalized_args = [
|
||||||
|
(cg.RawExpression(f"const std::remove_cvref_t<{cg.safe_exp(t)}> &"), n)
|
||||||
|
for t, n in args
|
||||||
|
]
|
||||||
|
|
||||||
fwd_args = ", ".join(name for _, name in args)
|
fwd_args = ", ".join(name for _, name in args)
|
||||||
body_lines: list[str] = []
|
body_lines: list[str] = []
|
||||||
for conf_key, setter, type_ in FIELDS:
|
for conf_key, setter, type_ in FIELDS:
|
||||||
if (value := config.get(conf_key)) is None:
|
if (value := config.get(conf_key)) is None:
|
||||||
continue
|
continue
|
||||||
if isinstance(value, Lambda):
|
if isinstance(value, Lambda):
|
||||||
inner = await cg.process_lambda(value, args, return_type=type_)
|
inner = await cg.process_lambda(value, normalized_args, return_type=type_)
|
||||||
body_lines.append(f"call.{setter}(({inner})({fwd_args}));")
|
body_lines.append(f"call.{setter}(({inner})({fwd_args}));")
|
||||||
else:
|
else:
|
||||||
body_lines.append(f"call.{setter}({cg.safe_exp(value)});")
|
body_lines.append(f"call.{setter}({cg.safe_exp(value)});")
|
||||||
|
|
||||||
# Match TurnOnAction::ApplyFn signature: const Ts &... for trigger args.
|
|
||||||
apply_args = [
|
apply_args = [
|
||||||
(FanCall.operator("ref"), "call"),
|
(FanCall.operator("ref"), "call"),
|
||||||
*((t.operator("const").operator("ref"), n) for t, n in args),
|
*normalized_args,
|
||||||
]
|
]
|
||||||
apply_lambda = LambdaExpression(
|
apply_lambda = LambdaExpression(
|
||||||
["\n".join(body_lines)],
|
["\n".join(body_lines)],
|
||||||
|
|||||||
@@ -12,9 +12,16 @@ namespace fan {
|
|||||||
// 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. `speed: !lambda "return x;"`) keep working.
|
// (e.g. `speed: !lambda "return x;"`) keep working.
|
||||||
|
//
|
||||||
|
// Trigger args are normalized to `const std::remove_cvref_t<Ts> &...` so
|
||||||
|
// the codegen can emit a matching parameter list for both the apply lambda
|
||||||
|
// and any inner field lambdas without producing invalid C++ source text
|
||||||
|
// (e.g. `const T & &` if Ts already carries a reference, or `const const
|
||||||
|
// T &` if Ts already carries a const). This keeps trigger args no-copy
|
||||||
|
// regardless of whether the trigger supplies `T`, `T &`, or `const T &`.
|
||||||
template<typename... Ts> class TurnOnAction : public Action<Ts...> {
|
template<typename... Ts> class TurnOnAction : public Action<Ts...> {
|
||||||
public:
|
public:
|
||||||
using ApplyFn = void (*)(FanCall &, const Ts &...);
|
using ApplyFn = void (*)(FanCall &, const std::remove_cvref_t<Ts> &...);
|
||||||
TurnOnAction(Fan *state, ApplyFn apply) : state_(state), apply_(apply) {}
|
TurnOnAction(Fan *state, ApplyFn apply) : state_(state), apply_(apply) {}
|
||||||
|
|
||||||
void play(const Ts &...x) override {
|
void play(const Ts &...x) override {
|
||||||
|
|||||||
@@ -9,6 +9,14 @@ fan:
|
|||||||
has_oscillating: true
|
has_oscillating: true
|
||||||
has_direction: true
|
has_direction: true
|
||||||
speed_count: 3
|
speed_count: 3
|
||||||
|
# Exercise fan.turn_on inside a trigger whose Ts pack is non-empty
|
||||||
|
# (StringRef from on_preset_set) so the apply-lambda + inner-lambda
|
||||||
|
# codegen runs through the cvref-normalized path.
|
||||||
|
on_preset_set:
|
||||||
|
then:
|
||||||
|
- fan.turn_on:
|
||||||
|
id: test_fan
|
||||||
|
speed: !lambda "return x.empty() ? 1 : 3;"
|
||||||
|
|
||||||
# Test lambdas using get_preset_mode() which returns StringRef
|
# Test lambdas using get_preset_mode() which returns StringRef
|
||||||
# These examples match the migration guide in the PR description
|
# These examples match the migration guide in the PR description
|
||||||
@@ -88,3 +96,21 @@ button:
|
|||||||
- fan.turn_on:
|
- fan.turn_on:
|
||||||
id: test_fan
|
id: test_fan
|
||||||
speed: !lambda 'return 1;'
|
speed: !lambda 'return 1;'
|
||||||
|
|
||||||
|
# Exercise fan.turn_on inside triggers with non-empty Ts:
|
||||||
|
# - number.on_value: Ts = float (Python value type; previously raised
|
||||||
|
# AttributeError on .operator("const"))
|
||||||
|
# - fan.on_preset_set: Ts = StringRef (already a value-type wrapper around
|
||||||
|
# a const char * + size; tests the cvref-normalized inner-lambda path)
|
||||||
|
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