From dce5face4e302b92a2a9925c87fd8f3794518505 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 13 Dec 2025 09:01:39 -0600 Subject: [PATCH] simplify more --- esphome/__main__.py | 5 ++-- esphome/components/api/api_connection.cpp | 5 ++-- esphome/components/mqtt/mqtt_component.cpp | 6 ++--- esphome/components/sen5x/sen5x.cpp | 4 ++-- esphome/components/sgp30/sgp30.cpp | 4 ++-- esphome/components/sgp4x/sgp4x.cpp | 4 ++-- .../version/version_text_sensor.cpp | 6 ++--- esphome/components/wifi/wifi_component.cpp | 4 ++-- esphome/core/application.cpp | 24 ++++++++++++++++--- esphome/core/application.h | 15 ++++++++++++ esphome/core/build_info.cpp | 24 ------------------- esphome/core/build_info.h | 21 ---------------- esphome/core/build_info_data.h | 4 ++-- esphome/writer.py | 4 ++-- 14 files changed, 59 insertions(+), 71 deletions(-) delete mode 100644 esphome/core/build_info.cpp delete mode 100644 esphome/core/build_info.h diff --git a/esphome/__main__.py b/esphome/__main__.py index 5a58abcc78..942f533038 100644 --- a/esphome/__main__.py +++ b/esphome/__main__.py @@ -555,9 +555,10 @@ def _check_and_emit_build_info() -> None: if config_hash is None or build_time is None: return - # Emit build_info + # Emit build_info with human-readable time + build_time_str = time.strftime("%b %d %Y, %H:%M:%S", time.localtime(build_time)) _LOGGER.info( - "Build Info: config_hash=0x%08x build_time=%s", config_hash, build_time + "Build Info: config_hash=0x%08x build_time=%s", config_hash, build_time_str ) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 992cae028c..c7afd72bf3 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -19,7 +19,6 @@ #endif #include "esphome/components/network/util.h" #include "esphome/core/application.h" -#include "esphome/core/build_info.h" #include "esphome/core/entity_base.h" #include "esphome/core/hal.h" #include "esphome/core/log.h" @@ -1474,8 +1473,8 @@ bool APIConnection::send_device_info_response(const DeviceInfoRequest &msg) { resp.set_esphome_version(ESPHOME_VERSION_REF); // Stack buffer for build time string - char build_time_str[BUILD_TIME_STR_SIZE]; - get_build_time_string(build_time_str); + char build_time_str[App.BUILD_TIME_STR_SIZE]; + App.get_build_time_string(build_time_str); resp.set_compilation_time(StringRef(build_time_str)); // Manufacturer string - define once, handle ESP8266 PROGMEM separately diff --git a/esphome/components/mqtt/mqtt_component.cpp b/esphome/components/mqtt/mqtt_component.cpp index f06013fb7e..6f5cf5edad 100644 --- a/esphome/components/mqtt/mqtt_component.cpp +++ b/esphome/components/mqtt/mqtt_component.cpp @@ -2,7 +2,7 @@ #ifdef USE_MQTT -#include "esphome/core/build_info.h" +#include "esphome/core/application.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" #include "esphome/core/version.h" @@ -154,8 +154,8 @@ bool MQTTComponent::send_discovery_() { device_info[MQTT_DEVICE_MANUFACTURER] = model == nullptr ? ESPHOME_PROJECT_NAME : std::string(ESPHOME_PROJECT_NAME, model - ESPHOME_PROJECT_NAME); #else - char build_time_str[BUILD_TIME_STR_SIZE]; - get_build_time_string(build_time_str); + char build_time_str[App.BUILD_TIME_STR_SIZE]; + App.get_build_time_string(build_time_str); device_info[MQTT_DEVICE_SW_VERSION] = str_sprintf(ESPHOME_VERSION " (%s)", build_time_str); device_info[MQTT_DEVICE_MODEL] = ESPHOME_BOARD; #if defined(USE_ESP8266) || defined(USE_ESP32) diff --git a/esphome/components/sen5x/sen5x.cpp b/esphome/components/sen5x/sen5x.cpp index 01e7b76101..82145d0b22 100644 --- a/esphome/components/sen5x/sen5x.cpp +++ b/esphome/components/sen5x/sen5x.cpp @@ -1,5 +1,5 @@ #include "sen5x.h" -#include "esphome/core/build_info.h" +#include "esphome/core/application.h" #include "esphome/core/hal.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" @@ -158,7 +158,7 @@ void SEN5XComponent::setup() { // Hash with build time and serial number // This ensures the baseline storage is cleared after OTA // Serial numbers are unique to each sensor, so mulitple sensors can be used without conflict - uint32_t hash = static_cast(get_build_time()) ^ combined_serial; + uint32_t hash = static_cast(App.get_build_time()) ^ combined_serial; this->pref_ = global_preferences->make_preference(hash, true); if (this->pref_.load(&this->voc_baselines_storage_)) { diff --git a/esphome/components/sgp30/sgp30.cpp b/esphome/components/sgp30/sgp30.cpp index 5174281ad5..0645d2faf9 100644 --- a/esphome/components/sgp30/sgp30.cpp +++ b/esphome/components/sgp30/sgp30.cpp @@ -1,5 +1,5 @@ #include "sgp30.h" -#include "esphome/core/build_info.h" +#include "esphome/core/application.h" #include "esphome/core/hal.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" @@ -75,7 +75,7 @@ void SGP30Component::setup() { // Hash with build time and serial number // This ensures the baseline storage is cleared after OTA // Serial numbers are unique to each sensor, so mulitple sensors can be used without conflict - uint32_t hash = static_cast(get_build_time()) ^ static_cast(this->serial_number_); + uint32_t hash = static_cast(App.get_build_time()) ^ static_cast(this->serial_number_); this->pref_ = global_preferences->make_preference(hash, true); if (this->store_baseline_ && this->pref_.load(&this->baselines_storage_)) { diff --git a/esphome/components/sgp4x/sgp4x.cpp b/esphome/components/sgp4x/sgp4x.cpp index ec54e6d8f6..fa984ba418 100644 --- a/esphome/components/sgp4x/sgp4x.cpp +++ b/esphome/components/sgp4x/sgp4x.cpp @@ -1,5 +1,5 @@ #include "sgp4x.h" -#include "esphome/core/build_info.h" +#include "esphome/core/application.h" #include "esphome/core/log.h" #include "esphome/core/hal.h" #include @@ -60,7 +60,7 @@ void SGP4xComponent::setup() { // Hash with build time and serial number // This ensures the baseline storage is cleared after OTA // Serial numbers are unique to each sensor, so mulitple sensors can be used without conflict - uint32_t hash = static_cast(get_build_time()) ^ static_cast(this->serial_number_); + uint32_t hash = static_cast(App.get_build_time()) ^ static_cast(this->serial_number_); this->pref_ = global_preferences->make_preference(hash, true); if (this->pref_.load(&this->voc_baselines_storage_)) { diff --git a/esphome/components/version/version_text_sensor.cpp b/esphome/components/version/version_text_sensor.cpp index 1b9bde7f81..7cec62a10a 100644 --- a/esphome/components/version/version_text_sensor.cpp +++ b/esphome/components/version/version_text_sensor.cpp @@ -1,5 +1,5 @@ #include "version_text_sensor.h" -#include "esphome/core/build_info.h" +#include "esphome/core/application.h" #include "esphome/core/log.h" #include "esphome/core/version.h" #include "esphome/core/helpers.h" @@ -13,8 +13,8 @@ void VersionTextSensor::setup() { if (this->hide_timestamp_) { this->publish_state(ESPHOME_VERSION); } else { - char build_time_str[BUILD_TIME_STR_SIZE]; - get_build_time_string(build_time_str); + char build_time_str[App.BUILD_TIME_STR_SIZE]; + App.get_build_time_string(build_time_str); this->publish_state(str_sprintf(ESPHOME_VERSION " %s", build_time_str)); } } diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index fbc5fefb00..9eaa5fcfb5 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -2,7 +2,7 @@ #ifdef USE_WIFI #include #include -#include "esphome/core/build_info.h" +#include "esphome/core/application.h" #ifdef USE_ESP32 #if (ESP_IDF_VERSION_MAJOR >= 5 && ESP_IDF_VERSION_MINOR >= 1) @@ -361,7 +361,7 @@ void WiFiComponent::start() { get_mac_address_pretty_into_buffer(mac_s)); this->last_connected_ = millis(); - uint32_t hash = this->has_sta() ? static_cast(get_build_time()) : 88491487UL; + uint32_t hash = this->has_sta() ? static_cast(App.get_build_time()) : 88491487UL; this->pref_ = global_preferences->make_preference(hash, true); #ifdef USE_WIFI_FAST_CONNECT diff --git a/esphome/core/application.cpp b/esphome/core/application.cpp index 2fee5a6fe8..376ea3c200 100644 --- a/esphome/core/application.cpp +++ b/esphome/core/application.cpp @@ -1,6 +1,11 @@ #include "esphome/core/application.h" -#include "esphome/core/build_info.h" +#include "esphome/core/build_info_data.h" #include "esphome/core/log.h" +#include + +#ifdef USE_ESP8266 +#include +#endif #include "esphome/core/version.h" #include "esphome/core/hal.h" #include @@ -192,8 +197,8 @@ void Application::loop() { if (this->dump_config_at_ < this->components_.size()) { if (this->dump_config_at_ == 0) { - char build_time_str[BUILD_TIME_STR_SIZE]; - get_build_time_string(build_time_str); + char build_time_str[Application::BUILD_TIME_STR_SIZE]; + this->get_build_time_string(build_time_str); ESP_LOGI(TAG, "ESPHome version " ESPHOME_VERSION " compiled on %s", build_time_str); #ifdef ESPHOME_PROJECT_NAME ESP_LOGI(TAG, "Project " ESPHOME_PROJECT_NAME " version " ESPHOME_PROJECT_VERSION); @@ -714,4 +719,17 @@ void Application::wake_loop_threadsafe() { } #endif // defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE) +uint32_t Application::get_config_hash() { return ESPHOME_CONFIG_HASH; } + +time_t Application::get_build_time() { return ESPHOME_BUILD_TIME; } + +void Application::get_build_time_string(std::span buffer) { +#ifdef USE_ESP8266 + strncpy_P(buffer.data(), ESPHOME_BUILD_TIME_STR, buffer.size()); +#else + strncpy(buffer.data(), ESPHOME_BUILD_TIME_STR, buffer.size()); +#endif + buffer[buffer.size() - 1] = '\0'; +} + } // namespace esphome diff --git a/esphome/core/application.h b/esphome/core/application.h index 09cd7e5bbc..93f409b6cb 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -1,7 +1,9 @@ #pragma once #include +#include #include +#include #include #include #include "esphome/core/component.h" @@ -260,6 +262,19 @@ class Application { bool is_name_add_mac_suffix_enabled() const { return this->name_add_mac_suffix_; } + /// Size of buffer required for build time string (including null terminator) + static constexpr size_t BUILD_TIME_STR_SIZE = 24; + + /// Get the config hash as a 32-bit integer + uint32_t get_config_hash(); + + /// Get the build time as a Unix timestamp + time_t get_build_time(); + + /// Copy the build time string into the provided buffer + /// Buffer must be BUILD_TIME_STR_SIZE bytes (compile-time enforced) + void get_build_time_string(std::span buffer); + /// Get the cached time in milliseconds from when the current component started its loop execution inline uint32_t IRAM_ATTR HOT get_loop_component_start_time() const { return this->loop_component_start_time_; } diff --git a/esphome/core/build_info.cpp b/esphome/core/build_info.cpp deleted file mode 100644 index 85d07ce020..0000000000 --- a/esphome/core/build_info.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "build_info.h" -#include "build_info_data.h" -#include - -#ifdef USE_ESP8266 -#include -#endif - -namespace esphome { - -uint32_t get_config_hash() { return ESPHOME_CONFIG_HASH; } - -time_t get_build_time() { return ESPHOME_BUILD_TIME; } - -void get_build_time_string(std::span buffer) { -#ifdef USE_ESP8266 - strncpy_P(buffer.data(), ESPHOME_BUILD_TIME_STR, buffer.size()); -#else - strncpy(buffer.data(), ESPHOME_BUILD_TIME_STR, buffer.size()); -#endif - buffer[buffer.size() - 1] = '\0'; -} - -} // namespace esphome diff --git a/esphome/core/build_info.h b/esphome/core/build_info.h deleted file mode 100644 index 6a427ea511..0000000000 --- a/esphome/core/build_info.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -#include -#include -#include - -namespace esphome { - -/// Size of buffer required for build time string (including null terminator) -static constexpr size_t BUILD_TIME_STR_SIZE = 24; - -/// Get the config hash as a 32-bit integer -uint32_t get_config_hash(); - -/// Get the build time as a Unix timestamp -time_t get_build_time(); - -/// Copy the build time string into the provided buffer -/// Buffer must be BUILD_TIME_STR_SIZE bytes (compile-time enforced) -void get_build_time_string(std::span buffer); - -} // namespace esphome diff --git a/esphome/core/build_info_data.h b/esphome/core/build_info_data.h index e81645a3da..81c24e0fb5 100644 --- a/esphome/core/build_info_data.h +++ b/esphome/core/build_info_data.h @@ -5,6 +5,6 @@ // // This file is only used by static analyzers and IDEs. -#define ESPHOME_CONFIG_HASH 0x12345678U -#define ESPHOME_BUILD_TIME 1700000000 +#define ESPHOME_CONFIG_HASH 0x12345678U // NOLINT +#define ESPHOME_BUILD_TIME 1700000000 // NOLINT static const char ESPHOME_BUILD_TIME_STR[] = "Jan 01 2024, 00:00:00"; diff --git a/esphome/writer.py b/esphome/writer.py index 5960dc0af5..4a87c576c3 100644 --- a/esphome/writer.py +++ b/esphome/writer.py @@ -306,8 +306,8 @@ def generate_build_info_data_h( """Generate build_info_data.h header with config hash and build time.""" return f"""#pragma once // Auto-generated build_info data -#define ESPHOME_CONFIG_HASH 0x{config_hash:08x}U -#define ESPHOME_BUILD_TIME {build_time} +#define ESPHOME_CONFIG_HASH 0x{config_hash:08x}U // NOLINT +#define ESPHOME_BUILD_TIME {build_time} // NOLINT #ifdef USE_ESP8266 #include static const char ESPHOME_BUILD_TIME_STR[] PROGMEM = "{build_time_str}";