mirror of
https://github.com/esphome/esphome.git
synced 2025-11-20 08:46:01 +00:00
Add script.wait action (#778)
Fixes https://github.com/esphome/feature-requests/issues/416, fixes https://github.com/esphome/issues/issues/572
This commit is contained in:
@@ -49,5 +49,47 @@ template<typename... Ts> class IsRunningCondition : public Condition<Ts...> {
|
||||
Script *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class ScriptWaitAction : public Action<Ts...>, public Component {
|
||||
public:
|
||||
ScriptWaitAction(Script *script) : script_(script) {}
|
||||
|
||||
void play(Ts... x) { /* ignore - see play_complex */
|
||||
}
|
||||
|
||||
void play_complex(Ts... x) override {
|
||||
// Check if we can continue immediately.
|
||||
if (!this->script_->is_running()) {
|
||||
this->triggered_ = false;
|
||||
this->play_next(x...);
|
||||
return;
|
||||
}
|
||||
this->var_ = std::make_tuple(x...);
|
||||
this->triggered_ = true;
|
||||
this->loop();
|
||||
}
|
||||
|
||||
void stop() override { this->triggered_ = false; }
|
||||
|
||||
void loop() override {
|
||||
if (!this->triggered_)
|
||||
return;
|
||||
|
||||
if (this->script_->is_running())
|
||||
return;
|
||||
|
||||
this->triggered_ = false;
|
||||
this->play_next_tuple(this->var_);
|
||||
}
|
||||
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
|
||||
bool is_running() override { return this->triggered_ || this->is_running_next(); }
|
||||
|
||||
protected:
|
||||
Script *script_;
|
||||
bool triggered_{false};
|
||||
std::tuple<Ts...> var_{};
|
||||
};
|
||||
|
||||
} // namespace script
|
||||
} // namespace esphome
|
||||
|
||||
Reference in New Issue
Block a user