mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-04 09:01:49 +00:00 
			
		
		
		
	Compare commits
	
		
			33 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					a7ad4482f0 | ||
| 
						 | 
					aa17661002 | ||
| 
						 | 
					4e65aac7ae | ||
| 
						 | 
					229ba18e6c | ||
| 
						 | 
					b99be250a0 | ||
| 
						 | 
					b9d4e2e501 | ||
| 
						 | 
					ef2531edf3 | ||
| 
						 | 
					eae3089201 | ||
| 
						 | 
					0ea4de5f4c | ||
| 
						 | 
					1e0daefa16 | ||
| 
						 | 
					6d991a1fc8 | ||
| 
						 | 
					a1845e1e72 | ||
| 
						 | 
					f96a839bcf | ||
| 
						 | 
					1282a15b14 | ||
| 
						 | 
					35039b45e4 | ||
| 
						 | 
					390766eb67 | ||
| 
						 | 
					899d280ac7 | ||
| 
						 | 
					96dc7f0259 | ||
| 
						 | 
					0104bf3fc8 | ||
| 
						 | 
					9b1e1bf56c | ||
| 
						 | 
					33e0f16b3b | ||
| 
						 | 
					0807d60c6a | ||
| 
						 | 
					f018fde369 | ||
| 
						 | 
					c47f8fc02c | ||
| 
						 | 
					76ab923780 | ||
| 
						 | 
					11dba3147d | ||
| 
						 | 
					8c2d9101d5 | ||
| 
						 | 
					61b8004536 | ||
| 
						 | 
					db02c4ea21 | ||
| 
						 | 
					f077a5962d | ||
| 
						 | 
					fa4ba43eb9 | ||
| 
						 | 
					9579423b24 | ||
| 
						 | 
					02449f24c9 | 
