ESPHome 2025.6.2
Loading...
Searching...
No Matches
debug_component.cpp
Go to the documentation of this file.
1#include "debug_component.h"
2
3#include <algorithm>
5#include "esphome/core/hal.h"
7#include "esphome/core/log.h"
9#include <cinttypes>
10#include <climits>
11
12namespace esphome {
13namespace debug {
14
15static const char *const TAG = "debug";
16
18 ESP_LOGCONFIG(TAG, "Debug component:");
19#ifdef USE_TEXT_SENSOR
20 LOG_TEXT_SENSOR(" ", "Device info", this->device_info_);
21#endif // USE_TEXT_SENSOR
22#ifdef USE_SENSOR
23 LOG_SENSOR(" ", "Free space on heap", this->free_sensor_);
24 LOG_SENSOR(" ", "Largest free heap block", this->block_sensor_);
25 LOG_SENSOR(" ", "CPU frequency", this->cpu_frequency_sensor_);
26#if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 5, 2)
27 LOG_SENSOR(" ", "Heap fragmentation", this->fragmentation_sensor_);
28#endif // defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 5, 2)
29#endif // USE_SENSOR
30
31 std::string device_info;
32 device_info.reserve(256);
33 ESP_LOGD(TAG, "ESPHome version %s", ESPHOME_VERSION);
34 device_info += ESPHOME_VERSION;
35
36 this->free_heap_ = get_free_heap_();
37 ESP_LOGD(TAG, "Free Heap Size: %" PRIu32 " bytes", this->free_heap_);
38
39 get_device_info_(device_info);
40
41#ifdef USE_TEXT_SENSOR
42 if (this->device_info_ != nullptr) {
43 if (device_info.length() > 255)
44 device_info.resize(255);
45 this->device_info_->publish_state(device_info);
46 }
47 if (this->reset_reason_ != nullptr) {
49 }
50#endif // USE_TEXT_SENSOR
51
52#ifdef USE_ESP32
53 this->log_partition_info_(); // Log partition information for ESP32
54#endif // USE_ESP32
55}
56
58 // log when free heap space has halved
59 uint32_t new_free_heap = get_free_heap_();
60 if (new_free_heap < this->free_heap_ / 2) {
61 this->free_heap_ = new_free_heap;
62 ESP_LOGD(TAG, "Free Heap Size: %" PRIu32 " bytes", this->free_heap_);
63 this->status_momentary_warning("heap", 1000);
64 }
65
66#ifdef USE_SENSOR
67 // calculate loop time - from last call to this one
68 if (this->loop_time_sensor_ != nullptr) {
69 uint32_t now = App.get_loop_component_start_time();
70 uint32_t loop_time = now - this->last_loop_timetag_;
71 this->max_loop_time_ = std::max(this->max_loop_time_, loop_time);
72 this->last_loop_timetag_ = now;
73 }
74#endif // USE_SENSOR
75}
76
78#ifdef USE_SENSOR
79 if (this->free_sensor_ != nullptr) {
81 }
82
83 if (this->loop_time_sensor_ != nullptr) {
85 this->max_loop_time_ = 0;
86 }
87 if (this->cpu_frequency_sensor_ != nullptr) {
89 }
90
91#endif // USE_SENSOR
93}
94
96
97} // namespace debug
98} // namespace esphome
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void status_momentary_warning(const std::string &name, uint32_t length=5000)
float get_setup_priority() const override
void log_partition_info_()
Logs information about the device's partition table.
sensor::Sensor * fragmentation_sensor_
text_sensor::TextSensor * reset_reason_
sensor::Sensor * cpu_frequency_sensor_
text_sensor::TextSensor * device_info_
void get_device_info_(std::string &device_info)
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
void publish_state(const std::string &state)
const float LATE
For components that should be initialized at the very end of the setup process.
Definition component.cpp:29
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t arch_get_cpu_freq_hz()
Definition core.cpp:64
Application App
Global storage of Application pointer - only one Application can exist.