diff --git a/esphome/components/ota/automation.h b/esphome/components/ota/automation.h index ded8378ff24..92c0050ba01 100644 --- a/esphome/components/ota/automation.h +++ b/esphome/components/ota/automation.h @@ -9,20 +9,30 @@ namespace ota { class OTAStateChangeTrigger final : public Trigger, public OTAStateListener { public: - explicit OTAStateChangeTrigger(OTAComponent *parent) { parent->add_state_listener(this); } + explicit OTAStateChangeTrigger(OTAComponent *parent) : parent_(parent) { parent->add_state_listener(this); } - void on_ota_state(OTAState state, float progress, uint8_t error) override { this->trigger(state); } + void on_ota_state(OTAState state, float progress, uint8_t error) override { + if (!this->parent_->is_failed()) { + this->trigger(state); + } + } + + protected: + OTAComponent *parent_; }; template class OTAStateTrigger final : public Trigger<>, public OTAStateListener { public: - explicit OTAStateTrigger(OTAComponent *parent) { parent->add_state_listener(this); } + explicit OTAStateTrigger(OTAComponent *parent) : parent_(parent) { parent->add_state_listener(this); } void on_ota_state(OTAState state, float progress, uint8_t error) override { - if (state == State) { + if (state == State && !this->parent_->is_failed()) { this->trigger(); } } + + protected: + OTAComponent *parent_; }; using OTAStartTrigger = OTAStateTrigger; @@ -31,24 +41,30 @@ using OTAAbortTrigger = OTAStateTrigger; class OTAProgressTrigger final : public Trigger, public OTAStateListener { public: - explicit OTAProgressTrigger(OTAComponent *parent) { parent->add_state_listener(this); } + explicit OTAProgressTrigger(OTAComponent *parent) : parent_(parent) { parent->add_state_listener(this); } void on_ota_state(OTAState state, float progress, uint8_t error) override { - if (state == OTA_IN_PROGRESS) { + if (state == OTA_IN_PROGRESS && !this->parent_->is_failed()) { this->trigger(progress); } } + + protected: + OTAComponent *parent_; }; class OTAErrorTrigger final : public Trigger, public OTAStateListener { public: - explicit OTAErrorTrigger(OTAComponent *parent) { parent->add_state_listener(this); } + explicit OTAErrorTrigger(OTAComponent *parent) : parent_(parent) { parent->add_state_listener(this); } void on_ota_state(OTAState state, float progress, uint8_t error) override { - if (state == OTA_ERROR) { + if (state == OTA_ERROR && !this->parent_->is_failed()) { this->trigger(error); } } + + protected: + OTAComponent *parent_; }; } // namespace ota