Otto Winter 2019-10-20 16:15:30 +02:00
parent b975caef1e
commit 268387f829
No known key found for this signature in database
GPG key ID: DB66C0BE6013F97E
3 changed files with 53 additions and 0 deletions

View file

@ -8,6 +8,7 @@ script_ns = cg.esphome_ns.namespace('script')
Script = script_ns.class_('Script', automation.Trigger.template())
ScriptExecuteAction = script_ns.class_('ScriptExecuteAction', automation.Action)
ScriptStopAction = script_ns.class_('ScriptStopAction', automation.Action)
ScriptWaitAction = script_ns.class_('ScriptWaitAction', automation.Action)
IsRunningCondition = script_ns.class_('IsRunningCondition', automation.Condition)
CONFIG_SCHEMA = automation.validate_automation({
@ -42,6 +43,14 @@ def script_stop_action_to_code(config, action_id, template_arg, args):
yield cg.new_Pvariable(action_id, template_arg, paren)
@automation.register_action('script.wait', ScriptWaitAction, maybe_simple_id({
cv.Required(CONF_ID): cv.use_id(Script)
}))
def script_wait_action_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(action_id, template_arg, paren)
@automation.register_condition('script.is_running', IsRunningCondition, automation.maybe_simple_id({
cv.Required(CONF_ID): cv.use_id(Script)
}))

View file

@ -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

View file

@ -383,6 +383,8 @@ text_sensor:
- lambda: !lambda |-
ESP_LOGD("main", "The state is %s=%s", x.c_str(), id(version_sensor).state.c_str());
- script.execute: my_script
- script.wait: my_script
- script.stop: my_script
- homeassistant.service:
service: notify.html5
data: