1
0
mirror of https://github.com/esphome/esphome.git synced 2026-02-08 16:51:52 +00:00

simplify more

This commit is contained in:
J. Nick Koston
2025-12-13 09:01:39 -06:00
parent 6d91f1cd77
commit dce5face4e
14 changed files with 59 additions and 71 deletions

View File

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

View File

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

View File

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

View File

@@ -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<uint32_t>(get_build_time()) ^ combined_serial;
uint32_t hash = static_cast<uint32_t>(App.get_build_time()) ^ combined_serial;
this->pref_ = global_preferences->make_preference<Sen5xBaselines>(hash, true);
if (this->pref_.load(&this->voc_baselines_storage_)) {

View File

@@ -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<uint32_t>(get_build_time()) ^ static_cast<uint32_t>(this->serial_number_);
uint32_t hash = static_cast<uint32_t>(App.get_build_time()) ^ static_cast<uint32_t>(this->serial_number_);
this->pref_ = global_preferences->make_preference<SGP30Baselines>(hash, true);
if (this->store_baseline_ && this->pref_.load(&this->baselines_storage_)) {

View File

@@ -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 <cinttypes>
@@ -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<uint32_t>(get_build_time()) ^ static_cast<uint32_t>(this->serial_number_);
uint32_t hash = static_cast<uint32_t>(App.get_build_time()) ^ static_cast<uint32_t>(this->serial_number_);
this->pref_ = global_preferences->make_preference<SGP4xBaselines>(hash, true);
if (this->pref_.load(&this->voc_baselines_storage_)) {

View File

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

View File

@@ -2,7 +2,7 @@
#ifdef USE_WIFI
#include <cassert>
#include <cinttypes>
#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<uint32_t>(get_build_time()) : 88491487UL;
uint32_t hash = this->has_sta() ? static_cast<uint32_t>(App.get_build_time()) : 88491487UL;
this->pref_ = global_preferences->make_preference<wifi::SavedWifiSettings>(hash, true);
#ifdef USE_WIFI_FAST_CONNECT

View File

@@ -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 <cstring>
#ifdef USE_ESP8266
#include <pgmspace.h>
#endif
#include "esphome/core/version.h"
#include "esphome/core/hal.h"
#include <algorithm>
@@ -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<char, BUILD_TIME_STR_SIZE> 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

View File

@@ -1,7 +1,9 @@
#pragma once
#include <algorithm>
#include <ctime>
#include <limits>
#include <span>
#include <string>
#include <vector>
#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<char, BUILD_TIME_STR_SIZE> 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_; }

View File

@@ -1,24 +0,0 @@
#include "build_info.h"
#include "build_info_data.h"
#include <cstring>
#ifdef USE_ESP8266
#include <pgmspace.h>
#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<char, BUILD_TIME_STR_SIZE> 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

View File

@@ -1,21 +0,0 @@
#pragma once
#include <cstdint>
#include <ctime>
#include <span>
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<char, BUILD_TIME_STR_SIZE> buffer);
} // namespace esphome

View File

@@ -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";

View File

@@ -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 <pgmspace.h>
static const char ESPHOME_BUILD_TIME_STR[] PROGMEM = "{build_time_str}";