Publish status in loop()

This commit is contained in:
2025-10-14 02:58:46 -06:00
parent a8ddd42e95
commit 4a347cf7ae
3 changed files with 35 additions and 13 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_() 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

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

View File

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