From 3773c385c702ba64d6a9836822334db553b6c016 Mon Sep 17 00:00:00 2001 From: Fabian Date: Tue, 7 Mar 2023 05:16:42 +0100 Subject: [PATCH] Ensure component is ready before update. (#4523) Co-authored-by: Your Name --- esphome/core/base_automation.h | 2 +- esphome/core/component.cpp | 4 ++++ esphome/core/component.h | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/esphome/core/base_automation.h b/esphome/core/base_automation.h index 5a4fb65198..daa09b912e 100644 --- a/esphome/core/base_automation.h +++ b/esphome/core/base_automation.h @@ -317,7 +317,7 @@ template class UpdateComponentAction : public Action { UpdateComponentAction(PollingComponent *component) : component_(component) {} void play(Ts... x) override { - if (this->component_->is_failed()) + if (!this->component_->is_ready()) return; this->component_->update(); } diff --git a/esphome/core/component.cpp b/esphome/core/component.cpp index b1ace8b2d1..49ef8ecde7 100644 --- a/esphome/core/component.cpp +++ b/esphome/core/component.cpp @@ -135,6 +135,10 @@ void Component::set_retry(uint32_t initial_wait_time, uint8_t max_attempts, std: App.scheduler.set_retry(this, "", initial_wait_time, max_attempts, std::move(f), backoff_increase_factor); } bool Component::is_failed() { return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_FAILED; } +bool Component::is_ready() { + return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_LOOP || + (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_SETUP; +} bool Component::can_proceed() { return true; } bool Component::status_has_warning() { return this->component_state_ & STATUS_LED_WARNING; } bool Component::status_has_error() { return this->component_state_ & STATUS_LED_ERROR; } diff --git a/esphome/core/component.h b/esphome/core/component.h index 769e74e645..7382f1c617 100644 --- a/esphome/core/component.h +++ b/esphome/core/component.h @@ -119,6 +119,8 @@ class Component { bool is_failed(); + bool is_ready(); + virtual bool can_proceed(); bool status_has_warning();