@@ -225,7 +225,7 @@ esphome/components/pn532_spi/* @OttoWinter @jesserockz
 | 
			
		||||
esphome/components/power_supply/* @esphome/core
 | 
			
		||||
esphome/components/preferences/* @esphome/core
 | 
			
		||||
esphome/components/psram/* @esphome/core
 | 
			
		||||
esphome/components/pulse_meter/* @cstaahl @stevebaxter
 | 
			
		||||
esphome/components/pulse_meter/* @TrentHouliston @cstaahl @stevebaxter
 | 
			
		||||
esphome/components/pvvx_mithermometer/* @pasiz
 | 
			
		||||
esphome/components/qmp6988/* @andrewpc
 | 
			
		||||
esphome/components/qr_code/* @wjtje
 | 
			
		||||
 
 | 
			
		||||
@@ -18,6 +18,8 @@ from esphome.const import (
 | 
			
		||||
    CONF_TRIGGER_ID,
 | 
			
		||||
    CONF_EVENT,
 | 
			
		||||
    CONF_TAG,
 | 
			
		||||
    CONF_ON_CLIENT_CONNECTED,
 | 
			
		||||
    CONF_ON_CLIENT_DISCONNECTED,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core import coroutine_with_priority
 | 
			
		||||
 | 
			
		||||
@@ -87,6 +89,12 @@ CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
                cv.Required(CONF_KEY): validate_encryption_key,
 | 
			
		||||
            }
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_ON_CLIENT_CONNECTED): automation.validate_automation(
 | 
			
		||||
            single=True
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_ON_CLIENT_DISCONNECTED): automation.validate_automation(
 | 
			
		||||
            single=True
 | 
			
		||||
        ),
 | 
			
		||||
    }
 | 
			
		||||
).extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
 | 
			
		||||
@@ -116,6 +124,20 @@ async def to_code(config):
 | 
			
		||||
        cg.add(var.register_user_service(trigger))
 | 
			
		||||
        await automation.build_automation(trigger, func_args, conf)
 | 
			
		||||
 | 
			
		||||
    if CONF_ON_CLIENT_CONNECTED in config:
 | 
			
		||||
        await automation.build_automation(
 | 
			
		||||
            var.get_client_connected_trigger(),
 | 
			
		||||
            [(cg.std_string, "client_info"), (cg.std_string, "client_address")],
 | 
			
		||||
            config[CONF_ON_CLIENT_CONNECTED],
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    if CONF_ON_CLIENT_DISCONNECTED in config:
 | 
			
		||||
        await automation.build_automation(
 | 
			
		||||
            var.get_client_disconnected_trigger(),
 | 
			
		||||
            [(cg.std_string, "client_info"), (cg.std_string, "client_address")],
 | 
			
		||||
            config[CONF_ON_CLIENT_DISCONNECTED],
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    if encryption_config := config.get(CONF_ENCRYPTION):
 | 
			
		||||
        decoded = base64.b64decode(encryption_config[CONF_KEY])
 | 
			
		||||
        cg.add(var.set_noise_psk(list(decoded)))
 | 
			
		||||
 
 | 
			
		||||
@@ -31,9 +31,9 @@ APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *pa
 | 
			
		||||
  this->proto_write_buffer_.reserve(64);
 | 
			
		||||
 | 
			
		||||
#if defined(USE_API_PLAINTEXT)
 | 
			
		||||
  helper_ = std::unique_ptr<APIFrameHelper>{new APIPlaintextFrameHelper(std::move(sock))};
 | 
			
		||||
  this->helper_ = std::unique_ptr<APIFrameHelper>{new APIPlaintextFrameHelper(std::move(sock))};
 | 
			
		||||
#elif defined(USE_API_NOISE)
 | 
			
		||||
  helper_ = std::unique_ptr<APIFrameHelper>{new APINoiseFrameHelper(std::move(sock), parent->get_noise_ctx())};
 | 
			
		||||
  this->helper_ = std::unique_ptr<APIFrameHelper>{new APINoiseFrameHelper(std::move(sock), parent->get_noise_ctx())};
 | 
			
		||||
#else
 | 
			
		||||
#error "No frame helper defined"
 | 
			
		||||
#endif
 | 
			
		||||
@@ -41,14 +41,16 @@ APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *pa
 | 
			
		||||
void APIConnection::start() {
 | 
			
		||||
  this->last_traffic_ = millis();
 | 
			
		||||
 | 
			
		||||
  APIError err = helper_->init();
 | 
			
		||||
  APIError err = this->helper_->init();
 | 
			
		||||
  if (err != APIError::OK) {
 | 
			
		||||
    on_fatal_error();
 | 
			
		||||
    ESP_LOGW(TAG, "%s: Helper init failed: %s errno=%d", client_info_.c_str(), api_error_to_str(err), errno);
 | 
			
		||||
    ESP_LOGW(TAG, "%s: Helper init failed: %s errno=%d", this->client_combined_info_.c_str(), api_error_to_str(err),
 | 
			
		||||
             errno);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  client_info_ = helper_->getpeername();
 | 
			
		||||
  helper_->set_log_info(client_info_);
 | 
			
		||||
  this->client_info_ = helper_->getpeername();
 | 
			
		||||
  this->client_peername_ = this->client_info_;
 | 
			
		||||
  this->helper_->set_log_info(this->client_info_);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
APIConnection::~APIConnection() {
 | 
			
		||||
@@ -57,6 +59,11 @@ APIConnection::~APIConnection() {
 | 
			
		||||
    bluetooth_proxy::global_bluetooth_proxy->unsubscribe_api_connection(this);
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_VOICE_ASSISTANT
 | 
			
		||||
  if (voice_assistant::global_voice_assistant->get_api_connection() == this) {
 | 
			
		||||
    voice_assistant::global_voice_assistant->client_subscription(this, false);
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void APIConnection::loop() {
 | 
			
		||||
@@ -67,7 +74,7 @@ void APIConnection::loop() {
 | 
			
		||||
    // when network is disconnected force disconnect immediately
 | 
			
		||||
    // don't wait for timeout
 | 
			
		||||
    this->on_fatal_error();
 | 
			
		||||
    ESP_LOGW(TAG, "%s: Network unavailable, disconnecting", client_info_.c_str());
 | 
			
		||||
    ESP_LOGW(TAG, "%s: Network unavailable, disconnecting", this->client_combined_info_.c_str());
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (this->next_close_) {
 | 
			
		||||
@@ -77,24 +84,26 @@ void APIConnection::loop() {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  APIError err = helper_->loop();
 | 
			
		||||
  APIError err = this->helper_->loop();
 | 
			
		||||
  if (err != APIError::OK) {
 | 
			
		||||
    on_fatal_error();
 | 
			
		||||
    ESP_LOGW(TAG, "%s: Socket operation failed: %s errno=%d", client_info_.c_str(), api_error_to_str(err), errno);
 | 
			
		||||
    ESP_LOGW(TAG, "%s: Socket operation failed: %s errno=%d", this->client_combined_info_.c_str(),
 | 
			
		||||
             api_error_to_str(err), errno);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  ReadPacketBuffer buffer;
 | 
			
		||||
  err = helper_->read_packet(&buffer);
 | 
			
		||||
  err = this->helper_->read_packet(&buffer);
 | 
			
		||||
  if (err == APIError::WOULD_BLOCK) {
 | 
			
		||||
    // pass
 | 
			
		||||
  } else if (err != APIError::OK) {
 | 
			
		||||
    on_fatal_error();
 | 
			
		||||
    if (err == APIError::SOCKET_READ_FAILED && errno == ECONNRESET) {
 | 
			
		||||
      ESP_LOGW(TAG, "%s: Connection reset", client_info_.c_str());
 | 
			
		||||
      ESP_LOGW(TAG, "%s: Connection reset", this->client_combined_info_.c_str());
 | 
			
		||||
    } else if (err == APIError::CONNECTION_CLOSED) {
 | 
			
		||||
      ESP_LOGW(TAG, "%s: Connection closed", client_info_.c_str());
 | 
			
		||||
      ESP_LOGW(TAG, "%s: Connection closed", this->client_combined_info_.c_str());
 | 
			
		||||
    } else {
 | 
			
		||||
      ESP_LOGW(TAG, "%s: Reading failed: %s errno=%d", client_info_.c_str(), api_error_to_str(err), errno);
 | 
			
		||||
      ESP_LOGW(TAG, "%s: Reading failed: %s errno=%d", this->client_combined_info_.c_str(), api_error_to_str(err),
 | 
			
		||||
               errno);
 | 
			
		||||
    }
 | 
			
		||||
    return;
 | 
			
		||||
  } else {
 | 
			
		||||
@@ -114,7 +123,7 @@ void APIConnection::loop() {
 | 
			
		||||
    // Disconnect if not responded within 2.5*keepalive
 | 
			
		||||
    if (now - this->last_traffic_ > (keepalive * 5) / 2) {
 | 
			
		||||
      on_fatal_error();
 | 
			
		||||
      ESP_LOGW(TAG, "%s didn't respond to ping request in time. Disconnecting...", this->client_info_.c_str());
 | 
			
		||||
      ESP_LOGW(TAG, "%s didn't respond to ping request in time. Disconnecting...", this->client_combined_info_.c_str());
 | 
			
		||||
    }
 | 
			
		||||
  } else if (now - this->last_traffic_ > keepalive) {
 | 
			
		||||
    ESP_LOGVV(TAG, "Sending keepalive PING...");
 | 
			
		||||
@@ -168,7 +177,7 @@ DisconnectResponse APIConnection::disconnect(const DisconnectRequest &msg) {
 | 
			
		||||
  // remote initiated disconnect_client
 | 
			
		||||
  // don't close yet, we still need to send the disconnect response
 | 
			
		||||
  // close will happen on next loop
 | 
			
		||||
  ESP_LOGD(TAG, "%s requested disconnected", client_info_.c_str());
 | 
			
		||||
  ESP_LOGD(TAG, "%s requested disconnected", this->client_combined_info_.c_str());
 | 
			
		||||
  this->next_close_ = true;
 | 
			
		||||
  DisconnectResponse resp;
 | 
			
		||||
  return resp;
 | 
			
		||||
@@ -907,14 +916,17 @@ BluetoothConnectionsFreeResponse APIConnection::subscribe_bluetooth_connections_
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_VOICE_ASSISTANT
 | 
			
		||||
bool APIConnection::request_voice_assistant(const VoiceAssistantRequest &msg) {
 | 
			
		||||
  if (!this->voice_assistant_subscription_)
 | 
			
		||||
    return false;
 | 
			
		||||
 | 
			
		||||
  return this->send_voice_assistant_request(msg);
 | 
			
		||||
void APIConnection::subscribe_voice_assistant(const SubscribeVoiceAssistantRequest &msg) {
 | 
			
		||||
  if (voice_assistant::global_voice_assistant != nullptr) {
 | 
			
		||||
    voice_assistant::global_voice_assistant->client_subscription(this, msg.subscribe);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
void APIConnection::on_voice_assistant_response(const VoiceAssistantResponse &msg) {
 | 
			
		||||
  if (voice_assistant::global_voice_assistant != nullptr) {
 | 
			
		||||
    if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (msg.error) {
 | 
			
		||||
      voice_assistant::global_voice_assistant->failed_to_start();
 | 
			
		||||
      return;
 | 
			
		||||
@@ -927,6 +939,10 @@ void APIConnection::on_voice_assistant_response(const VoiceAssistantResponse &ms
 | 
			
		||||
};
 | 
			
		||||
void APIConnection::on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg) {
 | 
			
		||||
  if (voice_assistant::global_voice_assistant != nullptr) {
 | 
			
		||||
    if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    voice_assistant::global_voice_assistant->on_event(msg);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -1006,12 +1022,14 @@ bool APIConnection::send_log_message(int level, const char *tag, const char *lin
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
HelloResponse APIConnection::hello(const HelloRequest &msg) {
 | 
			
		||||
  this->client_info_ = msg.client_info + " (" + this->helper_->getpeername() + ")";
 | 
			
		||||
  this->helper_->set_log_info(client_info_);
 | 
			
		||||
  this->client_info_ = msg.client_info;
 | 
			
		||||
  this->client_peername_ = this->helper_->getpeername();
 | 
			
		||||
  this->client_combined_info_ = this->client_info_ + " (" + this->client_peername_ + ")";
 | 
			
		||||
  this->helper_->set_log_info(this->client_combined_info_);
 | 
			
		||||
  this->client_api_version_major_ = msg.api_version_major;
 | 
			
		||||
  this->client_api_version_minor_ = msg.api_version_minor;
 | 
			
		||||
  ESP_LOGV(TAG, "Hello from client: '%s' | API Version %" PRIu32 ".%" PRIu32, this->client_info_.c_str(),
 | 
			
		||||
           this->client_api_version_major_, this->client_api_version_minor_);
 | 
			
		||||
  ESP_LOGV(TAG, "Hello from client: '%s' | %s | API Version %" PRIu32 ".%" PRIu32, this->client_info_.c_str(),
 | 
			
		||||
           this->client_peername_.c_str(), this->client_api_version_major_, this->client_api_version_minor_);
 | 
			
		||||
 | 
			
		||||
  HelloResponse resp;
 | 
			
		||||
  resp.api_version_major = 1;
 | 
			
		||||
@@ -1029,9 +1047,9 @@ ConnectResponse APIConnection::connect(const ConnectRequest &msg) {
 | 
			
		||||
  // bool invalid_password = 1;
 | 
			
		||||
  resp.invalid_password = !correct;
 | 
			
		||||
  if (correct) {
 | 
			
		||||
    ESP_LOGD(TAG, "%s: Connected successfully", this->client_info_.c_str());
 | 
			
		||||
    ESP_LOGD(TAG, "%s: Connected successfully", this->client_combined_info_.c_str());
 | 
			
		||||
    this->connection_state_ = ConnectionState::AUTHENTICATED;
 | 
			
		||||
 | 
			
		||||
    this->parent_->get_client_connected_trigger()->trigger(this->client_info_, this->client_peername_);
 | 
			
		||||
#ifdef USE_HOMEASSISTANT_TIME
 | 
			
		||||
    if (homeassistant::global_homeassistant_time != nullptr) {
 | 
			
		||||
      this->send_time_request();
 | 
			
		||||
@@ -1105,10 +1123,11 @@ bool APIConnection::send_buffer(ProtoWriteBuffer buffer, uint32_t message_type)
 | 
			
		||||
    return false;
 | 
			
		||||
  if (!this->helper_->can_write_without_blocking()) {
 | 
			
		||||
    delay(0);
 | 
			
		||||
    APIError err = helper_->loop();
 | 
			
		||||
    APIError err = this->helper_->loop();
 | 
			
		||||
    if (err != APIError::OK) {
 | 
			
		||||
      on_fatal_error();
 | 
			
		||||
      ESP_LOGW(TAG, "%s: Socket operation failed: %s errno=%d", client_info_.c_str(), api_error_to_str(err), errno);
 | 
			
		||||
      ESP_LOGW(TAG, "%s: Socket operation failed: %s errno=%d", this->client_combined_info_.c_str(),
 | 
			
		||||
               api_error_to_str(err), errno);
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
    if (!this->helper_->can_write_without_blocking()) {
 | 
			
		||||
@@ -1127,9 +1146,10 @@ bool APIConnection::send_buffer(ProtoWriteBuffer buffer, uint32_t message_type)
 | 
			
		||||
  if (err != APIError::OK) {
 | 
			
		||||
    on_fatal_error();
 | 
			
		||||
    if (err == APIError::SOCKET_WRITE_FAILED && errno == ECONNRESET) {
 | 
			
		||||
      ESP_LOGW(TAG, "%s: Connection reset", client_info_.c_str());
 | 
			
		||||
      ESP_LOGW(TAG, "%s: Connection reset", this->client_combined_info_.c_str());
 | 
			
		||||
    } else {
 | 
			
		||||
      ESP_LOGW(TAG, "%s: Packet write failed %s errno=%d", client_info_.c_str(), api_error_to_str(err), errno);
 | 
			
		||||
      ESP_LOGW(TAG, "%s: Packet write failed %s errno=%d", this->client_combined_info_.c_str(), api_error_to_str(err),
 | 
			
		||||
               errno);
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
@@ -1138,11 +1158,11 @@ bool APIConnection::send_buffer(ProtoWriteBuffer buffer, uint32_t message_type)
 | 
			
		||||
}
 | 
			
		||||
void APIConnection::on_unauthenticated_access() {
 | 
			
		||||
  this->on_fatal_error();
 | 
			
		||||
  ESP_LOGD(TAG, "%s: tried to access without authentication.", this->client_info_.c_str());
 | 
			
		||||
  ESP_LOGD(TAG, "%s: tried to access without authentication.", this->client_combined_info_.c_str());
 | 
			
		||||
}
 | 
			
		||||
void APIConnection::on_no_setup_connection() {
 | 
			
		||||
  this->on_fatal_error();
 | 
			
		||||
  ESP_LOGD(TAG, "%s: tried to access without full connection.", this->client_info_.c_str());
 | 
			
		||||
  ESP_LOGD(TAG, "%s: tried to access without full connection.", this->client_combined_info_.c_str());
 | 
			
		||||
}
 | 
			
		||||
void APIConnection::on_fatal_error() {
 | 
			
		||||
  this->helper_->close();
 | 
			
		||||
 
 | 
			
		||||
@@ -121,10 +121,7 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_VOICE_ASSISTANT
 | 
			
		||||
  void subscribe_voice_assistant(const SubscribeVoiceAssistantRequest &msg) override {
 | 
			
		||||
    this->voice_assistant_subscription_ = msg.subscribe;
 | 
			
		||||
  }
 | 
			
		||||
  bool request_voice_assistant(const VoiceAssistantRequest &msg);
 | 
			
		||||
  void subscribe_voice_assistant(const SubscribeVoiceAssistantRequest &msg) override;
 | 
			
		||||
  void on_voice_assistant_response(const VoiceAssistantResponse &msg) override;
 | 
			
		||||
  void on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg) override;
 | 
			
		||||
#endif
 | 
			
		||||
@@ -183,6 +180,8 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
  }
 | 
			
		||||
  bool send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) override;
 | 
			
		||||
 | 
			
		||||
  std::string get_client_combined_info() const { return this->client_combined_info_; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  friend APIServer;
 | 
			
		||||
 | 
			
		||||
@@ -202,6 +201,8 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
  std::unique_ptr<APIFrameHelper> helper_;
 | 
			
		||||
 | 
			
		||||
  std::string client_info_;
 | 
			
		||||
  std::string client_peername_;
 | 
			
		||||
  std::string client_combined_info_;
 | 
			
		||||
  uint32_t client_api_version_major_{0};
 | 
			
		||||
  uint32_t client_api_version_minor_{0};
 | 
			
		||||
#ifdef USE_ESP32_CAMERA
 | 
			
		||||
@@ -213,9 +214,6 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
  uint32_t last_traffic_;
 | 
			
		||||
  bool sent_ping_{false};
 | 
			
		||||
  bool service_call_subscription_{false};
 | 
			
		||||
#ifdef USE_VOICE_ASSISTANT
 | 
			
		||||
  bool voice_assistant_subscription_{false};
 | 
			
		||||
#endif
 | 
			
		||||
  bool next_close_ = false;
 | 
			
		||||
  APIServer *parent_;
 | 
			
		||||
  InitialStateIterator initial_state_iterator_;
 | 
			
		||||
 
 | 
			
		||||
@@ -111,6 +111,7 @@ void APIServer::loop() {
 | 
			
		||||
                                [](const std::unique_ptr<APIConnection> &conn) { return !conn->remove_; });
 | 
			
		||||
  // print disconnection messages
 | 
			
		||||
  for (auto it = new_end; it != this->clients_.end(); ++it) {
 | 
			
		||||
    this->client_disconnected_trigger_->trigger((*it)->client_info_, (*it)->client_peername_);
 | 
			
		||||
    ESP_LOGV(TAG, "Removing connection to %s", (*it)->client_info_.c_str());
 | 
			
		||||
  }
 | 
			
		||||
  // resize vector
 | 
			
		||||
@@ -322,30 +323,6 @@ void APIServer::on_shutdown() {
 | 
			
		||||
  delay(10);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef USE_VOICE_ASSISTANT
 | 
			
		||||
bool APIServer::start_voice_assistant(const std::string &conversation_id, uint32_t flags,
 | 
			
		||||
                                      const api::VoiceAssistantAudioSettings &audio_settings) {
 | 
			
		||||
  VoiceAssistantRequest msg;
 | 
			
		||||
  msg.start = true;
 | 
			
		||||
  msg.conversation_id = conversation_id;
 | 
			
		||||
  msg.flags = flags;
 | 
			
		||||
  msg.audio_settings = audio_settings;
 | 
			
		||||
  for (auto &c : this->clients_) {
 | 
			
		||||
    if (c->request_voice_assistant(msg))
 | 
			
		||||
      return true;
 | 
			
		||||
  }
 | 
			
		||||
  return false;
 | 
			
		||||
}
 | 
			
		||||
void APIServer::stop_voice_assistant() {
 | 
			
		||||
  VoiceAssistantRequest msg;
 | 
			
		||||
  msg.start = false;
 | 
			
		||||
  for (auto &c : this->clients_) {
 | 
			
		||||
    if (c->request_voice_assistant(msg))
 | 
			
		||||
      return;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ALARM_CONTROL_PANEL
 | 
			
		||||
void APIServer::on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) {
 | 
			
		||||
  if (obj->is_internal())
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@
 | 
			
		||||
#include "api_pb2.h"
 | 
			
		||||
#include "api_pb2_service.h"
 | 
			
		||||
#include "esphome/components/socket/socket.h"
 | 
			
		||||
#include "esphome/core/automation.h"
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/core/controller.h"
 | 
			
		||||
#include "esphome/core/defines.h"
 | 
			
		||||
@@ -80,12 +81,6 @@ class APIServer : public Component, public Controller {
 | 
			
		||||
  void request_time();
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_VOICE_ASSISTANT
 | 
			
		||||
  bool start_voice_assistant(const std::string &conversation_id, uint32_t flags,
 | 
			
		||||
                             const api::VoiceAssistantAudioSettings &audio_settings);
 | 
			
		||||
  void stop_voice_assistant();
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ALARM_CONTROL_PANEL
 | 
			
		||||
  void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override;
 | 
			
		||||
#endif
 | 
			
		||||
@@ -103,6 +98,11 @@ class APIServer : public Component, public Controller {
 | 
			
		||||
  const std::vector<HomeAssistantStateSubscription> &get_state_subs() const;
 | 
			
		||||
  const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
 | 
			
		||||
 | 
			
		||||
  Trigger<std::string, std::string> *get_client_connected_trigger() const { return this->client_connected_trigger_; }
 | 
			
		||||
  Trigger<std::string, std::string> *get_client_disconnected_trigger() const {
 | 
			
		||||
    return this->client_disconnected_trigger_;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  std::unique_ptr<socket::Socket> socket_ = nullptr;
 | 
			
		||||
  uint16_t port_{6053};
 | 
			
		||||
@@ -112,6 +112,8 @@ class APIServer : public Component, public Controller {
 | 
			
		||||
  std::string password_;
 | 
			
		||||
  std::vector<HomeAssistantStateSubscription> state_subs_;
 | 
			
		||||
  std::vector<UserServiceDescriptor *> user_services_;
 | 
			
		||||
  Trigger<std::string, std::string> *client_connected_trigger_ = new Trigger<std::string, std::string>();
 | 
			
		||||
  Trigger<std::string, std::string> *client_disconnected_trigger_ = new Trigger<std::string, std::string>();
 | 
			
		||||
 | 
			
		||||
#ifdef USE_API_NOISE
 | 
			
		||||
  std::shared_ptr<APINoiseContext> noise_ctx_ = std::make_shared<APINoiseContext>();
 | 
			
		||||
 
 | 
			
		||||
@@ -17,11 +17,12 @@ CONF_ON_FRAME = "on_frame"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def validate_id(config):
 | 
			
		||||
    can_id = config[CONF_CAN_ID]
 | 
			
		||||
    id_ext = config[CONF_USE_EXTENDED_ID]
 | 
			
		||||
    if not id_ext:
 | 
			
		||||
        if can_id > 0x7FF:
 | 
			
		||||
            raise cv.Invalid("Standard IDs must be 11 Bit (0x000-0x7ff / 0-2047)")
 | 
			
		||||
    if CONF_CAN_ID in config:
 | 
			
		||||
        can_id = config[CONF_CAN_ID]
 | 
			
		||||
        id_ext = config[CONF_USE_EXTENDED_ID]
 | 
			
		||||
        if not id_ext:
 | 
			
		||||
            if can_id > 0x7FF:
 | 
			
		||||
                raise cv.Invalid("Standard IDs must be 11 Bit (0x000-0x7ff / 0-2047)")
 | 
			
		||||
    return config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -151,22 +152,18 @@ async def canbus_action_to_code(config, action_id, template_arg, args):
 | 
			
		||||
    if can_id := config.get(CONF_CAN_ID):
 | 
			
		||||
        can_id = await cg.templatable(can_id, args, cg.uint32)
 | 
			
		||||
        cg.add(var.set_can_id(can_id))
 | 
			
		||||
    use_extended_id = await cg.templatable(
 | 
			
		||||
        config[CONF_USE_EXTENDED_ID], args, cg.uint32
 | 
			
		||||
    )
 | 
			
		||||
    cg.add(var.set_use_extended_id(use_extended_id))
 | 
			
		||||
        cg.add(var.set_use_extended_id(config[CONF_USE_EXTENDED_ID]))
 | 
			
		||||
 | 
			
		||||
    remote_transmission_request = await cg.templatable(
 | 
			
		||||
        config[CONF_REMOTE_TRANSMISSION_REQUEST], args, bool
 | 
			
		||||
    cg.add(
 | 
			
		||||
        var.set_remote_transmission_request(config[CONF_REMOTE_TRANSMISSION_REQUEST])
 | 
			
		||||
    )
 | 
			
		||||
    cg.add(var.set_remote_transmission_request(remote_transmission_request))
 | 
			
		||||
 | 
			
		||||
    data = config[CONF_DATA]
 | 
			
		||||
    if isinstance(data, bytes):
 | 
			
		||||
        data = [int(x) for x in data]
 | 
			
		||||
    if cg.is_template(data):
 | 
			
		||||
        templ = await cg.templatable(data, args, cg.std_vector.template(cg.uint8))
 | 
			
		||||
        cg.add(var.set_data_template(templ))
 | 
			
		||||
    else:
 | 
			
		||||
        if isinstance(data, bytes):
 | 
			
		||||
            data = [int(x) for x in data]
 | 
			
		||||
        cg.add(var.set_data_static(data))
 | 
			
		||||
    return var
 | 
			
		||||
 
 | 
			
		||||
@@ -48,7 +48,7 @@ void CaptivePortal::start() {
 | 
			
		||||
  this->dns_server_ = make_unique<DNSServer>();
 | 
			
		||||
  this->dns_server_->setErrorReplyCode(DNSReplyCode::NoError);
 | 
			
		||||
  network::IPAddress ip = wifi::global_wifi_component->wifi_soft_ap_ip();
 | 
			
		||||
  this->dns_server_->start(53, "*", IPAddress(ip));
 | 
			
		||||
  this->dns_server_->start(53, "*", ip);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  this->base_->get_server()->onNotFound([this](AsyncWebServerRequest *req) {
 | 
			
		||||
 
 | 
			
		||||
@@ -213,6 +213,8 @@ ClimateCall &ClimateCall::set_preset(const std::string &preset) {
 | 
			
		||||
    this->set_preset(CLIMATE_PRESET_SLEEP);
 | 
			
		||||
  } else if (str_equals_case_insensitive(preset, "ACTIVITY")) {
 | 
			
		||||
    this->set_preset(CLIMATE_PRESET_ACTIVITY);
 | 
			
		||||
  } else if (str_equals_case_insensitive(preset, "NONE")) {
 | 
			
		||||
    this->set_preset(CLIMATE_PRESET_NONE);
 | 
			
		||||
  } else {
 | 
			
		||||
    if (this->parent_->get_traits().supports_custom_preset(preset)) {
 | 
			
		||||
      this->custom_preset_ = preset;
 | 
			
		||||
 
 | 
			
		||||
@@ -104,7 +104,8 @@ void CurrentBasedCover::loop() {
 | 
			
		||||
      ESP_LOGD(TAG, "'%s' - Close position reached. Took %.1fs.", this->name_.c_str(), dur);
 | 
			
		||||
      this->direction_idle_(COVER_CLOSED);
 | 
			
		||||
    }
 | 
			
		||||
  } else if (now - this->start_dir_time_ > this->max_duration_) {
 | 
			
		||||
  }
 | 
			
		||||
  if (now - this->start_dir_time_ > this->max_duration_) {
 | 
			
		||||
    ESP_LOGD(TAG, "'%s' - Max duration reached. Stopping cover.", this->name_.c_str());
 | 
			
		||||
    this->direction_idle_();
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -18,7 +18,7 @@ _ESP_SDIO_PINS = {
 | 
			
		||||
    11: "Flash Command",
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
_ESP32_STRAPPING_PINS = {0, 2, 4, 12, 15}
 | 
			
		||||
_ESP32_STRAPPING_PINS = {0, 2, 5, 12, 15}
 | 
			
		||||
_LOGGER = logging.getLogger(__name__)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -36,6 +36,9 @@ CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
        cv.Optional(
 | 
			
		||||
            CONF_AUTHORIZED_DURATION, default="1min"
 | 
			
		||||
        ): cv.positive_time_period_milliseconds,
 | 
			
		||||
        cv.Optional(
 | 
			
		||||
            CONF_WIFI_TIMEOUT, default="1min"
 | 
			
		||||
        ): cv.positive_time_period_milliseconds,
 | 
			
		||||
    }
 | 
			
		||||
).extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
 | 
			
		||||
@@ -53,6 +56,8 @@ async def to_code(config):
 | 
			
		||||
    cg.add(var.set_identify_duration(config[CONF_IDENTIFY_DURATION]))
 | 
			
		||||
    cg.add(var.set_authorized_duration(config[CONF_AUTHORIZED_DURATION]))
 | 
			
		||||
 | 
			
		||||
    cg.add(var.set_wifi_timeout(config[CONF_WIFI_TIMEOUT]))
 | 
			
		||||
 | 
			
		||||
    if CONF_AUTHORIZER in config and config[CONF_AUTHORIZER] is not None:
 | 
			
		||||
        activator = await cg.get_variable(config[CONF_AUTHORIZER])
 | 
			
		||||
        cg.add(var.set_authorizer(activator))
 | 
			
		||||
 
 | 
			
		||||
@@ -51,6 +51,9 @@ class ESP32ImprovComponent : public Component, public BLEServiceComponent {
 | 
			
		||||
  void set_identify_duration(uint32_t identify_duration) { this->identify_duration_ = identify_duration; }
 | 
			
		||||
  void set_authorized_duration(uint32_t authorized_duration) { this->authorized_duration_ = authorized_duration; }
 | 
			
		||||
 | 
			
		||||
  void set_wifi_timeout(uint32_t wifi_timeout) { this->wifi_timeout_ = wifi_timeout; }
 | 
			
		||||
  uint32_t get_wifi_timeout() const { return this->wifi_timeout_; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  bool should_start_{false};
 | 
			
		||||
  bool setup_complete_{false};
 | 
			
		||||
@@ -60,6 +63,8 @@ class ESP32ImprovComponent : public Component, public BLEServiceComponent {
 | 
			
		||||
  uint32_t authorized_start_{0};
 | 
			
		||||
  uint32_t authorized_duration_;
 | 
			
		||||
 | 
			
		||||
  uint32_t wifi_timeout_{};
 | 
			
		||||
 | 
			
		||||
  std::vector<uint8_t> incoming_data_;
 | 
			
		||||
  wifi::WiFiAP connecting_sta_;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -137,11 +137,10 @@ def validate_weight_name(value):
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def download_gfonts(value):
 | 
			
		||||
    wght = value[CONF_WEIGHT]
 | 
			
		||||
    if value[CONF_ITALIC]:
 | 
			
		||||
        wght = f"1,{wght}"
 | 
			
		||||
    name = f"{value[CONF_FAMILY]}@{value[CONF_WEIGHT]}"
 | 
			
		||||
    url = f"https://fonts.googleapis.com/css2?family={value[CONF_FAMILY]}:wght@{wght}"
 | 
			
		||||
    name = (
 | 
			
		||||
        f"{value[CONF_FAMILY]}:ital,wght@{int(value[CONF_ITALIC])},{value[CONF_WEIGHT]}"
 | 
			
		||||
    )
 | 
			
		||||
    url = f"https://fonts.googleapis.com/css2?family={name}"
 | 
			
		||||
 | 
			
		||||
    path = _compute_gfonts_local_path(value)
 | 
			
		||||
    if path.is_file():
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,14 @@
 | 
			
		||||
from esphome.components.logger import USB_CDC, USB_SERIAL_JTAG
 | 
			
		||||
from esphome.components import improv_base
 | 
			
		||||
from esphome.components.esp32 import get_esp32_variant
 | 
			
		||||
from esphome.components.esp32.const import (
 | 
			
		||||
    VARIANT_ESP32S3,
 | 
			
		||||
)
 | 
			
		||||
from esphome.components.logger import USB_CDC
 | 
			
		||||
from esphome.const import CONF_BAUD_RATE, CONF_HARDWARE_UART, CONF_ID, CONF_LOGGER
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.core import CORE
 | 
			
		||||
import esphome.final_validate as fv
 | 
			
		||||
from esphome.components import improv_base
 | 
			
		||||
 | 
			
		||||
AUTO_LOAD = ["improv_base"]
 | 
			
		||||
CODEOWNERS = ["@esphome/core"]
 | 
			
		||||
@@ -30,7 +34,10 @@ def validate_logger(config):
 | 
			
		||||
    if logger_conf[CONF_BAUD_RATE] == 0:
 | 
			
		||||
        raise cv.Invalid("improv_serial requires the logger baud_rate to be not 0")
 | 
			
		||||
    if CORE.using_esp_idf:
 | 
			
		||||
        if logger_conf[CONF_HARDWARE_UART] in [USB_SERIAL_JTAG, USB_CDC]:
 | 
			
		||||
        if (
 | 
			
		||||
            logger_conf[CONF_HARDWARE_UART] == USB_CDC
 | 
			
		||||
            and get_esp32_variant() == VARIANT_ESP32S3
 | 
			
		||||
        ):
 | 
			
		||||
            raise cv.Invalid(
 | 
			
		||||
                "improv_serial does not support the selected logger hardware_uart"
 | 
			
		||||
            )
 | 
			
		||||
 
 | 
			
		||||
@@ -31,26 +31,57 @@ void ImprovSerialComponent::setup() {
 | 
			
		||||
 | 
			
		||||
void ImprovSerialComponent::dump_config() { ESP_LOGCONFIG(TAG, "Improv Serial:"); }
 | 
			
		||||
 | 
			
		||||
int ImprovSerialComponent::available_() {
 | 
			
		||||
optional<uint8_t> ImprovSerialComponent::read_byte_() {
 | 
			
		||||
  optional<uint8_t> byte;
 | 
			
		||||
  uint8_t data = 0;
 | 
			
		||||
#ifdef USE_ARDUINO
 | 
			
		||||
  return this->hw_serial_->available();
 | 
			
		||||
  if (this->hw_serial_->available()) {
 | 
			
		||||
    this->hw_serial_->readBytes(&data, 1);
 | 
			
		||||
    byte = data;
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_ESP_IDF
 | 
			
		||||
  size_t available;
 | 
			
		||||
  uart_get_buffered_data_len(this->uart_num_, &available);
 | 
			
		||||
  return available;
 | 
			
		||||
  switch (logger::global_logger->get_uart()) {
 | 
			
		||||
    case logger::UART_SELECTION_UART0:
 | 
			
		||||
    case logger::UART_SELECTION_UART1:
 | 
			
		||||
#if !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32C6) && \
 | 
			
		||||
    !defined(USE_ESP32_VARIANT_ESP32S2) && !defined(USE_ESP32_VARIANT_ESP32S3)
 | 
			
		||||
    case logger::UART_SELECTION_UART2:
 | 
			
		||||
#endif  // !USE_ESP32_VARIANT_ESP32C3 && !USE_ESP32_VARIANT_ESP32S2 && !USE_ESP32_VARIANT_ESP32S3
 | 
			
		||||
      if (this->uart_num_ >= 0) {
 | 
			
		||||
        size_t available;
 | 
			
		||||
        uart_get_buffered_data_len(this->uart_num_, &available);
 | 
			
		||||
        if (available) {
 | 
			
		||||
          uart_read_bytes(this->uart_num_, &data, 1, 20 / portTICK_PERIOD_MS);
 | 
			
		||||
          byte = data;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
#if defined(CONFIG_ESP_CONSOLE_USB_CDC) && (defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3))
 | 
			
		||||
    case logger::UART_SELECTION_USB_CDC:
 | 
			
		||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
 | 
			
		||||
      if (esp_usb_console_available_for_read()) {
 | 
			
		||||
#else
 | 
			
		||||
      if (esp_usb_console_read_available()) {
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t ImprovSerialComponent::read_byte_() {
 | 
			
		||||
  uint8_t data;
 | 
			
		||||
#ifdef USE_ARDUINO
 | 
			
		||||
  this->hw_serial_->readBytes(&data, 1);
 | 
			
		||||
        esp_usb_console_read_buf((char *) &data, 1);
 | 
			
		||||
        byte = data;
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
#endif  // USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3
 | 
			
		||||
#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S3)
 | 
			
		||||
    case logger::UART_SELECTION_USB_SERIAL_JTAG: {
 | 
			
		||||
      if (usb_serial_jtag_read_bytes((char *) &data, 1, 20 / portTICK_PERIOD_MS)) {
 | 
			
		||||
        byte = data;
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
#endif  // USE_ESP32_VARIANT_ESP32C3 || USE_ESP32_VARIANT_ESP32C6 || USE_ESP32_VARIANT_ESP32S3
 | 
			
		||||
    default:
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_ESP_IDF
 | 
			
		||||
  uart_read_bytes(this->uart_num_, &data, 1, 20 / portTICK_PERIOD_MS);
 | 
			
		||||
#endif
 | 
			
		||||
  return data;
 | 
			
		||||
  return byte;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ImprovSerialComponent::write_data_(std::vector<uint8_t> &data) {
 | 
			
		||||
@@ -59,24 +90,49 @@ void ImprovSerialComponent::write_data_(std::vector<uint8_t> &data) {
 | 
			
		||||
  this->hw_serial_->write(data.data(), data.size());
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_ESP_IDF
 | 
			
		||||
  uart_write_bytes(this->uart_num_, data.data(), data.size());
 | 
			
		||||
  switch (logger::global_logger->get_uart()) {
 | 
			
		||||
    case logger::UART_SELECTION_UART0:
 | 
			
		||||
    case logger::UART_SELECTION_UART1:
 | 
			
		||||
#if !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32C6) && \
 | 
			
		||||
    !defined(USE_ESP32_VARIANT_ESP32S2) && !defined(USE_ESP32_VARIANT_ESP32S3)
 | 
			
		||||
    case logger::UART_SELECTION_UART2:
 | 
			
		||||
#endif  // !USE_ESP32_VARIANT_ESP32C3 && !USE_ESP32_VARIANT_ESP32S2 && !USE_ESP32_VARIANT_ESP32S3
 | 
			
		||||
      uart_write_bytes(this->uart_num_, data.data(), data.size());
 | 
			
		||||
      break;
 | 
			
		||||
#if defined(CONFIG_ESP_CONSOLE_USB_CDC) && (defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3))
 | 
			
		||||
    case logger::UART_SELECTION_USB_CDC: {
 | 
			
		||||
      const char *msg = (char *) data.data();
 | 
			
		||||
      esp_usb_console_write_buf(msg, data.size());
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
#endif  // USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3
 | 
			
		||||
#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S3)
 | 
			
		||||
    case logger::UART_SELECTION_USB_SERIAL_JTAG:
 | 
			
		||||
      usb_serial_jtag_write_bytes((char *) data.data(), data.size(), 20 / portTICK_PERIOD_MS);
 | 
			
		||||
      break;
 | 
			
		||||
#endif  // USE_ESP32_VARIANT_ESP32C3 || USE_ESP32_VARIANT_ESP32S3
 | 
			
		||||
    default:
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ImprovSerialComponent::loop() {
 | 
			
		||||
  const uint32_t now = millis();
 | 
			
		||||
  if (now - this->last_read_byte_ > 50) {
 | 
			
		||||
  if (this->last_read_byte_ && (millis() - this->last_read_byte_ > IMPROV_SERIAL_TIMEOUT)) {
 | 
			
		||||
    this->last_read_byte_ = 0;
 | 
			
		||||
    this->rx_buffer_.clear();
 | 
			
		||||
    this->last_read_byte_ = now;
 | 
			
		||||
    ESP_LOGV(TAG, "Improv Serial timeout");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  while (this->available_()) {
 | 
			
		||||
    uint8_t byte = this->read_byte_();
 | 
			
		||||
    if (this->parse_improv_serial_byte_(byte)) {
 | 
			
		||||
      this->last_read_byte_ = now;
 | 
			
		||||
  auto byte = this->read_byte_();
 | 
			
		||||
  while (byte.has_value()) {
 | 
			
		||||
    if (this->parse_improv_serial_byte_(byte.value())) {
 | 
			
		||||
      this->last_read_byte_ = millis();
 | 
			
		||||
    } else {
 | 
			
		||||
      this->last_read_byte_ = 0;
 | 
			
		||||
      this->rx_buffer_.clear();
 | 
			
		||||
    }
 | 
			
		||||
    byte = this->read_byte_();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->state_ == improv::STATE_PROVISIONING) {
 | 
			
		||||
 
 | 
			
		||||
@@ -14,6 +14,13 @@
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_ESP_IDF
 | 
			
		||||
#include <driver/uart.h>
 | 
			
		||||
#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S3) || \
 | 
			
		||||
    defined(USE_ESP32_VARIANT_ESP32H2)
 | 
			
		||||
#include <driver/usb_serial_jtag.h>
 | 
			
		||||
#endif
 | 
			
		||||
#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
 | 
			
		||||
#include <esp_private/usb_console.h>
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
@@ -26,6 +33,7 @@ enum ImprovSerialType : uint8_t {
 | 
			
		||||
  TYPE_RPC_RESPONSE = 0x04
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static const uint16_t IMPROV_SERIAL_TIMEOUT = 100;
 | 
			
		||||
static const uint8_t IMPROV_SERIAL_VERSION = 1;
 | 
			
		||||
 | 
			
		||||
class ImprovSerialComponent : public Component, public improv_base::ImprovBase {
 | 
			
		||||
@@ -48,8 +56,7 @@ class ImprovSerialComponent : public Component, public improv_base::ImprovBase {
 | 
			
		||||
  std::vector<uint8_t> build_rpc_settings_response_(improv::Command command);
 | 
			
		||||
  std::vector<uint8_t> build_version_info_();
 | 
			
		||||
 | 
			
		||||
  int available_();
 | 
			
		||||
  uint8_t read_byte_();
 | 
			
		||||
  optional<uint8_t> read_byte_();
 | 
			
		||||
  void write_data_(std::vector<uint8_t> &data);
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ARDUINO
 | 
			
		||||
 
 | 
			
		||||
@@ -3,8 +3,21 @@
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP_IDF
 | 
			
		||||
#include <driver/uart.h>
 | 
			
		||||
 | 
			
		||||
#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S3) || \
 | 
			
		||||
    defined(USE_ESP32_VARIANT_ESP32H2)
 | 
			
		||||
#include <driver/usb_serial_jtag.h>
 | 
			
		||||
#include <esp_vfs_dev.h>
 | 
			
		||||
#include <esp_vfs_usb_serial_jtag.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include "freertos/FreeRTOS.h"
 | 
			
		||||
#include "esp_idf_version.h"
 | 
			
		||||
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include <fcntl.h>
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ESP_IDF
 | 
			
		||||
 | 
			
		||||
#if defined(USE_ESP32_FRAMEWORK_ARDUINO) || defined(USE_ESP_IDF)
 | 
			
		||||
@@ -93,6 +106,58 @@ void Logger::log_vprintf_(int level, const char *tag, int line, const __FlashStr
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP_IDF
 | 
			
		||||
void Logger::init_uart_() {
 | 
			
		||||
  uart_config_t uart_config{};
 | 
			
		||||
  uart_config.baud_rate = (int) baud_rate_;
 | 
			
		||||
  uart_config.data_bits = UART_DATA_8_BITS;
 | 
			
		||||
  uart_config.parity = UART_PARITY_DISABLE;
 | 
			
		||||
  uart_config.stop_bits = UART_STOP_BITS_1;
 | 
			
		||||
  uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
 | 
			
		||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
 | 
			
		||||
  uart_config.source_clk = UART_SCLK_DEFAULT;
 | 
			
		||||
#endif
 | 
			
		||||
  uart_param_config(this->uart_num_, &uart_config);
 | 
			
		||||
  const int uart_buffer_size = tx_buffer_size_;
 | 
			
		||||
  // Install UART driver using an event queue here
 | 
			
		||||
  uart_driver_install(this->uart_num_, uart_buffer_size, uart_buffer_size, 10, nullptr, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
 | 
			
		||||
void Logger::init_usb_cdc_() {}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S3) || \
 | 
			
		||||
    defined(USE_ESP32_VARIANT_ESP32H2)
 | 
			
		||||
void Logger::init_usb_serial_jtag_() {
 | 
			
		||||
  setvbuf(stdin, NULL, _IONBF, 0);  // Disable buffering on stdin
 | 
			
		||||
 | 
			
		||||
  // Minicom, screen, idf_monitor send CR when ENTER key is pressed
 | 
			
		||||
  esp_vfs_dev_usb_serial_jtag_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
 | 
			
		||||
  // Move the caret to the beginning of the next line on '\n'
 | 
			
		||||
  esp_vfs_dev_usb_serial_jtag_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
 | 
			
		||||
 | 
			
		||||
  // Enable non-blocking mode on stdin and stdout
 | 
			
		||||
  fcntl(fileno(stdout), F_SETFL, 0);
 | 
			
		||||
  fcntl(fileno(stdin), F_SETFL, 0);
 | 
			
		||||
 | 
			
		||||
  usb_serial_jtag_driver_config_t usb_serial_jtag_config{};
 | 
			
		||||
  usb_serial_jtag_config.rx_buffer_size = 512;
 | 
			
		||||
  usb_serial_jtag_config.tx_buffer_size = 512;
 | 
			
		||||
 | 
			
		||||
  esp_err_t ret = ESP_OK;
 | 
			
		||||
  // Install USB-SERIAL-JTAG driver for interrupt-driven reads and writes
 | 
			
		||||
  ret = usb_serial_jtag_driver_install(&usb_serial_jtag_config);
 | 
			
		||||
  if (ret != ESP_OK) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Tell vfs to use usb-serial-jtag driver
 | 
			
		||||
  esp_vfs_usb_serial_jtag_use_driver();
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
int HOT Logger::level_for(const char *tag) {
 | 
			
		||||
  // Uses std::vector<> for low memory footprint, though the vector
 | 
			
		||||
  // could be sorted to minimize lookup times. This feature isn't used that
 | 
			
		||||
@@ -120,19 +185,19 @@ void HOT Logger::log_message_(int level, const char *tag, int offset) {
 | 
			
		||||
#ifdef USE_ESP_IDF
 | 
			
		||||
    if (
 | 
			
		||||
#if defined(USE_ESP32_VARIANT_ESP32S2)
 | 
			
		||||
        uart_ == UART_SELECTION_USB_CDC
 | 
			
		||||
        this->uart_ == UART_SELECTION_USB_CDC
 | 
			
		||||
#elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32H2)
 | 
			
		||||
        uart_ == UART_SELECTION_USB_SERIAL_JTAG
 | 
			
		||||
        this->uart_ == UART_SELECTION_USB_SERIAL_JTAG
 | 
			
		||||
#elif defined(USE_ESP32_VARIANT_ESP32S3)
 | 
			
		||||
        uart_ == UART_SELECTION_USB_CDC || uart_ == UART_SELECTION_USB_SERIAL_JTAG
 | 
			
		||||
        this->uart_ == UART_SELECTION_USB_CDC || this->uart_ == UART_SELECTION_USB_SERIAL_JTAG
 | 
			
		||||
#else
 | 
			
		||||
        /* DISABLES CODE */ (false)  // NOLINT
 | 
			
		||||
