Add reset_reason text sensor to debug component (#3814)

This commit is contained in:
Kuba Szczodrzyński 2022-11-15 20:14:20 +01:00 committed by GitHub
parent 2f62426f09
commit 392dc8b0db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 4 deletions

View File

@ -38,6 +38,7 @@ static uint32_t get_free_heap() {
void DebugComponent::dump_config() { void DebugComponent::dump_config() {
std::string device_info; std::string device_info;
std::string reset_reason;
device_info.reserve(256); device_info.reserve(256);
#ifndef ESPHOME_LOG_HAS_DEBUG #ifndef ESPHOME_LOG_HAS_DEBUG
@ -146,7 +147,6 @@ void DebugComponent::dump_config() {
device_info += "|EFuse MAC: "; device_info += "|EFuse MAC: ";
device_info += mac; device_info += mac;
const char *reset_reason;
switch (rtc_get_reset_reason(0)) { switch (rtc_get_reset_reason(0)) {
case POWERON_RESET: case POWERON_RESET:
reset_reason = "Power On Reset"; reset_reason = "Power On Reset";
@ -196,7 +196,7 @@ void DebugComponent::dump_config() {
default: default:
reset_reason = "Unknown Reset Reason"; 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: ";
device_info += reset_reason; device_info += reset_reason;
@ -270,6 +270,8 @@ void DebugComponent::dump_config() {
device_info += ESP.getResetReason().c_str(); device_info += ESP.getResetReason().c_str();
device_info += "|"; device_info += "|";
device_info += ESP.getResetInfo().c_str(); device_info += ESP.getResetInfo().c_str();
reset_reason = ESP.getResetReason().c_str();
#endif #endif
#ifdef USE_TEXT_SENSOR #ifdef USE_TEXT_SENSOR
@ -278,6 +280,9 @@ void DebugComponent::dump_config() {
device_info.resize(255); device_info.resize(255);
this->device_info_->publish_state(device_info); this->device_info_->publish_state(device_info);
} }
if (this->reset_reason_ != nullptr) {
this->reset_reason_->publish_state(reset_reason);
}
#endif // USE_TEXT_SENSOR #endif // USE_TEXT_SENSOR
} }

View File

@ -24,6 +24,7 @@ class DebugComponent : public PollingComponent {
#ifdef USE_TEXT_SENSOR #ifdef USE_TEXT_SENSOR
void set_device_info_sensor(text_sensor::TextSensor *device_info) { device_info_ = device_info; } 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 #endif // USE_TEXT_SENSOR
#ifdef USE_SENSOR #ifdef USE_SENSOR
void set_free_sensor(sensor::Sensor *free_sensor) { free_sensor_ = free_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 #ifdef USE_TEXT_SENSOR
text_sensor::TextSensor *device_info_{nullptr}; text_sensor::TextSensor *device_info_{nullptr};
text_sensor::TextSensor *reset_reason_{nullptr};
#endif // USE_TEXT_SENSOR #endif // USE_TEXT_SENSOR
}; };

View File

@ -1,18 +1,29 @@
from esphome.components import text_sensor from esphome.components import text_sensor
import esphome.config_validation as cv import esphome.config_validation as cv
import esphome.codegen as cg 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 from . import CONF_DEBUG_ID, DebugComponent
DEPENDENCIES = ["debug"] DEPENDENCIES = ["debug"]
CONF_RESET_REASON = "reset_reason"
CONFIG_SCHEMA = cv.Schema( CONFIG_SCHEMA = cv.Schema(
{ {
cv.GenerateID(CONF_DEBUG_ID): cv.use_id(DebugComponent), cv.GenerateID(CONF_DEBUG_ID): cv.use_id(DebugComponent),
cv.Optional(CONF_DEVICE): text_sensor.text_sensor_schema( 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: if CONF_DEVICE in config:
sens = await text_sensor.new_text_sensor(config[CONF_DEVICE]) sens = await text_sensor.new_text_sensor(config[CONF_DEVICE])
cg.add(debug_component.set_device_info_sensor(sens)) 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))