[espnow] Small changes and fixes (#10014)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
NP v/d Spek 2025-08-05 21:46:40 +02:00 committed by GitHub
parent d443a97dd8
commit bd2b3b9da5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 24 additions and 34 deletions

View File

@ -65,15 +65,6 @@ CONF_WAIT_FOR_SENT = "wait_for_sent"
MAX_ESPNOW_PACKET_SIZE = 250 # Maximum size of the payload in bytes 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( CONFIG_SCHEMA = cv.All(
cv.Schema( cv.Schema(
{ {
@ -103,7 +94,6 @@ CONFIG_SCHEMA = cv.All(
}, },
).extend(cv.COMPONENT_SCHEMA), ).extend(cv.COMPONENT_SCHEMA),
cv.only_on_esp32, cv.only_on_esp32,
_validate_unknown_peer,
) )
@ -124,7 +114,6 @@ async def _trigger_to_code(config):
async def to_code(config): async def to_code(config):
print(config)
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config) await cg.register_component(var, config)

View File

@ -40,20 +40,20 @@ template<typename... Ts> class SendAction : public Action<Ts...>, public Parente
this->num_running_++; this->num_running_++;
send_callback_t send_callback = [this, x...](esp_err_t status) { send_callback_t send_callback = [this, x...](esp_err_t status) {
if (status == ESP_OK) { if (status == ESP_OK) {
if (this->sent_.empty() && this->flags_.wait_for_sent) { if (!this->sent_.empty()) {
this->play_next_(x...);
} else if (!this->sent_.empty()) {
this->sent_.play(x...); this->sent_.play(x...);
} else if (this->flags_.wait_for_sent) {
this->play_next_(x...);
} }
} else { } 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) { if (this->flags_.continue_on_error) {
this->play_next_(x...); this->play_next_(x...);
} else { } else {
this->stop_complex(); this->stop_complex();
} }
} else if (!this->error_.empty()) {
this->error_.play(x...);
} }
} }
}; };

View File

@ -154,7 +154,7 @@ void ESPNowComponent::setup() {
} }
void ESPNowComponent::enable() { void ESPNowComponent::enable() {
if (this->state_ != ESPNOW_STATE_ENABLED) if (this->state_ == ESPNOW_STATE_ENABLED)
return; return;
ESP_LOGD(TAG, "Enabling"); ESP_LOGD(TAG, "Enabling");
@ -178,11 +178,7 @@ void ESPNowComponent::enable_() {
this->apply_wifi_channel(); this->apply_wifi_channel();
} }
#ifdef USE_WIFI this->get_wifi_channel();
else {
this->wifi_channel_ = wifi::global_wifi_component->get_wifi_channel();
}
#endif
esp_err_t err = esp_now_init(); esp_err_t err = esp_now_init();
if (err != ESP_OK) { if (err != ESP_OK) {
@ -215,6 +211,7 @@ void ESPNowComponent::enable_() {
for (auto peer : this->peers_) { for (auto peer : this->peers_) {
this->add_peer(peer.address); this->add_peer(peer.address);
} }
this->state_ = ESPNOW_STATE_ENABLED; this->state_ = ESPNOW_STATE_ENABLED;
} }
@ -228,10 +225,6 @@ void ESPNowComponent::disable() {
esp_now_unregister_recv_cb(); esp_now_unregister_recv_cb();
esp_now_unregister_send_cb(); esp_now_unregister_send_cb();
for (auto peer : this->peers_) {
this->del_peer(peer.address);
}
esp_err_t err = esp_now_deinit(); esp_err_t err = esp_now_deinit();
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_now_deinit failed! 0x%x", err); ESP_LOGE(TAG, "esp_now_deinit failed! 0x%x", err);
@ -267,7 +260,6 @@ void ESPNowComponent::loop() {
} }
} }
#endif #endif
// Process received packets // Process received packets
ESPNowPacket *packet = this->receive_packet_queue_.pop(); ESPNowPacket *packet = this->receive_packet_queue_.pop();
while (packet != nullptr) { while (packet != nullptr) {
@ -275,14 +267,16 @@ void ESPNowComponent::loop() {
case ESPNowPacket::RECEIVED: { case ESPNowPacket::RECEIVED: {
const ESPNowRecvInfo info = packet->get_receive_info(); const ESPNowRecvInfo info = packet->get_receive_info();
if (!esp_now_is_peer_exist(info.src_addr)) { if (!esp_now_is_peer_exist(info.src_addr)) {
if (this->auto_add_peer_) { bool handled = false;
this->add_peer(info.src_addr); for (auto *handler : this->unknown_peer_handlers_) {
} else { if (handler->on_unknown_peer(info, packet->packet_.receive.data, packet->packet_.receive.size)) {
for (auto *handler : this->unknown_peer_handlers_) { handled = true;
if (handler->on_unknown_peer(info, packet->packet_.receive.data, packet->packet_.receive.size)) break; // If a handler returns true, stop processing further handlers
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 // Intentionally left as if instead of else in case the peer is added above
if (esp_now_is_peer_exist(info.src_addr)) { 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, esp_err_t ESPNowComponent::send(const uint8_t *peer_address, const uint8_t *payload, size_t size,
const send_callback_t &callback) { const send_callback_t &callback) {
if (this->state_ != ESPNOW_STATE_ENABLED) { if (this->state_ != ESPNOW_STATE_ENABLED) {

View File

@ -110,6 +110,7 @@ class ESPNowComponent : public Component {
void set_wifi_channel(uint8_t channel) { this->wifi_channel_ = channel; } void set_wifi_channel(uint8_t channel) { this->wifi_channel_ = channel; }
void apply_wifi_channel(); void apply_wifi_channel();
uint8_t get_wifi_channel();
void set_auto_add_peer(bool value) { this->auto_add_peer_ = value; } void set_auto_add_peer(bool value) { this->auto_add_peer_ = value; }