[grove_tb6612fng] Move direction logic from Python to C++ to fix lambda crash (#15513)

This commit is contained in:
Jonathan Swoboda
2026-04-07 16:56:25 -04:00
committed by GitHub
parent ac14b9e558
commit 9d396cea5a
3 changed files with 15 additions and 4 deletions
@@ -80,11 +80,9 @@ async def grove_tb6612fng_run_to_code(config, action_id, template_arg, args):
template_channel = await cg.templatable(config[CONF_CHANNEL], args, int)
template_speed = await cg.templatable(config[CONF_SPEED], args, cg.uint16)
template_speed = (
template_speed if config[CONF_DIRECTION] == "FORWARD" else -template_speed
)
cg.add(var.set_channel(template_channel))
cg.add(var.set_speed(template_speed))
cg.add(var.set_direction(config[CONF_DIRECTION] == "FORWARD"))
return var
@@ -168,11 +168,19 @@ class GROVETB6612FNGMotorRunAction : public Action<Ts...>, public Parented<Grove
TEMPLATABLE_VALUE(uint8_t, channel)
TEMPLATABLE_VALUE(uint16_t, speed)
void set_direction(bool forward) { this->forward_ = forward; }
void play(const Ts &...x) override {
auto channel = this->channel_.value(x...);
auto speed = this->speed_.value(x...);
int16_t speed = this->speed_.value(x...);
if (!this->forward_) {
speed = -speed;
}
this->parent_->dc_motor_run(channel, speed);
}
protected:
bool forward_{true};
};
template<typename... Ts>
@@ -6,6 +6,11 @@ esphome:
speed: 255
direction: BACKWARD
id: test_motor
- grove_tb6612fng.run:
channel: 0
speed: !lambda "return 200;"
direction: BACKWARD
id: test_motor
- grove_tb6612fng.stop:
channel: 1
id: test_motor