mirror of
https://github.com/esphome/esphome.git
synced 2026-05-27 11:56:11 +08:00
Merge remote-tracking branch 'upstream/fix-valve-action-trigger-args' into integration
This commit is contained in:
@@ -240,21 +240,29 @@ async def valve_control_to_code(config, action_id, template_arg, args):
|
|||||||
(CONF_POSITION, "set_position", cg.float_),
|
(CONF_POSITION, "set_position", cg.float_),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 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 ControlAction::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 ControlAction::ApplyFn signature: const Ts &... for trigger args.
|
|
||||||
apply_args = [
|
apply_args = [
|
||||||
(ValveCall.operator("ref"), "call"),
|
(ValveCall.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)],
|
||||||
|
|||||||
@@ -52,9 +52,16 @@ 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.
|
||||||
|
//
|
||||||
|
// 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 ControlAction : public Action<Ts...> {
|
template<typename... Ts> class ControlAction : public Action<Ts...> {
|
||||||
public:
|
public:
|
||||||
using ApplyFn = void (*)(ValveCall &, const Ts &...);
|
using ApplyFn = void (*)(ValveCall &, const std::remove_cvref_t<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