From bd2b3b9da52923141cb9521239125d54b7c34b57 Mon Sep 17 00:00:00 2001 From: NP v/d Spek Date: Tue, 5 Aug 2025 21:46:40 +0200 Subject: [PATCH] [espnow] Small changes and fixes (#10014) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/espnow/__init__.py | 11 ------ esphome/components/espnow/automation.h | 12 +++---- .../components/espnow/espnow_component.cpp | 34 +++++++++---------- esphome/components/espnow/espnow_component.h | 1 + 4 files changed, 24 insertions(+), 34 deletions(-) diff --git a/esphome/components/espnow/__init__.py b/esphome/components/espnow/__init__.py index d15817cf92..9d2f17440c 100644 --- a/esphome/components/espnow/__init__.py +++ b/esphome/components/espnow/__init__.py @@ -65,15 +65,6 @@ CONF_WAIT_FOR_SENT = "wait_for_sent" MAX_ESPNOW_PACKET_SIZE = 250 # Maximum size of the payload in bytes -def _validate_unknown_peer(config): - if config[CONF_AUTO_ADD_PEER] and config.get(CONF_ON_UNKNOWN_PEER): - raise cv.Invalid( - f"'{CONF_ON_UNKNOWN_PEER}' cannot be used when '{CONF_AUTO_ADD_PEER}' is enabled.", - path=[CONF_ON_UNKNOWN_PEER], - ) - return config - - CONFIG_SCHEMA = cv.All( cv.Schema( { @@ -103,7 +94,6 @@ CONFIG_SCHEMA = cv.All( }, ).extend(cv.COMPONENT_SCHEMA), cv.only_on_esp32, - _validate_unknown_peer, ) @@ -124,7 +114,6 @@ async def _trigger_to_code(config): async def to_code(config): - print(config) var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) diff --git a/esphome/components/espnow/automation.h b/esphome/components/espnow/automation.h index ad534b279a..2416377859 100644 --- a/esphome/components/espnow/automation.h +++ b/esphome/components/espnow/automation.h @@ -40,20 +40,20 @@ template class SendAction : public Action, public Parente this->num_running_++; send_callback_t send_callback = [this, x...](esp_err_t status) { if (status == ESP_OK) { - if (this->sent_.empty() && this->flags_.wait_for_sent) { - this->play_next_(x...); - } else if (!this->sent_.empty()) { + if (!this->sent_.empty()) { this->sent_.play(x...); + } else if (this->flags_.wait_for_sent) { + this->play_next_(x...); } } else { - if (this->error_.empty() && this->flags_.wait_for_sent) { + if (!this->error_.empty()) { + this->error_.play(x...); + } else if (this->flags_.wait_for_sent) { if (this->flags_.continue_on_error) { this->play_next_(x...); } else { this->stop_complex(); } - } else if (!this->error_.empty()) { - this->error_.play(x...); } } }; diff --git a/esphome/components/espnow/espnow_component.cpp b/esphome/components/espnow/espnow_component.cpp index dab8e2b726..82f8e3230e 100644 --- a/esphome/components/espnow/espnow_component.cpp +++ b/esphome/components/espnow/espnow_component.cpp @@ -154,7 +154,7 @@ void ESPNowComponent::setup() { } void ESPNowComponent::enable() { - if (this->state_ != ESPNOW_STATE_ENABLED) + if (this->state_ == ESPNOW_STATE_ENABLED) return; ESP_LOGD(TAG, "Enabling"); @@ -178,11 +178,7 @@ void ESPNowComponent::enable_() { this->apply_wifi_channel(); } -#ifdef USE_WIFI - else { - this->wifi_channel_ = wifi::global_wifi_component->get_wifi_channel(); - } -#endif + this->get_wifi_channel(); esp_err_t err = esp_now_init(); if (err != ESP_OK) { @@ -215,6 +211,7 @@ void ESPNowComponent::enable_() { for (auto peer : this->peers_) { this->add_peer(peer.address); } + this->state_ = ESPNOW_STATE_ENABLED; } @@ -228,10 +225,6 @@ void ESPNowComponent::disable() { esp_now_unregister_recv_cb(); esp_now_unregister_send_cb(); - for (auto peer : this->peers_) { - this->del_peer(peer.address); - } - esp_err_t err = esp_now_deinit(); if (err != ESP_OK) { ESP_LOGE(TAG, "esp_now_deinit failed! 0x%x", err); @@ -267,7 +260,6 @@ void ESPNowComponent::loop() { } } #endif - // Process received packets ESPNowPacket *packet = this->receive_packet_queue_.pop(); while (packet != nullptr) { @@ -275,14 +267,16 @@ void ESPNowComponent::loop() { case ESPNowPacket::RECEIVED: { const ESPNowRecvInfo info = packet->get_receive_info(); if (!esp_now_is_peer_exist(info.src_addr)) { - if (this->auto_add_peer_) { - this->add_peer(info.src_addr); - } else { - for (auto *handler : this->unknown_peer_handlers_) { - if (handler->on_unknown_peer(info, packet->packet_.receive.data, packet->packet_.receive.size)) - break; // If a handler returns true, stop processing further handlers + bool handled = false; + for (auto *handler : this->unknown_peer_handlers_) { + if (handler->on_unknown_peer(info, packet->packet_.receive.data, packet->packet_.receive.size)) { + handled = true; + break; // If a handler returns true, stop processing further handlers } } + if (!handled && this->auto_add_peer_) { + this->add_peer(info.src_addr); + } } // Intentionally left as if instead of else in case the peer is added above if (esp_now_is_peer_exist(info.src_addr)) { @@ -343,6 +337,12 @@ void ESPNowComponent::loop() { } } +uint8_t ESPNowComponent::get_wifi_channel() { + wifi_second_chan_t dummy; + esp_wifi_get_channel(&this->wifi_channel_, &dummy); + return this->wifi_channel_; +} + esp_err_t ESPNowComponent::send(const uint8_t *peer_address, const uint8_t *payload, size_t size, const send_callback_t &callback) { if (this->state_ != ESPNOW_STATE_ENABLED) { diff --git a/esphome/components/espnow/espnow_component.h b/esphome/components/espnow/espnow_component.h index 3a523d1f7e..9941e97227 100644 --- a/esphome/components/espnow/espnow_component.h +++ b/esphome/components/espnow/espnow_component.h @@ -110,6 +110,7 @@ class ESPNowComponent : public Component { void set_wifi_channel(uint8_t channel) { this->wifi_channel_ = channel; } void apply_wifi_channel(); + uint8_t get_wifi_channel(); void set_auto_add_peer(bool value) { this->auto_add_peer_ = value; }