#endif
 | 
			
		||||
    ) {
 | 
			
		||||
      puts(msg);
 | 
			
		||||
    } else {
 | 
			
		||||
      uart_write_bytes(uart_num_, msg, strlen(msg));
 | 
			
		||||
      uart_write_bytes(uart_num_, "\n", 1);
 | 
			
		||||
      uart_write_bytes(this->uart_num_, msg, strlen(msg));
 | 
			
		||||
      uart_write_bytes(this->uart_num_, "\n", 1);
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
  }
 | 
			
		||||
@@ -209,48 +274,38 @@ void Logger::pre_setup() {
 | 
			
		||||
    }
 | 
			
		||||
#endif  // USE_ARDUINO
 | 
			
		||||
#ifdef USE_ESP_IDF
 | 
			
		||||
    uart_num_ = UART_NUM_0;
 | 
			
		||||
    switch (uart_) {
 | 
			
		||||
    this->uart_num_ = UART_NUM_0;
 | 
			
		||||
    switch (this->uart_) {
 | 
			
		||||
      case UART_SELECTION_UART0:
 | 
			
		||||
        uart_num_ = UART_NUM_0;
 | 
			
		||||
        this->uart_num_ = UART_NUM_0;
 | 
			
		||||
        break;
 | 
			
		||||
      case UART_SELECTION_UART1:
 | 
			
		||||
        uart_num_ = UART_NUM_1;
 | 
			
		||||
        this->uart_num_ = UART_NUM_1;
 | 
			
		||||
        break;
 | 
			
		||||
#if !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32C6) && \
 | 
			
		||||
    !defined(USE_ESP32_VARIANT_ESP32S2) && !defined(USE_ESP32_VARIANT_ESP32S3) && !defined(USE_ESP32_VARIANT_ESP32H2)
 | 
			
		||||
      case UART_SELECTION_UART2:
 | 
			
		||||
        uart_num_ = UART_NUM_2;
 | 
			
		||||
        this->uart_num_ = UART_NUM_2;
 | 
			
		||||
        break;
 | 
			
		||||
#endif  // !USE_ESP32_VARIANT_ESP32C3 && !USE_ESP32_VARIANT_ESP32S2 && !USE_ESP32_VARIANT_ESP32S3 &&
 | 
			
		||||
        // !USE_ESP32_VARIANT_ESP32H2
 | 
			
		||||
#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
 | 
			
		||||
      case UART_SELECTION_USB_CDC:
 | 
			
		||||
        uart_num_ = -1;
 | 
			
		||||
        this->uart_num_ = -1;
 | 
			
		||||
        this->init_usb_cdc_();
 | 
			
		||||
        break;
 | 
			
		||||
#endif  // USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3
 | 
			
		||||
#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S3) || \
 | 
			
		||||
    defined(USE_ESP32_VARIANT_ESP32H2)
 | 
			
		||||
      case UART_SELECTION_USB_SERIAL_JTAG:
 | 
			
		||||
        uart_num_ = -1;
 | 
			
		||||
        this->uart_num_ = -1;
 | 
			
		||||
        this->init_usb_serial_jtag_();
 | 
			
		||||
        break;
 | 
			
		||||
#endif  // USE_ESP32_VARIANT_ESP32C3 || USE_ESP32_VARIANT_ESP32C6 || USE_ESP32_VARIANT_ESP32S3 ||
 | 
			
		||||
        // USE_ESP32_VARIANT_ESP32H2
 | 
			
		||||
    }
 | 
			
		||||
    if (uart_num_ >= 0) {
 | 
			
		||||
      uart_config_t uart_config{};
 | 
			
		||||
      uart_config.baud_rate = (int) baud_rate_;
 | 
			
		||||
      uart_config.data_bits = UART_DATA_8_BITS;
 | 
			
		||||
      uart_config.parity = UART_PARITY_DISABLE;
 | 
			
		||||
      uart_config.stop_bits = UART_STOP_BITS_1;
 | 
			
		||||
      uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
 | 
			
		||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
 | 
			
		||||
      uart_config.source_clk = UART_SCLK_DEFAULT;
 | 
			
		||||
#endif
 | 
			
		||||
      uart_param_config(uart_num_, &uart_config);
 | 
			
		||||
      const int uart_buffer_size = tx_buffer_size_;
 | 
			
		||||
      // Install UART driver using an event queue here
 | 
			
		||||
      uart_driver_install(uart_num_, uart_buffer_size, uart_buffer_size, 10, nullptr, 0);
 | 
			
		||||
    if (this->uart_num_ >= 0) {
 | 
			
		||||
      this->init_uart_();
 | 
			
		||||
    }
 | 
			
		||||
#endif  // USE_ESP_IDF
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -107,6 +107,16 @@ class Logger : public Component {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
#ifdef USE_ESP_IDF
 | 
			
		||||
  void init_uart_();
 | 
			
		||||
#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
 | 
			
		||||
  void init_usb_cdc_();
 | 
			
		||||
#endif
 | 
			
		||||
#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S3) || \
 | 
			
		||||
    defined(USE_ESP32_VARIANT_ESP32H2)
 | 
			
		||||
  void init_usb_serial_jtag_();
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
  void write_header_(int level, const char *tag, int line);
 | 
			
		||||
  void write_footer_();
 | 
			
		||||
  void log_message_(int level, const char *tag, int offset = 0);
 | 
			
		||||
 
 | 
			
		||||
@@ -19,7 +19,7 @@ class MQTTBackendESP8266 final : public MQTTBackend {
 | 
			
		||||
  void set_will(const char *topic, uint8_t qos, bool retain, const char *payload) final {
 | 
			
		||||
    mqtt_client_.setWill(topic, qos, retain, payload);
 | 
			
		||||
  }
 | 
			
		||||
  void set_server(network::IPAddress ip, uint16_t port) final { mqtt_client_.setServer(IPAddress(ip), port); }
 | 
			
		||||
  void set_server(network::IPAddress ip, uint16_t port) final { mqtt_client_.setServer(ip, port); }
 | 
			
		||||
  void set_server(const char *host, uint16_t port) final { mqtt_client_.setServer(host, port); }
 | 
			
		||||
#if ASYNC_TCP_SSL_ENABLED
 | 
			
		||||
  void set_secure(bool secure) { mqtt_client.setSecure(secure); }
 | 
			
		||||
 
 | 
			
		||||
@@ -19,9 +19,7 @@ class MQTTBackendLibreTiny final : public MQTTBackend {
 | 
			
		||||
  void set_will(const char *topic, uint8_t qos, bool retain, const char *payload) final {
 | 
			
		||||
    mqtt_client_.setWill(topic, qos, retain, payload);
 | 
			
		||||
  }
 | 
			
		||||
  void set_server(network::IPAddress ip, uint16_t port) final {
 | 
			
		||||
    mqtt_client_.setServer(IPAddress(static_cast<uint32_t>(ip)), port);
 | 
			
		||||
  }
 | 
			
		||||
  void set_server(network::IPAddress ip, uint16_t port) final { mqtt_client_.setServer(IPAddress(ip), port); }
 | 
			
		||||
  void set_server(const char *host, uint16_t port) final { mqtt_client_.setServer(host, port); }
 | 
			
		||||
#if ASYNC_TCP_SSL_ENABLED
 | 
			
		||||
  void set_secure(bool secure) { mqtt_client.setSecure(secure); }
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,11 @@
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include <array>
 | 
			
		||||
#include "esphome/core/macros.h"
 | 
			
		||||
 | 
			
		||||
#if defined(USE_ESP_IDF) || defined(USE_LIBRETINY) || USE_ARDUINO_VERSION_CODE > VERSION_CODE(3, 0, 0)
 | 
			
		||||
#include <lwip/ip_addr.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if USE_ARDUINO
 | 
			
		||||
#include <Arduino.h>
 | 
			
		||||
@@ -34,7 +38,12 @@ struct IPAddress {
 | 
			
		||||
  }
 | 
			
		||||
  IPAddress(const ip_addr_t *other_ip) { ip_addr_copy(ip_addr_, *other_ip); }
 | 
			
		||||
  IPAddress(const std::string &in_address) { ipaddr_aton(in_address.c_str(), &ip_addr_); }
 | 
			
		||||
  IPAddress(ip4_addr_t *other_ip) { memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip4_addr_t)); }
 | 
			
		||||
  IPAddress(ip4_addr_t *other_ip) {
 | 
			
		||||
    memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip4_addr_t));
 | 
			
		||||
#if USE_ESP32 && LWIP_IPV6
 | 
			
		||||
    ip_addr_.type = IPADDR_TYPE_V4;
 | 
			
		||||
#endif
 | 
			
		||||
  }
 | 
			
		||||
#if USE_ARDUINO
 | 
			
		||||
  IPAddress(const arduino_ns::IPAddress &other_ip) { ip_addr_set_ip4_u32(&ip_addr_, other_ip); }
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,13 @@ namespace pulse_meter {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "pulse_meter";
 | 
			
		||||
 | 
			
		||||
void PulseMeterSensor::set_total_pulses(uint32_t pulses) {
 | 
			
		||||
  this->total_pulses_ = pulses;
 | 
			
		||||
  if (this->total_sensor_ != nullptr) {
 | 
			
		||||
    this->total_sensor_->publish_state(this->total_pulses_);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void PulseMeterSensor::setup() {
 | 
			
		||||
  this->pin_->setup();
 | 
			
		||||
  this->isr_pin_ = pin_->to_isr();
 | 
			
		||||
 
 | 
			
		||||
@@ -20,7 +20,8 @@ class PulseMeterSensor : public sensor::Sensor, public Component {
 | 
			
		||||
  void set_timeout_us(uint32_t timeout) { this->timeout_us_ = timeout; }
 | 
			
		||||
  void set_total_sensor(sensor::Sensor *sensor) { this->total_sensor_ = sensor; }
 | 
			
		||||
  void set_filter_mode(InternalFilterMode mode) { this->filter_mode_ = mode; }
 | 
			
		||||
  void set_total_pulses(uint32_t pulses) { this->total_pulses_ = pulses; }
 | 
			
		||||
 | 
			
		||||
  void set_total_pulses(uint32_t pulses);
 | 
			
		||||
 | 
			
		||||
  void setup() override;
 | 
			
		||||
  void loop() override;
 | 
			
		||||
 
 | 
			
		||||
@@ -19,7 +19,7 @@ from esphome.const import (
 | 
			
		||||
)
 | 
			
		||||
from esphome.core import CORE
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@stevebaxter", "@cstaahl"]
 | 
			
		||||
CODEOWNERS = ["@stevebaxter", "@cstaahl", "@TrentHouliston"]
 | 
			
		||||
 | 
			
		||||
pulse_meter_ns = cg.esphome_ns.namespace("pulse_meter")
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,7 @@ namespace socket {
 | 
			
		||||
Socket::~Socket() {}
 | 
			
		||||
 | 
			
		||||
std::unique_ptr<Socket> socket_ip(int type, int protocol) {
 | 
			
		||||
#if LWIP_IPV6
 | 
			
		||||
#if ENABLE_IPV6
 | 
			
		||||
  return socket(AF_INET6, type, protocol);
 | 
			
		||||
#else
 | 
			
		||||
  return socket(AF_INET, type, protocol);
 | 
			
		||||
@@ -18,7 +18,7 @@ std::unique_ptr<Socket> socket_ip(int type, int protocol) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const std::string &ip_address, uint16_t port) {
 | 
			
		||||
#if LWIP_IPV6
 | 
			
		||||
#if ENABLE_IPV6
 | 
			
		||||
  if (addrlen < sizeof(sockaddr_in6)) {
 | 
			
		||||
    errno = EINVAL;
 | 
			
		||||
    return 0;
 | 
			
		||||
@@ -51,7 +51,7 @@ socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const std::stri
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port) {
 | 
			
		||||
#if LWIP_IPV6
 | 
			
		||||
#if ENABLE_IPV6
 | 
			
		||||
  if (addrlen < sizeof(sockaddr_in6)) {
 | 
			
		||||
    errno = EINVAL;
 | 
			
		||||
    return 0;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,5 @@
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
#include "tuya_text_sensor.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace tuya {
 | 
			
		||||
@@ -19,6 +19,12 @@ void TuyaTextSensor::setup() {
 | 
			
		||||
        this->publish_state(data);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      case TuyaDatapointType::ENUM: {
 | 
			
		||||
        std::string data = to_string(datapoint.value_enum);
 | 
			
		||||
        ESP_LOGD(TAG, "MCU reported text sensor %u is: %s", datapoint.id, data.c_str());
 | 
			
		||||
        this->publish_state(data);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      default:
 | 
			
		||||
        ESP_LOGW(TAG, "Unsupported data type for tuya text sensor %u: %#02hhX", datapoint.id, (uint8_t) datapoint.type);
 | 
			
		||||
        break;
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,8 @@ from esphome.const import (
 | 
			
		||||
    CONF_MICROPHONE,
 | 
			
		||||
    CONF_SPEAKER,
 | 
			
		||||
    CONF_MEDIA_PLAYER,
 | 
			
		||||
    CONF_ON_CLIENT_CONNECTED,
 | 
			
		||||
    CONF_ON_CLIENT_DISCONNECTED,
 | 
			
		||||
)
 | 
			
		||||
from esphome import automation
 | 
			
		||||
from esphome.automation import register_action, register_condition
 | 
			
		||||
@@ -80,6 +82,12 @@ CONFIG_SCHEMA = cv.All(
 | 
			
		||||
            cv.Optional(CONF_ON_TTS_END): automation.validate_automation(single=True),
 | 
			
		||||
            cv.Optional(CONF_ON_END): automation.validate_automation(single=True),
 | 
			
		||||
            cv.Optional(CONF_ON_ERROR): automation.validate_automation(single=True),
 | 
			
		||||
            cv.Optional(CONF_ON_CLIENT_CONNECTED): automation.validate_automation(
 | 
			
		||||
                single=True
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_ON_CLIENT_DISCONNECTED): automation.validate_automation(
 | 
			
		||||
                single=True
 | 
			
		||||
            ),
 | 
			
		||||
        }
 | 
			
		||||
    ).extend(cv.COMPONENT_SCHEMA),
 | 
			
		||||
)
 | 
			
		||||
@@ -155,6 +163,20 @@ async def to_code(config):
 | 
			
		||||
            config[CONF_ON_ERROR],
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    if CONF_ON_CLIENT_CONNECTED in config:
 | 
			
		||||
        await automation.build_automation(
 | 
			
		||||
            var.get_client_connected_trigger(),
 | 
			
		||||
            [],
 | 
			
		||||
            config[CONF_ON_CLIENT_CONNECTED],
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    if CONF_ON_CLIENT_DISCONNECTED in config:
 | 
			
		||||
        await automation.build_automation(
 | 
			
		||||
            var.get_client_disconnected_trigger(),
 | 
			
		||||
            [],
 | 
			
		||||
            config[CONF_ON_CLIENT_DISCONNECTED],
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    cg.add_define("USE_VOICE_ASSISTANT")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -127,8 +127,8 @@ int VoiceAssistant::read_microphone_() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void VoiceAssistant::loop() {
 | 
			
		||||
  if (this->state_ != State::IDLE && this->state_ != State::STOP_MICROPHONE &&
 | 
			
		||||
      this->state_ != State::STOPPING_MICROPHONE && !api::global_api_server->is_connected()) {
 | 
			
		||||
  if (this->api_client_ == nullptr && this->state_ != State::IDLE && this->state_ != State::STOP_MICROPHONE &&
 | 
			
		||||
      this->state_ != State::STOPPING_MICROPHONE) {
 | 
			
		||||
    if (this->mic_->is_running() || this->state_ == State::STARTING_MICROPHONE) {
 | 
			
		||||
      this->set_state_(State::STOP_MICROPHONE, State::IDLE);
 | 
			
		||||
    } else {
 | 
			
		||||
@@ -213,7 +213,14 @@ void VoiceAssistant::loop() {
 | 
			
		||||
      audio_settings.noise_suppression_level = this->noise_suppression_level_;
 | 
			
		||||
      audio_settings.auto_gain = this->auto_gain_;
 | 
			
		||||
      audio_settings.volume_multiplier = this->volume_multiplier_;
 | 
			
		||||
      if (!api::global_api_server->start_voice_assistant(this->conversation_id_, flags, audio_settings)) {
 | 
			
		||||
 | 
			
		||||
      api::VoiceAssistantRequest msg;
 | 
			
		||||
      msg.start = true;
 | 
			
		||||
      msg.conversation_id = this->conversation_id_;
 | 
			
		||||
      msg.flags = flags;
 | 
			
		||||
      msg.audio_settings = audio_settings;
 | 
			
		||||
 | 
			
		||||
      if (this->api_client_ == nullptr || !this->api_client_->send_voice_assistant_request(msg)) {
 | 
			
		||||
        ESP_LOGW(TAG, "Could not request start.");
 | 
			
		||||
        this->error_trigger_->trigger("not-connected", "Could not request start.");
 | 
			
		||||
        this->continuous_ = false;
 | 
			
		||||
@@ -316,8 +323,8 @@ void VoiceAssistant::loop() {
 | 
			
		||||
        this->speaker_buffer_index_ = 0;
 | 
			
		||||
        memset(this->speaker_buffer_, 0, SPEAKER_BUFFER_SIZE);
 | 
			
		||||
      }
 | 
			
		||||
#endif
 | 
			
		||||
      this->wait_for_stream_end_ = false;
 | 
			
		||||
#endif
 | 
			
		||||
      this->set_state_(State::IDLE, State::IDLE);
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
@@ -326,6 +333,28 @@ void VoiceAssistant::loop() {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void VoiceAssistant::client_subscription(api::APIConnection *client, bool subscribe) {
 | 
			
		||||
  if (!subscribe) {
 | 
			
		||||
    if (this->api_client_ == nullptr || client != this->api_client_) {
 | 
			
		||||
      ESP_LOGE(TAG, "Client attempting to unsubscribe that is not the current API Client");
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    this->api_client_ = nullptr;
 | 
			
		||||
    this->client_disconnected_trigger_->trigger();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->api_client_ != nullptr) {
 | 
			
		||||
    ESP_LOGE(TAG, "Multiple API Clients attempting to connect to Voice Assistant");
 | 
			
		||||
    ESP_LOGE(TAG, "Current client: %s", this->api_client_->get_client_combined_info().c_str());
 | 
			
		||||
    ESP_LOGE(TAG, "New client: %s", client->get_client_combined_info().c_str());
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->api_client_ = client;
 | 
			
		||||
  this->client_connected_trigger_->trigger();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const LogString *voice_assistant_state_to_string(State state) {
 | 
			
		||||
  switch (state) {
 | 
			
		||||
    case State::IDLE:
 | 
			
		||||
@@ -408,7 +437,7 @@ void VoiceAssistant::start_streaming(struct sockaddr_storage *addr, uint16_t por
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void VoiceAssistant::request_start(bool continuous, bool silence_detection) {
 | 
			
		||||
  if (!api::global_api_server->is_connected()) {
 | 
			
		||||
  if (this->api_client_ == nullptr) {
 | 
			
		||||
    ESP_LOGE(TAG, "No API client connected");
 | 
			
		||||
    this->set_state_(State::IDLE, State::IDLE);
 | 
			
		||||
    this->continuous_ = false;
 | 
			
		||||
@@ -459,9 +488,14 @@ void VoiceAssistant::request_stop() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void VoiceAssistant::signal_stop_() {
 | 
			
		||||
  ESP_LOGD(TAG, "Signaling stop...");
 | 
			
		||||
  api::global_api_server->stop_voice_assistant();
 | 
			
		||||
  memset(&this->dest_addr_, 0, sizeof(this->dest_addr_));
 | 
			
		||||
  if (this->api_client_ == nullptr) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGD(TAG, "Signaling stop...");
 | 
			
		||||
  api::VoiceAssistantRequest msg;
 | 
			
		||||
  msg.start = false;
 | 
			
		||||
  this->api_client_->send_voice_assistant_request(msg);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void VoiceAssistant::on_event(const api::VoiceAssistantEventResponse &msg) {
 | 
			
		||||
@@ -586,7 +620,9 @@ void VoiceAssistant::on_event(const api::VoiceAssistantEventResponse &msg) {
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case api::enums::VOICE_ASSISTANT_TTS_STREAM_START: {
 | 
			
		||||
#ifdef USE_SPEAKER
 | 
			
		||||
      this->wait_for_stream_end_ = true;
 | 
			
		||||
#endif
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case api::enums::VOICE_ASSISTANT_TTS_STREAM_END: {
 | 
			
		||||
 
 | 
			
		||||
@@ -8,8 +8,8 @@
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/api/api_connection.h"
 | 
			
		||||
#include "esphome/components/api/api_pb2.h"
 | 
			
		||||
#include "esphome/components/api/api_server.h"
 | 
			
		||||
#include "esphome/components/microphone/microphone.h"
 | 
			
		||||
#ifdef USE_SPEAKER
 | 
			
		||||
#include "esphome/components/speaker/speaker.h"
 | 
			
		||||
@@ -109,6 +109,12 @@ class VoiceAssistant : public Component {
 | 
			
		||||
  Trigger<> *get_end_trigger() const { return this->end_trigger_; }
 | 
			
		||||
  Trigger<std::string, std::string> *get_error_trigger() const { return this->error_trigger_; }
 | 
			
		||||
 | 
			
		||||
  Trigger<> *get_client_connected_trigger() const { return this->client_connected_trigger_; }
 | 
			
		||||
  Trigger<> *get_client_disconnected_trigger() const { return this->client_disconnected_trigger_; }
 | 
			
		||||
 | 
			
		||||
  void client_subscription(api::APIConnection *client, bool subscribe);
 | 
			
		||||
  api::APIConnection *get_api_connection() const { return this->api_client_; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  int read_microphone_();
 | 
			
		||||
  void set_state_(State state);
 | 
			
		||||
@@ -127,6 +133,11 @@ class VoiceAssistant : public Component {
 | 
			
		||||
  Trigger<> *end_trigger_ = new Trigger<>();
 | 
			
		||||
  Trigger<std::string, std::string> *error_trigger_ = new Trigger<std::string, std::string>();
 | 
			
		||||
 | 
			
		||||
  Trigger<> *client_connected_trigger_ = new Trigger<>();
 | 
			
		||||
  Trigger<> *client_disconnected_trigger_ = new Trigger<>();
 | 
			
		||||
 | 
			
		||||
  api::APIConnection *api_client_{nullptr};
 | 
			
		||||
 | 
			
		||||
  microphone::Microphone *mic_{nullptr};
 | 
			
		||||
#ifdef USE_SPEAKER
 | 
			
		||||
  speaker::Speaker *speaker_{nullptr};
 | 
			
		||||
 
 | 
			
		||||
@@ -8,16 +8,16 @@
 | 
			
		||||
#include <user_interface.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include <utility>
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
#include "lwip/err.h"
 | 
			
		||||
#include <utility>
 | 
			
		||||
#include "lwip/dns.h"
 | 
			
		||||
#include "lwip/err.h"
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/application.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
#include "esphome/core/util.h"
 | 
			
		||||
#include "esphome/core/application.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_CAPTIVE_PORTAL
 | 
			
		||||
#include "esphome/components/captive_portal/captive_portal.h"
 | 
			
		||||
@@ -96,7 +96,7 @@ void WiFiComponent::start() {
 | 
			
		||||
#endif
 | 
			
		||||
  }
 | 
			
		||||
#ifdef USE_IMPROV
 | 
			
		||||
  if (esp32_improv::global_improv_component != nullptr) {
 | 
			
		||||
  if (!this->has_sta() && esp32_improv::global_improv_component != nullptr) {
 | 
			
		||||
    if (this->wifi_mode_(true, {}))
 | 
			
		||||
      esp32_improv::global_improv_component->start();
 | 
			
		||||
  }
 | 
			
		||||
@@ -163,8 +163,8 @@ void WiFiComponent::loop() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#ifdef USE_IMPROV
 | 
			
		||||
    if (esp32_improv::global_improv_component != nullptr) {
 | 
			
		||||
      if (!this->is_connected()) {
 | 
			
		||||
    if (esp32_improv::global_improv_component != nullptr && !esp32_improv::global_improv_component->is_active()) {
 | 
			
		||||
      if (now - this->last_connected_ > esp32_improv::global_improv_component->get_wifi_timeout()) {
 | 
			
		||||
        if (this->wifi_mode_(true, {}))
 | 
			
		||||
          esp32_improv::global_improv_component->start();
 | 
			
		||||
      }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
"""Constants used by esphome."""
 | 
			
		||||
 | 
			
		||||
__version__ = "2023.10.0"
 | 
			
		||||
__version__ = "2023.10.6"
 | 
			
		||||
 | 
			
		||||
ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_"
 | 
			
		||||
VALID_SUBSTITUTIONS_CHARACTERS = (
 | 
			
		||||
@@ -481,6 +481,8 @@ CONF_ON_BLE_MANUFACTURER_DATA_ADVERTISE = "on_ble_manufacturer_data_advertise"
 | 
			
		||||
CONF_ON_BLE_SERVICE_DATA_ADVERTISE = "on_ble_service_data_advertise"
 | 
			
		||||
CONF_ON_BOOT = "on_boot"
 | 
			
		||||
CONF_ON_CLICK = "on_click"
 | 
			
		||||
CONF_ON_CLIENT_CONNECTED = "on_client_connected"
 | 
			
		||||
CONF_ON_CLIENT_DISCONNECTED = "on_client_disconnected"
 | 
			
		||||
CONF_ON_CONNECT = "on_connect"
 | 
			
		||||
CONF_ON_CONTROL = "on_control"
 | 
			
		||||
CONF_ON_DISCONNECT = "on_disconnect"
 | 
			
		||||
 
 | 
			
		||||
@@ -52,12 +52,12 @@ template<typename... Ts> class XorCondition : public Condition<Ts...> {
 | 
			
		||||
 public:
 | 
			
		||||
  explicit XorCondition(const std::vector<Condition<Ts...> *> &conditions) : conditions_(conditions) {}
 | 
			
		||||
  bool check(Ts... x) override {
 | 
			
		||||
    bool xor_state = false;
 | 
			
		||||
    size_t result = 0;
 | 
			
		||||
    for (auto *condition : this->conditions_) {
 | 
			
		||||
      xor_state = xor_state ^ condition->check(x...);
 | 
			
		||||
      result += condition->check(x...);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return xor_state;
 | 
			
		||||
    return result == 1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
 
 | 
			
		||||
@@ -3326,6 +3326,10 @@ text_sensor:
 | 
			
		||||
          canbus_id: mcp2515_can
 | 
			
		||||
          can_id: 23
 | 
			
		||||
          data: [0x10, 0x20, 0x30]
 | 
			
		||||
      - canbus.send:
 | 
			
		||||
          canbus_id: mcp2515_can
 | 
			
		||||
          can_id: 23
 | 
			
		||||
          data: !lambda return {0x10, 0x20, 0x30};
 | 
			
		||||
      - canbus.send:
 | 
			
		||||
          canbus_id: esp32_internal_can
 | 
			
		||||
          can_id: 23
 | 
			
		||||
 
 | 
			
		||||
@@ -26,3 +26,9 @@ sensor:
 | 
			
		||||
    name: ADC
 | 
			
		||||
    pin: GPIO23
 | 
			
		||||
    update_interval: 1s
 | 
			
		||||
 | 
			
		||||
mqtt:
 | 
			
		||||
  broker: test.mosquitto.org
 | 
			
		||||
  port: 1883
 | 
			
		||||
  discovery: true
 | 
			
		||||
  discovery_prefix: homeassistant
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user