Publish status in loop()

This commit is contained in:
2025-10-14 02:58:46 -06:00
parent a8ddd42e95
commit 5de4c1db51
2 changed files with 34 additions and 12 deletions

View File

@@ -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_() {
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

View File

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