mirror of
https://github.com/esphome/esphome.git
synced 2026-02-09 01:01:56 +00:00
Compare commits
15 Commits
compact_st
...
web_server
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14b42d5997 | ||
|
|
4db761ef03 | ||
|
|
b3e09e5c68 | ||
|
|
d4110bf650 | ||
|
|
ff6f7d3248 | ||
|
|
a430b3a426 | ||
|
|
fbeb0e8e54 | ||
|
|
9d63642bdb | ||
|
|
8cb701e412 | ||
|
|
d41c84d624 | ||
|
|
9f1a427ce2 | ||
|
|
ae71f07abb | ||
|
|
ccf5c1f7e9 | ||
|
|
efecea9450 | ||
|
|
26e4cda610 |
4
.github/workflows/codeql.yml
vendored
4
.github/workflows/codeql.yml
vendored
@@ -58,7 +58,7 @@ jobs:
|
||||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@b20883b0cd1f46c72ae0ba6d1090936928f9fa30 # v4.32.0
|
||||
uses: github/codeql-action/init@6bc82e05fd0ea64601dd4b465378bbcf57de0314 # v4.32.1
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
build-mode: ${{ matrix.build-mode }}
|
||||
@@ -86,6 +86,6 @@ jobs:
|
||||
exit 1
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@b20883b0cd1f46c72ae0ba6d1090936928f9fa30 # v4.32.0
|
||||
uses: github/codeql-action/analyze@6bc82e05fd0ea64601dd4b465378bbcf57de0314 # v4.32.1
|
||||
with:
|
||||
category: "/language:${{matrix.language}}"
|
||||
|
||||
@@ -124,10 +124,14 @@ COMPILER_OPTIMIZATIONS = {
|
||||
# - "sdmmc": driver -> esp_driver_sdmmc -> sdmmc dependency chain
|
||||
DEFAULT_EXCLUDED_IDF_COMPONENTS = (
|
||||
"cmock", # Unit testing mock framework - ESPHome doesn't use IDF's testing
|
||||
"driver", # Legacy driver shim - only needed by esp32_touch, esp32_can for legacy headers
|
||||
"esp_adc", # ADC driver - only needed by adc component
|
||||
"esp_driver_dac", # DAC driver - only needed by esp32_dac component
|
||||
"esp_driver_i2s", # I2S driver - only needed by i2s_audio component
|
||||
"esp_driver_mcpwm", # MCPWM driver - ESPHome doesn't use motor control PWM
|
||||
"esp_driver_rmt", # RMT driver - only needed by remote_transmitter/receiver, neopixelbus
|
||||
"esp_driver_touch_sens", # Touch sensor driver - only needed by esp32_touch
|
||||
"esp_driver_twai", # TWAI/CAN driver - only needed by esp32_can component
|
||||
"esp_eth", # Ethernet driver - only needed by ethernet component
|
||||
"esp_hid", # HID host/device support - ESPHome doesn't implement HID functionality
|
||||
"esp_http_client", # HTTP client - only needed by http_request component
|
||||
@@ -138,9 +142,11 @@ DEFAULT_EXCLUDED_IDF_COMPONENTS = (
|
||||
"espcoredump", # Core dump support - ESPHome has its own debug component
|
||||
"fatfs", # FAT filesystem - ESPHome doesn't use filesystem storage
|
||||
"mqtt", # ESP-IDF MQTT library - ESPHome has its own MQTT implementation
|
||||
"openthread", # Thread protocol - only needed by openthread component
|
||||
"perfmon", # Xtensa performance monitor - ESPHome has its own debug component
|
||||
"protocomm", # Protocol communication for provisioning - unused by ESPHome
|
||||
"spiffs", # SPIFFS filesystem - ESPHome doesn't use filesystem storage (IDF only)
|
||||
"ulp", # ULP coprocessor - not currently used by any ESPHome component
|
||||
"unity", # Unit testing framework - ESPHome doesn't use IDF's testing
|
||||
"wear_levelling", # Flash wear levelling for fatfs - unused since fatfs unused
|
||||
"wifi_provisioning", # WiFi provisioning - ESPHome uses its own improv implementation
|
||||
|
||||
@@ -203,10 +203,11 @@ class ESP32Preferences : public ESPPreferences {
|
||||
}
|
||||
};
|
||||
|
||||
static ESP32Preferences s_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
void setup_preferences() {
|
||||
auto *prefs = new ESP32Preferences(); // NOLINT(cppcoreguidelines-owning-memory)
|
||||
prefs->open();
|
||||
global_preferences = prefs;
|
||||
s_preferences.open();
|
||||
global_preferences = &s_preferences;
|
||||
}
|
||||
|
||||
} // namespace esp32
|
||||
|
||||
@@ -15,6 +15,7 @@ from esphome.components.esp32 import (
|
||||
VARIANT_ESP32S2,
|
||||
VARIANT_ESP32S3,
|
||||
get_esp32_variant,
|
||||
include_builtin_idf_component,
|
||||
)
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
@@ -121,6 +122,10 @@ def get_default_tx_enqueue_timeout(bit_rate):
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
# Legacy driver component provides driver/twai.h header
|
||||
include_builtin_idf_component("driver")
|
||||
# Also enable esp_driver_twai for future migration to new API
|
||||
include_builtin_idf_component("esp_driver_twai")
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await canbus.register_canbus(var, config)
|
||||
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
from esphome import pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import output
|
||||
from esphome.components.esp32 import VARIANT_ESP32, VARIANT_ESP32S2, get_esp32_variant
|
||||
from esphome.components.esp32 import (
|
||||
VARIANT_ESP32,
|
||||
VARIANT_ESP32S2,
|
||||
get_esp32_variant,
|
||||
include_builtin_idf_component,
|
||||
)
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID, CONF_NUMBER, CONF_PIN
|
||||
|
||||
@@ -38,6 +43,7 @@ CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
include_builtin_idf_component("esp_driver_dac")
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await output.register_output(var, config)
|
||||
|
||||
@@ -211,11 +211,14 @@ bool Esp32HostedUpdate::fetch_manifest_() {
|
||||
int read_or_error = container->read(buf, sizeof(buf));
|
||||
App.feed_wdt();
|
||||
yield();
|
||||
auto result = http_request::http_read_loop_result(read_or_error, last_data_time, read_timeout);
|
||||
auto result =
|
||||
http_request::http_read_loop_result(read_or_error, last_data_time, read_timeout, container->is_read_complete());
|
||||
if (result == http_request::HttpReadLoopResult::RETRY)
|
||||
continue;
|
||||
// Note: COMPLETE is currently unreachable since the loop condition checks bytes_read < content_length,
|
||||
// but this is defensive code in case chunked transfer encoding support is added in the future.
|
||||
if (result != http_request::HttpReadLoopResult::DATA)
|
||||
break; // ERROR or TIMEOUT
|
||||
break; // COMPLETE, ERROR, or TIMEOUT
|
||||
json_str.append(reinterpret_cast<char *>(buf), read_or_error);
|
||||
}
|
||||
container->end();
|
||||
@@ -336,9 +339,14 @@ bool Esp32HostedUpdate::stream_firmware_to_coprocessor_() {
|
||||
App.feed_wdt();
|
||||
yield();
|
||||
|
||||
auto result = http_request::http_read_loop_result(read_or_error, last_data_time, read_timeout);
|
||||
auto result =
|
||||
http_request::http_read_loop_result(read_or_error, last_data_time, read_timeout, container->is_read_complete());
|
||||
if (result == http_request::HttpReadLoopResult::RETRY)
|
||||
continue;
|
||||
// Note: COMPLETE is currently unreachable since the loop condition checks bytes_read < content_length,
|
||||
// but this is defensive code in case chunked transfer encoding support is added in the future.
|
||||
if (result == http_request::HttpReadLoopResult::COMPLETE)
|
||||
break;
|
||||
if (result != http_request::HttpReadLoopResult::DATA) {
|
||||
if (result == http_request::HttpReadLoopResult::TIMEOUT) {
|
||||
ESP_LOGE(TAG, "Timeout reading firmware data");
|
||||
|
||||
@@ -269,6 +269,8 @@ CONFIG_SCHEMA = cv.All(
|
||||
async def to_code(config):
|
||||
# Re-enable ESP-IDF's touch sensor driver (excluded by default to save compile time)
|
||||
include_builtin_idf_component("esp_driver_touch_sens")
|
||||
# Legacy driver component provides driver/touch_sensor.h header
|
||||
include_builtin_idf_component("driver")
|
||||
|
||||
touch = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(touch, config)
|
||||
|
||||
@@ -17,10 +17,6 @@ namespace esphome::esp8266 {
|
||||
|
||||
static const char *const TAG = "esp8266.preferences";
|
||||
|
||||
static uint32_t *s_flash_storage = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
static bool s_prevent_write = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
static bool s_flash_dirty = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
static constexpr uint32_t ESP_RTC_USER_MEM_START = 0x60001200;
|
||||
static constexpr uint32_t ESP_RTC_USER_MEM_SIZE_WORDS = 128;
|
||||
static constexpr uint32_t ESP_RTC_USER_MEM_SIZE_BYTES = ESP_RTC_USER_MEM_SIZE_WORDS * 4;
|
||||
@@ -43,6 +39,11 @@ static constexpr uint32_t ESP8266_FLASH_STORAGE_SIZE = 128;
|
||||
static constexpr uint32_t ESP8266_FLASH_STORAGE_SIZE = 64;
|
||||
#endif
|
||||
|
||||
static uint32_t
|
||||
s_flash_storage[ESP8266_FLASH_STORAGE_SIZE]; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
static bool s_prevent_write = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
static bool s_flash_dirty = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
static inline bool esp_rtc_user_mem_read(uint32_t index, uint32_t *dest) {
|
||||
if (index >= ESP_RTC_USER_MEM_SIZE_WORDS) {
|
||||
return false;
|
||||
@@ -180,7 +181,6 @@ class ESP8266Preferences : public ESPPreferences {
|
||||
uint32_t current_flash_offset = 0; // in words
|
||||
|
||||
void setup() {
|
||||
s_flash_storage = new uint32_t[ESP8266_FLASH_STORAGE_SIZE]; // NOLINT
|
||||
ESP_LOGVV(TAG, "Loading preferences from flash");
|
||||
|
||||
{
|
||||
@@ -283,10 +283,11 @@ class ESP8266Preferences : public ESPPreferences {
|
||||
}
|
||||
};
|
||||
|
||||
static ESP8266Preferences s_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
void setup_preferences() {
|
||||
auto *pref = new ESP8266Preferences(); // NOLINT(cppcoreguidelines-owning-memory)
|
||||
pref->setup();
|
||||
global_preferences = pref;
|
||||
s_preferences.setup();
|
||||
global_preferences = &s_preferences;
|
||||
}
|
||||
void preferences_prevent_write(bool prevent) { s_prevent_write = prevent; }
|
||||
|
||||
|
||||
@@ -66,10 +66,11 @@ ESPPreferenceObject HostPreferences::make_preference(size_t length, uint32_t typ
|
||||
return ESPPreferenceObject(backend);
|
||||
};
|
||||
|
||||
static HostPreferences s_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
void setup_preferences() {
|
||||
auto *pref = new HostPreferences(); // NOLINT(cppcoreguidelines-owning-memory)
|
||||
host_preferences = pref;
|
||||
global_preferences = pref;
|
||||
host_preferences = &s_preferences;
|
||||
global_preferences = &s_preferences;
|
||||
}
|
||||
|
||||
bool HostPreferenceBackend::save(const uint8_t *data, size_t len) {
|
||||
|
||||
@@ -26,6 +26,7 @@ struct Header {
|
||||
enum HttpStatus {
|
||||
HTTP_STATUS_OK = 200,
|
||||
HTTP_STATUS_NO_CONTENT = 204,
|
||||
HTTP_STATUS_RESET_CONTENT = 205,
|
||||
HTTP_STATUS_PARTIAL_CONTENT = 206,
|
||||
|
||||
/* 3xx - Redirection */
|
||||
@@ -126,19 +127,21 @@ struct HttpReadResult {
|
||||
|
||||
/// Result of processing a non-blocking read with timeout (for manual loops)
|
||||
enum class HttpReadLoopResult : uint8_t {
|
||||
DATA, ///< Data was read, process it
|
||||
RETRY, ///< No data yet, already delayed, caller should continue loop
|
||||
ERROR, ///< Read error, caller should exit loop
|
||||
TIMEOUT, ///< Timeout waiting for data, caller should exit loop
|
||||
DATA, ///< Data was read, process it
|
||||
COMPLETE, ///< All content has been read, caller should exit loop
|
||||
RETRY, ///< No data yet, already delayed, caller should continue loop
|
||||
ERROR, ///< Read error, caller should exit loop
|
||||
TIMEOUT, ///< Timeout waiting for data, caller should exit loop
|
||||
};
|
||||
|
||||
/// Process a read result with timeout tracking and delay handling
|
||||
/// @param bytes_read_or_error Return value from read() - positive for bytes read, negative for error
|
||||
/// @param last_data_time Time of last successful read, updated when data received
|
||||
/// @param timeout_ms Maximum time to wait for data
|
||||
/// @return DATA if data received, RETRY if should continue loop, ERROR/TIMEOUT if should exit
|
||||
inline HttpReadLoopResult http_read_loop_result(int bytes_read_or_error, uint32_t &last_data_time,
|
||||
uint32_t timeout_ms) {
|
||||
/// @param is_read_complete Whether all expected content has been read (from HttpContainer::is_read_complete())
|
||||
/// @return How the caller should proceed - see HttpReadLoopResult enum
|
||||
inline HttpReadLoopResult http_read_loop_result(int bytes_read_or_error, uint32_t &last_data_time, uint32_t timeout_ms,
|
||||
bool is_read_complete) {
|
||||
if (bytes_read_or_error > 0) {
|
||||
last_data_time = millis();
|
||||
return HttpReadLoopResult::DATA;
|
||||
@@ -146,7 +149,10 @@ inline HttpReadLoopResult http_read_loop_result(int bytes_read_or_error, uint32_
|
||||
if (bytes_read_or_error < 0) {
|
||||
return HttpReadLoopResult::ERROR;
|
||||
}
|
||||
// bytes_read_or_error == 0: no data available yet
|
||||
// bytes_read_or_error == 0: either "no data yet" or "all content read"
|
||||
if (is_read_complete) {
|
||||
return HttpReadLoopResult::COMPLETE;
|
||||
}
|
||||
if (millis() - last_data_time >= timeout_ms) {
|
||||
return HttpReadLoopResult::TIMEOUT;
|
||||
}
|
||||
@@ -159,9 +165,9 @@ class HttpRequestComponent;
|
||||
class HttpContainer : public Parented<HttpRequestComponent> {
|
||||
public:
|
||||
virtual ~HttpContainer() = default;
|
||||
size_t content_length;
|
||||
int status_code;
|
||||
uint32_t duration_ms;
|
||||
size_t content_length{0};
|
||||
int status_code{-1}; ///< -1 indicates no response received yet
|
||||
uint32_t duration_ms{0};
|
||||
|
||||
/**
|
||||
* @brief Read data from the HTTP response body.
|
||||
@@ -194,9 +200,24 @@ class HttpContainer : public Parented<HttpRequestComponent> {
|
||||
virtual void end() = 0;
|
||||
|
||||
void set_secure(bool secure) { this->secure_ = secure; }
|
||||
void set_chunked(bool chunked) { this->is_chunked_ = chunked; }
|
||||
|
||||
size_t get_bytes_read() const { return this->bytes_read_; }
|
||||
|
||||
/// Check if all expected content has been read
|
||||
/// For chunked responses, returns false (completion detected via read() returning error/EOF)
|
||||
bool is_read_complete() const {
|
||||
// Per RFC 9112, these responses have no body:
|
||||
// - 1xx (Informational), 204 No Content, 205 Reset Content, 304 Not Modified
|
||||
if ((this->status_code >= 100 && this->status_code < 200) || this->status_code == HTTP_STATUS_NO_CONTENT ||
|
||||
this->status_code == HTTP_STATUS_RESET_CONTENT || this->status_code == HTTP_STATUS_NOT_MODIFIED) {
|
||||
return true;
|
||||
}
|
||||
// For non-chunked responses, complete when bytes_read >= content_length
|
||||
// This handles both Content-Length: 0 and Content-Length: N cases
|
||||
return !this->is_chunked_ && this->bytes_read_ >= this->content_length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get response headers.
|
||||
*
|
||||
@@ -209,6 +230,7 @@ class HttpContainer : public Parented<HttpRequestComponent> {
|
||||
protected:
|
||||
size_t bytes_read_{0};
|
||||
bool secure_{false};
|
||||
bool is_chunked_{false}; ///< True if response uses chunked transfer encoding
|
||||
std::map<std::string, std::list<std::string>> response_headers_{};
|
||||
};
|
||||
|
||||
@@ -219,7 +241,7 @@ class HttpContainer : public Parented<HttpRequestComponent> {
|
||||
/// @param total_size Total bytes to read
|
||||
/// @param chunk_size Maximum bytes per read call
|
||||
/// @param timeout_ms Read timeout in milliseconds
|
||||
/// @return HttpReadResult with status and error_code on failure
|
||||
/// @return HttpReadResult with status and error_code on failure; use container->get_bytes_read() for total bytes read
|
||||
inline HttpReadResult http_read_fully(HttpContainer *container, uint8_t *buffer, size_t total_size, size_t chunk_size,
|
||||
uint32_t timeout_ms) {
|
||||
size_t read_index = 0;
|
||||
@@ -231,9 +253,11 @@ inline HttpReadResult http_read_fully(HttpContainer *container, uint8_t *buffer,
|
||||
App.feed_wdt();
|
||||
yield();
|
||||
|
||||
auto result = http_read_loop_result(read_bytes_or_error, last_data_time, timeout_ms);
|
||||
auto result = http_read_loop_result(read_bytes_or_error, last_data_time, timeout_ms, container->is_read_complete());
|
||||
if (result == HttpReadLoopResult::RETRY)
|
||||
continue;
|
||||
if (result == HttpReadLoopResult::COMPLETE)
|
||||
break; // Server sent less data than requested, but transfer is complete
|
||||
if (result == HttpReadLoopResult::ERROR)
|
||||
return {HttpReadStatus::ERROR, read_bytes_or_error};
|
||||
if (result == HttpReadLoopResult::TIMEOUT)
|
||||
@@ -393,11 +417,12 @@ template<typename... Ts> class HttpRequestSendAction : public Action<Ts...> {
|
||||
int read_or_error = container->read(buf + read_index, std::min<size_t>(max_length - read_index, 512));
|
||||
App.feed_wdt();
|
||||
yield();
|
||||
auto result = http_read_loop_result(read_or_error, last_data_time, read_timeout);
|
||||
auto result =
|
||||
http_read_loop_result(read_or_error, last_data_time, read_timeout, container->is_read_complete());
|
||||
if (result == HttpReadLoopResult::RETRY)
|
||||
continue;
|
||||
if (result != HttpReadLoopResult::DATA)
|
||||
break; // ERROR or TIMEOUT
|
||||
break; // COMPLETE, ERROR, or TIMEOUT
|
||||
read_index += read_or_error;
|
||||
}
|
||||
response_body.reserve(read_index);
|
||||
|
||||
@@ -135,9 +135,23 @@ std::shared_ptr<HttpContainer> HttpRequestArduino::perform(const std::string &ur
|
||||
// When cast to size_t, -1 becomes SIZE_MAX (4294967295 on 32-bit).
|
||||
// The read() method handles this: bytes_read_ can never reach SIZE_MAX, so the
|
||||
// early return check (bytes_read_ >= content_length) will never trigger.
|
||||
//
|
||||
// TODO: Chunked transfer encoding is NOT properly supported on Arduino.
|
||||
// The implementation in #7884 was incomplete - it only works correctly on ESP-IDF where
|
||||
// esp_http_client_read() decodes chunks internally. On Arduino, using getStreamPtr()
|
||||
// returns raw TCP data with chunk framing (e.g., "12a\r\n{json}\r\n0\r\n\r\n") instead
|
||||
// of decoded content. This wasn't noticed because requests would complete and payloads
|
||||
// were only examined on IDF. The long transfer times were also masked by the misleading
|
||||
// "HTTP on Arduino version >= 3.1 is **very** slow" warning above. This causes two issues:
|
||||
// 1. Response body is corrupted - contains chunk size headers mixed with data
|
||||
// 2. Cannot detect end of transfer - connection stays open (keep-alive), causing timeout
|
||||
// The proper fix would be to use getString() for chunked responses, which decodes chunks
|
||||
// internally, but this buffers the entire response in memory.
|
||||
int content_length = container->client_.getSize();
|
||||
ESP_LOGD(TAG, "Content-Length: %d", content_length);
|
||||
container->content_length = (size_t) content_length;
|
||||
// -1 (SIZE_MAX when cast to size_t) means chunked transfer encoding
|
||||
container->set_chunked(content_length == -1);
|
||||
container->duration_ms = millis() - start;
|
||||
|
||||
return container;
|
||||
@@ -178,9 +192,9 @@ int HttpContainerArduino::read(uint8_t *buf, size_t max_len) {
|
||||
|
||||
if (bufsize == 0) {
|
||||
this->duration_ms += (millis() - start);
|
||||
// Check if we've read all expected content (only valid when content_length is known and not SIZE_MAX)
|
||||
// For chunked encoding (content_length == SIZE_MAX), we can't use this check
|
||||
if (this->content_length > 0 && this->bytes_read_ >= this->content_length) {
|
||||
// Check if we've read all expected content (non-chunked only)
|
||||
// For chunked encoding (content_length == SIZE_MAX), is_read_complete() returns false
|
||||
if (this->is_read_complete()) {
|
||||
return 0; // All content read successfully
|
||||
}
|
||||
// No data available - check if connection is still open
|
||||
|
||||
@@ -160,6 +160,7 @@ std::shared_ptr<HttpContainer> HttpRequestIDF::perform(const std::string &url, c
|
||||
// esp_http_client_fetch_headers() returns 0 for chunked transfer encoding (no Content-Length header).
|
||||
// The read() method handles content_length == 0 specially to support chunked responses.
|
||||
container->content_length = esp_http_client_fetch_headers(client);
|
||||
container->set_chunked(esp_http_client_is_chunked_response(client));
|
||||
container->feed_wdt();
|
||||
container->status_code = esp_http_client_get_status_code(client);
|
||||
container->feed_wdt();
|
||||
@@ -195,6 +196,7 @@ std::shared_ptr<HttpContainer> HttpRequestIDF::perform(const std::string &url, c
|
||||
|
||||
container->feed_wdt();
|
||||
container->content_length = esp_http_client_fetch_headers(client);
|
||||
container->set_chunked(esp_http_client_is_chunked_response(client));
|
||||
container->feed_wdt();
|
||||
container->status_code = esp_http_client_get_status_code(client);
|
||||
container->feed_wdt();
|
||||
@@ -239,10 +241,9 @@ int HttpContainerIDF::read(uint8_t *buf, size_t max_len) {
|
||||
const uint32_t start = millis();
|
||||
watchdog::WatchdogManager wdm(this->parent_->get_watchdog_timeout());
|
||||
|
||||
// Check if we've already read all expected content
|
||||
// Skip this check when content_length is 0 (chunked transfer encoding or unknown length)
|
||||
// For chunked responses, esp_http_client_read() will return 0 when all data is received
|
||||
if (this->content_length > 0 && this->bytes_read_ >= this->content_length) {
|
||||
// Check if we've already read all expected content (non-chunked only)
|
||||
// For chunked responses (content_length == 0), esp_http_client_read() handles EOF
|
||||
if (this->is_read_complete()) {
|
||||
return 0; // All content read successfully
|
||||
}
|
||||
|
||||
|
||||
@@ -130,9 +130,13 @@ uint8_t OtaHttpRequestComponent::do_ota_() {
|
||||
App.feed_wdt();
|
||||
yield();
|
||||
|
||||
auto result = http_read_loop_result(bufsize_or_error, last_data_time, read_timeout);
|
||||
auto result = http_read_loop_result(bufsize_or_error, last_data_time, read_timeout, container->is_read_complete());
|
||||
if (result == HttpReadLoopResult::RETRY)
|
||||
continue;
|
||||
// Note: COMPLETE is currently unreachable since the loop condition checks bytes_read < content_length,
|
||||
// but this is defensive code in case chunked transfer encoding support is added for OTA in the future.
|
||||
if (result == HttpReadLoopResult::COMPLETE)
|
||||
break;
|
||||
if (result != HttpReadLoopResult::DATA) {
|
||||
if (result == HttpReadLoopResult::TIMEOUT) {
|
||||
ESP_LOGE(TAG, "Timeout reading data");
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
from esphome import automation
|
||||
from esphome.automation import Trigger
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import key_provider
|
||||
import esphome.config_validation as cv
|
||||
@@ -10,7 +13,10 @@ from esphome.const import (
|
||||
CONF_ON_TIMEOUT,
|
||||
CONF_SOURCE_ID,
|
||||
CONF_TIMEOUT,
|
||||
CONF_TRIGGER_ID,
|
||||
)
|
||||
from esphome.cpp_generator import MockObj, literal
|
||||
from esphome.types import TemplateArgsType
|
||||
|
||||
CODEOWNERS = ["@ssieb"]
|
||||
|
||||
@@ -32,22 +38,50 @@ KeyCollector = key_collector_ns.class_("KeyCollector", cg.Component)
|
||||
EnableAction = key_collector_ns.class_("EnableAction", automation.Action)
|
||||
DisableAction = key_collector_ns.class_("DisableAction", automation.Action)
|
||||
|
||||
X_TYPE = cg.std_string_ref.operator("const")
|
||||
|
||||
|
||||
@dataclass
|
||||
class Argument:
|
||||
type: MockObj
|
||||
name: str
|
||||
|
||||
|
||||
TRIGGER_TYPES = {
|
||||
CONF_ON_PROGRESS: [Argument(X_TYPE, "x"), Argument(cg.uint8, "start")],
|
||||
CONF_ON_RESULT: [
|
||||
Argument(X_TYPE, "x"),
|
||||
Argument(cg.uint8, "start"),
|
||||
Argument(cg.uint8, "end"),
|
||||
],
|
||||
CONF_ON_TIMEOUT: [Argument(X_TYPE, "x"), Argument(cg.uint8, "start")],
|
||||
}
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
cv.COMPONENT_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(KeyCollector),
|
||||
cv.Optional(CONF_SOURCE_ID): cv.use_id(key_provider.KeyProvider),
|
||||
cv.Optional(CONF_MIN_LENGTH): cv.int_,
|
||||
cv.Optional(CONF_MAX_LENGTH): cv.int_,
|
||||
cv.Optional(CONF_SOURCE_ID): cv.ensure_list(
|
||||
cv.use_id(key_provider.KeyProvider)
|
||||
),
|
||||
cv.Optional(CONF_MIN_LENGTH): cv.uint16_t,
|
||||
cv.Optional(CONF_MAX_LENGTH): cv.uint16_t,
|
||||
cv.Optional(CONF_START_KEYS): cv.string,
|
||||
cv.Optional(CONF_END_KEYS): cv.string,
|
||||
cv.Optional(CONF_END_KEY_REQUIRED): cv.boolean,
|
||||
cv.Optional(CONF_BACK_KEYS): cv.string,
|
||||
cv.Optional(CONF_CLEAR_KEYS): cv.string,
|
||||
cv.Optional(CONF_ALLOWED_KEYS): cv.string,
|
||||
cv.Optional(CONF_ON_PROGRESS): automation.validate_automation(single=True),
|
||||
cv.Optional(CONF_ON_RESULT): automation.validate_automation(single=True),
|
||||
cv.Optional(CONF_ON_TIMEOUT): automation.validate_automation(single=True),
|
||||
**{
|
||||
cv.Optional(trigger_type): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
|
||||
Trigger.template(*[arg.type for arg in args])
|
||||
),
|
||||
}
|
||||
)
|
||||
for trigger_type, args in TRIGGER_TYPES.items()
|
||||
},
|
||||
cv.Optional(CONF_TIMEOUT): cv.positive_time_period_milliseconds,
|
||||
cv.Optional(CONF_ENABLE_ON_BOOT, default=True): cv.boolean,
|
||||
}
|
||||
@@ -59,9 +93,9 @@ CONFIG_SCHEMA = cv.All(
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
if CONF_SOURCE_ID in config:
|
||||
source = await cg.get_variable(config[CONF_SOURCE_ID])
|
||||
cg.add(var.set_provider(source))
|
||||
for source_conf in config.get(CONF_SOURCE_ID, ()):
|
||||
source = await cg.get_variable(source_conf)
|
||||
cg.add(var.add_provider(source))
|
||||
if CONF_MIN_LENGTH in config:
|
||||
cg.add(var.set_min_length(config[CONF_MIN_LENGTH]))
|
||||
if CONF_MAX_LENGTH in config:
|
||||
@@ -78,26 +112,25 @@ async def to_code(config):
|
||||
cg.add(var.set_clear_keys(config[CONF_CLEAR_KEYS]))
|
||||
if CONF_ALLOWED_KEYS in config:
|
||||
cg.add(var.set_allowed_keys(config[CONF_ALLOWED_KEYS]))
|
||||
if CONF_ON_PROGRESS in config:
|
||||
await automation.build_automation(
|
||||
var.get_progress_trigger(),
|
||||
[(cg.std_string, "x"), (cg.uint8, "start")],
|
||||
config[CONF_ON_PROGRESS],
|
||||
)
|
||||
if CONF_ON_RESULT in config:
|
||||
await automation.build_automation(
|
||||
var.get_result_trigger(),
|
||||
[(cg.std_string, "x"), (cg.uint8, "start"), (cg.uint8, "end")],
|
||||
config[CONF_ON_RESULT],
|
||||
)
|
||||
if CONF_ON_TIMEOUT in config:
|
||||
await automation.build_automation(
|
||||
var.get_timeout_trigger(),
|
||||
[(cg.std_string, "x"), (cg.uint8, "start")],
|
||||
config[CONF_ON_TIMEOUT],
|
||||
)
|
||||
if CONF_TIMEOUT in config:
|
||||
cg.add(var.set_timeout(config[CONF_TIMEOUT]))
|
||||
|
||||
for trigger_name, args in TRIGGER_TYPES.items():
|
||||
arglist: TemplateArgsType = [(arg.type, arg.name) for arg in args]
|
||||
for conf in config.get(trigger_name, ()):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
|
||||
add_trig = getattr(
|
||||
var,
|
||||
f"add_on_{trigger_name.rsplit('_', maxsplit=1)[-1].lower()}_callback",
|
||||
)
|
||||
await automation.build_automation(
|
||||
trigger,
|
||||
arglist,
|
||||
conf,
|
||||
)
|
||||
lamb = trigger.trigger(*[literal(arg.name) for arg in args])
|
||||
cg.add(add_trig(await cg.process_lambda(lamb, arglist)))
|
||||
|
||||
if timeout := config.get(CONF_TIMEOUT):
|
||||
cg.add(var.set_timeout(timeout))
|
||||
cg.add(var.set_enabled(config[CONF_ENABLE_ON_BOOT]))
|
||||
|
||||
|
||||
|
||||
@@ -7,15 +7,10 @@ namespace key_collector {
|
||||
|
||||
static const char *const TAG = "key_collector";
|
||||
|
||||
KeyCollector::KeyCollector()
|
||||
: progress_trigger_(new Trigger<std::string, uint8_t>()),
|
||||
result_trigger_(new Trigger<std::string, uint8_t, uint8_t>()),
|
||||
timeout_trigger_(new Trigger<std::string, uint8_t>()) {}
|
||||
|
||||
void KeyCollector::loop() {
|
||||
if ((this->timeout_ == 0) || this->result_.empty() || (millis() - this->last_key_time_ < this->timeout_))
|
||||
return;
|
||||
this->timeout_trigger_->trigger(this->result_, this->start_key_);
|
||||
this->timeout_callbacks_.call(this->result_, this->start_key_);
|
||||
this->clear();
|
||||
}
|
||||
|
||||
@@ -43,64 +38,68 @@ void KeyCollector::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, " entry timeout: %0.1f", this->timeout_ / 1000.0);
|
||||
}
|
||||
|
||||
void KeyCollector::set_provider(key_provider::KeyProvider *provider) {
|
||||
provider->add_on_key_callback([this](uint8_t key) { this->key_pressed_(key); });
|
||||
void KeyCollector::add_provider(key_provider::KeyProvider *provider) {
|
||||
provider->add_on_key_callback([this](uint8_t key) { this->send_key(key); });
|
||||
}
|
||||
|
||||
void KeyCollector::set_enabled(bool enabled) {
|
||||
this->enabled_ = enabled;
|
||||
if (!enabled)
|
||||
if (!enabled) {
|
||||
this->clear(false);
|
||||
}
|
||||
}
|
||||
|
||||
void KeyCollector::clear(bool progress_update) {
|
||||
auto had_state = !this->result_.empty() || this->start_key_ != 0;
|
||||
this->result_.clear();
|
||||
this->start_key_ = 0;
|
||||
if (progress_update)
|
||||
this->progress_trigger_->trigger(this->result_, 0);
|
||||
if (progress_update && had_state) {
|
||||
this->progress_callbacks_.call(this->result_, 0);
|
||||
}
|
||||
this->disable_loop();
|
||||
}
|
||||
|
||||
void KeyCollector::send_key(uint8_t key) { this->key_pressed_(key); }
|
||||
|
||||
void KeyCollector::key_pressed_(uint8_t key) {
|
||||
void KeyCollector::send_key(uint8_t key) {
|
||||
if (!this->enabled_)
|
||||
return;
|
||||
this->last_key_time_ = millis();
|
||||
if (!this->start_keys_.empty() && !this->start_key_) {
|
||||
if (this->start_keys_.find(key) != std::string::npos) {
|
||||
this->start_key_ = key;
|
||||
this->progress_trigger_->trigger(this->result_, this->start_key_);
|
||||
this->progress_callbacks_.call(this->result_, this->start_key_);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (this->back_keys_.find(key) != std::string::npos) {
|
||||
if (!this->result_.empty()) {
|
||||
this->result_.pop_back();
|
||||
this->progress_trigger_->trigger(this->result_, this->start_key_);
|
||||
this->progress_callbacks_.call(this->result_, this->start_key_);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (this->clear_keys_.find(key) != std::string::npos) {
|
||||
if (!this->result_.empty())
|
||||
this->clear();
|
||||
this->clear();
|
||||
return;
|
||||
}
|
||||
if (this->end_keys_.find(key) != std::string::npos) {
|
||||
if ((this->min_length_ == 0) || (this->result_.size() >= this->min_length_)) {
|
||||
this->result_trigger_->trigger(this->result_, this->start_key_, key);
|
||||
this->result_callbacks_.call(this->result_, this->start_key_, key);
|
||||
this->clear();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!this->allowed_keys_.empty() && (this->allowed_keys_.find(key) == std::string::npos))
|
||||
if (!this->allowed_keys_.empty() && this->allowed_keys_.find(key) == std::string::npos)
|
||||
return;
|
||||
if ((this->max_length_ == 0) || (this->result_.size() < this->max_length_))
|
||||
if ((this->max_length_ == 0) || (this->result_.size() < this->max_length_)) {
|
||||
if (this->result_.empty())
|
||||
this->enable_loop();
|
||||
this->result_.push_back(key);
|
||||
}
|
||||
if ((this->max_length_ > 0) && (this->result_.size() == this->max_length_) && (!this->end_key_required_)) {
|
||||
this->result_trigger_->trigger(this->result_, this->start_key_, 0);
|
||||
this->result_callbacks_.call(this->result_, this->start_key_, 0);
|
||||
this->clear(false);
|
||||
}
|
||||
this->progress_trigger_->trigger(this->result_, this->start_key_);
|
||||
this->progress_callbacks_.call(this->result_, this->start_key_);
|
||||
}
|
||||
|
||||
} // namespace key_collector
|
||||
|
||||
@@ -3,27 +3,33 @@
|
||||
#include <utility>
|
||||
#include "esphome/components/key_provider/key_provider.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace key_collector {
|
||||
|
||||
class KeyCollector : public Component {
|
||||
public:
|
||||
KeyCollector();
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
void set_provider(key_provider::KeyProvider *provider);
|
||||
void set_min_length(uint32_t min_length) { this->min_length_ = min_length; };
|
||||
void set_max_length(uint32_t max_length) { this->max_length_ = max_length; };
|
||||
void add_provider(key_provider::KeyProvider *provider);
|
||||
void set_min_length(uint16_t min_length) { this->min_length_ = min_length; };
|
||||
void set_max_length(uint16_t max_length) { this->max_length_ = max_length; };
|
||||
void set_start_keys(std::string start_keys) { this->start_keys_ = std::move(start_keys); };
|
||||
void set_end_keys(std::string end_keys) { this->end_keys_ = std::move(end_keys); };
|
||||
void set_end_key_required(bool end_key_required) { this->end_key_required_ = end_key_required; };
|
||||
void set_back_keys(std::string back_keys) { this->back_keys_ = std::move(back_keys); };
|
||||
void set_clear_keys(std::string clear_keys) { this->clear_keys_ = std::move(clear_keys); };
|
||||
void set_allowed_keys(std::string allowed_keys) { this->allowed_keys_ = std::move(allowed_keys); };
|
||||
Trigger<std::string, uint8_t> *get_progress_trigger() const { return this->progress_trigger_; };
|
||||
Trigger<std::string, uint8_t, uint8_t> *get_result_trigger() const { return this->result_trigger_; };
|
||||
Trigger<std::string, uint8_t> *get_timeout_trigger() const { return this->timeout_trigger_; };
|
||||
void add_on_progress_callback(std::function<void(const std::string &, uint8_t)> &&callback) {
|
||||
this->progress_callbacks_.add(std::move(callback));
|
||||
}
|
||||
void add_on_result_callback(std::function<void(const std::string &, uint8_t, uint8_t)> &&callback) {
|
||||
this->result_callbacks_.add(std::move(callback));
|
||||
}
|
||||
void add_on_timeout_callback(std::function<void(const std::string &, uint8_t)> &&callback) {
|
||||
this->timeout_callbacks_.add(std::move(callback));
|
||||
}
|
||||
void set_timeout(int timeout) { this->timeout_ = timeout; };
|
||||
void set_enabled(bool enabled);
|
||||
|
||||
@@ -31,10 +37,8 @@ class KeyCollector : public Component {
|
||||
void send_key(uint8_t key);
|
||||
|
||||
protected:
|
||||
void key_pressed_(uint8_t key);
|
||||
|
||||
uint32_t min_length_{0};
|
||||
uint32_t max_length_{0};
|
||||
uint16_t min_length_{0};
|
||||
uint16_t max_length_{0};
|
||||
std::string start_keys_;
|
||||
std::string end_keys_;
|
||||
bool end_key_required_{false};
|
||||
@@ -43,12 +47,12 @@ class KeyCollector : public Component {
|
||||
std::string allowed_keys_;
|
||||
std::string result_;
|
||||
uint8_t start_key_{0};
|
||||
Trigger<std::string, uint8_t> *progress_trigger_;
|
||||
Trigger<std::string, uint8_t, uint8_t> *result_trigger_;
|
||||
Trigger<std::string, uint8_t> *timeout_trigger_;
|
||||
uint32_t last_key_time_;
|
||||
LazyCallbackManager<void(const std::string &, uint8_t)> progress_callbacks_;
|
||||
LazyCallbackManager<void(const std::string &, uint8_t, uint8_t)> result_callbacks_;
|
||||
LazyCallbackManager<void(const std::string &, uint8_t)> timeout_callbacks_;
|
||||
uint32_t last_key_time_{};
|
||||
uint32_t timeout_{0};
|
||||
bool enabled_;
|
||||
bool enabled_{};
|
||||
};
|
||||
|
||||
template<typename... Ts> class EnableAction : public Action<Ts...>, public Parented<KeyCollector> {
|
||||
|
||||
28
esphome/components/key_collector/text_sensor/__init__.py
Normal file
28
esphome/components/key_collector/text_sensor/__init__.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import text_sensor
|
||||
from esphome.components.text_sensor import TextSensor
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID
|
||||
from esphome.cpp_generator import literal
|
||||
from esphome.types import TemplateArgsType
|
||||
|
||||
from .. import CONF_ON_RESULT, CONF_SOURCE_ID, TRIGGER_TYPES, KeyCollector
|
||||
|
||||
CONFIG_SCHEMA = text_sensor.text_sensor_schema(TextSensor).extend(
|
||||
{
|
||||
cv.GenerateID(CONF_SOURCE_ID): cv.use_id(KeyCollector),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
parent = await cg.get_variable(config[CONF_SOURCE_ID])
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await text_sensor.register_text_sensor(var, config)
|
||||
args = TRIGGER_TYPES[CONF_ON_RESULT]
|
||||
arglist: TemplateArgsType = [(arg.type, arg.name) for arg in args]
|
||||
cg.add(
|
||||
parent.add_on_result_callback(
|
||||
await cg.process_lambda(var.publish_state(literal(args[0].name)), arglist)
|
||||
)
|
||||
)
|
||||
@@ -189,10 +189,11 @@ class LibreTinyPreferences : public ESPPreferences {
|
||||
}
|
||||
};
|
||||
|
||||
static LibreTinyPreferences s_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
void setup_preferences() {
|
||||
auto *prefs = new LibreTinyPreferences(); // NOLINT(cppcoreguidelines-owning-memory)
|
||||
prefs->open();
|
||||
global_preferences = prefs;
|
||||
s_preferences.open();
|
||||
global_preferences = &s_preferences;
|
||||
}
|
||||
|
||||
} // namespace libretiny
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/controller_registry.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/progmem.h"
|
||||
|
||||
namespace esphome::lock {
|
||||
|
||||
@@ -84,21 +85,21 @@ LockCall &LockCall::set_state(optional<LockState> state) {
|
||||
this->state_ = state;
|
||||
return *this;
|
||||
}
|
||||
LockCall &LockCall::set_state(const std::string &state) {
|
||||
if (str_equals_case_insensitive(state, "LOCKED")) {
|
||||
LockCall &LockCall::set_state(const char *state) {
|
||||
if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("LOCKED")) == 0) {
|
||||
this->set_state(LOCK_STATE_LOCKED);
|
||||
} else if (str_equals_case_insensitive(state, "UNLOCKED")) {
|
||||
} else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("UNLOCKED")) == 0) {
|
||||
this->set_state(LOCK_STATE_UNLOCKED);
|
||||
} else if (str_equals_case_insensitive(state, "JAMMED")) {
|
||||
} else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("JAMMED")) == 0) {
|
||||
this->set_state(LOCK_STATE_JAMMED);
|
||||
} else if (str_equals_case_insensitive(state, "LOCKING")) {
|
||||
} else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("LOCKING")) == 0) {
|
||||
this->set_state(LOCK_STATE_LOCKING);
|
||||
} else if (str_equals_case_insensitive(state, "UNLOCKING")) {
|
||||
} else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("UNLOCKING")) == 0) {
|
||||
this->set_state(LOCK_STATE_UNLOCKING);
|
||||
} else if (str_equals_case_insensitive(state, "NONE")) {
|
||||
} else if (ESPHOME_strcasecmp_P(state, ESPHOME_PSTR("NONE")) == 0) {
|
||||
this->set_state(LOCK_STATE_NONE);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "'%s' - Unrecognized state %s", this->parent_->get_name().c_str(), state.c_str());
|
||||
ESP_LOGW(TAG, "'%s' - Unrecognized state %s", this->parent_->get_name().c_str(), state);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -83,7 +83,8 @@ class LockCall {
|
||||
/// Set the state of the lock device.
|
||||
LockCall &set_state(optional<LockState> state);
|
||||
/// Set the state of the lock device based on a string.
|
||||
LockCall &set_state(const std::string &state);
|
||||
LockCall &set_state(const char *state);
|
||||
LockCall &set_state(const std::string &state) { return this->set_state(state.c_str()); }
|
||||
|
||||
void perform();
|
||||
|
||||
|
||||
@@ -128,22 +128,7 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch
|
||||
// Note: USE_STORE_LOG_STR_IN_FLASH is only defined for ESP8266.
|
||||
//
|
||||
// This function handles format strings stored in flash memory (PROGMEM) to save RAM.
|
||||
// The buffer is used in a special way to avoid allocating extra memory:
|
||||
//
|
||||
// Memory layout during execution:
|
||||
// Step 1: Copy format string from flash to buffer
|
||||
// tx_buffer_: [format_string][null][.....................]
|
||||
// tx_buffer_at_: ------------------^
|
||||
// msg_start: saved here -----------^
|
||||
//
|
||||
// Step 2: format_log_to_buffer_with_terminator_ reads format string from beginning
|
||||
// and writes formatted output starting at msg_start position
|
||||
// tx_buffer_: [format_string][null][formatted_message][null]
|
||||
// tx_buffer_at_: -------------------------------------^
|
||||
//
|
||||
// Step 3: Output the formatted message (starting at msg_start)
|
||||
// write_msg_ and callbacks receive: this->tx_buffer_ + msg_start
|
||||
// which points to: [formatted_message][null]
|
||||
// Uses vsnprintf_P to read the format string directly from flash without copying to RAM.
|
||||
//
|
||||
void Logger::log_vprintf_(uint8_t level, const char *tag, int line, const __FlashStringHelper *format,
|
||||
va_list args) { // NOLINT
|
||||
@@ -153,35 +138,25 @@ void Logger::log_vprintf_(uint8_t level, const char *tag, int line, const __Flas
|
||||
RecursionGuard guard(global_recursion_guard_);
|
||||
this->tx_buffer_at_ = 0;
|
||||
|
||||
// Copy format string from progmem
|
||||
auto *format_pgm_p = reinterpret_cast<const uint8_t *>(format);
|
||||
char ch = '.';
|
||||
while (this->tx_buffer_at_ < this->tx_buffer_size_ && ch != '\0') {
|
||||
this->tx_buffer_[this->tx_buffer_at_++] = ch = (char) progmem_read_byte(format_pgm_p++);
|
||||
}
|
||||
// Write header, format body directly from flash, and write footer
|
||||
this->write_header_to_buffer_(level, tag, line, nullptr, this->tx_buffer_, &this->tx_buffer_at_,
|
||||
this->tx_buffer_size_);
|
||||
this->format_body_to_buffer_P_(this->tx_buffer_, &this->tx_buffer_at_, this->tx_buffer_size_,
|
||||
reinterpret_cast<PGM_P>(format), args);
|
||||
this->write_footer_to_buffer_(this->tx_buffer_, &this->tx_buffer_at_, this->tx_buffer_size_);
|
||||
|
||||
// Buffer full from copying format - RAII guard handles cleanup on return
|
||||
if (this->tx_buffer_at_ >= this->tx_buffer_size_) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Save the offset before calling format_log_to_buffer_with_terminator_
|
||||
// since it will increment tx_buffer_at_ to the end of the formatted string
|
||||
uint16_t msg_start = this->tx_buffer_at_;
|
||||
this->format_log_to_buffer_with_terminator_(level, tag, line, this->tx_buffer_, args, this->tx_buffer_,
|
||||
&this->tx_buffer_at_, this->tx_buffer_size_);
|
||||
|
||||
uint16_t msg_length =
|
||||
this->tx_buffer_at_ - msg_start; // Don't subtract 1 - tx_buffer_at_ is already at the null terminator position
|
||||
// Ensure null termination
|
||||
uint16_t null_pos = this->tx_buffer_at_ >= this->tx_buffer_size_ ? this->tx_buffer_size_ - 1 : this->tx_buffer_at_;
|
||||
this->tx_buffer_[null_pos] = '\0';
|
||||
|
||||
// Listeners get message first (before console write)
|
||||
#ifdef USE_LOG_LISTENERS
|
||||
for (auto *listener : this->log_listeners_)
|
||||
listener->on_log(level, tag, this->tx_buffer_ + msg_start, msg_length);
|
||||
listener->on_log(level, tag, this->tx_buffer_, this->tx_buffer_at_);
|
||||
#endif
|
||||
|
||||
// Write to console starting at the msg_start
|
||||
this->write_tx_buffer_to_console_(msg_start, &msg_length);
|
||||
// Write to console
|
||||
this->write_tx_buffer_to_console_();
|
||||
}
|
||||
#endif // USE_STORE_LOG_STR_IN_FLASH
|
||||
|
||||
|
||||
@@ -597,31 +597,40 @@ class Logger : public Component {
|
||||
*buffer_at = pos;
|
||||
}
|
||||
|
||||
// Helper to process vsnprintf return value and strip trailing newlines.
|
||||
// Updates buffer_at with the formatted length, handling truncation:
|
||||
// - When vsnprintf truncates (ret >= remaining), it writes (remaining - 1) chars + null terminator
|
||||
// - When it doesn't truncate (ret < remaining), it writes ret chars + null terminator
|
||||
__attribute__((always_inline)) static inline void process_vsnprintf_result(const char *buffer, uint16_t *buffer_at,
|
||||
uint16_t remaining, int ret) {
|
||||
if (ret < 0)
|
||||
return; // Encoding error, do not increment buffer_at
|
||||
*buffer_at += (ret >= remaining) ? (remaining - 1) : static_cast<uint16_t>(ret);
|
||||
// Remove all trailing newlines right after formatting
|
||||
while (*buffer_at > 0 && buffer[*buffer_at - 1] == '\n')
|
||||
(*buffer_at)--;
|
||||
}
|
||||
|
||||
inline void HOT format_body_to_buffer_(char *buffer, uint16_t *buffer_at, uint16_t buffer_size, const char *format,
|
||||
va_list args) {
|
||||
// Get remaining capacity in the buffer
|
||||
// Check remaining capacity in the buffer
|
||||
if (*buffer_at >= buffer_size)
|
||||
return;
|
||||
const uint16_t remaining = buffer_size - *buffer_at;
|
||||
|
||||
const int ret = vsnprintf(buffer + *buffer_at, remaining, format, args);
|
||||
|
||||
if (ret < 0) {
|
||||
return; // Encoding error, do not increment buffer_at
|
||||
}
|
||||
|
||||
// Update buffer_at with the formatted length (handle truncation)
|
||||
// When vsnprintf truncates (ret >= remaining), it writes (remaining - 1) chars + null terminator
|
||||
// When it doesn't truncate (ret < remaining), it writes ret chars + null terminator
|
||||
uint16_t formatted_len = (ret >= remaining) ? (remaining - 1) : ret;
|
||||
*buffer_at += formatted_len;
|
||||
|
||||
// Remove all trailing newlines right after formatting
|
||||
while (*buffer_at > 0 && buffer[*buffer_at - 1] == '\n') {
|
||||
(*buffer_at)--;
|
||||
}
|
||||
process_vsnprintf_result(buffer, buffer_at, remaining, vsnprintf(buffer + *buffer_at, remaining, format, args));
|
||||
}
|
||||
|
||||
#ifdef USE_STORE_LOG_STR_IN_FLASH
|
||||
// ESP8266 variant that reads format string directly from flash using vsnprintf_P
|
||||
inline void HOT format_body_to_buffer_P_(char *buffer, uint16_t *buffer_at, uint16_t buffer_size, PGM_P format,
|
||||
va_list args) {
|
||||
if (*buffer_at >= buffer_size)
|
||||
return;
|
||||
const uint16_t remaining = buffer_size - *buffer_at;
|
||||
process_vsnprintf_result(buffer, buffer_at, remaining, vsnprintf_P(buffer + *buffer_at, remaining, format, args));
|
||||
}
|
||||
#endif
|
||||
|
||||
inline void HOT write_footer_to_buffer_(char *buffer, uint16_t *buffer_at, uint16_t buffer_size) {
|
||||
static constexpr uint16_t RESET_COLOR_LEN = sizeof(ESPHOME_LOG_RESET_COLOR) - 1;
|
||||
this->write_body_to_buffer_(ESPHOME_LOG_RESET_COLOR, RESET_COLOR_LEN, buffer, buffer_at, buffer_size);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/controller_registry.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/progmem.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace media_player {
|
||||
@@ -107,25 +108,25 @@ MediaPlayerCall &MediaPlayerCall::set_command(optional<MediaPlayerCommand> comma
|
||||
this->command_ = command;
|
||||
return *this;
|
||||
}
|
||||
MediaPlayerCall &MediaPlayerCall::set_command(const std::string &command) {
|
||||
if (str_equals_case_insensitive(command, "PLAY")) {
|
||||
MediaPlayerCall &MediaPlayerCall::set_command(const char *command) {
|
||||
if (ESPHOME_strcasecmp_P(command, ESPHOME_PSTR("PLAY")) == 0) {
|
||||
this->set_command(MEDIA_PLAYER_COMMAND_PLAY);
|
||||
} else if (str_equals_case_insensitive(command, "PAUSE")) {
|
||||
} else if (ESPHOME_strcasecmp_P(command, ESPHOME_PSTR("PAUSE")) == 0) {
|
||||
this->set_command(MEDIA_PLAYER_COMMAND_PAUSE);
|
||||
} else if (str_equals_case_insensitive(command, "STOP")) {
|
||||
} else if (ESPHOME_strcasecmp_P(command, ESPHOME_PSTR("STOP")) == 0) {
|
||||
this->set_command(MEDIA_PLAYER_COMMAND_STOP);
|
||||
} else if (str_equals_case_insensitive(command, "MUTE")) {
|
||||
} else if (ESPHOME_strcasecmp_P(command, ESPHOME_PSTR("MUTE")) == 0) {
|
||||
this->set_command(MEDIA_PLAYER_COMMAND_MUTE);
|
||||
} else if (str_equals_case_insensitive(command, "UNMUTE")) {
|
||||
} else if (ESPHOME_strcasecmp_P(command, ESPHOME_PSTR("UNMUTE")) == 0) {
|
||||
this->set_command(MEDIA_PLAYER_COMMAND_UNMUTE);
|
||||
} else if (str_equals_case_insensitive(command, "TOGGLE")) {
|
||||
} else if (ESPHOME_strcasecmp_P(command, ESPHOME_PSTR("TOGGLE")) == 0) {
|
||||
this->set_command(MEDIA_PLAYER_COMMAND_TOGGLE);
|
||||
} else if (str_equals_case_insensitive(command, "TURN_ON")) {
|
||||
} else if (ESPHOME_strcasecmp_P(command, ESPHOME_PSTR("TURN_ON")) == 0) {
|
||||
this->set_command(MEDIA_PLAYER_COMMAND_TURN_ON);
|
||||
} else if (str_equals_case_insensitive(command, "TURN_OFF")) {
|
||||
} else if (ESPHOME_strcasecmp_P(command, ESPHOME_PSTR("TURN_OFF")) == 0) {
|
||||
this->set_command(MEDIA_PLAYER_COMMAND_TURN_OFF);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "'%s' - Unrecognized command %s", this->parent_->get_name().c_str(), command.c_str());
|
||||
ESP_LOGW(TAG, "'%s' - Unrecognized command %s", this->parent_->get_name().c_str(), command);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -114,7 +114,8 @@ class MediaPlayerCall {
|
||||
|
||||
MediaPlayerCall &set_command(MediaPlayerCommand command);
|
||||
MediaPlayerCall &set_command(optional<MediaPlayerCommand> command);
|
||||
MediaPlayerCall &set_command(const std::string &command);
|
||||
MediaPlayerCall &set_command(const char *command);
|
||||
MediaPlayerCall &set_command(const std::string &command) { return this->set_command(command.c_str()); }
|
||||
|
||||
MediaPlayerCall &set_media_url(const std::string &url);
|
||||
|
||||
|
||||
@@ -94,3 +94,29 @@ DriverChip(
|
||||
(0x29, 0x00),
|
||||
],
|
||||
)
|
||||
|
||||
DriverChip(
|
||||
"WAVESHARE-ESP32-P4-WIFI6-TOUCH-LCD-7B",
|
||||
height=600,
|
||||
width=1024,
|
||||
hsync_back_porch=160,
|
||||
hsync_pulse_width=10,
|
||||
hsync_front_porch=160,
|
||||
vsync_back_porch=23,
|
||||
vsync_pulse_width=1,
|
||||
vsync_front_porch=12,
|
||||
pclk_frequency="52MHz",
|
||||
lane_bit_rate="900Mbps",
|
||||
no_transform=True,
|
||||
color_order="RGB",
|
||||
initsequence=[
|
||||
(0x80, 0x8B),
|
||||
(0x81, 0x78),
|
||||
(0x82, 0x84),
|
||||
(0x83, 0x88),
|
||||
(0x84, 0xA8),
|
||||
(0x85, 0xE3),
|
||||
(0x86, 0x88),
|
||||
(0xB2, 0x10),
|
||||
],
|
||||
)
|
||||
|
||||
@@ -4,8 +4,10 @@ from typing import Any
|
||||
from esphome import automation, pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import sensor
|
||||
from esphome.components.esp32 import include_builtin_idf_component
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID, CONF_TRIGGER_ID, PLATFORM_ESP32, PLATFORM_ESP8266
|
||||
from esphome.core import CORE
|
||||
|
||||
from . import const, generate, schema, validate
|
||||
|
||||
@@ -83,6 +85,12 @@ CONFIG_SCHEMA = cv.All(
|
||||
|
||||
|
||||
async def to_code(config: dict[str, Any]) -> None:
|
||||
if CORE.is_esp32:
|
||||
# Re-enable ESP-IDF's legacy driver component (excluded by default to save compile time)
|
||||
# Provides driver/timer.h header for hardware timer API
|
||||
# TODO: Remove this once opentherm migrates to GPTimer API (driver/gptimer.h)
|
||||
include_builtin_idf_component("driver")
|
||||
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include "opentherm.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include <cinttypes>
|
||||
// TODO: Migrate from legacy timer API (driver/timer.h) to GPTimer API (driver/gptimer.h)
|
||||
// The legacy timer API is deprecated in ESP-IDF 5.x. See opentherm.h for details.
|
||||
#ifdef USE_ESP32
|
||||
#include "driver/timer.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
@@ -12,6 +12,10 @@
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
// TODO: Migrate from legacy timer API (driver/timer.h) to GPTimer API (driver/gptimer.h)
|
||||
// The legacy timer API is deprecated in ESP-IDF 5.x. Migration would allow removing the
|
||||
// "driver" IDF component dependency. See:
|
||||
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/migration-guides/release-5.x/5.0/peripherals.html#id4
|
||||
#ifdef USE_ESP32
|
||||
#include "driver/timer.h"
|
||||
#endif
|
||||
|
||||
@@ -4,6 +4,7 @@ from esphome.components.esp32 import (
|
||||
VARIANT_ESP32C6,
|
||||
VARIANT_ESP32H2,
|
||||
add_idf_sdkconfig_option,
|
||||
include_builtin_idf_component,
|
||||
only_on_variant,
|
||||
require_vfs_select,
|
||||
)
|
||||
@@ -172,6 +173,9 @@ FINAL_VALIDATE_SCHEMA = _final_validate
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
# Re-enable openthread IDF component (excluded by default)
|
||||
include_builtin_idf_component("openthread")
|
||||
|
||||
cg.add_define("USE_OPENTHREAD")
|
||||
|
||||
# OpenThread SRP needs access to mDNS services after setup
|
||||
|
||||
@@ -18,11 +18,12 @@ namespace rp2040 {
|
||||
|
||||
static const char *const TAG = "rp2040.preferences";
|
||||
|
||||
static bool s_prevent_write = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
static uint8_t *s_flash_storage = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
static bool s_flash_dirty = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
static constexpr uint32_t RP2040_FLASH_STORAGE_SIZE = 512;
|
||||
|
||||
static const uint32_t RP2040_FLASH_STORAGE_SIZE = 512;
|
||||
static bool s_prevent_write = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
static uint8_t
|
||||
s_flash_storage[RP2040_FLASH_STORAGE_SIZE]; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
static bool s_flash_dirty = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
// Stack buffer size for preferences - covers virtually all real-world preferences without heap allocation
|
||||
static constexpr size_t PREF_BUFFER_SIZE = 64;
|
||||
@@ -91,7 +92,6 @@ class RP2040Preferences : public ESPPreferences {
|
||||
|
||||
RP2040Preferences() : eeprom_sector_(&_EEPROM_start) {}
|
||||
void setup() {
|
||||
s_flash_storage = new uint8_t[RP2040_FLASH_STORAGE_SIZE]; // NOLINT
|
||||
ESP_LOGVV(TAG, "Loading preferences from flash");
|
||||
memcpy(s_flash_storage, this->eeprom_sector_, RP2040_FLASH_STORAGE_SIZE);
|
||||
}
|
||||
@@ -149,10 +149,11 @@ class RP2040Preferences : public ESPPreferences {
|
||||
uint8_t *eeprom_sector_;
|
||||
};
|
||||
|
||||
static RP2040Preferences s_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
void setup_preferences() {
|
||||
auto *prefs = new RP2040Preferences(); // NOLINT(cppcoreguidelines-owning-memory)
|
||||
prefs->setup();
|
||||
global_preferences = prefs;
|
||||
s_preferences.setup();
|
||||
global_preferences = &s_preferences;
|
||||
}
|
||||
void preferences_prevent_write(bool prevent) { s_prevent_write = prevent; }
|
||||
|
||||
|
||||
@@ -157,8 +157,14 @@ def _read_audio_file_and_type(file_config):
|
||||
|
||||
import puremagic
|
||||
|
||||
file_type: str = puremagic.from_string(data)
|
||||
file_type = file_type.removeprefix(".")
|
||||
try:
|
||||
file_type: str = puremagic.from_string(data)
|
||||
file_type = file_type.removeprefix(".")
|
||||
except puremagic.PureError as e:
|
||||
raise cv.Invalid(
|
||||
f"Unable to determine audio file type of '{path}'. "
|
||||
f"Try re-encoding the file into a supported format. Details: {e}"
|
||||
)
|
||||
|
||||
media_file_type = audio.AUDIO_FILE_TYPE_ENUM["NONE"]
|
||||
if file_type in ("wav"):
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/controller_registry.h"
|
||||
#include "esphome/core/progmem.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
@@ -22,23 +23,23 @@ WaterHeaterCall &WaterHeaterCall::set_mode(WaterHeaterMode mode) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
WaterHeaterCall &WaterHeaterCall::set_mode(const std::string &mode) {
|
||||
if (str_equals_case_insensitive(mode, "OFF")) {
|
||||
WaterHeaterCall &WaterHeaterCall::set_mode(const char *mode) {
|
||||
if (ESPHOME_strcasecmp_P(mode, ESPHOME_PSTR("OFF")) == 0) {
|
||||
this->set_mode(WATER_HEATER_MODE_OFF);
|
||||
} else if (str_equals_case_insensitive(mode, "ECO")) {
|
||||
} else if (ESPHOME_strcasecmp_P(mode, ESPHOME_PSTR("ECO")) == 0) {
|
||||
this->set_mode(WATER_HEATER_MODE_ECO);
|
||||
} else if (str_equals_case_insensitive(mode, "ELECTRIC")) {
|
||||
} else if (ESPHOME_strcasecmp_P(mode, ESPHOME_PSTR("ELECTRIC")) == 0) {
|
||||
this->set_mode(WATER_HEATER_MODE_ELECTRIC);
|
||||
} else if (str_equals_case_insensitive(mode, "PERFORMANCE")) {
|
||||
} else if (ESPHOME_strcasecmp_P(mode, ESPHOME_PSTR("PERFORMANCE")) == 0) {
|
||||
this->set_mode(WATER_HEATER_MODE_PERFORMANCE);
|
||||
} else if (str_equals_case_insensitive(mode, "HIGH_DEMAND")) {
|
||||
} else if (ESPHOME_strcasecmp_P(mode, ESPHOME_PSTR("HIGH_DEMAND")) == 0) {
|
||||
this->set_mode(WATER_HEATER_MODE_HIGH_DEMAND);
|
||||
} else if (str_equals_case_insensitive(mode, "HEAT_PUMP")) {
|
||||
} else if (ESPHOME_strcasecmp_P(mode, ESPHOME_PSTR("HEAT_PUMP")) == 0) {
|
||||
this->set_mode(WATER_HEATER_MODE_HEAT_PUMP);
|
||||
} else if (str_equals_case_insensitive(mode, "GAS")) {
|
||||
} else if (ESPHOME_strcasecmp_P(mode, ESPHOME_PSTR("GAS")) == 0) {
|
||||
this->set_mode(WATER_HEATER_MODE_GAS);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "'%s' - Unrecognized mode %s", this->parent_->get_name().c_str(), mode.c_str());
|
||||
ESP_LOGW(TAG, "'%s' - Unrecognized mode %s", this->parent_->get_name().c_str(), mode);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -75,7 +75,8 @@ class WaterHeaterCall {
|
||||
WaterHeaterCall(WaterHeater *parent);
|
||||
|
||||
WaterHeaterCall &set_mode(WaterHeaterMode mode);
|
||||
WaterHeaterCall &set_mode(const std::string &mode);
|
||||
WaterHeaterCall &set_mode(const char *mode);
|
||||
WaterHeaterCall &set_mode(const std::string &mode) { return this->set_mode(mode.c_str()); }
|
||||
WaterHeaterCall &set_target_temperature(float temperature);
|
||||
WaterHeaterCall &set_target_temperature_low(float temperature);
|
||||
WaterHeaterCall &set_target_temperature_high(float temperature);
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
#include "StreamString.h"
|
||||
#endif
|
||||
|
||||
#include <chrono>
|
||||
#include <cinttypes>
|
||||
#include <cstdlib>
|
||||
|
||||
#ifdef USE_LIGHT
|
||||
@@ -365,7 +367,13 @@ void WebServer::set_css_include(const char *css_include) { this->css_include_ =
|
||||
void WebServer::set_js_include(const char *js_include) { this->js_include_ = js_include; }
|
||||
#endif
|
||||
|
||||
std::string WebServer::get_config_json() {
|
||||
/// Get uptime in milliseconds using std::chrono::steady_clock (64-bit, no rollover)
|
||||
static int64_t get_uptime_ms() {
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
|
||||
json::SerializationBuffer<> WebServer::get_config_json() {
|
||||
json::JsonBuilder builder;
|
||||
JsonObject root = builder.root();
|
||||
|
||||
@@ -380,6 +388,7 @@ std::string WebServer::get_config_json() {
|
||||
#endif
|
||||
root[ESPHOME_F("log")] = this->expose_log_;
|
||||
root[ESPHOME_F("lang")] = "en";
|
||||
root[ESPHOME_F("uptime")] = get_uptime_ms();
|
||||
|
||||
return builder.serialize();
|
||||
}
|
||||
@@ -403,7 +412,11 @@ void WebServer::setup() {
|
||||
|
||||
// doesn't need defer functionality - if the queue is full, the client JS knows it's alive because it's clearly
|
||||
// getting a lot of events
|
||||
this->set_interval(10000, [this]() { this->events_.try_send_nodefer("", "ping", millis(), 30000); });
|
||||
this->set_interval(10000, [this]() {
|
||||
char buf[32];
|
||||
buf_append_printf(buf, sizeof(buf), 0, "{\"uptime\":%" PRId64 "}", get_uptime_ms());
|
||||
this->events_.try_send_nodefer(buf, "ping", millis(), 30000);
|
||||
});
|
||||
}
|
||||
void WebServer::loop() { this->events_.loop(); }
|
||||
|
||||
|
||||
@@ -585,11 +585,13 @@ async def to_code(config):
|
||||
await cg.past_safe_mode()
|
||||
|
||||
if on_connect_config := config.get(CONF_ON_CONNECT):
|
||||
cg.add_define("USE_WIFI_CONNECT_TRIGGER")
|
||||
await automation.build_automation(
|
||||
var.get_connect_trigger(), [], on_connect_config
|
||||
)
|
||||
|
||||
if on_disconnect_config := config.get(CONF_ON_DISCONNECT):
|
||||
cg.add_define("USE_WIFI_DISCONNECT_TRIGGER")
|
||||
await automation.build_automation(
|
||||
var.get_disconnect_trigger(), [], on_disconnect_config
|
||||
)
|
||||
|
||||
@@ -48,7 +48,7 @@ template<typename... Ts> class WiFiConfigureAction : public Action<Ts...>, publi
|
||||
char ssid_buf[SSID_BUFFER_SIZE];
|
||||
if (strcmp(global_wifi_component->wifi_ssid_to(ssid_buf), ssid.c_str()) == 0) {
|
||||
// Callback to notify the user that the connection was successful
|
||||
this->connect_trigger_->trigger();
|
||||
this->connect_trigger_.trigger();
|
||||
return;
|
||||
}
|
||||
// Create a new WiFiAP object with the new SSID and password
|
||||
@@ -79,13 +79,13 @@ template<typename... Ts> class WiFiConfigureAction : public Action<Ts...>, publi
|
||||
// Start a timeout for the fallback if the connection to the old AP fails
|
||||
this->set_timeout("wifi-fallback-timeout", this->connection_timeout_.value(x...), [this]() {
|
||||
this->connecting_ = false;
|
||||
this->error_trigger_->trigger();
|
||||
this->error_trigger_.trigger();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Trigger<> *get_connect_trigger() const { return this->connect_trigger_; }
|
||||
Trigger<> *get_error_trigger() const { return this->error_trigger_; }
|
||||
Trigger<> *get_connect_trigger() { return &this->connect_trigger_; }
|
||||
Trigger<> *get_error_trigger() { return &this->error_trigger_; }
|
||||
|
||||
void loop() override {
|
||||
if (!this->connecting_)
|
||||
@@ -98,10 +98,10 @@ template<typename... Ts> class WiFiConfigureAction : public Action<Ts...>, publi
|
||||
char ssid_buf[SSID_BUFFER_SIZE];
|
||||
if (strcmp(global_wifi_component->wifi_ssid_to(ssid_buf), this->new_sta_.get_ssid().c_str()) == 0) {
|
||||
// Callback to notify the user that the connection was successful
|
||||
this->connect_trigger_->trigger();
|
||||
this->connect_trigger_.trigger();
|
||||
} else {
|
||||
// Callback to notify the user that the connection failed
|
||||
this->error_trigger_->trigger();
|
||||
this->error_trigger_.trigger();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,8 +110,8 @@ template<typename... Ts> class WiFiConfigureAction : public Action<Ts...>, publi
|
||||
bool connecting_{false};
|
||||
WiFiAP new_sta_;
|
||||
WiFiAP old_sta_;
|
||||
Trigger<> *connect_trigger_{new Trigger<>()};
|
||||
Trigger<> *error_trigger_{new Trigger<>()};
|
||||
Trigger<> connect_trigger_;
|
||||
Trigger<> error_trigger_;
|
||||
};
|
||||
|
||||
} // namespace esphome::wifi
|
||||
|
||||
@@ -651,14 +651,21 @@ void WiFiComponent::loop() {
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
|
||||
if (this->has_sta()) {
|
||||
#if defined(USE_WIFI_CONNECT_TRIGGER) || defined(USE_WIFI_DISCONNECT_TRIGGER)
|
||||
if (this->is_connected() != this->handled_connected_state_) {
|
||||
#ifdef USE_WIFI_DISCONNECT_TRIGGER
|
||||
if (this->handled_connected_state_) {
|
||||
this->disconnect_trigger_->trigger();
|
||||
} else {
|
||||
this->connect_trigger_->trigger();
|
||||
this->disconnect_trigger_.trigger();
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_WIFI_CONNECT_TRIGGER
|
||||
if (!this->handled_connected_state_) {
|
||||
this->connect_trigger_.trigger();
|
||||
}
|
||||
#endif
|
||||
this->handled_connected_state_ = this->is_connected();
|
||||
}
|
||||
#endif // USE_WIFI_CONNECT_TRIGGER || USE_WIFI_DISCONNECT_TRIGGER
|
||||
|
||||
switch (this->state_) {
|
||||
case WIFI_COMPONENT_STATE_COOLDOWN: {
|
||||
|
||||
@@ -454,8 +454,12 @@ class WiFiComponent : public Component {
|
||||
void set_keep_scan_results(bool keep_scan_results) { this->keep_scan_results_ = keep_scan_results; }
|
||||
void set_post_connect_roaming(bool enabled) { this->post_connect_roaming_ = enabled; }
|
||||
|
||||
Trigger<> *get_connect_trigger() const { return this->connect_trigger_; };
|
||||
Trigger<> *get_disconnect_trigger() const { return this->disconnect_trigger_; };
|
||||
#ifdef USE_WIFI_CONNECT_TRIGGER
|
||||
Trigger<> *get_connect_trigger() { return &this->connect_trigger_; }
|
||||
#endif
|
||||
#ifdef USE_WIFI_DISCONNECT_TRIGGER
|
||||
Trigger<> *get_disconnect_trigger() { return &this->disconnect_trigger_; }
|
||||
#endif
|
||||
|
||||
int32_t get_wifi_channel();
|
||||
|
||||
@@ -706,7 +710,9 @@ class WiFiComponent : public Component {
|
||||
|
||||
// Group all boolean values together
|
||||
bool has_ap_{false};
|
||||
#if defined(USE_WIFI_CONNECT_TRIGGER) || defined(USE_WIFI_DISCONNECT_TRIGGER)
|
||||
bool handled_connected_state_{false};
|
||||
#endif
|
||||
bool error_from_callback_{false};
|
||||
bool scan_done_{false};
|
||||
bool ap_setup_{false};
|
||||
@@ -733,9 +739,12 @@ class WiFiComponent : public Component {
|
||||
SemaphoreHandle_t high_performance_semaphore_{nullptr};
|
||||
#endif
|
||||
|
||||
// Pointers at the end (naturally aligned)
|
||||
Trigger<> *connect_trigger_{new Trigger<>()};
|
||||
Trigger<> *disconnect_trigger_{new Trigger<>()};
|
||||
#ifdef USE_WIFI_CONNECT_TRIGGER
|
||||
Trigger<> connect_trigger_;
|
||||
#endif
|
||||
#ifdef USE_WIFI_DISCONNECT_TRIGGER
|
||||
Trigger<> disconnect_trigger_;
|
||||
#endif
|
||||
|
||||
private:
|
||||
// Stores a pointer to a string literal (static storage duration).
|
||||
|
||||
@@ -152,10 +152,11 @@ class ZephyrPreferences : public ESPPreferences {
|
||||
}
|
||||
};
|
||||
|
||||
static ZephyrPreferences s_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
void setup_preferences() {
|
||||
auto *prefs = new ZephyrPreferences(); // NOLINT(cppcoreguidelines-owning-memory)
|
||||
global_preferences = prefs;
|
||||
prefs->open();
|
||||
global_preferences = &s_preferences;
|
||||
s_preferences.open();
|
||||
}
|
||||
|
||||
} // namespace zephyr
|
||||
|
||||
@@ -227,6 +227,8 @@
|
||||
#define USE_WIFI_SCAN_RESULTS_LISTENERS
|
||||
#define USE_WIFI_CONNECT_STATE_LISTENERS
|
||||
#define USE_WIFI_POWER_SAVE_LISTENERS
|
||||
#define USE_WIFI_CONNECT_TRIGGER
|
||||
#define USE_WIFI_DISCONNECT_TRIGGER
|
||||
#define ESPHOME_WIFI_IP_STATE_LISTENERS 2
|
||||
#define ESPHOME_WIFI_SCAN_RESULTS_LISTENERS 2
|
||||
#define ESPHOME_WIFI_CONNECT_STATE_LISTENERS 2
|
||||
|
||||
@@ -18,14 +18,23 @@ key_collector:
|
||||
- logger.log:
|
||||
format: "input progress: '%s', started by '%c'"
|
||||
args: ['x.c_str()', "(start == 0 ? '~' : start)"]
|
||||
- logger.log:
|
||||
format: "second listener - progress: '%s'"
|
||||
args: ['x.c_str()']
|
||||
on_result:
|
||||
- logger.log:
|
||||
format: "input result: '%s', started by '%c', ended by '%c'"
|
||||
args: ['x.c_str()', "(start == 0 ? '~' : start)", "(end == 0 ? '~' : end)"]
|
||||
- logger.log:
|
||||
format: "second listener - result: '%s'"
|
||||
args: ['x.c_str()']
|
||||
on_timeout:
|
||||
- logger.log:
|
||||
format: "input timeout: '%s', started by '%c'"
|
||||
args: ['x.c_str()', "(start == 0 ? '~' : start)"]
|
||||
- logger.log:
|
||||
format: "second listener - timeout: '%s'"
|
||||
args: ['x.c_str()']
|
||||
enable_on_boot: false
|
||||
|
||||
button:
|
||||
@@ -34,3 +43,8 @@ button:
|
||||
on_press:
|
||||
- key_collector.enable:
|
||||
- key_collector.disable:
|
||||
|
||||
text_sensor:
|
||||
- platform: key_collector
|
||||
id: collected_keys
|
||||
source_id: reader
|
||||
|
||||
@@ -26,3 +26,7 @@ wifi:
|
||||
- ssid: MySSID3
|
||||
password: password3
|
||||
priority: 0
|
||||
on_connect:
|
||||
- logger.log: "WiFi connected!"
|
||||
on_disconnect:
|
||||
- logger.log: "WiFi disconnected!"
|
||||
|
||||
Reference in New Issue
Block a user