mirror of
https://github.com/esphome/esphome.git
synced 2025-11-20 00:35:44 +00:00
Compare commits
5 Commits
ipaddress_
...
light_loop
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ed236b969c | ||
|
|
085aeeb8d5 | ||
|
|
20649ce8ce | ||
|
|
9b458d25ea | ||
|
|
4e23a7a3e1 |
@@ -66,7 +66,7 @@ static void dump_field(std::string &out, const char *field_name, float value, in
|
||||
static void dump_field(std::string &out, const char *field_name, uint64_t value, int indent = 2) {
|
||||
char buffer[64];
|
||||
append_field_prefix(out, field_name, indent);
|
||||
snprintf(buffer, 64, "%" PRIu64, value);
|
||||
snprintf(buffer, 64, "%llu", value);
|
||||
append_with_newline(out, buffer);
|
||||
}
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ def customise_schema(config):
|
||||
"""
|
||||
config = cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_MODEL): cv.one_of(*MODELS, upper=True, space="-"),
|
||||
cv.Required(CONF_MODEL): cv.one_of(*MODELS, upper=True),
|
||||
},
|
||||
extra=cv.ALLOW_EXTRA,
|
||||
)(config)
|
||||
|
||||
@@ -32,15 +32,11 @@ class SpectraE6(EpaperModel):
|
||||
|
||||
spectra_e6 = SpectraE6("spectra-e6")
|
||||
|
||||
spectra_e6_7p3 = spectra_e6.extend(
|
||||
"7.3in-Spectra-E6",
|
||||
spectra_e6.extend(
|
||||
"Seeed-reTerminal-E1002",
|
||||
width=800,
|
||||
height=480,
|
||||
data_rate="20MHz",
|
||||
)
|
||||
|
||||
spectra_e6_7p3.extend(
|
||||
"Seeed-reTerminal-E1002",
|
||||
cs_pin=10,
|
||||
dc_pin=11,
|
||||
reset_pin=12,
|
||||
|
||||
@@ -23,6 +23,9 @@ void LightState::setup() {
|
||||
effect->init_internal(this);
|
||||
}
|
||||
|
||||
// Start with loop disabled if idle - respects any effects/transitions set up during initialization
|
||||
this->disable_loop_if_idle_();
|
||||
|
||||
// When supported color temperature range is known, initialize color temperature setting within bounds.
|
||||
auto traits = this->get_traits();
|
||||
float min_mireds = traits.get_min_mireds();
|
||||
@@ -125,6 +128,9 @@ void LightState::loop() {
|
||||
this->is_transformer_active_ = false;
|
||||
this->transformer_ = nullptr;
|
||||
this->target_state_reached_callback_.call();
|
||||
|
||||
// Disable loop if idle (no transformer and no effect)
|
||||
this->disable_loop_if_idle_();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,6 +138,8 @@ void LightState::loop() {
|
||||
if (this->next_write_) {
|
||||
this->next_write_ = false;
|
||||
this->output_->write_state(this);
|
||||
// Disable loop if idle (no transformer and no effect)
|
||||
this->disable_loop_if_idle_();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,6 +235,8 @@ void LightState::start_effect_(uint32_t effect_index) {
|
||||
this->active_effect_index_ = effect_index;
|
||||
auto *effect = this->get_active_effect_();
|
||||
effect->start_internal();
|
||||
// Enable loop while effect is active
|
||||
this->enable_loop();
|
||||
}
|
||||
LightEffect *LightState::get_active_effect_() {
|
||||
if (this->active_effect_index_ == 0) {
|
||||
@@ -241,6 +251,8 @@ void LightState::stop_effect_() {
|
||||
effect->stop();
|
||||
}
|
||||
this->active_effect_index_ = 0;
|
||||
// Disable loop if idle (no effect and no transformer)
|
||||
this->disable_loop_if_idle_();
|
||||
}
|
||||
|
||||
void LightState::start_transition_(const LightColorValues &target, uint32_t length, bool set_remote_values) {
|
||||
@@ -250,6 +262,8 @@ void LightState::start_transition_(const LightColorValues &target, uint32_t leng
|
||||
if (set_remote_values) {
|
||||
this->remote_values = target;
|
||||
}
|
||||
// Enable loop while transition is active
|
||||
this->enable_loop();
|
||||
}
|
||||
|
||||
void LightState::start_flash_(const LightColorValues &target, uint32_t length, bool set_remote_values) {
|
||||
@@ -265,6 +279,8 @@ void LightState::start_flash_(const LightColorValues &target, uint32_t length, b
|
||||
if (set_remote_values) {
|
||||
this->remote_values = target;
|
||||
};
|
||||
// Enable loop while flash is active
|
||||
this->enable_loop();
|
||||
}
|
||||
|
||||
void LightState::set_immediately_(const LightColorValues &target, bool set_remote_values) {
|
||||
@@ -276,6 +292,14 @@ void LightState::set_immediately_(const LightColorValues &target, bool set_remot
|
||||
}
|
||||
this->output_->update_state(this);
|
||||
this->next_write_ = true;
|
||||
this->enable_loop();
|
||||
}
|
||||
|
||||
void LightState::disable_loop_if_idle_() {
|
||||
// Only disable loop if both transformer and effect are inactive, and no pending writes
|
||||
if (this->transformer_ == nullptr && this->get_active_effect_() == nullptr && !this->next_write_) {
|
||||
this->disable_loop();
|
||||
}
|
||||
}
|
||||
|
||||
void LightState::save_remote_values_() {
|
||||
|
||||
@@ -255,6 +255,9 @@ class LightState : public EntityBase, public Component {
|
||||
/// Internal method to save the current remote_values to the preferences
|
||||
void save_remote_values_();
|
||||
|
||||
/// Disable loop if neither transformer nor effect is active
|
||||
void disable_loop_if_idle_();
|
||||
|
||||
/// Store the output to allow effects to have more access.
|
||||
LightOutput *output_;
|
||||
/// The currently active transformer for this light (transition/flash).
|
||||
|
||||
@@ -81,12 +81,7 @@ struct IPAddress {
|
||||
ip_addr_.type = IPADDR_TYPE_V6;
|
||||
}
|
||||
#endif /* LWIP_IPV6 */
|
||||
IPAddress(esp_ip4_addr_t *other_ip) {
|
||||
memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(esp_ip4_addr_t));
|
||||
#if LWIP_IPV6
|
||||
ip_addr_.type = IPADDR_TYPE_V4;
|
||||
#endif
|
||||
}
|
||||
IPAddress(esp_ip4_addr_t *other_ip) { memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(esp_ip4_addr_t)); }
|
||||
IPAddress(esp_ip_addr_t *other_ip) {
|
||||
#if LWIP_IPV6
|
||||
memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip_addr_));
|
||||
|
||||
@@ -66,14 +66,10 @@ SubstituteFilter::SubstituteFilter(const std::initializer_list<Substitution> &su
|
||||
: substitutions_(substitutions) {}
|
||||
|
||||
optional<std::string> SubstituteFilter::new_value(std::string value) {
|
||||
std::size_t pos;
|
||||
for (const auto &sub : this->substitutions_) {
|
||||
std::size_t pos = 0;
|
||||
while ((pos = value.find(sub.from, pos)) != std::string::npos) {
|
||||
while ((pos = value.find(sub.from)) != std::string::npos)
|
||||
value.replace(pos, sub.from.size(), sub.to);
|
||||
// Advance past the replacement to avoid infinite loop when
|
||||
// the replacement contains the search pattern (e.g., f -> foo)
|
||||
pos += sub.to.size();
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -87,29 +87,6 @@ int nonblocking_send(httpd_handle_t hd, int sockfd, const char *buf, size_t buf_
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void AsyncWebServer::safe_close_with_shutdown(httpd_handle_t hd, int sockfd) {
|
||||
// CRITICAL: Shut down receive BEFORE closing to prevent lwIP race conditions
|
||||
//
|
||||
// The race condition occurs because close() initiates lwIP teardown while
|
||||
// the TCP/IP thread can still receive packets, causing assertions when
|
||||
// recv_tcp() sees partially-torn-down state.
|
||||
//
|
||||
// By shutting down receive first, we tell lwIP to stop accepting new data BEFORE
|
||||
// the teardown begins, eliminating the race window. We only shutdown RD (not RDWR)
|
||||
// to allow the FIN packet to be sent cleanly during close().
|
||||
//
|
||||
// Note: This function may be called with an already-closed socket if the network
|
||||
// stack closed it. In that case, shutdown() will fail but close() is safe to call.
|
||||
//
|
||||
// See: https://github.com/esphome/esphome-webserver/issues/163
|
||||
|
||||
// Attempt shutdown - ignore errors as socket may already be closed
|
||||
shutdown(sockfd, SHUT_RD);
|
||||
|
||||
// Always close - safe even if socket is already closed by network stack
|
||||
close(sockfd);
|
||||
}
|
||||
|
||||
void AsyncWebServer::end() {
|
||||
if (this->server_) {
|
||||
httpd_stop(this->server_);
|
||||
@@ -138,8 +115,6 @@ void AsyncWebServer::begin() {
|
||||
config.uri_match_fn = [](const char * /*unused*/, const char * /*unused*/, size_t /*unused*/) { return true; };
|
||||
// Enable LRU purging if requested (e.g., by captive portal to handle probe bursts)
|
||||
config.lru_purge_enable = this->lru_purge_enable_;
|
||||
// Use custom close function that shuts down before closing to prevent lwIP race conditions
|
||||
config.close_fn = AsyncWebServer::safe_close_with_shutdown;
|
||||
if (httpd_start(&this->server_, &config) == ESP_OK) {
|
||||
const httpd_uri_t handler_get = {
|
||||
.uri = "",
|
||||
@@ -530,11 +505,17 @@ AsyncEventSourceResponse::AsyncEventSourceResponse(const AsyncWebServerRequest *
|
||||
void AsyncEventSourceResponse::destroy(void *ptr) {
|
||||
auto *rsp = static_cast<AsyncEventSourceResponse *>(ptr);
|
||||
int fd = rsp->fd_.exchange(0); // Atomically get and clear fd
|
||||
ESP_LOGD(TAG, "Event source connection closed (fd: %d)", fd);
|
||||
// Mark as dead - will be cleaned up in the main loop
|
||||
// Note: We don't delete or remove from set here to avoid race conditions
|
||||
// httpd will call our custom close_fn (safe_close_with_shutdown) which handles
|
||||
// shutdown() before close() to prevent lwIP race conditions
|
||||
|
||||
if (fd > 0) {
|
||||
ESP_LOGD(TAG, "Event source connection closed (fd: %d)", fd);
|
||||
// Immediately shut down the socket to prevent lwIP from delivering more data
|
||||
// This prevents "recv_tcp: recv for wrong pcb!" assertions when the TCP stack
|
||||
// tries to deliver queued data after the session is marked as dead
|
||||
// See: https://github.com/esphome/esphome/issues/11936
|
||||
shutdown(fd, SHUT_RDWR);
|
||||
// Note: We don't close() the socket - httpd owns it and will close it
|
||||
}
|
||||
// Session will be cleaned up in the main loop to avoid race conditions
|
||||
}
|
||||
|
||||
// helper for allowing only unique entries in the queue
|
||||
|
||||
@@ -209,7 +209,6 @@ class AsyncWebServer {
|
||||
static esp_err_t request_handler(httpd_req_t *r);
|
||||
static esp_err_t request_post_handler(httpd_req_t *r);
|
||||
esp_err_t request_handler_(AsyncWebServerRequest *request) const;
|
||||
static void safe_close_with_shutdown(httpd_handle_t hd, int sockfd);
|
||||
#ifdef USE_WEBSERVER_OTA
|
||||
esp_err_t handle_multipart_upload_(httpd_req_t *r, const char *content_type);
|
||||
#endif
|
||||
|
||||
@@ -872,13 +872,7 @@ bssid_t WiFiComponent::wifi_bssid() {
|
||||
return bssid;
|
||||
}
|
||||
std::string WiFiComponent::wifi_ssid() { return WiFi.SSID().c_str(); }
|
||||
int8_t WiFiComponent::wifi_rssi() {
|
||||
if (WiFi.status() != WL_CONNECTED)
|
||||
return WIFI_RSSI_DISCONNECTED;
|
||||
int8_t rssi = WiFi.RSSI();
|
||||
// Values >= 31 are error codes per NONOS SDK API, not valid RSSI readings
|
||||
return rssi >= 31 ? WIFI_RSSI_DISCONNECTED : rssi;
|
||||
}
|
||||
int8_t WiFiComponent::wifi_rssi() { return WiFi.status() == WL_CONNECTED ? WiFi.RSSI() : WIFI_RSSI_DISCONNECTED; }
|
||||
int32_t WiFiComponent::get_wifi_channel() { return WiFi.channel(); }
|
||||
network::IPAddress WiFiComponent::wifi_subnet_mask_() { return {(const ip_addr_t *) WiFi.subnetMask()}; }
|
||||
network::IPAddress WiFiComponent::wifi_gateway_ip_() { return {(const ip_addr_t *) WiFi.gatewayIP()}; }
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
pylint==4.0.3
|
||||
flake8==7.3.0 # also change in .pre-commit-config.yaml when updating
|
||||
ruff==0.14.5 # also change in .pre-commit-config.yaml when updating
|
||||
pyupgrade==3.21.2 # also change in .pre-commit-config.yaml when updating
|
||||
pyupgrade==3.21.1 # also change in .pre-commit-config.yaml when updating
|
||||
pre-commit
|
||||
|
||||
# Unit tests
|
||||
|
||||
@@ -462,7 +462,7 @@ class Int64Type(TypeInfo):
|
||||
wire_type = WireType.VARINT # Uses wire type 0
|
||||
|
||||
def dump(self, name: str) -> str:
|
||||
o = f'snprintf(buffer, sizeof(buffer), "%" PRId64, {name});\n'
|
||||
o = f'snprintf(buffer, sizeof(buffer), "%lld", {name});\n'
|
||||
o += "out.append(buffer);"
|
||||
return o
|
||||
|
||||
@@ -482,7 +482,7 @@ class UInt64Type(TypeInfo):
|
||||
wire_type = WireType.VARINT # Uses wire type 0
|
||||
|
||||
def dump(self, name: str) -> str:
|
||||
o = f'snprintf(buffer, sizeof(buffer), "%" PRIu64, {name});\n'
|
||||
o = f'snprintf(buffer, sizeof(buffer), "%llu", {name});\n'
|
||||
o += "out.append(buffer);"
|
||||
return o
|
||||
|
||||
@@ -522,7 +522,7 @@ class Fixed64Type(TypeInfo):
|
||||
wire_type = WireType.FIXED64 # Uses wire type 1
|
||||
|
||||
def dump(self, name: str) -> str:
|
||||
o = f'snprintf(buffer, sizeof(buffer), "%" PRIu64, {name});\n'
|
||||
o = f'snprintf(buffer, sizeof(buffer), "%llu", {name});\n'
|
||||
o += "out.append(buffer);"
|
||||
return o
|
||||
|
||||
@@ -1106,7 +1106,7 @@ class SFixed64Type(TypeInfo):
|
||||
wire_type = WireType.FIXED64 # Uses wire type 1
|
||||
|
||||
def dump(self, name: str) -> str:
|
||||
o = f'snprintf(buffer, sizeof(buffer), "%" PRId64, {name});\n'
|
||||
o = f'snprintf(buffer, sizeof(buffer), "%lld", {name});\n'
|
||||
o += "out.append(buffer);"
|
||||
return o
|
||||
|
||||
@@ -1150,7 +1150,7 @@ class SInt64Type(TypeInfo):
|
||||
wire_type = WireType.VARINT # Uses wire type 0
|
||||
|
||||
def dump(self, name: str) -> str:
|
||||
o = f'snprintf(buffer, sizeof(buffer), "%" PRId64, {name});\n'
|
||||
o = f'snprintf(buffer, sizeof(buffer), "%lld", {name});\n'
|
||||
o += "out.append(buffer);"
|
||||
return o
|
||||
|
||||
@@ -2546,7 +2546,7 @@ static void dump_field(std::string &out, const char *field_name, float value, in
|
||||
static void dump_field(std::string &out, const char *field_name, uint64_t value, int indent = 2) {
|
||||
char buffer[64];
|
||||
append_field_prefix(out, field_name, indent);
|
||||
snprintf(buffer, 64, "%" PRIu64, value);
|
||||
snprintf(buffer, 64, "%llu", value);
|
||||
append_with_newline(out, buffer);
|
||||
}
|
||||
|
||||
|
||||
@@ -115,8 +115,8 @@ wifi:
|
||||
password: PASSWORD123
|
||||
|
||||
time:
|
||||
- platform: sntp
|
||||
id: sntp_time
|
||||
platform: sntp
|
||||
id: time_id
|
||||
|
||||
text:
|
||||
- id: lvgl_text
|
||||
|
||||
@@ -478,19 +478,19 @@ lvgl:
|
||||
id: hello_label
|
||||
text:
|
||||
time_format: "%c"
|
||||
time: sntp_time
|
||||
time: time_id
|
||||
- lvgl.label.update:
|
||||
id: hello_label
|
||||
text:
|
||||
time_format: "%c"
|
||||
time: !lambda return id(sntp_time).now();
|
||||
time: !lambda return id(time_id).now();
|
||||
- lvgl.label.update:
|
||||
id: hello_label
|
||||
text:
|
||||
time_format: "%c"
|
||||
time: !lambda |-
|
||||
ESP_LOGD("label", "multi-line lambda");
|
||||
return id(sntp_time).now();
|
||||
return id(time_id).now();
|
||||
on_value:
|
||||
logger.log:
|
||||
format: "state now %d"
|
||||
|
||||
@@ -4,7 +4,6 @@ wifi:
|
||||
|
||||
time:
|
||||
- platform: sntp
|
||||
id: sntp_time
|
||||
|
||||
mqtt:
|
||||
broker: "192.168.178.84"
|
||||
|
||||
@@ -3,7 +3,6 @@ wifi:
|
||||
|
||||
time:
|
||||
- platform: sntp
|
||||
id: sntp_time
|
||||
|
||||
sensor:
|
||||
- platform: uptime
|
||||
|
||||
@@ -4,10 +4,8 @@ wifi:
|
||||
|
||||
time:
|
||||
- platform: sntp
|
||||
id: sntp_time
|
||||
|
||||
wireguard:
|
||||
time_id: sntp_time
|
||||
address: 172.16.34.100
|
||||
netmask: 255.255.255.0
|
||||
# NEVER use the following keys for your VPN -- they are now public!
|
||||
|
||||
Reference in New Issue
Block a user