mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-29 22:24:26 +00:00 
			
		
		
		
	[espnow] Small changes and fixes (#10014)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
		| @@ -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) | ||||
|  | ||||
|   | ||||
| @@ -40,20 +40,20 @@ template<typename... Ts> class SendAction : public Action<Ts...>, 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...); | ||||
|         } | ||||
|       } | ||||
|     }; | ||||
|   | ||||
| @@ -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) { | ||||
|   | ||||
| @@ -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; } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user