diff --git a/esphome/components/debug/debug_component.cpp b/esphome/components/debug/debug_component.cpp index 97d5aeb8a8..a594dc7a50 100644 --- a/esphome/components/debug/debug_component.cpp +++ b/esphome/components/debug/debug_component.cpp @@ -38,6 +38,7 @@ static uint32_t get_free_heap() { void DebugComponent::dump_config() { std::string device_info; + std::string reset_reason; device_info.reserve(256); #ifndef ESPHOME_LOG_HAS_DEBUG @@ -146,7 +147,6 @@ void DebugComponent::dump_config() { device_info += "|EFuse MAC: "; device_info += mac; - const char *reset_reason; switch (rtc_get_reset_reason(0)) { case POWERON_RESET: reset_reason = "Power On Reset"; @@ -196,7 +196,7 @@ void DebugComponent::dump_config() { default: reset_reason = "Unknown Reset Reason"; } - ESP_LOGD(TAG, "Reset Reason: %s", reset_reason); + ESP_LOGD(TAG, "Reset Reason: %s", reset_reason.c_str()); device_info += "|Reset: "; device_info += reset_reason; @@ -270,6 +270,8 @@ void DebugComponent::dump_config() { device_info += ESP.getResetReason().c_str(); device_info += "|"; device_info += ESP.getResetInfo().c_str(); + + reset_reason = ESP.getResetReason().c_str(); #endif #ifdef USE_TEXT_SENSOR @@ -278,6 +280,9 @@ void DebugComponent::dump_config() { device_info.resize(255); this->device_info_->publish_state(device_info); } + if (this->reset_reason_ != nullptr) { + this->reset_reason_->publish_state(reset_reason); + } #endif // USE_TEXT_SENSOR } diff --git a/esphome/components/debug/debug_component.h b/esphome/components/debug/debug_component.h index 4dc1659616..b80fda55eb 100644 --- a/esphome/components/debug/debug_component.h +++ b/esphome/components/debug/debug_component.h @@ -24,6 +24,7 @@ class DebugComponent : public PollingComponent { #ifdef USE_TEXT_SENSOR void set_device_info_sensor(text_sensor::TextSensor *device_info) { device_info_ = device_info; } + void set_reset_reason_sensor(text_sensor::TextSensor *reset_reason) { reset_reason_ = reset_reason; } #endif // USE_TEXT_SENSOR #ifdef USE_SENSOR void set_free_sensor(sensor::Sensor *free_sensor) { free_sensor_ = free_sensor; } @@ -50,6 +51,7 @@ class DebugComponent : public PollingComponent { #ifdef USE_TEXT_SENSOR text_sensor::TextSensor *device_info_{nullptr}; + text_sensor::TextSensor *reset_reason_{nullptr}; #endif // USE_TEXT_SENSOR }; diff --git a/esphome/components/debug/text_sensor.py b/esphome/components/debug/text_sensor.py index 11e6354f57..24f938a0e2 100644 --- a/esphome/components/debug/text_sensor.py +++ b/esphome/components/debug/text_sensor.py @@ -1,18 +1,29 @@ from esphome.components import text_sensor import esphome.config_validation as cv import esphome.codegen as cg -from esphome.const import CONF_DEVICE, ENTITY_CATEGORY_DIAGNOSTIC +from esphome.const import ( + CONF_DEVICE, + ENTITY_CATEGORY_DIAGNOSTIC, + ICON_CHIP, + ICON_RESTART, +) from . import CONF_DEBUG_ID, DebugComponent DEPENDENCIES = ["debug"] +CONF_RESET_REASON = "reset_reason" CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(CONF_DEBUG_ID): cv.use_id(DebugComponent), cv.Optional(CONF_DEVICE): text_sensor.text_sensor_schema( - entity_category=ENTITY_CATEGORY_DIAGNOSTIC + icon=ICON_CHIP, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + ), + cv.Optional(CONF_RESET_REASON): text_sensor.text_sensor_schema( + icon=ICON_RESTART, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, ), } ) @@ -24,3 +35,6 @@ async def to_code(config): if CONF_DEVICE in config: sens = await text_sensor.new_text_sensor(config[CONF_DEVICE]) cg.add(debug_component.set_device_info_sensor(sens)) + if CONF_RESET_REASON in config: + sens = await text_sensor.new_text_sensor(config[CONF_RESET_REASON]) + cg.add(debug_component.set_reset_reason_sensor(sens))