From 4a347cf7ae69afedc18b4d5ca57e0592c37a70f9 Mon Sep 17 00:00:00 2001 From: Darsey Litzenberger Date: Tue, 14 Oct 2025 02:58:46 -0600 Subject: [PATCH] Publish status in loop() --- components/app_status/app_status.cpp | 37 +++++++++++++++++++------- components/app_status/app_status.h | 9 ++++--- components/app_status/binary_sensor.py | 2 +- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/components/app_status/app_status.cpp b/components/app_status/app_status.cpp index eb2222c..10e441a 100644 --- a/components/app_status/app_status.cpp +++ b/components/app_status/app_status.cpp @@ -11,17 +11,21 @@ void AppStatus::setup() { } +void AppStatus::loop() { + auto status = App.get_app_state() & STATUS_LED_MASK; + if (status == this->last_status_) { + return; + } + + this->publish_status_(status); + this->last_status_ = status; +} + void AppStatus::update() { ESP_LOGV(TAG, "update() called", TRUEFALSE(this->inverted_)); - bool result = false; - auto app_state = App.get_app_state() & STATUS_LED_MASK; - if (this->include_errors_ && (app_state & STATUS_LED_ERROR)) { - result = true; - } - if (this->include_warnings_ && (app_state & STATUS_LED_WARNING)) { - result = true; - } - this->publish_state(result); + auto status = this->get_status_(); + this->publish_status_(status); + this->last_status_ = status; } void AppStatus::dump_config() { @@ -30,6 +34,21 @@ void AppStatus::dump_config() { ESP_LOGCONFIG(TAG, " include errors = %s", TRUEFALSE(this->include_errors_)); } +uint8_t AppStatus::get_status_() const { + return App.get_app_state() & STATUS_LED_MASK; +} + +void AppStatus::publish_status_(uint8_t status) { + bool result = false; + if (this->include_errors_ && (status & STATUS_LED_ERROR)) { + result = true; + } + if (this->include_warnings_ && (status & STATUS_LED_WARNING)) { + result = true; + } + this->publish_state(result); +} + } // namespace app_status } // namespace esphome diff --git a/components/app_status/app_status.h b/components/app_status/app_status.h index 86f0c98..fd3de4b 100644 --- a/components/app_status/app_status.h +++ b/components/app_status/app_status.h @@ -8,20 +8,23 @@ namespace app_status { class AppStatus : public binary_sensor::BinarySensor, public PollingComponent { public: - //void loop() override; void setup() override; + void loop() override; void update() override; void dump_config() override; - //float get_loop_priority() const override { return 50.0f; } + float get_loop_priority() const override { return 50.0f; } void set_include_errors(bool allow) { this->include_errors_ = allow; } void set_include_warnings(bool allow) { this->include_warnings_ = allow; } protected: - //uint8_t last_app_state_(0xFF); + uint8_t last_status_{0xFF}; bool include_warnings_{true}; bool include_errors_{true}; + + uint8_t get_status_() const; + void publish_status_(uint8_t status); }; } // namespace app_status diff --git a/components/app_status/binary_sensor.py b/components/app_status/binary_sensor.py index 44f4fc0..169f2eb 100644 --- a/components/app_status/binary_sensor.py +++ b/components/app_status/binary_sensor.py @@ -21,7 +21,7 @@ AppStatus = app_status_ns.class_("AppStatus", binary_sensor.BinarySensor, cg.Pol CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(AppStatus).extend({ cv.GenerateID(): cv.declare_id(AppStatus), cv.Optional(CONF_DETECT_STATUS, default=[CONF_WARNING, CONF_ERROR]): cv.ensure_list(cv.enum(DETECT_STATUS_ENUM)), -}).extend(cv.polling_component_schema(default_update_interval="1s")) +}).extend(cv.polling_component_schema(default_update_interval="60s")) async def to_code(config): var = await binary_sensor.new_binary_sensor(config)