mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-03 16:41:50 +00:00 
			
		
		
		
	Compare commits
	
		
			24 Commits
		
	
	
		
			2025.7.0b1
			...
			2025.7.0b2
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					18787b0be0 | ||
| 
						 | 
					39e01c42e1 | ||
| 
						 | 
					c760f89e46 | ||
| 
						 | 
					01b4e214b9 | ||
| 
						 | 
					bc7cfeb9cd | ||
| 
						 | 
					36dd203e74 | ||
| 
						 | 
					8605994cc6 | ||
| 
						 | 
					80fbe28088 | ||
| 
						 | 
					1d9f17a57c | ||
| 
						 | 
					42947bcf56 | ||
| 
						 | 
					3c864b2bca | ||
| 
						 | 
					35d88fc0d6 | ||
| 
						 | 
					7a6894e087 | ||
| 
						 | 
					1b222ceca3 | ||
| 
						 | 
					bab3deee1b | ||
| 
						 | 
					ccd30110b1 | ||
| 
						 | 
					904c7b8a3a | ||
| 
						 | 
					fa262673e4 | ||
| 
						 | 
					0ef5f1fd65 | ||
| 
						 | 
					23dd2d648e | ||
| 
						 | 
					5ba493acc3 | ||
| 
						 | 
					a5055094d0 | ||
| 
						 | 
					92d03dd196 | ||
| 
						 | 
					bd75f0dfea | 
							
								
								
									
										2
									
								
								Doxyfile
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								Doxyfile
									
									
									
									
									
								
							@@ -48,7 +48,7 @@ PROJECT_NAME           = ESPHome
 | 
			
		||||
# could be handy for archiving the generated documentation or if some version
 | 
			
		||||
# control system is used.
 | 
			
		||||
 | 
			
		||||
PROJECT_NUMBER         = 2025.7.0b1
 | 
			
		||||
PROJECT_NUMBER         = 2025.7.0b2
 | 
			
		||||
 | 
			
		||||
# Using the PROJECT_BRIEF tag one can provide an optional one line description
 | 
			
		||||
# for a project that appears at the top of each page and should give viewer a
 | 
			
		||||
 
 | 
			
		||||
@@ -24,8 +24,9 @@ from esphome.const import (
 | 
			
		||||
    CONF_TRIGGER_ID,
 | 
			
		||||
    CONF_VARIABLES,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core import coroutine_with_priority
 | 
			
		||||
from esphome.core import CORE, coroutine_with_priority
 | 
			
		||||
 | 
			
		||||
DOMAIN = "api"
 | 
			
		||||
DEPENDENCIES = ["network"]
 | 
			
		||||
AUTO_LOAD = ["socket"]
 | 
			
		||||
CODEOWNERS = ["@OttoWinter"]
 | 
			
		||||
@@ -51,6 +52,7 @@ SERVICE_ARG_NATIVE_TYPES = {
 | 
			
		||||
}
 | 
			
		||||
CONF_ENCRYPTION = "encryption"
 | 
			
		||||
CONF_BATCH_DELAY = "batch_delay"
 | 
			
		||||
CONF_CUSTOM_SERVICES = "custom_services"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def validate_encryption_key(value):
 | 
			
		||||
@@ -115,6 +117,7 @@ CONFIG_SCHEMA = cv.All(
 | 
			
		||||
                cv.positive_time_period_milliseconds,
 | 
			
		||||
                cv.Range(max=cv.TimePeriod(milliseconds=65535)),
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_CUSTOM_SERVICES, default=False): cv.boolean,
 | 
			
		||||
            cv.Optional(CONF_ON_CLIENT_CONNECTED): automation.validate_automation(
 | 
			
		||||
                single=True
 | 
			
		||||
            ),
 | 
			
		||||
@@ -139,8 +142,11 @@ async def to_code(config):
 | 
			
		||||
    cg.add(var.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT]))
 | 
			
		||||
    cg.add(var.set_batch_delay(config[CONF_BATCH_DELAY]))
 | 
			
		||||
 | 
			
		||||
    # Set USE_API_SERVICES if any services are enabled
 | 
			
		||||
    if config.get(CONF_ACTIONS) or config[CONF_CUSTOM_SERVICES]:
 | 
			
		||||
        cg.add_define("USE_API_SERVICES")
 | 
			
		||||
 | 
			
		||||
    if actions := config.get(CONF_ACTIONS, []):
 | 
			
		||||
        cg.add_define("USE_API_YAML_SERVICES")
 | 
			
		||||
        for conf in actions:
 | 
			
		||||
            template_args = []
 | 
			
		||||
            func_args = []
 | 
			
		||||
@@ -317,7 +323,10 @@ async def api_connected_to_code(config, condition_id, template_arg, args):
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def FILTER_SOURCE_FILES() -> list[str]:
 | 
			
		||||
    """Filter out api_pb2_dump.cpp when proto message dumping is not enabled."""
 | 
			
		||||
    """Filter out api_pb2_dump.cpp when proto message dumping is not enabled
 | 
			
		||||
    and user_services.cpp when no services are defined."""
 | 
			
		||||
    files_to_filter = []
 | 
			
		||||
 | 
			
		||||
    # api_pb2_dump.cpp is only needed when HAS_PROTO_MESSAGE_DUMP is defined
 | 
			
		||||
    # This is a particularly large file that still needs to be opened and read
 | 
			
		||||
    # all the way to the end even when ifdef'd out
 | 
			
		||||
@@ -325,6 +334,11 @@ def FILTER_SOURCE_FILES() -> list[str]:
 | 
			
		||||
    # HAS_PROTO_MESSAGE_DUMP is defined when ESPHOME_LOG_HAS_VERY_VERBOSE is set,
 | 
			
		||||
    # which happens when the logger level is VERY_VERBOSE
 | 
			
		||||
    if get_logger_level() != "VERY_VERBOSE":
 | 
			
		||||
        return ["api_pb2_dump.cpp"]
 | 
			
		||||
        files_to_filter.append("api_pb2_dump.cpp")
 | 
			
		||||
 | 
			
		||||
    return []
 | 
			
		||||
    # user_services.cpp is only needed when services are defined
 | 
			
		||||
    config = CORE.config.get(DOMAIN, {})
 | 
			
		||||
    if config and not config.get(CONF_ACTIONS) and not config[CONF_CUSTOM_SERVICES]:
 | 
			
		||||
        files_to_filter.append("user_services.cpp")
 | 
			
		||||
 | 
			
		||||
    return files_to_filter
 | 
			
		||||
 
 | 
			
		||||
@@ -374,6 +374,7 @@ message CoverCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_COVER";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
 | 
			
		||||
@@ -387,6 +388,7 @@ message CoverCommandRequest {
 | 
			
		||||
  bool has_tilt = 6;
 | 
			
		||||
  float tilt = 7;
 | 
			
		||||
  bool stop = 8;
 | 
			
		||||
  uint32 device_id = 9;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== FAN ====================
 | 
			
		||||
@@ -441,6 +443,7 @@ message FanCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_FAN";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  bool has_state = 2;
 | 
			
		||||
@@ -455,6 +458,7 @@ message FanCommandRequest {
 | 
			
		||||
  int32 speed_level = 11;
 | 
			
		||||
  bool has_preset_mode = 12;
 | 
			
		||||
  string preset_mode = 13;
 | 
			
		||||
  uint32 device_id = 14;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== LIGHT ====================
 | 
			
		||||
@@ -523,6 +527,7 @@ message LightCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_LIGHT";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  bool has_state = 2;
 | 
			
		||||
@@ -551,6 +556,7 @@ message LightCommandRequest {
 | 
			
		||||
  uint32 flash_length = 17;
 | 
			
		||||
  bool has_effect = 18;
 | 
			
		||||
  string effect = 19;
 | 
			
		||||
  uint32 device_id = 28;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== SENSOR ====================
 | 
			
		||||
@@ -640,9 +646,11 @@ message SwitchCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_SWITCH";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  bool state = 2;
 | 
			
		||||
  uint32 device_id = 3;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== TEXT SENSOR ====================
 | 
			
		||||
@@ -799,18 +807,21 @@ enum ServiceArgType {
 | 
			
		||||
  SERVICE_ARG_TYPE_STRING_ARRAY = 7;
 | 
			
		||||
}
 | 
			
		||||
message ListEntitiesServicesArgument {
 | 
			
		||||
  option (ifdef) = "USE_API_SERVICES";
 | 
			
		||||
  string name = 1;
 | 
			
		||||
  ServiceArgType type = 2;
 | 
			
		||||
}
 | 
			
		||||
message ListEntitiesServicesResponse {
 | 
			
		||||
  option (id) = 41;
 | 
			
		||||
  option (source) = SOURCE_SERVER;
 | 
			
		||||
  option (ifdef) = "USE_API_SERVICES";
 | 
			
		||||
 | 
			
		||||
  string name = 1;
 | 
			
		||||
  fixed32 key = 2;
 | 
			
		||||
  repeated ListEntitiesServicesArgument args = 3;
 | 
			
		||||
}
 | 
			
		||||
message ExecuteServiceArgument {
 | 
			
		||||
  option (ifdef) = "USE_API_SERVICES";
 | 
			
		||||
  bool bool_ = 1;
 | 
			
		||||
  int32 legacy_int = 2;
 | 
			
		||||
  float float_ = 3;
 | 
			
		||||
@@ -826,6 +837,7 @@ message ExecuteServiceRequest {
 | 
			
		||||
  option (id) = 42;
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (ifdef) = "USE_API_SERVICES";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  repeated ExecuteServiceArgument args = 2;
 | 
			
		||||
@@ -850,12 +862,14 @@ message ListEntitiesCameraResponse {
 | 
			
		||||
 | 
			
		||||
message CameraImageResponse {
 | 
			
		||||
  option (id) = 44;
 | 
			
		||||
  option (base_class) = "StateResponseProtoMessage";
 | 
			
		||||
  option (source) = SOURCE_SERVER;
 | 
			
		||||
  option (ifdef) = "USE_CAMERA";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  bytes data = 2;
 | 
			
		||||
  bool done = 3;
 | 
			
		||||
  uint32 device_id = 4;
 | 
			
		||||
}
 | 
			
		||||
message CameraImageRequest {
 | 
			
		||||
  option (id) = 45;
 | 
			
		||||
@@ -980,6 +994,7 @@ message ClimateCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_CLIMATE";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  bool has_mode = 2;
 | 
			
		||||
@@ -1005,6 +1020,7 @@ message ClimateCommandRequest {
 | 
			
		||||
  string custom_preset = 21;
 | 
			
		||||
  bool has_target_humidity = 22;
 | 
			
		||||
  float target_humidity = 23;
 | 
			
		||||
  uint32 device_id = 24;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== NUMBER ====================
 | 
			
		||||
@@ -1054,9 +1070,11 @@ message NumberCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_NUMBER";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  float state = 2;
 | 
			
		||||
  uint32 device_id = 3;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== SELECT ====================
 | 
			
		||||
@@ -1096,9 +1114,11 @@ message SelectCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_SELECT";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  string state = 2;
 | 
			
		||||
  uint32 device_id = 3;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== SIREN ====================
 | 
			
		||||
@@ -1137,6 +1157,7 @@ message SirenCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_SIREN";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  bool has_state = 2;
 | 
			
		||||
@@ -1147,6 +1168,7 @@ message SirenCommandRequest {
 | 
			
		||||
  uint32 duration = 7;
 | 
			
		||||
  bool has_volume = 8;
 | 
			
		||||
  float volume = 9;
 | 
			
		||||
  uint32 device_id = 10;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== LOCK ====================
 | 
			
		||||
@@ -1201,12 +1223,14 @@ message LockCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_LOCK";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  LockCommand command = 2;
 | 
			
		||||
 | 
			
		||||
  // Not yet implemented:
 | 
			
		||||
  bool has_code = 3;
 | 
			
		||||
  string code = 4;
 | 
			
		||||
  uint32 device_id = 5;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== BUTTON ====================
 | 
			
		||||
@@ -1232,8 +1256,10 @@ message ButtonCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_BUTTON";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  uint32 device_id = 2;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== MEDIA PLAYER ====================
 | 
			
		||||
@@ -1301,6 +1327,7 @@ message MediaPlayerCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_MEDIA_PLAYER";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
 | 
			
		||||
@@ -1315,6 +1342,7 @@ message MediaPlayerCommandRequest {
 | 
			
		||||
 | 
			
		||||
  bool has_announcement = 8;
 | 
			
		||||
  bool announcement = 9;
 | 
			
		||||
  uint32 device_id = 10;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== BLUETOOTH ====================
 | 
			
		||||
@@ -1843,9 +1871,11 @@ message AlarmControlPanelCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_ALARM_CONTROL_PANEL";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  AlarmControlPanelStateCommand command = 2;
 | 
			
		||||
  string code = 3;
 | 
			
		||||
  uint32 device_id = 4;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ===================== TEXT =====================
 | 
			
		||||
@@ -1892,9 +1922,11 @@ message TextCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_TEXT";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  string state = 2;
 | 
			
		||||
  uint32 device_id = 3;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -1936,11 +1968,13 @@ message DateCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_DATETIME_DATE";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  uint32 year = 2;
 | 
			
		||||
  uint32 month = 3;
 | 
			
		||||
  uint32 day = 4;
 | 
			
		||||
  uint32 device_id = 5;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== DATETIME TIME ====================
 | 
			
		||||
@@ -1981,11 +2015,13 @@ message TimeCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_DATETIME_TIME";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  uint32 hour = 2;
 | 
			
		||||
  uint32 minute = 3;
 | 
			
		||||
  uint32 second = 4;
 | 
			
		||||
  uint32 device_id = 5;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== EVENT ====================
 | 
			
		||||
@@ -2065,11 +2101,13 @@ message ValveCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_VALVE";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  bool has_position = 2;
 | 
			
		||||
  float position = 3;
 | 
			
		||||
  bool stop = 4;
 | 
			
		||||
  uint32 device_id = 5;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== DATETIME DATETIME ====================
 | 
			
		||||
@@ -2108,9 +2146,11 @@ message DateTimeCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_DATETIME_DATETIME";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  fixed32 epoch_seconds = 2;
 | 
			
		||||
  uint32 device_id = 3;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== UPDATE ====================
 | 
			
		||||
@@ -2160,7 +2200,9 @@ message UpdateCommandRequest {
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_UPDATE";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  option (base_class) = "CommandProtoMessage";
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  UpdateCommand command = 2;
 | 
			
		||||
  uint32 device_id = 3;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -193,14 +193,15 @@ void APIConnection::loop() {
 | 
			
		||||
      // If we can't send the ping request directly (tx_buffer full),
 | 
			
		||||
      // schedule it at the front of the batch so it will be sent with priority
 | 
			
		||||
      ESP_LOGW(TAG, "Buffer full, ping queued");
 | 
			
		||||
      this->schedule_message_front_(nullptr, &APIConnection::try_send_ping_request, PingRequest::MESSAGE_TYPE);
 | 
			
		||||
      this->schedule_message_front_(nullptr, &APIConnection::try_send_ping_request, PingRequest::MESSAGE_TYPE,
 | 
			
		||||
                                    PingRequest::ESTIMATED_SIZE);
 | 
			
		||||
      this->flags_.sent_ping = true;  // Mark as sent to avoid scheduling multiple pings
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
#ifdef USE_CAMERA
 | 
			
		||||
  if (this->image_reader_ && this->image_reader_->available() && this->helper_->can_write_without_blocking()) {
 | 
			
		||||
    uint32_t to_send = std::min((size_t) MAX_PACKET_SIZE, this->image_reader_->available());
 | 
			
		||||
    uint32_t to_send = std::min((size_t) MAX_BATCH_PACKET_SIZE, this->image_reader_->available());
 | 
			
		||||
    bool done = this->image_reader_->available() == to_send;
 | 
			
		||||
    uint32_t msg_size = 0;
 | 
			
		||||
    ProtoSize::add_fixed_field<4>(msg_size, 1, true);
 | 
			
		||||
@@ -265,7 +266,7 @@ void APIConnection::on_disconnect_response(const DisconnectResponse &value) {
 | 
			
		||||
 | 
			
		||||
// Encodes a message to the buffer and returns the total number of bytes used,
 | 
			
		||||
// including header and footer overhead. Returns 0 if the message doesn't fit.
 | 
			
		||||
uint16_t APIConnection::encode_message_to_buffer(ProtoMessage &msg, uint16_t message_type, APIConnection *conn,
 | 
			
		||||
uint16_t APIConnection::encode_message_to_buffer(ProtoMessage &msg, uint8_t message_type, APIConnection *conn,
 | 
			
		||||
                                                 uint32_t remaining_size, bool is_single) {
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
  // If in log-only mode, just log and return
 | 
			
		||||
@@ -316,7 +317,7 @@ uint16_t APIConnection::encode_message_to_buffer(ProtoMessage &msg, uint16_t mes
 | 
			
		||||
#ifdef USE_BINARY_SENSOR
 | 
			
		||||
bool APIConnection::send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor) {
 | 
			
		||||
  return this->send_message_smart_(binary_sensor, &APIConnection::try_send_binary_sensor_state,
 | 
			
		||||
                                   BinarySensorStateResponse::MESSAGE_TYPE);
 | 
			
		||||
                                   BinarySensorStateResponse::MESSAGE_TYPE, BinarySensorStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t APIConnection::try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
@@ -343,7 +344,8 @@ uint16_t APIConnection::try_send_binary_sensor_info(EntityBase *entity, APIConne
 | 
			
		||||
 | 
			
		||||
#ifdef USE_COVER
 | 
			
		||||
bool APIConnection::send_cover_state(cover::Cover *cover) {
 | 
			
		||||
  return this->send_message_smart_(cover, &APIConnection::try_send_cover_state, CoverStateResponse::MESSAGE_TYPE);
 | 
			
		||||
  return this->send_message_smart_(cover, &APIConnection::try_send_cover_state, CoverStateResponse::MESSAGE_TYPE,
 | 
			
		||||
                                   CoverStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
uint16_t APIConnection::try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
                                             bool is_single) {
 | 
			
		||||
@@ -400,7 +402,8 @@ void APIConnection::cover_command(const CoverCommandRequest &msg) {
 | 
			
		||||
 | 
			
		||||
#ifdef USE_FAN
 | 
			
		||||
bool APIConnection::send_fan_state(fan::Fan *fan) {
 | 
			
		||||
  return this->send_message_smart_(fan, &APIConnection::try_send_fan_state, FanStateResponse::MESSAGE_TYPE);
 | 
			
		||||
  return this->send_message_smart_(fan, &APIConnection::try_send_fan_state, FanStateResponse::MESSAGE_TYPE,
 | 
			
		||||
                                   FanStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
uint16_t APIConnection::try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
                                           bool is_single) {
 | 
			
		||||
@@ -455,7 +458,8 @@ void APIConnection::fan_command(const FanCommandRequest &msg) {
 | 
			
		||||
 | 
			
		||||
#ifdef USE_LIGHT
 | 
			
		||||
bool APIConnection::send_light_state(light::LightState *light) {
 | 
			
		||||
  return this->send_message_smart_(light, &APIConnection::try_send_light_state, LightStateResponse::MESSAGE_TYPE);
 | 
			
		||||
  return this->send_message_smart_(light, &APIConnection::try_send_light_state, LightStateResponse::MESSAGE_TYPE,
 | 
			
		||||
                                   LightStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
uint16_t APIConnection::try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
                                             bool is_single) {
 | 
			
		||||
@@ -543,7 +547,8 @@ void APIConnection::light_command(const LightCommandRequest &msg) {
 | 
			
		||||
 | 
			
		||||
#ifdef USE_SENSOR
 | 
			
		||||
bool APIConnection::send_sensor_state(sensor::Sensor *sensor) {
 | 
			
		||||
  return this->send_message_smart_(sensor, &APIConnection::try_send_sensor_state, SensorStateResponse::MESSAGE_TYPE);
 | 
			
		||||
  return this->send_message_smart_(sensor, &APIConnection::try_send_sensor_state, SensorStateResponse::MESSAGE_TYPE,
 | 
			
		||||
                                   SensorStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t APIConnection::try_send_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
@@ -575,7 +580,8 @@ uint16_t APIConnection::try_send_sensor_info(EntityBase *entity, APIConnection *
 | 
			
		||||
 | 
			
		||||
#ifdef USE_SWITCH
 | 
			
		||||
bool APIConnection::send_switch_state(switch_::Switch *a_switch) {
 | 
			
		||||
  return this->send_message_smart_(a_switch, &APIConnection::try_send_switch_state, SwitchStateResponse::MESSAGE_TYPE);
 | 
			
		||||
  return this->send_message_smart_(a_switch, &APIConnection::try_send_switch_state, SwitchStateResponse::MESSAGE_TYPE,
 | 
			
		||||
                                   SwitchStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t APIConnection::try_send_switch_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
@@ -611,7 +617,7 @@ void APIConnection::switch_command(const SwitchCommandRequest &msg) {
 | 
			
		||||
#ifdef USE_TEXT_SENSOR
 | 
			
		||||
bool APIConnection::send_text_sensor_state(text_sensor::TextSensor *text_sensor) {
 | 
			
		||||
  return this->send_message_smart_(text_sensor, &APIConnection::try_send_text_sensor_state,
 | 
			
		||||
                                   TextSensorStateResponse::MESSAGE_TYPE);
 | 
			
		||||
                                   TextSensorStateResponse::MESSAGE_TYPE, TextSensorStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t APIConnection::try_send_text_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
@@ -638,7 +644,8 @@ uint16_t APIConnection::try_send_text_sensor_info(EntityBase *entity, APIConnect
 | 
			
		||||
 | 
			
		||||
#ifdef USE_CLIMATE
 | 
			
		||||
bool APIConnection::send_climate_state(climate::Climate *climate) {
 | 
			
		||||
  return this->send_message_smart_(climate, &APIConnection::try_send_climate_state, ClimateStateResponse::MESSAGE_TYPE);
 | 
			
		||||
  return this->send_message_smart_(climate, &APIConnection::try_send_climate_state, ClimateStateResponse::MESSAGE_TYPE,
 | 
			
		||||
                                   ClimateStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
uint16_t APIConnection::try_send_climate_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
                                               bool is_single) {
 | 
			
		||||
@@ -734,7 +741,8 @@ void APIConnection::climate_command(const ClimateCommandRequest &msg) {
 | 
			
		||||
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
bool APIConnection::send_number_state(number::Number *number) {
 | 
			
		||||
  return this->send_message_smart_(number, &APIConnection::try_send_number_state, NumberStateResponse::MESSAGE_TYPE);
 | 
			
		||||
  return this->send_message_smart_(number, &APIConnection::try_send_number_state, NumberStateResponse::MESSAGE_TYPE,
 | 
			
		||||
                                   NumberStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t APIConnection::try_send_number_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
@@ -770,7 +778,8 @@ void APIConnection::number_command(const NumberCommandRequest &msg) {
 | 
			
		||||
 | 
			
		||||
#ifdef USE_DATETIME_DATE
 | 
			
		||||
bool APIConnection::send_date_state(datetime::DateEntity *date) {
 | 
			
		||||
  return this->send_message_smart_(date, &APIConnection::try_send_date_state, DateStateResponse::MESSAGE_TYPE);
 | 
			
		||||
  return this->send_message_smart_(date, &APIConnection::try_send_date_state, DateStateResponse::MESSAGE_TYPE,
 | 
			
		||||
                                   DateStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
uint16_t APIConnection::try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
                                            bool is_single) {
 | 
			
		||||
@@ -800,7 +809,8 @@ void APIConnection::date_command(const DateCommandRequest &msg) {
 | 
			
		||||
 | 
			
		||||
#ifdef USE_DATETIME_TIME
 | 
			
		||||
bool APIConnection::send_time_state(datetime::TimeEntity *time) {
 | 
			
		||||
  return this->send_message_smart_(time, &APIConnection::try_send_time_state, TimeStateResponse::MESSAGE_TYPE);
 | 
			
		||||
  return this->send_message_smart_(time, &APIConnection::try_send_time_state, TimeStateResponse::MESSAGE_TYPE,
 | 
			
		||||
                                   TimeStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
uint16_t APIConnection::try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
                                            bool is_single) {
 | 
			
		||||
@@ -831,7 +841,7 @@ void APIConnection::time_command(const TimeCommandRequest &msg) {
 | 
			
		||||
#ifdef USE_DATETIME_DATETIME
 | 
			
		||||
bool APIConnection::send_datetime_state(datetime::DateTimeEntity *datetime) {
 | 
			
		||||
  return this->send_message_smart_(datetime, &APIConnection::try_send_datetime_state,
 | 
			
		||||
                                   DateTimeStateResponse::MESSAGE_TYPE);
 | 
			
		||||
                                   DateTimeStateResponse::MESSAGE_TYPE, DateTimeStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
uint16_t APIConnection::try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
                                                bool is_single) {
 | 
			
		||||
@@ -862,7 +872,8 @@ void APIConnection::datetime_command(const DateTimeCommandRequest &msg) {
 | 
			
		||||
 | 
			
		||||
#ifdef USE_TEXT
 | 
			
		||||
bool APIConnection::send_text_state(text::Text *text) {
 | 
			
		||||
  return this->send_message_smart_(text, &APIConnection::try_send_text_state, TextStateResponse::MESSAGE_TYPE);
 | 
			
		||||
  return this->send_message_smart_(text, &APIConnection::try_send_text_state, TextStateResponse::MESSAGE_TYPE,
 | 
			
		||||
                                   TextStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t APIConnection::try_send_text_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
@@ -896,7 +907,8 @@ void APIConnection::text_command(const TextCommandRequest &msg) {
 | 
			
		||||
 | 
			
		||||
#ifdef USE_SELECT
 | 
			
		||||
bool APIConnection::send_select_state(select::Select *select) {
 | 
			
		||||
  return this->send_message_smart_(select, &APIConnection::try_send_select_state, SelectStateResponse::MESSAGE_TYPE);
 | 
			
		||||
  return this->send_message_smart_(select, &APIConnection::try_send_select_state, SelectStateResponse::MESSAGE_TYPE,
 | 
			
		||||
                                   SelectStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t APIConnection::try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
@@ -944,7 +956,8 @@ void esphome::api::APIConnection::button_command(const ButtonCommandRequest &msg
 | 
			
		||||
 | 
			
		||||
#ifdef USE_LOCK
 | 
			
		||||
bool APIConnection::send_lock_state(lock::Lock *a_lock) {
 | 
			
		||||
  return this->send_message_smart_(a_lock, &APIConnection::try_send_lock_state, LockStateResponse::MESSAGE_TYPE);
 | 
			
		||||
  return this->send_message_smart_(a_lock, &APIConnection::try_send_lock_state, LockStateResponse::MESSAGE_TYPE,
 | 
			
		||||
                                   LockStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t APIConnection::try_send_lock_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
@@ -986,7 +999,8 @@ void APIConnection::lock_command(const LockCommandRequest &msg) {
 | 
			
		||||
 | 
			
		||||
#ifdef USE_VALVE
 | 
			
		||||
bool APIConnection::send_valve_state(valve::Valve *valve) {
 | 
			
		||||
  return this->send_message_smart_(valve, &APIConnection::try_send_valve_state, ValveStateResponse::MESSAGE_TYPE);
 | 
			
		||||
  return this->send_message_smart_(valve, &APIConnection::try_send_valve_state, ValveStateResponse::MESSAGE_TYPE,
 | 
			
		||||
                                   ValveStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
uint16_t APIConnection::try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
                                             bool is_single) {
 | 
			
		||||
@@ -1023,7 +1037,7 @@ void APIConnection::valve_command(const ValveCommandRequest &msg) {
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
bool APIConnection::send_media_player_state(media_player::MediaPlayer *media_player) {
 | 
			
		||||
  return this->send_message_smart_(media_player, &APIConnection::try_send_media_player_state,
 | 
			
		||||
                                   MediaPlayerStateResponse::MESSAGE_TYPE);
 | 
			
		||||
                                   MediaPlayerStateResponse::MESSAGE_TYPE, MediaPlayerStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
uint16_t APIConnection::try_send_media_player_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
                                                    bool is_single) {
 | 
			
		||||
@@ -1262,7 +1276,8 @@ void APIConnection::voice_assistant_set_configuration(const VoiceAssistantSetCon
 | 
			
		||||
#ifdef USE_ALARM_CONTROL_PANEL
 | 
			
		||||
bool APIConnection::send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) {
 | 
			
		||||
  return this->send_message_smart_(a_alarm_control_panel, &APIConnection::try_send_alarm_control_panel_state,
 | 
			
		||||
                                   AlarmControlPanelStateResponse::MESSAGE_TYPE);
 | 
			
		||||
                                   AlarmControlPanelStateResponse::MESSAGE_TYPE,
 | 
			
		||||
                                   AlarmControlPanelStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
uint16_t APIConnection::try_send_alarm_control_panel_state(EntityBase *entity, APIConnection *conn,
 | 
			
		||||
                                                           uint32_t remaining_size, bool is_single) {
 | 
			
		||||
@@ -1316,7 +1331,8 @@ void APIConnection::alarm_control_panel_command(const AlarmControlPanelCommandRe
 | 
			
		||||
 | 
			
		||||
#ifdef USE_EVENT
 | 
			
		||||
void APIConnection::send_event(event::Event *event, const std::string &event_type) {
 | 
			
		||||
  this->schedule_message_(event, MessageCreator(event_type), EventResponse::MESSAGE_TYPE);
 | 
			
		||||
  this->schedule_message_(event, MessageCreator(event_type), EventResponse::MESSAGE_TYPE,
 | 
			
		||||
                          EventResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
uint16_t APIConnection::try_send_event_response(event::Event *event, const std::string &event_type, APIConnection *conn,
 | 
			
		||||
                                                uint32_t remaining_size, bool is_single) {
 | 
			
		||||
@@ -1341,7 +1357,8 @@ uint16_t APIConnection::try_send_event_info(EntityBase *entity, APIConnection *c
 | 
			
		||||
 | 
			
		||||
#ifdef USE_UPDATE
 | 
			
		||||
bool APIConnection::send_update_state(update::UpdateEntity *update) {
 | 
			
		||||
  return this->send_message_smart_(update, &APIConnection::try_send_update_state, UpdateStateResponse::MESSAGE_TYPE);
 | 
			
		||||
  return this->send_message_smart_(update, &APIConnection::try_send_update_state, UpdateStateResponse::MESSAGE_TYPE,
 | 
			
		||||
                                   UpdateStateResponse::ESTIMATED_SIZE);
 | 
			
		||||
}
 | 
			
		||||
uint16_t APIConnection::try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
                                              bool is_single) {
 | 
			
		||||
@@ -1534,6 +1551,7 @@ void APIConnection::on_home_assistant_state_response(const HomeAssistantStateRes
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
void APIConnection::execute_service(const ExecuteServiceRequest &msg) {
 | 
			
		||||
  bool found = false;
 | 
			
		||||
  for (auto *service : this->parent_->get_user_services()) {
 | 
			
		||||
@@ -1545,6 +1563,7 @@ void APIConnection::execute_service(const ExecuteServiceRequest &msg) {
 | 
			
		||||
    ESP_LOGV(TAG, "Could not find service");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_API_NOISE
 | 
			
		||||
NoiseEncryptionSetKeyResponse APIConnection::noise_encryption_set_key(const NoiseEncryptionSetKeyRequest &msg) {
 | 
			
		||||
  psk_t psk{};
 | 
			
		||||
@@ -1588,7 +1607,7 @@ bool APIConnection::try_to_clear_buffer(bool log_out_of_space) {
 | 
			
		||||
  }
 | 
			
		||||
  return false;
 | 
			
		||||
}
 | 
			
		||||
bool APIConnection::send_buffer(ProtoWriteBuffer buffer, uint16_t message_type) {
 | 
			
		||||
bool APIConnection::send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) {
 | 
			
		||||
  if (!this->try_to_clear_buffer(message_type != SubscribeLogsResponse::MESSAGE_TYPE)) {  // SubscribeLogsResponse
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
@@ -1622,7 +1641,8 @@ void APIConnection::on_fatal_error() {
 | 
			
		||||
  this->flags_.remove = true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void APIConnection::DeferredBatch::add_item(EntityBase *entity, MessageCreator creator, uint16_t message_type) {
 | 
			
		||||
void APIConnection::DeferredBatch::add_item(EntityBase *entity, MessageCreator creator, uint8_t message_type,
 | 
			
		||||
                                            uint8_t estimated_size) {
 | 
			
		||||
  // Check if we already have a message of this type for this entity
 | 
			
		||||
  // This provides deduplication per entity/message_type combination
 | 
			
		||||
  // O(n) but optimized for RAM and not performance.
 | 
			
		||||
@@ -1637,12 +1657,13 @@ void APIConnection::DeferredBatch::add_item(EntityBase *entity, MessageCreator c
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // No existing item found, add new one
 | 
			
		||||
  items.emplace_back(entity, std::move(creator), message_type);
 | 
			
		||||
  items.emplace_back(entity, std::move(creator), message_type, estimated_size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void APIConnection::DeferredBatch::add_item_front(EntityBase *entity, MessageCreator creator, uint16_t message_type) {
 | 
			
		||||
void APIConnection::DeferredBatch::add_item_front(EntityBase *entity, MessageCreator creator, uint8_t message_type,
 | 
			
		||||
                                                  uint8_t estimated_size) {
 | 
			
		||||
  // Insert at front for high priority messages (no deduplication check)
 | 
			
		||||
  items.insert(items.begin(), BatchItem(entity, std::move(creator), message_type));
 | 
			
		||||
  items.insert(items.begin(), BatchItem(entity, std::move(creator), message_type, estimated_size));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool APIConnection::schedule_batch_() {
 | 
			
		||||
@@ -1714,7 +1735,7 @@ void APIConnection::process_batch_() {
 | 
			
		||||
  uint32_t total_estimated_size = 0;
 | 
			
		||||
  for (size_t i = 0; i < this->deferred_batch_.size(); i++) {
 | 
			
		||||
    const auto &item = this->deferred_batch_[i];
 | 
			
		||||
    total_estimated_size += get_estimated_message_size(item.message_type);
 | 
			
		||||
    total_estimated_size += item.estimated_size;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Calculate total overhead for all messages
 | 
			
		||||
@@ -1752,9 +1773,9 @@ void APIConnection::process_batch_() {
 | 
			
		||||
 | 
			
		||||
    // Update tracking variables
 | 
			
		||||
    items_processed++;
 | 
			
		||||
    // After first message, set remaining size to MAX_PACKET_SIZE to avoid fragmentation
 | 
			
		||||
    // After first message, set remaining size to MAX_BATCH_PACKET_SIZE to avoid fragmentation
 | 
			
		||||
    if (items_processed == 1) {
 | 
			
		||||
      remaining_size = MAX_PACKET_SIZE;
 | 
			
		||||
      remaining_size = MAX_BATCH_PACKET_SIZE;
 | 
			
		||||
    }
 | 
			
		||||
    remaining_size -= payload_size;
 | 
			
		||||
    // Calculate where the next message's header padding will start
 | 
			
		||||
@@ -1808,7 +1829,7 @@ void APIConnection::process_batch_() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t APIConnection::MessageCreator::operator()(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
                                                   bool is_single, uint16_t message_type) const {
 | 
			
		||||
                                                   bool is_single, uint8_t message_type) const {
 | 
			
		||||
#ifdef USE_EVENT
 | 
			
		||||
  // Special case: EventResponse uses string pointer
 | 
			
		||||
  if (message_type == EventResponse::MESSAGE_TYPE) {
 | 
			
		||||
@@ -1839,149 +1860,6 @@ uint16_t APIConnection::try_send_ping_request(EntityBase *entity, APIConnection
 | 
			
		||||
  return encode_message_to_buffer(req, PingRequest::MESSAGE_TYPE, conn, remaining_size, is_single);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t APIConnection::get_estimated_message_size(uint16_t message_type) {
 | 
			
		||||
  // Use generated ESTIMATED_SIZE constants from each message type
 | 
			
		||||
  switch (message_type) {
 | 
			
		||||
#ifdef USE_BINARY_SENSOR
 | 
			
		||||
    case BinarySensorStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return BinarySensorStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesBinarySensorResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesBinarySensorResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SENSOR
 | 
			
		||||
    case SensorStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return SensorStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesSensorResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesSensorResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SWITCH
 | 
			
		||||
    case SwitchStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return SwitchStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesSwitchResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesSwitchResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_TEXT_SENSOR
 | 
			
		||||
    case TextSensorStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return TextSensorStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesTextSensorResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesTextSensorResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
    case NumberStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return NumberStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesNumberResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesNumberResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_TEXT
 | 
			
		||||
    case TextStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return TextStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesTextResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesTextResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SELECT
 | 
			
		||||
    case SelectStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return SelectStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesSelectResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesSelectResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_LOCK
 | 
			
		||||
    case LockStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return LockStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesLockResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesLockResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_EVENT
 | 
			
		||||
    case EventResponse::MESSAGE_TYPE:
 | 
			
		||||
      return EventResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesEventResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesEventResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_COVER
 | 
			
		||||
    case CoverStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return CoverStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesCoverResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesCoverResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_FAN
 | 
			
		||||
    case FanStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return FanStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesFanResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesFanResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_LIGHT
 | 
			
		||||
    case LightStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return LightStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesLightResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesLightResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_CLIMATE
 | 
			
		||||
    case ClimateStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ClimateStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesClimateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesClimateResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_ESP32_CAMERA
 | 
			
		||||
    case ListEntitiesCameraResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesCameraResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_BUTTON
 | 
			
		||||
    case ListEntitiesButtonResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesButtonResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
    case MediaPlayerStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return MediaPlayerStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesMediaPlayerResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesMediaPlayerResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_ALARM_CONTROL_PANEL
 | 
			
		||||
    case AlarmControlPanelStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return AlarmControlPanelStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesAlarmControlPanelResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesAlarmControlPanelResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_DATETIME_DATE
 | 
			
		||||
    case DateStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return DateStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesDateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesDateResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_DATETIME_TIME
 | 
			
		||||
    case TimeStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return TimeStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesTimeResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesTimeResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_DATETIME_DATETIME
 | 
			
		||||
    case DateTimeStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return DateTimeStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesDateTimeResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesDateTimeResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_VALVE
 | 
			
		||||
    case ValveStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ValveStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesValveResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesValveResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_UPDATE
 | 
			
		||||
    case UpdateStateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return UpdateStateResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesUpdateResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesUpdateResponse::ESTIMATED_SIZE;
 | 
			
		||||
#endif
 | 
			
		||||
    case ListEntitiesServicesResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesServicesResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case ListEntitiesDoneResponse::MESSAGE_TYPE:
 | 
			
		||||
      return ListEntitiesDoneResponse::ESTIMATED_SIZE;
 | 
			
		||||
    case DisconnectRequest::MESSAGE_TYPE:
 | 
			
		||||
      return DisconnectRequest::ESTIMATED_SIZE;
 | 
			
		||||
    default:
 | 
			
		||||
      // Fallback for unknown message types
 | 
			
		||||
      return 24;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace api
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -33,7 +33,7 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
 | 
			
		||||
  bool send_list_info_done() {
 | 
			
		||||
    return this->schedule_message_(nullptr, &APIConnection::try_send_list_info_done,
 | 
			
		||||
                                   ListEntitiesDoneResponse::MESSAGE_TYPE);
 | 
			
		||||
                                   ListEntitiesDoneResponse::MESSAGE_TYPE, ListEntitiesDoneResponse::ESTIMATED_SIZE);
 | 
			
		||||
  }
 | 
			
		||||
#ifdef USE_BINARY_SENSOR
 | 
			
		||||
  bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor);
 | 
			
		||||
@@ -195,7 +195,9 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
    // TODO
 | 
			
		||||
    return {};
 | 
			
		||||
  }
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
  void execute_service(const ExecuteServiceRequest &msg) override;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_API_NOISE
 | 
			
		||||
  NoiseEncryptionSetKeyResponse noise_encryption_set_key(const NoiseEncryptionSetKeyRequest &msg) override;
 | 
			
		||||
#endif
 | 
			
		||||
@@ -256,7 +258,7 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  bool try_to_clear_buffer(bool log_out_of_space);
 | 
			
		||||
  bool send_buffer(ProtoWriteBuffer buffer, uint16_t message_type) override;
 | 
			
		||||
  bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) override;
 | 
			
		||||
 | 
			
		||||
  std::string get_client_combined_info() const {
 | 
			
		||||
    if (this->client_info_ == this->client_peername_) {
 | 
			
		||||
@@ -298,7 +300,7 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Non-template helper to encode any ProtoMessage
 | 
			
		||||
  static uint16_t encode_message_to_buffer(ProtoMessage &msg, uint16_t message_type, APIConnection *conn,
 | 
			
		||||
  static uint16_t encode_message_to_buffer(ProtoMessage &msg, uint8_t message_type, APIConnection *conn,
 | 
			
		||||
                                           uint32_t remaining_size, bool is_single);
 | 
			
		||||
 | 
			
		||||
#ifdef USE_VOICE_ASSISTANT
 | 
			
		||||
@@ -443,9 +445,6 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
  static uint16_t try_send_disconnect_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
                                              bool is_single);
 | 
			
		||||
 | 
			
		||||
  // Helper function to get estimated message size for buffer pre-allocation
 | 
			
		||||
  static uint16_t get_estimated_message_size(uint16_t message_type);
 | 
			
		||||
 | 
			
		||||
  // Batch message method for ping requests
 | 
			
		||||
  static uint16_t try_send_ping_request(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
 | 
			
		||||
                                        bool is_single);
 | 
			
		||||
@@ -505,10 +504,10 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
 | 
			
		||||
    // Call operator - uses message_type to determine union type
 | 
			
		||||
    uint16_t operator()(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single,
 | 
			
		||||
                        uint16_t message_type) const;
 | 
			
		||||
                        uint8_t message_type) const;
 | 
			
		||||
 | 
			
		||||
    // Manual cleanup method - must be called before destruction for string types
 | 
			
		||||
    void cleanup(uint16_t message_type) {
 | 
			
		||||
    void cleanup(uint8_t message_type) {
 | 
			
		||||
#ifdef USE_EVENT
 | 
			
		||||
      if (message_type == EventResponse::MESSAGE_TYPE && data_.string_ptr != nullptr) {
 | 
			
		||||
        delete data_.string_ptr;
 | 
			
		||||
@@ -529,11 +528,12 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
    struct BatchItem {
 | 
			
		||||
      EntityBase *entity;      // Entity pointer
 | 
			
		||||
      MessageCreator creator;  // Function that creates the message when needed
 | 
			
		||||
      uint16_t message_type;   // Message type for overhead calculation
 | 
			
		||||
      uint8_t message_type;    // Message type for overhead calculation (max 255)
 | 
			
		||||
      uint8_t estimated_size;  // Estimated message size (max 255 bytes)
 | 
			
		||||
 | 
			
		||||
      // Constructor for creating BatchItem
 | 
			
		||||
      BatchItem(EntityBase *entity, MessageCreator creator, uint16_t message_type)
 | 
			
		||||
          : entity(entity), creator(std::move(creator)), message_type(message_type) {}
 | 
			
		||||
      BatchItem(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size)
 | 
			
		||||
          : entity(entity), creator(std::move(creator)), message_type(message_type), estimated_size(estimated_size) {}
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    std::vector<BatchItem> items;
 | 
			
		||||
@@ -559,9 +559,9 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Add item to the batch
 | 
			
		||||
    void add_item(EntityBase *entity, MessageCreator creator, uint16_t message_type);
 | 
			
		||||
    void add_item(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size);
 | 
			
		||||
    // Add item to the front of the batch (for high priority messages like ping)
 | 
			
		||||
    void add_item_front(EntityBase *entity, MessageCreator creator, uint16_t message_type);
 | 
			
		||||
    void add_item_front(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size);
 | 
			
		||||
 | 
			
		||||
    // Clear all items with proper cleanup
 | 
			
		||||
    void clear() {
 | 
			
		||||
@@ -630,7 +630,7 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
  // to send in one go. This is the maximum size of a single packet
 | 
			
		||||
  // that can be sent over the network.
 | 
			
		||||
  // This is to avoid fragmentation of the packet.
 | 
			
		||||
  static constexpr size_t MAX_PACKET_SIZE = 1390;  // MTU
 | 
			
		||||
  static constexpr size_t MAX_BATCH_PACKET_SIZE = 1390;  // MTU
 | 
			
		||||
 | 
			
		||||
  bool schedule_batch_();
 | 
			
		||||
  void process_batch_();
 | 
			
		||||
@@ -641,9 +641,9 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
  // Helper to log a proto message from a MessageCreator object
 | 
			
		||||
  void log_proto_message_(EntityBase *entity, const MessageCreator &creator, uint16_t message_type) {
 | 
			
		||||
  void log_proto_message_(EntityBase *entity, const MessageCreator &creator, uint8_t message_type) {
 | 
			
		||||
    this->flags_.log_only_mode = true;
 | 
			
		||||
    creator(entity, this, MAX_PACKET_SIZE, true, message_type);
 | 
			
		||||
    creator(entity, this, MAX_BATCH_PACKET_SIZE, true, message_type);
 | 
			
		||||
    this->flags_.log_only_mode = false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -654,7 +654,8 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  // Helper method to send a message either immediately or via batching
 | 
			
		||||
  bool send_message_smart_(EntityBase *entity, MessageCreatorPtr creator, uint16_t message_type) {
 | 
			
		||||
  bool send_message_smart_(EntityBase *entity, MessageCreatorPtr creator, uint8_t message_type,
 | 
			
		||||
                           uint8_t estimated_size) {
 | 
			
		||||
    // Try to send immediately if:
 | 
			
		||||
    // 1. We should try to send immediately (should_try_send_immediately = true)
 | 
			
		||||
    // 2. Batch delay is 0 (user has opted in to immediate sending)
 | 
			
		||||
@@ -662,7 +663,7 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
    if (this->flags_.should_try_send_immediately && this->get_batch_delay_ms_() == 0 &&
 | 
			
		||||
        this->helper_->can_write_without_blocking()) {
 | 
			
		||||
      // Now actually encode and send
 | 
			
		||||
      if (creator(entity, this, MAX_PACKET_SIZE, true) &&
 | 
			
		||||
      if (creator(entity, this, MAX_BATCH_PACKET_SIZE, true) &&
 | 
			
		||||
          this->send_buffer(ProtoWriteBuffer{&this->parent_->get_shared_buffer_ref()}, message_type)) {
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
        // Log the message in verbose mode
 | 
			
		||||
@@ -675,23 +676,25 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Fall back to scheduled batching
 | 
			
		||||
    return this->schedule_message_(entity, creator, message_type);
 | 
			
		||||
    return this->schedule_message_(entity, creator, message_type, estimated_size);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Helper function to schedule a deferred message with known message type
 | 
			
		||||
  bool schedule_message_(EntityBase *entity, MessageCreator creator, uint16_t message_type) {
 | 
			
		||||
    this->deferred_batch_.add_item(entity, std::move(creator), message_type);
 | 
			
		||||
  bool schedule_message_(EntityBase *entity, MessageCreator creator, uint8_t message_type, uint8_t estimated_size) {
 | 
			
		||||
    this->deferred_batch_.add_item(entity, std::move(creator), message_type, estimated_size);
 | 
			
		||||
    return this->schedule_batch_();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Overload for function pointers (for info messages and current state reads)
 | 
			
		||||
  bool schedule_message_(EntityBase *entity, MessageCreatorPtr function_ptr, uint16_t message_type) {
 | 
			
		||||
    return schedule_message_(entity, MessageCreator(function_ptr), message_type);
 | 
			
		||||
  bool schedule_message_(EntityBase *entity, MessageCreatorPtr function_ptr, uint8_t message_type,
 | 
			
		||||
                         uint8_t estimated_size) {
 | 
			
		||||
    return schedule_message_(entity, MessageCreator(function_ptr), message_type, estimated_size);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Helper function to schedule a high priority message at the front of the batch
 | 
			
		||||
  bool schedule_message_front_(EntityBase *entity, MessageCreatorPtr function_ptr, uint16_t message_type) {
 | 
			
		||||
    this->deferred_batch_.add_item_front(entity, MessageCreator(function_ptr), message_type);
 | 
			
		||||
  bool schedule_message_front_(EntityBase *entity, MessageCreatorPtr function_ptr, uint8_t message_type,
 | 
			
		||||
                               uint8_t estimated_size) {
 | 
			
		||||
    this->deferred_batch_.add_item_front(entity, MessageCreator(function_ptr), message_type, estimated_size);
 | 
			
		||||
    return this->schedule_batch_();
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,6 @@
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
#include "proto.h"
 | 
			
		||||
#include "api_pb2_size.h"
 | 
			
		||||
#include <cstring>
 | 
			
		||||
#include <cinttypes>
 | 
			
		||||
 | 
			
		||||
@@ -613,7 +612,7 @@ APIError APINoiseFrameHelper::read_packet(ReadPacketBuffer *buffer) {
 | 
			
		||||
  buffer->type = type;
 | 
			
		||||
  return APIError::OK;
 | 
			
		||||
}
 | 
			
		||||
APIError APINoiseFrameHelper::write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) {
 | 
			
		||||
APIError APINoiseFrameHelper::write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) {
 | 
			
		||||
  // Resize to include MAC space (required for Noise encryption)
 | 
			
		||||
  buffer.get_buffer()->resize(buffer.get_buffer()->size() + frame_footer_size_);
 | 
			
		||||
  PacketInfo packet{type, 0,
 | 
			
		||||
@@ -1002,7 +1001,7 @@ APIError APIPlaintextFrameHelper::read_packet(ReadPacketBuffer *buffer) {
 | 
			
		||||
  buffer->type = rx_header_parsed_type_;
 | 
			
		||||
  return APIError::OK;
 | 
			
		||||
}
 | 
			
		||||
APIError APIPlaintextFrameHelper::write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) {
 | 
			
		||||
APIError APIPlaintextFrameHelper::write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) {
 | 
			
		||||
  PacketInfo packet{type, 0, static_cast<uint16_t>(buffer.get_buffer()->size() - frame_header_padding_)};
 | 
			
		||||
  return write_protobuf_packets(buffer, std::span<const PacketInfo>(&packet, 1));
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -30,13 +30,11 @@ struct ReadPacketBuffer {
 | 
			
		||||
 | 
			
		||||
// Packed packet info structure to minimize memory usage
 | 
			
		||||
struct PacketInfo {
 | 
			
		||||
  uint16_t message_type;  // 2 bytes
 | 
			
		||||
  uint16_t offset;        // 2 bytes (sufficient for packet size ~1460 bytes)
 | 
			
		||||
  uint16_t payload_size;  // 2 bytes (up to 65535 bytes)
 | 
			
		||||
  uint16_t padding;       // 2 byte (for alignment)
 | 
			
		||||
  uint16_t offset;        // Offset in buffer where message starts
 | 
			
		||||
  uint16_t payload_size;  // Size of the message payload
 | 
			
		||||
  uint8_t message_type;   // Message type (0-255)
 | 
			
		||||
 | 
			
		||||
  PacketInfo(uint16_t type, uint16_t off, uint16_t size)
 | 
			
		||||
      : message_type(type), offset(off), payload_size(size), padding(0) {}
 | 
			
		||||
  PacketInfo(uint8_t type, uint16_t off, uint16_t size) : offset(off), payload_size(size), message_type(type) {}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum class APIError : uint16_t {
 | 
			
		||||
@@ -98,7 +96,7 @@ class APIFrameHelper {
 | 
			
		||||
  }
 | 
			
		||||
  // Give this helper a name for logging
 | 
			
		||||
  void set_log_info(std::string info) { info_ = std::move(info); }
 | 
			
		||||
  virtual APIError write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) = 0;
 | 
			
		||||
  virtual APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) = 0;
 | 
			
		||||
  // Write multiple protobuf packets in a single operation
 | 
			
		||||
  // packets contains (message_type, offset, length) for each message in the buffer
 | 
			
		||||
  // The buffer contains all messages with appropriate padding before each
 | 
			
		||||
@@ -197,7 +195,7 @@ class APINoiseFrameHelper : public APIFrameHelper {
 | 
			
		||||
  APIError init() override;
 | 
			
		||||
  APIError loop() override;
 | 
			
		||||
  APIError read_packet(ReadPacketBuffer *buffer) override;
 | 
			
		||||
  APIError write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) override;
 | 
			
		||||
  APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override;
 | 
			
		||||
  APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span<const PacketInfo> packets) override;
 | 
			
		||||
  // Get the frame header padding required by this protocol
 | 
			
		||||
  uint8_t frame_header_padding() override { return frame_header_padding_; }
 | 
			
		||||
@@ -251,7 +249,7 @@ class APIPlaintextFrameHelper : public APIFrameHelper {
 | 
			
		||||
  APIError init() override;
 | 
			
		||||
  APIError loop() override;
 | 
			
		||||
  APIError read_packet(ReadPacketBuffer *buffer) override;
 | 
			
		||||
  APIError write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) override;
 | 
			
		||||
  APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override;
 | 
			
		||||
  APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span<const PacketInfo> packets) override;
 | 
			
		||||
  uint8_t frame_header_padding() override { return frame_header_padding_; }
 | 
			
		||||
  // Get the frame footer size required by this protocol
 | 
			
		||||
 
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -162,6 +162,7 @@ template<> const char *proto_enum_to_string<enums::LogLevel>(enums::LogLevel val
 | 
			
		||||
      return "UNKNOWN";
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
template<> const char *proto_enum_to_string<enums::ServiceArgType>(enums::ServiceArgType value) {
 | 
			
		||||
  switch (value) {
 | 
			
		||||
    case enums::SERVICE_ARG_TYPE_BOOL:
 | 
			
		||||
@@ -184,6 +185,7 @@ template<> const char *proto_enum_to_string<enums::ServiceArgType>(enums::Servic
 | 
			
		||||
      return "UNKNOWN";
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_CLIMATE
 | 
			
		||||
template<> const char *proto_enum_to_string<enums::ClimateMode>(enums::ClimateMode value) {
 | 
			
		||||
  switch (value) {
 | 
			
		||||
@@ -986,6 +988,11 @@ void CoverCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("  stop: ");
 | 
			
		||||
  out.append(YESNO(this->stop));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -1146,6 +1153,11 @@ void FanCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("  preset_mode: ");
 | 
			
		||||
  out.append("'").append(this->preset_mode).append("'");
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -1419,6 +1431,11 @@ void LightCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("  effect: ");
 | 
			
		||||
  out.append("'").append(this->effect).append("'");
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -1586,6 +1603,11 @@ void SwitchCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("  state: ");
 | 
			
		||||
  out.append(YESNO(this->state));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -1791,6 +1813,7 @@ void GetTimeResponse::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
void ListEntitiesServicesArgument::dump_to(std::string &out) const {
 | 
			
		||||
  __attribute__((unused)) char buffer[64];
 | 
			
		||||
  out.append("ListEntitiesServicesArgument {\n");
 | 
			
		||||
@@ -1890,6 +1913,7 @@ void ExecuteServiceRequest::dump_to(std::string &out) const {
 | 
			
		||||
  }
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_CAMERA
 | 
			
		||||
void ListEntitiesCameraResponse::dump_to(std::string &out) const {
 | 
			
		||||
  __attribute__((unused)) char buffer[64];
 | 
			
		||||
@@ -1944,6 +1968,11 @@ void CameraImageResponse::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("  done: ");
 | 
			
		||||
  out.append(YESNO(this->done));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
void CameraImageRequest::dump_to(std::string &out) const {
 | 
			
		||||
@@ -2263,6 +2292,11 @@ void ClimateCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%g", this->target_humidity);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -2367,6 +2401,11 @@ void NumberCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%g", this->state);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -2448,6 +2487,11 @@ void SelectCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("  state: ");
 | 
			
		||||
  out.append("'").append(this->state).append("'");
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -2563,6 +2607,11 @@ void SirenCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%g", this->volume);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -2658,6 +2707,11 @@ void LockCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("  code: ");
 | 
			
		||||
  out.append("'").append(this->code).append("'");
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -2711,6 +2765,11 @@ void ButtonCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->key);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -2857,6 +2916,11 @@ void MediaPlayerCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("  announcement: ");
 | 
			
		||||
  out.append(YESNO(this->announcement));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -3682,6 +3746,11 @@ void AlarmControlPanelCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("  code: ");
 | 
			
		||||
  out.append("'").append(this->code).append("'");
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -3775,6 +3844,11 @@ void TextCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("  state: ");
 | 
			
		||||
  out.append("'").append(this->state).append("'");
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -3872,6 +3946,11 @@ void DateCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->day);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -3969,6 +4048,11 @@ void TimeCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->second);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -4138,6 +4222,11 @@ void ValveCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("  stop: ");
 | 
			
		||||
  out.append(YESNO(this->stop));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -4215,6 +4304,11 @@ void DateTimeCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->epoch_seconds);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -4323,6 +4417,11 @@ void UpdateCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("  command: ");
 | 
			
		||||
  out.append(proto_enum_to_string<enums::UpdateCommand>(this->command));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  device_id: ");
 | 
			
		||||
  snprintf(buffer, sizeof(buffer), "%" PRIu32, this->device_id);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -195,6 +195,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
 | 
			
		||||
      this->on_home_assistant_state_response(msg);
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
    case 42: {
 | 
			
		||||
      ExecuteServiceRequest msg;
 | 
			
		||||
      msg.decode(msg_data, msg_size);
 | 
			
		||||
@@ -204,6 +205,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
 | 
			
		||||
      this->on_execute_service_request(msg);
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_CAMERA
 | 
			
		||||
    case 45: {
 | 
			
		||||
      CameraImageRequest msg;
 | 
			
		||||
@@ -660,11 +662,13 @@ void APIServerConnection::on_get_time_request(const GetTimeRequest &msg) {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
void APIServerConnection::on_execute_service_request(const ExecuteServiceRequest &msg) {
 | 
			
		||||
  if (this->check_authenticated_()) {
 | 
			
		||||
    this->execute_service(msg);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_API_NOISE
 | 
			
		||||
void APIServerConnection::on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &msg) {
 | 
			
		||||
  if (this->check_authenticated_()) {
 | 
			
		||||
 
 | 
			
		||||
@@ -69,7 +69,9 @@ class APIServerConnectionBase : public ProtoService {
 | 
			
		||||
  virtual void on_get_time_request(const GetTimeRequest &value){};
 | 
			
		||||
  virtual void on_get_time_response(const GetTimeResponse &value){};
 | 
			
		||||
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
  virtual void on_execute_service_request(const ExecuteServiceRequest &value){};
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_CAMERA
 | 
			
		||||
  virtual void on_camera_image_request(const CameraImageRequest &value){};
 | 
			
		||||
@@ -216,7 +218,9 @@ class APIServerConnection : public APIServerConnectionBase {
 | 
			
		||||
  virtual void subscribe_homeassistant_services(const SubscribeHomeassistantServicesRequest &msg) = 0;
 | 
			
		||||
  virtual void subscribe_home_assistant_states(const SubscribeHomeAssistantStatesRequest &msg) = 0;
 | 
			
		||||
  virtual GetTimeResponse get_time(const GetTimeRequest &msg) = 0;
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
  virtual void execute_service(const ExecuteServiceRequest &msg) = 0;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_API_NOISE
 | 
			
		||||
  virtual NoiseEncryptionSetKeyResponse noise_encryption_set_key(const NoiseEncryptionSetKeyRequest &msg) = 0;
 | 
			
		||||
#endif
 | 
			
		||||
@@ -333,7 +337,9 @@ class APIServerConnection : public APIServerConnectionBase {
 | 
			
		||||
  void on_subscribe_homeassistant_services_request(const SubscribeHomeassistantServicesRequest &msg) override;
 | 
			
		||||
  void on_subscribe_home_assistant_states_request(const SubscribeHomeAssistantStatesRequest &msg) override;
 | 
			
		||||
  void on_get_time_request(const GetTimeRequest &msg) override;
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
  void on_execute_service_request(const ExecuteServiceRequest &msg) override;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_API_NOISE
 | 
			
		||||
  void on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &msg) override;
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -1,359 +0,0 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "proto.h"
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <string>
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace api {
 | 
			
		||||
 | 
			
		||||
class ProtoSize {
 | 
			
		||||
 public:
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief ProtoSize class for Protocol Buffer serialization size calculation
 | 
			
		||||
   *
 | 
			
		||||
   * This class provides static methods to calculate the exact byte counts needed
 | 
			
		||||
   * for encoding various Protocol Buffer field types. All methods are designed to be
 | 
			
		||||
   * efficient for the common case where many fields have default values.
 | 
			
		||||
   *
 | 
			
		||||
   * Implements Protocol Buffer encoding size calculation according to:
 | 
			
		||||
   * https://protobuf.dev/programming-guides/encoding/
 | 
			
		||||
   *
 | 
			
		||||
   * Key features:
 | 
			
		||||
   * - Early-return optimization for zero/default values
 | 
			
		||||
   * - Direct total_size updates to avoid unnecessary additions
 | 
			
		||||
   * - Specialized handling for different field types according to protobuf spec
 | 
			
		||||
   * - Templated helpers for repeated fields and messages
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates the size in bytes needed to encode a uint32_t value as a varint
 | 
			
		||||
   *
 | 
			
		||||
   * @param value The uint32_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static inline uint32_t varint(uint32_t value) {
 | 
			
		||||
    // Optimized varint size calculation using leading zeros
 | 
			
		||||
    // Each 7 bits requires one byte in the varint encoding
 | 
			
		||||
    if (value < 128)
 | 
			
		||||
      return 1;  // 7 bits, common case for small values
 | 
			
		||||
 | 
			
		||||
    // For larger values, count bytes needed based on the position of the highest bit set
 | 
			
		||||
    if (value < 16384) {
 | 
			
		||||
      return 2;  // 14 bits
 | 
			
		||||
    } else if (value < 2097152) {
 | 
			
		||||
      return 3;  // 21 bits
 | 
			
		||||
    } else if (value < 268435456) {
 | 
			
		||||
      return 4;  // 28 bits
 | 
			
		||||
    } else {
 | 
			
		||||
      return 5;  // 32 bits (maximum for uint32_t)
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates the size in bytes needed to encode a uint64_t value as a varint
 | 
			
		||||
   *
 | 
			
		||||
   * @param value The uint64_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static inline uint32_t varint(uint64_t value) {
 | 
			
		||||
    // Handle common case of values fitting in uint32_t (vast majority of use cases)
 | 
			
		||||
    if (value <= UINT32_MAX) {
 | 
			
		||||
      return varint(static_cast<uint32_t>(value));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // For larger values, determine size based on highest bit position
 | 
			
		||||
    if (value < (1ULL << 35)) {
 | 
			
		||||
      return 5;  // 35 bits
 | 
			
		||||
    } else if (value < (1ULL << 42)) {
 | 
			
		||||
      return 6;  // 42 bits
 | 
			
		||||
    } else if (value < (1ULL << 49)) {
 | 
			
		||||
      return 7;  // 49 bits
 | 
			
		||||
    } else if (value < (1ULL << 56)) {
 | 
			
		||||
      return 8;  // 56 bits
 | 
			
		||||
    } else if (value < (1ULL << 63)) {
 | 
			
		||||
      return 9;  // 63 bits
 | 
			
		||||
    } else {
 | 
			
		||||
      return 10;  // 64 bits (maximum for uint64_t)
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates the size in bytes needed to encode an int32_t value as a varint
 | 
			
		||||
   *
 | 
			
		||||
   * Special handling is needed for negative values, which are sign-extended to 64 bits
 | 
			
		||||
   * in Protocol Buffers, resulting in a 10-byte varint.
 | 
			
		||||
   *
 | 
			
		||||
   * @param value The int32_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static inline uint32_t varint(int32_t value) {
 | 
			
		||||
    // Negative values are sign-extended to 64 bits in protocol buffers,
 | 
			
		||||
    // which always results in a 10-byte varint for negative int32
 | 
			
		||||
    if (value < 0) {
 | 
			
		||||
      return 10;  // Negative int32 is always 10 bytes long
 | 
			
		||||
    }
 | 
			
		||||
    // For non-negative values, use the uint32_t implementation
 | 
			
		||||
    return varint(static_cast<uint32_t>(value));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates the size in bytes needed to encode an int64_t value as a varint
 | 
			
		||||
   *
 | 
			
		||||
   * @param value The int64_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static inline uint32_t varint(int64_t value) {
 | 
			
		||||
    // For int64_t, we convert to uint64_t and calculate the size
 | 
			
		||||
    // This works because the bit pattern determines the encoding size,
 | 
			
		||||
    // and we've handled negative int32 values as a special case above
 | 
			
		||||
    return varint(static_cast<uint64_t>(value));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates the size in bytes needed to encode a field ID and wire type
 | 
			
		||||
   *
 | 
			
		||||
   * @param field_id The field identifier
 | 
			
		||||
   * @param type The wire type value (from the WireType enum in the protobuf spec)
 | 
			
		||||
   * @return The number of bytes needed to encode the field ID and wire type
 | 
			
		||||
   */
 | 
			
		||||
  static inline uint32_t field(uint32_t field_id, uint32_t type) {
 | 
			
		||||
    uint32_t tag = (field_id << 3) | (type & 0b111);
 | 
			
		||||
    return varint(tag);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Common parameters for all add_*_field methods
 | 
			
		||||
   *
 | 
			
		||||
   * All add_*_field methods follow these common patterns:
 | 
			
		||||
   *
 | 
			
		||||
   * @param total_size Reference to the total message size to update
 | 
			
		||||
   * @param field_id_size Pre-calculated size of the field ID in bytes
 | 
			
		||||
   * @param value The value to calculate size for (type varies)
 | 
			
		||||
   * @param force Whether to calculate size even if the value is default/zero/empty
 | 
			
		||||
   *
 | 
			
		||||
   * Each method follows this implementation pattern:
 | 
			
		||||
   * 1. Skip calculation if value is default (0, false, empty) and not forced
 | 
			
		||||
   * 2. Calculate the size based on the field's encoding rules
 | 
			
		||||
   * 3. Add the field_id_size + calculated value size to total_size
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of an int32 field to the total message size
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_int32_field(uint32_t &total_size, uint32_t field_id_size, int32_t value, bool force = false) {
 | 
			
		||||
    // Skip calculation if value is zero and not forced
 | 
			
		||||
    if (value == 0 && !force) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Calculate and directly add to total_size
 | 
			
		||||
    if (value < 0) {
 | 
			
		||||
      // Negative values are encoded as 10-byte varints in protobuf
 | 
			
		||||
      total_size += field_id_size + 10;
 | 
			
		||||
    } else {
 | 
			
		||||
      // For non-negative values, use the standard varint size
 | 
			
		||||
      total_size += field_id_size + varint(static_cast<uint32_t>(value));
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a uint32 field to the total message size
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_uint32_field(uint32_t &total_size, uint32_t field_id_size, uint32_t value,
 | 
			
		||||
                                      bool force = false) {
 | 
			
		||||
    // Skip calculation if value is zero and not forced
 | 
			
		||||
    if (value == 0 && !force) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Calculate and directly add to total_size
 | 
			
		||||
    total_size += field_id_size + varint(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a boolean field to the total message size
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_bool_field(uint32_t &total_size, uint32_t field_id_size, bool value, bool force = false) {
 | 
			
		||||
    // Skip calculation if value is false and not forced
 | 
			
		||||
    if (!value && !force) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Boolean fields always use 1 byte when true
 | 
			
		||||
    total_size += field_id_size + 1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a fixed field to the total message size
 | 
			
		||||
   *
 | 
			
		||||
   * Fixed fields always take exactly N bytes (4 for fixed32/float, 8 for fixed64/double).
 | 
			
		||||
   *
 | 
			
		||||
   * @tparam NumBytes The number of bytes for this fixed field (4 or 8)
 | 
			
		||||
   * @param is_nonzero Whether the value is non-zero
 | 
			
		||||
   */
 | 
			
		||||
  template<uint32_t NumBytes>
 | 
			
		||||
  static inline void add_fixed_field(uint32_t &total_size, uint32_t field_id_size, bool is_nonzero,
 | 
			
		||||
                                     bool force = false) {
 | 
			
		||||
    // Skip calculation if value is zero and not forced
 | 
			
		||||
    if (!is_nonzero && !force) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Fixed fields always take exactly NumBytes
 | 
			
		||||
    total_size += field_id_size + NumBytes;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of an enum field to the total message size
 | 
			
		||||
   *
 | 
			
		||||
   * Enum fields are encoded as uint32 varints.
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_enum_field(uint32_t &total_size, uint32_t field_id_size, uint32_t value, bool force = false) {
 | 
			
		||||
    // Skip calculation if value is zero and not forced
 | 
			
		||||
    if (value == 0 && !force) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Enums are encoded as uint32
 | 
			
		||||
    total_size += field_id_size + varint(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a sint32 field to the total message size
 | 
			
		||||
   *
 | 
			
		||||
   * Sint32 fields use ZigZag encoding, which is more efficient for negative values.
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_sint32_field(uint32_t &total_size, uint32_t field_id_size, int32_t value, bool force = false) {
 | 
			
		||||
    // Skip calculation if value is zero and not forced
 | 
			
		||||
    if (value == 0 && !force) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // ZigZag encoding for sint32: (n << 1) ^ (n >> 31)
 | 
			
		||||
    uint32_t zigzag = (static_cast<uint32_t>(value) << 1) ^ (static_cast<uint32_t>(value >> 31));
 | 
			
		||||
    total_size += field_id_size + varint(zigzag);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of an int64 field to the total message size
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_int64_field(uint32_t &total_size, uint32_t field_id_size, int64_t value, bool force = false) {
 | 
			
		||||
    // Skip calculation if value is zero and not forced
 | 
			
		||||
    if (value == 0 && !force) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Calculate and directly add to total_size
 | 
			
		||||
    total_size += field_id_size + varint(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a uint64 field to the total message size
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_uint64_field(uint32_t &total_size, uint32_t field_id_size, uint64_t value,
 | 
			
		||||
                                      bool force = false) {
 | 
			
		||||
    // Skip calculation if value is zero and not forced
 | 
			
		||||
    if (value == 0 && !force) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Calculate and directly add to total_size
 | 
			
		||||
    total_size += field_id_size + varint(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a sint64 field to the total message size
 | 
			
		||||
   *
 | 
			
		||||
   * Sint64 fields use ZigZag encoding, which is more efficient for negative values.
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_sint64_field(uint32_t &total_size, uint32_t field_id_size, int64_t value, bool force = false) {
 | 
			
		||||
    // Skip calculation if value is zero and not forced
 | 
			
		||||
    if (value == 0 && !force) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // ZigZag encoding for sint64: (n << 1) ^ (n >> 63)
 | 
			
		||||
    uint64_t zigzag = (static_cast<uint64_t>(value) << 1) ^ (static_cast<uint64_t>(value >> 63));
 | 
			
		||||
    total_size += field_id_size + varint(zigzag);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a string/bytes field to the total message size
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_string_field(uint32_t &total_size, uint32_t field_id_size, const std::string &str,
 | 
			
		||||
                                      bool force = false) {
 | 
			
		||||
    // Skip calculation if string is empty and not forced
 | 
			
		||||
    if (str.empty() && !force) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Calculate and directly add to total_size
 | 
			
		||||
    const uint32_t str_size = static_cast<uint32_t>(str.size());
 | 
			
		||||
    total_size += field_id_size + varint(str_size) + str_size;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a nested message field to the total message size
 | 
			
		||||
   *
 | 
			
		||||
   * This helper function directly updates the total_size reference if the nested size
 | 
			
		||||
   * is greater than zero or force is true.
 | 
			
		||||
   *
 | 
			
		||||
   * @param nested_size The pre-calculated size of the nested message
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_message_field(uint32_t &total_size, uint32_t field_id_size, uint32_t nested_size,
 | 
			
		||||
                                       bool force = false) {
 | 
			
		||||
    // Skip calculation if nested message is empty and not forced
 | 
			
		||||
    if (nested_size == 0 && !force) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Calculate and directly add to total_size
 | 
			
		||||
    // Field ID + length varint + nested message content
 | 
			
		||||
    total_size += field_id_size + varint(nested_size) + nested_size;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a nested message field to the total message size
 | 
			
		||||
   *
 | 
			
		||||
   * This version takes a ProtoMessage object, calculates its size internally,
 | 
			
		||||
   * and updates the total_size reference. This eliminates the need for a temporary variable
 | 
			
		||||
   * at the call site.
 | 
			
		||||
   *
 | 
			
		||||
   * @param message The nested message object
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_message_object(uint32_t &total_size, uint32_t field_id_size, const ProtoMessage &message,
 | 
			
		||||
                                        bool force = false) {
 | 
			
		||||
    uint32_t nested_size = 0;
 | 
			
		||||
    message.calculate_size(nested_size);
 | 
			
		||||
 | 
			
		||||
    // Use the base implementation with the calculated nested_size
 | 
			
		||||
    add_message_field(total_size, field_id_size, nested_size, force);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the sizes of all messages in a repeated field to the total message size
 | 
			
		||||
   *
 | 
			
		||||
   * This helper processes a vector of message objects, calculating the size for each message
 | 
			
		||||
   * and adding it to the total size.
 | 
			
		||||
   *
 | 
			
		||||
   * @tparam MessageType The type of the nested messages in the vector
 | 
			
		||||
   * @param messages Vector of message objects
 | 
			
		||||
   */
 | 
			
		||||
  template<typename MessageType>
 | 
			
		||||
  static inline void add_repeated_message(uint32_t &total_size, uint32_t field_id_size,
 | 
			
		||||
                                          const std::vector<MessageType> &messages) {
 | 
			
		||||
    // Skip if the vector is empty
 | 
			
		||||
    if (messages.empty()) {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // For repeated fields, always use force=true
 | 
			
		||||
    for (const auto &message : messages) {
 | 
			
		||||
      add_message_object(total_size, field_id_size, message, true);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace api
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -24,14 +24,6 @@ static const char *const TAG = "api";
 | 
			
		||||
// APIServer
 | 
			
		||||
APIServer *global_api_server = nullptr;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
 | 
			
		||||
 | 
			
		||||
#ifndef USE_API_YAML_SERVICES
 | 
			
		||||
// Global empty vector to avoid guard variables (saves 8 bytes)
 | 
			
		||||
// This is initialized at program startup before any threads
 | 
			
		||||
static const std::vector<UserServiceDescriptor *> empty_user_services{};
 | 
			
		||||
 | 
			
		||||
const std::vector<UserServiceDescriptor *> &get_empty_user_services_instance() { return empty_user_services; }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
APIServer::APIServer() {
 | 
			
		||||
  global_api_server = this;
 | 
			
		||||
  // Pre-allocate shared write buffer
 | 
			
		||||
@@ -475,7 +467,8 @@ void APIServer::on_shutdown() {
 | 
			
		||||
    if (!c->send_message(DisconnectRequest())) {
 | 
			
		||||
      // If we can't send the disconnect request directly (tx_buffer full),
 | 
			
		||||
      // schedule it at the front of the batch so it will be sent with priority
 | 
			
		||||
      c->schedule_message_front_(nullptr, &APIConnection::try_send_disconnect_request, DisconnectRequest::MESSAGE_TYPE);
 | 
			
		||||
      c->schedule_message_front_(nullptr, &APIConnection::try_send_disconnect_request, DisconnectRequest::MESSAGE_TYPE,
 | 
			
		||||
                                 DisconnectRequest::ESTIMATED_SIZE);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,9 @@
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
#include "list_entities.h"
 | 
			
		||||
#include "subscribe_state.h"
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
#include "user_services.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
@@ -25,11 +27,6 @@ struct SavedNoisePsk {
 | 
			
		||||
} PACKED;  // NOLINT
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifndef USE_API_YAML_SERVICES
 | 
			
		||||
// Forward declaration of helper function
 | 
			
		||||
const std::vector<UserServiceDescriptor *> &get_empty_user_services_instance();
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
class APIServer : public Component, public Controller {
 | 
			
		||||
 public:
 | 
			
		||||
  APIServer();
 | 
			
		||||
@@ -112,18 +109,9 @@ class APIServer : public Component, public Controller {
 | 
			
		||||
  void on_media_player_update(media_player::MediaPlayer *obj) override;
 | 
			
		||||
#endif
 | 
			
		||||
  void send_homeassistant_service_call(const HomeassistantServiceResponse &call);
 | 
			
		||||
  void register_user_service(UserServiceDescriptor *descriptor) {
 | 
			
		||||
#ifdef USE_API_YAML_SERVICES
 | 
			
		||||
    // Vector is pre-allocated when services are defined in YAML
 | 
			
		||||
    this->user_services_.push_back(descriptor);
 | 
			
		||||
#else
 | 
			
		||||
    // Lazy allocate vector on first use for CustomAPIDevice
 | 
			
		||||
    if (!this->user_services_) {
 | 
			
		||||
      this->user_services_ = std::make_unique<std::vector<UserServiceDescriptor *>>();
 | 
			
		||||
    }
 | 
			
		||||
    this->user_services_->push_back(descriptor);
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
  void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); }
 | 
			
		||||
#endif
 | 
			
		||||
  }
 | 
			
		||||
#ifdef USE_HOMEASSISTANT_TIME
 | 
			
		||||
  void request_time();
 | 
			
		||||
#endif
 | 
			
		||||
@@ -152,17 +140,9 @@ class APIServer : public Component, public Controller {
 | 
			
		||||
  void get_home_assistant_state(std::string entity_id, optional<std::string> attribute,
 | 
			
		||||
                                std::function<void(std::string)> f);
 | 
			
		||||
  const std::vector<HomeAssistantStateSubscription> &get_state_subs() const;
 | 
			
		||||
  const std::vector<UserServiceDescriptor *> &get_user_services() const {
 | 
			
		||||
#ifdef USE_API_YAML_SERVICES
 | 
			
		||||
    return this->user_services_;
 | 
			
		||||
#else
 | 
			
		||||
    if (this->user_services_) {
 | 
			
		||||
      return *this->user_services_;
 | 
			
		||||
    }
 | 
			
		||||
    // Return reference to global empty instance (no guard needed)
 | 
			
		||||
    return get_empty_user_services_instance();
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
  const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
 | 
			
		||||
#endif
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
#ifdef USE_API_CLIENT_CONNECTED_TRIGGER
 | 
			
		||||
  Trigger<std::string, std::string> *get_client_connected_trigger() const { return this->client_connected_trigger_; }
 | 
			
		||||
@@ -194,14 +174,8 @@ class APIServer : public Component, public Controller {
 | 
			
		||||
#endif
 | 
			
		||||
  std::vector<uint8_t> shared_write_buffer_;  // Shared proto write buffer for all connections
 | 
			
		||||
  std::vector<HomeAssistantStateSubscription> state_subs_;
 | 
			
		||||
#ifdef USE_API_YAML_SERVICES
 | 
			
		||||
  // When services are defined in YAML, we know at compile time that services will be registered
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
  std::vector<UserServiceDescriptor *> user_services_;
 | 
			
		||||
#else
 | 
			
		||||
  // Services can still be registered at runtime by CustomAPIDevice components even when not
 | 
			
		||||
  // defined in YAML. Using unique_ptr allows lazy allocation, saving 12 bytes in the common
 | 
			
		||||
  // case where no services (YAML or custom) are used.
 | 
			
		||||
  std::unique_ptr<std::vector<UserServiceDescriptor *>> user_services_;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  // Group smaller types together
 | 
			
		||||
 
 | 
			
		||||
@@ -3,10 +3,13 @@
 | 
			
		||||
#include <map>
 | 
			
		||||
#include "api_server.h"
 | 
			
		||||
#ifdef USE_API
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
#include "user_services.h"
 | 
			
		||||
#endif
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace api {
 | 
			
		||||
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
template<typename T, typename... Ts> class CustomAPIDeviceService : public UserServiceBase<Ts...> {
 | 
			
		||||
 public:
 | 
			
		||||
  CustomAPIDeviceService(const std::string &name, const std::array<std::string, sizeof...(Ts)> &arg_names, T *obj,
 | 
			
		||||
@@ -19,6 +22,7 @@ template<typename T, typename... Ts> class CustomAPIDeviceService : public UserS
 | 
			
		||||
  T *obj_;
 | 
			
		||||
  void (T::*callback_)(Ts...);
 | 
			
		||||
};
 | 
			
		||||
#endif  // USE_API_SERVICES
 | 
			
		||||
 | 
			
		||||
class CustomAPIDevice {
 | 
			
		||||
 public:
 | 
			
		||||
@@ -46,12 +50,14 @@ class CustomAPIDevice {
 | 
			
		||||
   * @param name The name of the service to register.
 | 
			
		||||
   * @param arg_names The name of the arguments for the service, must match the arguments of the function.
 | 
			
		||||
   */
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
  template<typename T, typename... Ts>
 | 
			
		||||
  void register_service(void (T::*callback)(Ts...), const std::string &name,
 | 
			
		||||
                        const std::array<std::string, sizeof...(Ts)> &arg_names) {
 | 
			
		||||
    auto *service = new CustomAPIDeviceService<T, Ts...>(name, arg_names, (T *) this, callback);  // NOLINT
 | 
			
		||||
    global_api_server->register_user_service(service);
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  /** Register a custom native API service that will show up in Home Assistant.
 | 
			
		||||
   *
 | 
			
		||||
@@ -71,10 +77,12 @@ class CustomAPIDevice {
 | 
			
		||||
   * @param callback The member function to call when the service is triggered.
 | 
			
		||||
   * @param name The name of the arguments for the service, must match the arguments of the function.
 | 
			
		||||
   */
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
  template<typename T> void register_service(void (T::*callback)(), const std::string &name) {
 | 
			
		||||
    auto *service = new CustomAPIDeviceService<T>(name, {}, (T *) this, callback);  // NOLINT
 | 
			
		||||
    global_api_server->register_user_service(service);
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  /** Subscribe to the state (or attribute state) of an entity from Home Assistant.
 | 
			
		||||
   *
 | 
			
		||||
 
 | 
			
		||||
@@ -83,10 +83,12 @@ bool ListEntitiesIterator::on_end() { return this->client_->send_list_info_done(
 | 
			
		||||
 | 
			
		||||
ListEntitiesIterator::ListEntitiesIterator(APIConnection *client) : client_(client) {}
 | 
			
		||||
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
bool ListEntitiesIterator::on_service(UserServiceDescriptor *service) {
 | 
			
		||||
  auto resp = service->encode_list_service_response();
 | 
			
		||||
  return this->client_->send_message(resp);
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
}  // namespace api
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,7 @@ class APIConnection;
 | 
			
		||||
#define LIST_ENTITIES_HANDLER(entity_type, EntityClass, ResponseType) \
 | 
			
		||||
  bool ListEntitiesIterator::on_##entity_type(EntityClass *entity) { /* NOLINT(bugprone-macro-parentheses) */ \
 | 
			
		||||
    return this->client_->schedule_message_(entity, &APIConnection::try_send_##entity_type##_info, \
 | 
			
		||||
                                            ResponseType::MESSAGE_TYPE); \
 | 
			
		||||
                                            ResponseType::MESSAGE_TYPE, ResponseType::ESTIMATED_SIZE); \
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
class ListEntitiesIterator : public ComponentIterator {
 | 
			
		||||
@@ -44,7 +44,9 @@ class ListEntitiesIterator : public ComponentIterator {
 | 
			
		||||
#ifdef USE_TEXT_SENSOR
 | 
			
		||||
  bool on_text_sensor(text_sensor::TextSensor *entity) override;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
  bool on_service(UserServiceDescriptor *service) override;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_CAMERA
 | 
			
		||||
  bool on_camera(camera::Camera *entity) override;
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
#include <cassert>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
 | 
			
		||||
@@ -59,7 +60,6 @@ class ProtoVarInt {
 | 
			
		||||
  uint32_t as_uint32() const { return this->value_; }
 | 
			
		||||
  uint64_t as_uint64() const { return this->value_; }
 | 
			
		||||
  bool as_bool() const { return this->value_; }
 | 
			
		||||
  template<typename T> T as_enum() const { return static_cast<T>(this->as_uint32()); }
 | 
			
		||||
  int32_t as_int32() const {
 | 
			
		||||
    // Not ZigZag encoded
 | 
			
		||||
    return static_cast<int32_t>(this->as_int64());
 | 
			
		||||
@@ -133,15 +133,24 @@ class ProtoVarInt {
 | 
			
		||||
  uint64_t value_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Forward declaration for decode_to_message and encode_to_writer
 | 
			
		||||
class ProtoMessage;
 | 
			
		||||
 | 
			
		||||
class ProtoLengthDelimited {
 | 
			
		||||
 public:
 | 
			
		||||
  explicit ProtoLengthDelimited(const uint8_t *value, size_t length) : value_(value), length_(length) {}
 | 
			
		||||
  std::string as_string() const { return std::string(reinterpret_cast<const char *>(this->value_), this->length_); }
 | 
			
		||||
  template<class C> C as_message() const {
 | 
			
		||||
    auto msg = C();
 | 
			
		||||
    msg.decode(this->value_, this->length_);
 | 
			
		||||
    return msg;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Decode the length-delimited data into an existing ProtoMessage instance.
 | 
			
		||||
   *
 | 
			
		||||
   * This method allows decoding without templates, enabling use in contexts
 | 
			
		||||
   * where the message type is not known at compile time. The ProtoMessage's
 | 
			
		||||
   * decode() method will be called with the raw data and length.
 | 
			
		||||
   *
 | 
			
		||||
   * @param msg The ProtoMessage instance to decode into
 | 
			
		||||
   */
 | 
			
		||||
  void decode_to_message(ProtoMessage &msg) const;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  const uint8_t *const value_;
 | 
			
		||||
@@ -263,9 +272,6 @@ class ProtoWriteBuffer {
 | 
			
		||||
    this->write((value >> 48) & 0xFF);
 | 
			
		||||
    this->write((value >> 56) & 0xFF);
 | 
			
		||||
  }
 | 
			
		||||
  template<typename T> void encode_enum(uint32_t field_id, T value, bool force = false) {
 | 
			
		||||
    this->encode_uint32(field_id, static_cast<uint32_t>(value), force);
 | 
			
		||||
  }
 | 
			
		||||
  void encode_float(uint32_t field_id, float value, bool force = false) {
 | 
			
		||||
    if (value == 0.0f && !force)
 | 
			
		||||
      return;
 | 
			
		||||
@@ -306,18 +312,7 @@ class ProtoWriteBuffer {
 | 
			
		||||
    }
 | 
			
		||||
    this->encode_uint64(field_id, uvalue, force);
 | 
			
		||||
  }
 | 
			
		||||
  template<class C> void encode_message(uint32_t field_id, const C &value, bool force = false) {
 | 
			
		||||
    this->encode_field_raw(field_id, 2);  // type 2: Length-delimited message
 | 
			
		||||
    size_t begin = this->buffer_->size();
 | 
			
		||||
 | 
			
		||||
    value.encode(*this);
 | 
			
		||||
 | 
			
		||||
    const uint32_t nested_length = this->buffer_->size() - begin;
 | 
			
		||||
    // add size varint
 | 
			
		||||
    std::vector<uint8_t> var;
 | 
			
		||||
    ProtoVarInt(nested_length).encode(var);
 | 
			
		||||
    this->buffer_->insert(this->buffer_->begin() + begin, var.begin(), var.end());
 | 
			
		||||
  }
 | 
			
		||||
  void encode_message(uint32_t field_id, const ProtoMessage &value, bool force = false);
 | 
			
		||||
  std::vector<uint8_t> *get_buffer() const { return buffer_; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
@@ -345,6 +340,494 @@ class ProtoMessage {
 | 
			
		||||
  virtual bool decode_64bit(uint32_t field_id, Proto64Bit value) { return false; }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class ProtoSize {
 | 
			
		||||
 public:
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief ProtoSize class for Protocol Buffer serialization size calculation
 | 
			
		||||
   *
 | 
			
		||||
   * This class provides static methods to calculate the exact byte counts needed
 | 
			
		||||
   * for encoding various Protocol Buffer field types. All methods are designed to be
 | 
			
		||||
   * efficient for the common case where many fields have default values.
 | 
			
		||||
   *
 | 
			
		||||
   * Implements Protocol Buffer encoding size calculation according to:
 | 
			
		||||
   * https://protobuf.dev/programming-guides/encoding/
 | 
			
		||||
   *
 | 
			
		||||
   * Key features:
 | 
			
		||||
   * - Early-return optimization for zero/default values
 | 
			
		||||
   * - Direct total_size updates to avoid unnecessary additions
 | 
			
		||||
   * - Specialized handling for different field types according to protobuf spec
 | 
			
		||||
   * - Templated helpers for repeated fields and messages
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates the size in bytes needed to encode a uint32_t value as a varint
 | 
			
		||||
   *
 | 
			
		||||
   * @param value The uint32_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static inline uint32_t varint(uint32_t value) {
 | 
			
		||||
    // Optimized varint size calculation using leading zeros
 | 
			
		||||
    // Each 7 bits requires one byte in the varint encoding
 | 
			
		||||
    if (value < 128)
 | 
			
		||||
      return 1;  // 7 bits, common case for small values
 | 
			
		||||
 | 
			
		||||
    // For larger values, count bytes needed based on the position of the highest bit set
 | 
			
		||||
    if (value < 16384) {
 | 
			
		||||
      return 2;  // 14 bits
 | 
			
		||||
    } else if (value < 2097152) {
 | 
			
		||||
      return 3;  // 21 bits
 | 
			
		||||
    } else if (value < 268435456) {
 | 
			
		||||
      return 4;  // 28 bits
 | 
			
		||||
    } else {
 | 
			
		||||
      return 5;  // 32 bits (maximum for uint32_t)
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates the size in bytes needed to encode a uint64_t value as a varint
 | 
			
		||||
   *
 | 
			
		||||
   * @param value The uint64_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static inline uint32_t varint(uint64_t value) {
 | 
			
		||||
    // Handle common case of values fitting in uint32_t (vast majority of use cases)
 | 
			
		||||
    if (value <= UINT32_MAX) {
 | 
			
		||||
      return varint(static_cast<uint32_t>(value));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // For larger values, determine size based on highest bit position
 | 
			
		||||
    if (value < (1ULL << 35)) {
 | 
			
		||||
      return 5;  // 35 bits
 | 
			
		||||
    } else if (value < (1ULL << 42)) {
 | 
			
		||||
      return 6;  // 42 bits
 | 
			
		||||
    } else if (value < (1ULL << 49)) {
 | 
			
		||||
      return 7;  // 49 bits
 | 
			
		||||
    } else if (value < (1ULL << 56)) {
 | 
			
		||||
      return 8;  // 56 bits
 | 
			
		||||
    } else if (value < (1ULL << 63)) {
 | 
			
		||||
      return 9;  // 63 bits
 | 
			
		||||
    } else {
 | 
			
		||||
      return 10;  // 64 bits (maximum for uint64_t)
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates the size in bytes needed to encode an int32_t value as a varint
 | 
			
		||||
   *
 | 
			
		||||
   * Special handling is needed for negative values, which are sign-extended to 64 bits
 | 
			
		||||
   * in Protocol Buffers, resulting in a 10-byte varint.
 | 
			
		||||
   *
 | 
			
		||||
   * @param value The int32_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static inline uint32_t varint(int32_t value) {
 | 
			
		||||
    // Negative values are sign-extended to 64 bits in protocol buffers,
 | 
			
		||||
    // which always results in a 10-byte varint for negative int32
 | 
			
		||||
    if (value < 0) {
 | 
			
		||||
      return 10;  // Negative int32 is always 10 bytes long
 | 
			
		||||
    }
 | 
			
		||||
    // For non-negative values, use the uint32_t implementation
 | 
			
		||||
    return varint(static_cast<uint32_t>(value));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates the size in bytes needed to encode an int64_t value as a varint
 | 
			
		||||
   *
 | 
			
		||||
   * @param value The int64_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static inline uint32_t varint(int64_t value) {
 | 
			
		||||
    // For int64_t, we convert to uint64_t and calculate the size
 | 
			
		||||
    // This works because the bit pattern determines the encoding size,
 | 
			
		||||
    // and we've handled negative int32 values as a special case above
 | 
			
		||||
    return varint(static_cast<uint64_t>(value));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates the size in bytes needed to encode a field ID and wire type
 | 
			
		||||
   *
 | 
			
		||||
   * @param field_id The field identifier
 | 
			
		||||
   * @param type The wire type value (from the WireType enum in the protobuf spec)
 | 
			
		||||
   * @return The number of bytes needed to encode the field ID and wire type
 | 
			
		||||
   */
 | 
			
		||||
  static inline uint32_t field(uint32_t field_id, uint32_t type) {
 | 
			
		||||
    uint32_t tag = (field_id << 3) | (type & 0b111);
 | 
			
		||||
    return varint(tag);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Common parameters for all add_*_field methods
 | 
			
		||||
   *
 | 
			
		||||
   * All add_*_field methods follow these common patterns:
 | 
			
		||||
   *
 | 
			
		||||
   * @param total_size Reference to the total message size to update
 | 
			
		||||
   * @param field_id_size Pre-calculated size of the field ID in bytes
 | 
			
		||||
   * @param value The value to calculate size for (type varies)
 | 
			
		||||
   * @param force Whether to calculate size even if the value is default/zero/empty
 | 
			
		||||
   *
 | 
			
		||||
   * Each method follows this implementation pattern:
 | 
			
		||||
   * 1. Skip calculation if value is default (0, false, empty) and not forced
 | 
			
		||||
   * 2. Calculate the size based on the field's encoding rules
 | 
			
		||||
   * 3. Add the field_id_size + calculated value size to total_size
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of an int32 field to the total message size
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_int32_field(uint32_t &total_size, uint32_t field_id_size, int32_t value) {
 | 
			
		||||
    // Skip calculation if value is zero
 | 
			
		||||
    if (value == 0) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Calculate and directly add to total_size
 | 
			
		||||
    if (value < 0) {
 | 
			
		||||
      // Negative values are encoded as 10-byte varints in protobuf
 | 
			
		||||
      total_size += field_id_size + 10;
 | 
			
		||||
    } else {
 | 
			
		||||
      // For non-negative values, use the standard varint size
 | 
			
		||||
      total_size += field_id_size + varint(static_cast<uint32_t>(value));
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of an int32 field to the total message size (repeated field version)
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_int32_field_repeated(uint32_t &total_size, uint32_t field_id_size, int32_t value) {
 | 
			
		||||
    // Always calculate size for repeated fields
 | 
			
		||||
    if (value < 0) {
 | 
			
		||||
      // Negative values are encoded as 10-byte varints in protobuf
 | 
			
		||||
      total_size += field_id_size + 10;
 | 
			
		||||
    } else {
 | 
			
		||||
      // For non-negative values, use the standard varint size
 | 
			
		||||
      total_size += field_id_size + varint(static_cast<uint32_t>(value));
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a uint32 field to the total message size
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_uint32_field(uint32_t &total_size, uint32_t field_id_size, uint32_t value) {
 | 
			
		||||
    // Skip calculation if value is zero
 | 
			
		||||
    if (value == 0) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Calculate and directly add to total_size
 | 
			
		||||
    total_size += field_id_size + varint(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a uint32 field to the total message size (repeated field version)
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_uint32_field_repeated(uint32_t &total_size, uint32_t field_id_size, uint32_t value) {
 | 
			
		||||
    // Always calculate size for repeated fields
 | 
			
		||||
    total_size += field_id_size + varint(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a boolean field to the total message size
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_bool_field(uint32_t &total_size, uint32_t field_id_size, bool value) {
 | 
			
		||||
    // Skip calculation if value is false
 | 
			
		||||
    if (!value) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Boolean fields always use 1 byte when true
 | 
			
		||||
    total_size += field_id_size + 1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a boolean field to the total message size (repeated field version)
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_bool_field_repeated(uint32_t &total_size, uint32_t field_id_size, bool value) {
 | 
			
		||||
    // Always calculate size for repeated fields
 | 
			
		||||
    // Boolean fields always use 1 byte
 | 
			
		||||
    total_size += field_id_size + 1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a fixed field to the total message size
 | 
			
		||||
   *
 | 
			
		||||
   * Fixed fields always take exactly N bytes (4 for fixed32/float, 8 for fixed64/double).
 | 
			
		||||
   *
 | 
			
		||||
   * @tparam NumBytes The number of bytes for this fixed field (4 or 8)
 | 
			
		||||
   * @param is_nonzero Whether the value is non-zero
 | 
			
		||||
   */
 | 
			
		||||
  template<uint32_t NumBytes>
 | 
			
		||||
  static inline void add_fixed_field(uint32_t &total_size, uint32_t field_id_size, bool is_nonzero) {
 | 
			
		||||
    // Skip calculation if value is zero
 | 
			
		||||
    if (!is_nonzero) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Fixed fields always take exactly NumBytes
 | 
			
		||||
    total_size += field_id_size + NumBytes;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of an enum field to the total message size
 | 
			
		||||
   *
 | 
			
		||||
   * Enum fields are encoded as uint32 varints.
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_enum_field(uint32_t &total_size, uint32_t field_id_size, uint32_t value) {
 | 
			
		||||
    // Skip calculation if value is zero
 | 
			
		||||
    if (value == 0) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Enums are encoded as uint32
 | 
			
		||||
    total_size += field_id_size + varint(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of an enum field to the total message size (repeated field version)
 | 
			
		||||
   *
 | 
			
		||||
   * Enum fields are encoded as uint32 varints.
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_enum_field_repeated(uint32_t &total_size, uint32_t field_id_size, uint32_t value) {
 | 
			
		||||
    // Always calculate size for repeated fields
 | 
			
		||||
    // Enums are encoded as uint32
 | 
			
		||||
    total_size += field_id_size + varint(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a sint32 field to the total message size
 | 
			
		||||
   *
 | 
			
		||||
   * Sint32 fields use ZigZag encoding, which is more efficient for negative values.
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_sint32_field(uint32_t &total_size, uint32_t field_id_size, int32_t value) {
 | 
			
		||||
    // Skip calculation if value is zero
 | 
			
		||||
    if (value == 0) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // ZigZag encoding for sint32: (n << 1) ^ (n >> 31)
 | 
			
		||||
    uint32_t zigzag = (static_cast<uint32_t>(value) << 1) ^ (static_cast<uint32_t>(value >> 31));
 | 
			
		||||
    total_size += field_id_size + varint(zigzag);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a sint32 field to the total message size (repeated field version)
 | 
			
		||||
   *
 | 
			
		||||
   * Sint32 fields use ZigZag encoding, which is more efficient for negative values.
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_sint32_field_repeated(uint32_t &total_size, uint32_t field_id_size, int32_t value) {
 | 
			
		||||
    // Always calculate size for repeated fields
 | 
			
		||||
    // ZigZag encoding for sint32: (n << 1) ^ (n >> 31)
 | 
			
		||||
    uint32_t zigzag = (static_cast<uint32_t>(value) << 1) ^ (static_cast<uint32_t>(value >> 31));
 | 
			
		||||
    total_size += field_id_size + varint(zigzag);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of an int64 field to the total message size
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_int64_field(uint32_t &total_size, uint32_t field_id_size, int64_t value) {
 | 
			
		||||
    // Skip calculation if value is zero
 | 
			
		||||
    if (value == 0) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Calculate and directly add to total_size
 | 
			
		||||
    total_size += field_id_size + varint(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of an int64 field to the total message size (repeated field version)
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_int64_field_repeated(uint32_t &total_size, uint32_t field_id_size, int64_t value) {
 | 
			
		||||
    // Always calculate size for repeated fields
 | 
			
		||||
    total_size += field_id_size + varint(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a uint64 field to the total message size
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_uint64_field(uint32_t &total_size, uint32_t field_id_size, uint64_t value) {
 | 
			
		||||
    // Skip calculation if value is zero
 | 
			
		||||
    if (value == 0) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Calculate and directly add to total_size
 | 
			
		||||
    total_size += field_id_size + varint(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a uint64 field to the total message size (repeated field version)
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_uint64_field_repeated(uint32_t &total_size, uint32_t field_id_size, uint64_t value) {
 | 
			
		||||
    // Always calculate size for repeated fields
 | 
			
		||||
    total_size += field_id_size + varint(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a sint64 field to the total message size
 | 
			
		||||
   *
 | 
			
		||||
   * Sint64 fields use ZigZag encoding, which is more efficient for negative values.
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_sint64_field(uint32_t &total_size, uint32_t field_id_size, int64_t value) {
 | 
			
		||||
    // Skip calculation if value is zero
 | 
			
		||||
    if (value == 0) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // ZigZag encoding for sint64: (n << 1) ^ (n >> 63)
 | 
			
		||||
    uint64_t zigzag = (static_cast<uint64_t>(value) << 1) ^ (static_cast<uint64_t>(value >> 63));
 | 
			
		||||
    total_size += field_id_size + varint(zigzag);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a sint64 field to the total message size (repeated field version)
 | 
			
		||||
   *
 | 
			
		||||
   * Sint64 fields use ZigZag encoding, which is more efficient for negative values.
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_sint64_field_repeated(uint32_t &total_size, uint32_t field_id_size, int64_t value) {
 | 
			
		||||
    // Always calculate size for repeated fields
 | 
			
		||||
    // ZigZag encoding for sint64: (n << 1) ^ (n >> 63)
 | 
			
		||||
    uint64_t zigzag = (static_cast<uint64_t>(value) << 1) ^ (static_cast<uint64_t>(value >> 63));
 | 
			
		||||
    total_size += field_id_size + varint(zigzag);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a string/bytes field to the total message size
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_string_field(uint32_t &total_size, uint32_t field_id_size, const std::string &str) {
 | 
			
		||||
    // Skip calculation if string is empty
 | 
			
		||||
    if (str.empty()) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Calculate and directly add to total_size
 | 
			
		||||
    const uint32_t str_size = static_cast<uint32_t>(str.size());
 | 
			
		||||
    total_size += field_id_size + varint(str_size) + str_size;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a string/bytes field to the total message size (repeated field version)
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_string_field_repeated(uint32_t &total_size, uint32_t field_id_size, const std::string &str) {
 | 
			
		||||
    // Always calculate size for repeated fields
 | 
			
		||||
    const uint32_t str_size = static_cast<uint32_t>(str.size());
 | 
			
		||||
    total_size += field_id_size + varint(str_size) + str_size;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a nested message field to the total message size
 | 
			
		||||
   *
 | 
			
		||||
   * This helper function directly updates the total_size reference if the nested size
 | 
			
		||||
   * is greater than zero.
 | 
			
		||||
   *
 | 
			
		||||
   * @param nested_size The pre-calculated size of the nested message
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_message_field(uint32_t &total_size, uint32_t field_id_size, uint32_t nested_size) {
 | 
			
		||||
    // Skip calculation if nested message is empty
 | 
			
		||||
    if (nested_size == 0) {
 | 
			
		||||
      return;  // No need to update total_size
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Calculate and directly add to total_size
 | 
			
		||||
    // Field ID + length varint + nested message content
 | 
			
		||||
    total_size += field_id_size + varint(nested_size) + nested_size;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a nested message field to the total message size (repeated field version)
 | 
			
		||||
   *
 | 
			
		||||
   * @param nested_size The pre-calculated size of the nested message
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_message_field_repeated(uint32_t &total_size, uint32_t field_id_size, uint32_t nested_size) {
 | 
			
		||||
    // Always calculate size for repeated fields
 | 
			
		||||
    // Field ID + length varint + nested message content
 | 
			
		||||
    total_size += field_id_size + varint(nested_size) + nested_size;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a nested message field to the total message size
 | 
			
		||||
   *
 | 
			
		||||
   * This version takes a ProtoMessage object, calculates its size internally,
 | 
			
		||||
   * and updates the total_size reference. This eliminates the need for a temporary variable
 | 
			
		||||
   * at the call site.
 | 
			
		||||
   *
 | 
			
		||||
   * @param message The nested message object
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_message_object(uint32_t &total_size, uint32_t field_id_size, const ProtoMessage &message) {
 | 
			
		||||
    uint32_t nested_size = 0;
 | 
			
		||||
    message.calculate_size(nested_size);
 | 
			
		||||
 | 
			
		||||
    // Use the base implementation with the calculated nested_size
 | 
			
		||||
    add_message_field(total_size, field_id_size, nested_size);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the size of a nested message field to the total message size (repeated field version)
 | 
			
		||||
   *
 | 
			
		||||
   * @param message The nested message object
 | 
			
		||||
   */
 | 
			
		||||
  static inline void add_message_object_repeated(uint32_t &total_size, uint32_t field_id_size,
 | 
			
		||||
                                                 const ProtoMessage &message) {
 | 
			
		||||
    uint32_t nested_size = 0;
 | 
			
		||||
    message.calculate_size(nested_size);
 | 
			
		||||
 | 
			
		||||
    // Use the base implementation with the calculated nested_size
 | 
			
		||||
    add_message_field_repeated(total_size, field_id_size, nested_size);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * @brief Calculates and adds the sizes of all messages in a repeated field to the total message size
 | 
			
		||||
   *
 | 
			
		||||
   * This helper processes a vector of message objects, calculating the size for each message
 | 
			
		||||
   * and adding it to the total size.
 | 
			
		||||
   *
 | 
			
		||||
   * @tparam MessageType The type of the nested messages in the vector
 | 
			
		||||
   * @param messages Vector of message objects
 | 
			
		||||
   */
 | 
			
		||||
  template<typename MessageType>
 | 
			
		||||
  static inline void add_repeated_message(uint32_t &total_size, uint32_t field_id_size,
 | 
			
		||||
                                          const std::vector<MessageType> &messages) {
 | 
			
		||||
    // Skip if the vector is empty
 | 
			
		||||
    if (messages.empty()) {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Use the repeated field version for all messages
 | 
			
		||||
    for (const auto &message : messages) {
 | 
			
		||||
      add_message_object_repeated(total_size, field_id_size, message);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Implementation of encode_message - must be after ProtoMessage is defined
 | 
			
		||||
inline void ProtoWriteBuffer::encode_message(uint32_t field_id, const ProtoMessage &value, bool force) {
 | 
			
		||||
  this->encode_field_raw(field_id, 2);  // type 2: Length-delimited message
 | 
			
		||||
 | 
			
		||||
  // Calculate the message size first
 | 
			
		||||
  uint32_t msg_length_bytes = 0;
 | 
			
		||||
  value.calculate_size(msg_length_bytes);
 | 
			
		||||
 | 
			
		||||
  // Calculate how many bytes the length varint needs
 | 
			
		||||
  uint32_t varint_length_bytes = ProtoSize::varint(msg_length_bytes);
 | 
			
		||||
 | 
			
		||||
  // Reserve exact space for the length varint
 | 
			
		||||
  size_t begin = this->buffer_->size();
 | 
			
		||||
  this->buffer_->resize(this->buffer_->size() + varint_length_bytes);
 | 
			
		||||
 | 
			
		||||
  // Write the length varint directly
 | 
			
		||||
  ProtoVarInt(msg_length_bytes).encode_to_buffer_unchecked(this->buffer_->data() + begin, varint_length_bytes);
 | 
			
		||||
 | 
			
		||||
  // Now encode the message content - it will append to the buffer
 | 
			
		||||
  value.encode(*this);
 | 
			
		||||
 | 
			
		||||
  // Verify that the encoded size matches what we calculated
 | 
			
		||||
  assert(this->buffer_->size() == begin + varint_length_bytes + msg_length_bytes);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Implementation of decode_to_message - must be after ProtoMessage is defined
 | 
			
		||||
inline void ProtoLengthDelimited::decode_to_message(ProtoMessage &msg) const {
 | 
			
		||||
  msg.decode(this->value_, this->length_);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<typename T> const char *proto_enum_to_string(T value);
 | 
			
		||||
 | 
			
		||||
class ProtoService {
 | 
			
		||||
@@ -363,11 +846,11 @@ class ProtoService {
 | 
			
		||||
   * @return A ProtoWriteBuffer object with the reserved size.
 | 
			
		||||
   */
 | 
			
		||||
  virtual ProtoWriteBuffer create_buffer(uint32_t reserve_size) = 0;
 | 
			
		||||
  virtual bool send_buffer(ProtoWriteBuffer buffer, uint16_t message_type) = 0;
 | 
			
		||||
  virtual bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) = 0;
 | 
			
		||||
  virtual void read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data) = 0;
 | 
			
		||||
 | 
			
		||||
  // Optimized method that pre-allocates buffer based on message size
 | 
			
		||||
  bool send_message_(const ProtoMessage &msg, uint16_t message_type) {
 | 
			
		||||
  bool send_message_(const ProtoMessage &msg, uint8_t message_type) {
 | 
			
		||||
    uint32_t msg_size = 0;
 | 
			
		||||
    msg.calculate_size(msg_size);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@
 | 
			
		||||
#include "esphome/core/automation.h"
 | 
			
		||||
#include "api_pb2.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace api {
 | 
			
		||||
 | 
			
		||||
@@ -73,3 +74,4 @@ template<typename... Ts> class UserServiceTrigger : public UserServiceBase<Ts...
 | 
			
		||||
 | 
			
		||||
}  // namespace api
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
#endif  // USE_API_SERVICES
 | 
			
		||||
 
 | 
			
		||||
@@ -53,6 +53,7 @@ void DebugComponent::on_shutdown() {
 | 
			
		||||
  auto pref = global_preferences->make_preference(REBOOT_MAX_LEN, fnv1_hash(REBOOT_KEY + App.get_name()));
 | 
			
		||||
  if (component != nullptr) {
 | 
			
		||||
    strncpy(buffer, component->get_component_source(), REBOOT_MAX_LEN - 1);
 | 
			
		||||
    buffer[REBOOT_MAX_LEN - 1] = '\0';
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGD(TAG, "Storing reboot source: %s", buffer);
 | 
			
		||||
  pref.save(&buffer);
 | 
			
		||||
@@ -68,6 +69,7 @@ std::string DebugComponent::get_reset_reason_() {
 | 
			
		||||
      auto pref = global_preferences->make_preference(REBOOT_MAX_LEN, fnv1_hash(REBOOT_KEY + App.get_name()));
 | 
			
		||||
      char buffer[REBOOT_MAX_LEN]{};
 | 
			
		||||
      if (pref.load(&buffer)) {
 | 
			
		||||
        buffer[REBOOT_MAX_LEN - 1] = '\0';
 | 
			
		||||
        reset_reason = "Reboot request from " + std::string(buffer);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -707,6 +707,7 @@ async def to_code(config):
 | 
			
		||||
    cg.add_define("ESPHOME_VARIANT", VARIANT_FRIENDLY[config[CONF_VARIANT]])
 | 
			
		||||
 | 
			
		||||
    cg.add_platformio_option("lib_ldf_mode", "off")
 | 
			
		||||
    cg.add_platformio_option("lib_compat_mode", "strict")
 | 
			
		||||
 | 
			
		||||
    framework_ver: cv.Version = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -308,7 +308,7 @@ async def to_code(config):
 | 
			
		||||
    cg.add(var.set_frame_buffer_count(config[CONF_FRAME_BUFFER_COUNT]))
 | 
			
		||||
    cg.add(var.set_frame_size(config[CONF_RESOLUTION]))
 | 
			
		||||
 | 
			
		||||
    cg.add_define("USE_ESP32_CAMERA")
 | 
			
		||||
    cg.add_define("USE_CAMERA")
 | 
			
		||||
 | 
			
		||||
    if CORE.using_esp_idf:
 | 
			
		||||
        add_idf_component(name="espressif/esp32-camera", ref="2.0.15")
 | 
			
		||||
 
 | 
			
		||||
@@ -109,6 +109,7 @@ void ESP32TouchComponent::loop() {
 | 
			
		||||
 | 
			
		||||
      // Only publish if state changed - this filters out repeated events
 | 
			
		||||
      if (new_state != child->last_state_) {
 | 
			
		||||
        child->initial_state_published_ = true;
 | 
			
		||||
        child->last_state_ = new_state;
 | 
			
		||||
        child->publish_state(new_state);
 | 
			
		||||
        // Original ESP32: ISR only fires when touched, release is detected by timeout
 | 
			
		||||
@@ -175,6 +176,9 @@ void ESP32TouchComponent::on_shutdown() {
 | 
			
		||||
void IRAM_ATTR ESP32TouchComponent::touch_isr_handler(void *arg) {
 | 
			
		||||
  ESP32TouchComponent *component = static_cast<ESP32TouchComponent *>(arg);
 | 
			
		||||
 | 
			
		||||
  uint32_t mask = 0;
 | 
			
		||||
  touch_ll_read_trigger_status_mask(&mask);
 | 
			
		||||
  touch_ll_clear_trigger_status_mask();
 | 
			
		||||
  touch_pad_clear_status();
 | 
			
		||||
 | 
			
		||||
  // INTERRUPT BEHAVIOR: On ESP32 v1 hardware, the interrupt fires when ANY configured
 | 
			
		||||
@@ -184,6 +188,11 @@ void IRAM_ATTR ESP32TouchComponent::touch_isr_handler(void *arg) {
 | 
			
		||||
  // as any pad remains touched. This allows us to detect both new touches and
 | 
			
		||||
  // continued touches, but releases must be detected by timeout in the main loop.
 | 
			
		||||
 | 
			
		||||
  // IMPORTANT: ESP32 v1 touch detection logic - INVERTED compared to v2!
 | 
			
		||||
  // ESP32 v1: Touch is detected when capacitance INCREASES, causing the measured value to DECREASE
 | 
			
		||||
  // Therefore: touched = (value < threshold)
 | 
			
		||||
  // This is opposite to ESP32-S2/S3 v2 where touched = (value > threshold)
 | 
			
		||||
 | 
			
		||||
  // Process all configured pads to check their current state
 | 
			
		||||
  // Note: ESP32 v1 doesn't tell us which specific pad triggered the interrupt,
 | 
			
		||||
  // so we must scan all configured pads to find which ones were touched
 | 
			
		||||
@@ -201,19 +210,12 @@ void IRAM_ATTR ESP32TouchComponent::touch_isr_handler(void *arg) {
 | 
			
		||||
      value = touch_ll_read_raw_data(pad);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Skip pads with 0 value - they haven't been measured in this cycle
 | 
			
		||||
    // This is important: not all pads are measured every interrupt cycle,
 | 
			
		||||
    // only those that the hardware has updated
 | 
			
		||||
    if (value == 0) {
 | 
			
		||||
    // Skip pads that aren’t in the trigger mask
 | 
			
		||||
    bool is_touched = (mask >> pad) & 1;
 | 
			
		||||
    if (!is_touched) {
 | 
			
		||||
      continue;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // IMPORTANT: ESP32 v1 touch detection logic - INVERTED compared to v2!
 | 
			
		||||
    // ESP32 v1: Touch is detected when capacitance INCREASES, causing the measured value to DECREASE
 | 
			
		||||
    // Therefore: touched = (value < threshold)
 | 
			
		||||
    // This is opposite to ESP32-S2/S3 v2 where touched = (value > threshold)
 | 
			
		||||
    bool is_touched = value < child->get_threshold();
 | 
			
		||||
 | 
			
		||||
    // Always send the current state - the main loop will filter for changes
 | 
			
		||||
    // We send both touched and untouched states because the ISR doesn't
 | 
			
		||||
    // track previous state (to keep ISR fast and simple)
 | 
			
		||||
 
 | 
			
		||||
@@ -180,6 +180,7 @@ async def to_code(config):
 | 
			
		||||
    cg.add(esp8266_ns.setup_preferences())
 | 
			
		||||
 | 
			
		||||
    cg.add_platformio_option("lib_ldf_mode", "off")
 | 
			
		||||
    cg.add_platformio_option("lib_compat_mode", "strict")
 | 
			
		||||
 | 
			
		||||
    cg.add_platformio_option("board", config[CONF_BOARD])
 | 
			
		||||
    cg.add_build_flag("-DUSE_ESP8266")
 | 
			
		||||
 
 | 
			
		||||
@@ -45,3 +45,4 @@ async def to_code(config):
 | 
			
		||||
    cg.add_define("ESPHOME_BOARD", "host")
 | 
			
		||||
    cg.add_platformio_option("platform", "platformio/native")
 | 
			
		||||
    cg.add_platformio_option("lib_ldf_mode", "off")
 | 
			
		||||
    cg.add_platformio_option("lib_compat_mode", "strict")
 | 
			
		||||
 
 | 
			
		||||
@@ -178,13 +178,8 @@ static constexpr uint8_t NO_MAC[] = {0x08, 0x05, 0x04, 0x03, 0x02, 0x01};
 | 
			
		||||
 | 
			
		||||
static inline int two_byte_to_int(char firstbyte, char secondbyte) { return (int16_t) (secondbyte << 8) + firstbyte; }
 | 
			
		||||
 | 
			
		||||
static bool validate_header_footer(const uint8_t *header_footer, const uint8_t *buffer) {
 | 
			
		||||
  for (uint8_t i = 0; i < HEADER_FOOTER_SIZE; i++) {
 | 
			
		||||
    if (header_footer[i] != buffer[i]) {
 | 
			
		||||
      return false;  // Mismatch in header/footer
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return true;  // Valid header/footer
 | 
			
		||||
static inline bool validate_header_footer(const uint8_t *header_footer, const uint8_t *buffer) {
 | 
			
		||||
  return std::memcmp(header_footer, buffer, HEADER_FOOTER_SIZE) == 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2410Component::dump_config() {
 | 
			
		||||
@@ -300,14 +295,12 @@ void LD2410Component::send_command_(uint8_t command, const uint8_t *command_valu
 | 
			
		||||
  if (command_value != nullptr) {
 | 
			
		||||
    len += command_value_len;
 | 
			
		||||
  }
 | 
			
		||||
  uint8_t len_cmd[] = {lowbyte(len), highbyte(len), command, 0x00};
 | 
			
		||||
  // 2 length bytes (low, high) + 2 command bytes (low, high)
 | 
			
		||||
  uint8_t len_cmd[] = {len, 0x00, command, 0x00};
 | 
			
		||||
  this->write_array(len_cmd, sizeof(len_cmd));
 | 
			
		||||
 | 
			
		||||
  // command value bytes
 | 
			
		||||
  if (command_value != nullptr) {
 | 
			
		||||
    for (uint8_t i = 0; i < command_value_len; i++) {
 | 
			
		||||
      this->write_byte(command_value[i]);
 | 
			
		||||
    }
 | 
			
		||||
    this->write_array(command_value, command_value_len);
 | 
			
		||||
  }
 | 
			
		||||
  // frame footer bytes
 | 
			
		||||
  this->write_array(CMD_FRAME_FOOTER, sizeof(CMD_FRAME_FOOTER));
 | 
			
		||||
@@ -401,7 +394,7 @@ void LD2410Component::handle_periodic_data_() {
 | 
			
		||||
    /*
 | 
			
		||||
      Moving distance range: 18th byte
 | 
			
		||||
      Still distance range: 19th byte
 | 
			
		||||
      Moving enery: 20~28th bytes
 | 
			
		||||
      Moving energy: 20~28th bytes
 | 
			
		||||
    */
 | 
			
		||||
    for (std::vector<sensor::Sensor *>::size_type i = 0; i != this->gate_move_sensors_.size(); i++) {
 | 
			
		||||
      sensor::Sensor *s = this->gate_move_sensors_[i];
 | 
			
		||||
@@ -480,7 +473,7 @@ bool LD2410Component::handle_ack_data_() {
 | 
			
		||||
    ESP_LOGE(TAG, "Invalid status");
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
  if (ld2410::two_byte_to_int(this->buffer_data_[8], this->buffer_data_[9]) != 0x00) {
 | 
			
		||||
  if (this->buffer_data_[8] || this->buffer_data_[9]) {
 | 
			
		||||
    ESP_LOGW(TAG, "Invalid command: %02X, %02X", this->buffer_data_[8], this->buffer_data_[9]);
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
@@ -534,8 +527,8 @@ bool LD2410Component::handle_ack_data_() {
 | 
			
		||||
      const auto *light_function_str = find_str(LIGHT_FUNCTIONS_BY_UINT, this->light_function_);
 | 
			
		||||
      const auto *out_pin_level_str = find_str(OUT_PIN_LEVELS_BY_UINT, this->out_pin_level_);
 | 
			
		||||
      ESP_LOGV(TAG,
 | 
			
		||||
               "Light function is: %s\n"
 | 
			
		||||
               "Light threshold is: %u\n"
 | 
			
		||||
               "Light function: %s\n"
 | 
			
		||||
               "Light threshold: %u\n"
 | 
			
		||||
               "Out pin level: %s",
 | 
			
		||||
               light_function_str, this->light_threshold_, out_pin_level_str);
 | 
			
		||||
#ifdef USE_SELECT
 | 
			
		||||
@@ -600,7 +593,7 @@ bool LD2410Component::handle_ack_data_() {
 | 
			
		||||
      break;
 | 
			
		||||
 | 
			
		||||
    case CMD_QUERY: {  // Query parameters response
 | 
			
		||||
      if (this->buffer_data_[10] != 0xAA)
 | 
			
		||||
      if (this->buffer_data_[10] != HEADER)
 | 
			
		||||
        return true;  // value head=0xAA
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
      /*
 | 
			
		||||
@@ -656,17 +649,11 @@ void LD2410Component::readline_(int readch) {
 | 
			
		||||
  if (this->buffer_pos_ < 4) {
 | 
			
		||||
    return;  // Not enough data to process yet
 | 
			
		||||
  }
 | 
			
		||||
  if (this->buffer_data_[this->buffer_pos_ - 4] == DATA_FRAME_FOOTER[0] &&
 | 
			
		||||
      this->buffer_data_[this->buffer_pos_ - 3] == DATA_FRAME_FOOTER[1] &&
 | 
			
		||||
      this->buffer_data_[this->buffer_pos_ - 2] == DATA_FRAME_FOOTER[2] &&
 | 
			
		||||
      this->buffer_data_[this->buffer_pos_ - 1] == DATA_FRAME_FOOTER[3]) {
 | 
			
		||||
  if (ld2410::validate_header_footer(DATA_FRAME_FOOTER, &this->buffer_data_[this->buffer_pos_ - 4])) {
 | 
			
		||||
    ESP_LOGV(TAG, "Handling Periodic Data: %s", format_hex_pretty(this->buffer_data_, this->buffer_pos_).c_str());
 | 
			
		||||
    this->handle_periodic_data_();
 | 
			
		||||
    this->buffer_pos_ = 0;  // Reset position index for next message
 | 
			
		||||
  } else if (this->buffer_data_[this->buffer_pos_ - 4] == CMD_FRAME_FOOTER[0] &&
 | 
			
		||||
             this->buffer_data_[this->buffer_pos_ - 3] == CMD_FRAME_FOOTER[1] &&
 | 
			
		||||
             this->buffer_data_[this->buffer_pos_ - 2] == CMD_FRAME_FOOTER[2] &&
 | 
			
		||||
             this->buffer_data_[this->buffer_pos_ - 1] == CMD_FRAME_FOOTER[3]) {
 | 
			
		||||
  } else if (ld2410::validate_header_footer(CMD_FRAME_FOOTER, &this->buffer_data_[this->buffer_pos_ - 4])) {
 | 
			
		||||
    ESP_LOGV(TAG, "Handling Ack Data: %s", format_hex_pretty(this->buffer_data_, this->buffer_pos_).c_str());
 | 
			
		||||
    if (this->handle_ack_data_()) {
 | 
			
		||||
      this->buffer_pos_ = 0;  // Reset position index for next message
 | 
			
		||||
@@ -772,7 +759,6 @@ void LD2410Component::set_max_distances_timeout() {
 | 
			
		||||
                       0x00};
 | 
			
		||||
  this->set_config_mode_(true);
 | 
			
		||||
  this->send_command_(CMD_MAXDIST_DURATION, value, sizeof(value));
 | 
			
		||||
  delay(50);  // NOLINT
 | 
			
		||||
  this->query_parameters_();
 | 
			
		||||
  this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
 | 
			
		||||
  this->set_config_mode_(false);
 | 
			
		||||
@@ -802,7 +788,6 @@ void LD2410Component::set_gate_threshold(uint8_t gate) {
 | 
			
		||||
                       0x01, 0x00, lowbyte(motion), highbyte(motion), 0x00, 0x00,
 | 
			
		||||
                       0x02, 0x00, lowbyte(still),  highbyte(still),  0x00, 0x00};
 | 
			
		||||
  this->send_command_(CMD_GATE_SENS, value, sizeof(value));
 | 
			
		||||
  delay(50);  // NOLINT
 | 
			
		||||
  this->query_parameters_();
 | 
			
		||||
  this->set_config_mode_(false);
 | 
			
		||||
}
 | 
			
		||||
@@ -833,7 +818,6 @@ void LD2410Component::set_light_out_control() {
 | 
			
		||||
  this->set_config_mode_(true);
 | 
			
		||||
  uint8_t value[4] = {this->light_function_, this->light_threshold_, this->out_pin_level_, 0x00};
 | 
			
		||||
  this->send_command_(CMD_SET_LIGHT_CONTROL, value, sizeof(value));
 | 
			
		||||
  delay(50);  // NOLINT
 | 
			
		||||
  this->query_light_control_();
 | 
			
		||||
  this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
 | 
			
		||||
  this->set_config_mode_(false);
 | 
			
		||||
 
 | 
			
		||||
@@ -268,6 +268,7 @@ async def component_to_code(config):
 | 
			
		||||
 | 
			
		||||
    # disable library compatibility checks
 | 
			
		||||
    cg.add_platformio_option("lib_ldf_mode", "off")
 | 
			
		||||
    cg.add_platformio_option("lib_compat_mode", "soft")
 | 
			
		||||
    # include <Arduino.h> in every file
 | 
			
		||||
    cg.add_platformio_option("build_src_flags", "-include Arduino.h")
 | 
			
		||||
    # dummy version code
 | 
			
		||||
 
 | 
			
		||||
@@ -153,11 +153,15 @@ void MQTTBackendESP32::mqtt_event_handler_(const Event &event) {
 | 
			
		||||
    case MQTT_EVENT_DATA: {
 | 
			
		||||
      static std::string topic;
 | 
			
		||||
      if (!event.topic.empty()) {
 | 
			
		||||
        // When a single message arrives as multiple chunks, the topic will be empty
 | 
			
		||||
        // on any but the first message, leading to event.topic being an empty string.
 | 
			
		||||
        // To ensure handlers get the correct topic, cache the last seen topic to
 | 
			
		||||
        // simulate always receiving the topic from underlying library
 | 
			
		||||
        topic = event.topic;
 | 
			
		||||
      }
 | 
			
		||||
      ESP_LOGV(TAG, "MQTT_EVENT_DATA %s", topic.c_str());
 | 
			
		||||
      this->on_message_.call(!event.topic.empty() ? topic.c_str() : nullptr, event.data.data(), event.data.size(),
 | 
			
		||||
                             event.current_data_offset, event.total_data_len);
 | 
			
		||||
      this->on_message_.call(topic.c_str(), event.data.data(), event.data.size(), event.current_data_offset,
 | 
			
		||||
                             event.total_data_len);
 | 
			
		||||
    } break;
 | 
			
		||||
    case MQTT_EVENT_ERROR:
 | 
			
		||||
      ESP_LOGE(TAG, "MQTT_EVENT_ERROR");
 | 
			
		||||
 
 | 
			
		||||
@@ -314,6 +314,9 @@ void PacketTransport::send_data_(bool all) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void PacketTransport::update() {
 | 
			
		||||
  if (!this->ping_pong_enable_) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  auto now = millis() / 1000;
 | 
			
		||||
  if (this->last_key_time_ + this->ping_pong_recyle_time_ < now) {
 | 
			
		||||
    this->resend_ping_key_ = this->ping_pong_enable_;
 | 
			
		||||
 
 | 
			
		||||
@@ -165,6 +165,7 @@ async def to_code(config):
 | 
			
		||||
    # Allow LDF to properly discover dependency including those in preprocessor
 | 
			
		||||
    # conditionals
 | 
			
		||||
    cg.add_platformio_option("lib_ldf_mode", "chain+")
 | 
			
		||||
    cg.add_platformio_option("lib_compat_mode", "strict")
 | 
			
		||||
    cg.add_platformio_option("board", config[CONF_BOARD])
 | 
			
		||||
    cg.add_build_flag("-DUSE_RP2040")
 | 
			
		||||
    cg.set_cpp_standard("gnu++20")
 | 
			
		||||
 
 | 
			
		||||
@@ -167,8 +167,8 @@ def validate_config(config):
 | 
			
		||||
    if config[CONF_MODULATION] == "LORA":
 | 
			
		||||
        if config[CONF_BANDWIDTH] not in lora_bws:
 | 
			
		||||
            raise cv.Invalid(f"{config[CONF_BANDWIDTH]} is not available with LORA")
 | 
			
		||||
        if config[CONF_PREAMBLE_SIZE] > 0 and config[CONF_PREAMBLE_SIZE] < 6:
 | 
			
		||||
            raise cv.Invalid("Minimum preamble size is 6 with LORA")
 | 
			
		||||
        if config[CONF_PREAMBLE_SIZE] < 6:
 | 
			
		||||
            raise cv.Invalid("Minimum 'preamble_size' is 6 with LORA")
 | 
			
		||||
        if config[CONF_SPREADING_FACTOR] == 6 and config[CONF_PAYLOAD_LENGTH] == 0:
 | 
			
		||||
            raise cv.Invalid("Payload length must be set when spreading factor is 6")
 | 
			
		||||
    else:
 | 
			
		||||
@@ -200,7 +200,7 @@ CONFIG_SCHEMA = (
 | 
			
		||||
            cv.Optional(CONF_PA_RAMP, default="40us"): cv.enum(RAMP),
 | 
			
		||||
            cv.Optional(CONF_PAYLOAD_LENGTH, default=0): cv.int_range(min=0, max=256),
 | 
			
		||||
            cv.Optional(CONF_PREAMBLE_DETECT, default=2): cv.int_range(min=0, max=4),
 | 
			
		||||
            cv.Required(CONF_PREAMBLE_SIZE): cv.int_range(min=1, max=65535),
 | 
			
		||||
            cv.Optional(CONF_PREAMBLE_SIZE, default=8): cv.int_range(min=1, max=65535),
 | 
			
		||||
            cv.Required(CONF_RST_PIN): pins.internal_gpio_output_pin_schema,
 | 
			
		||||
            cv.Optional(CONF_RX_START, default=True): cv.boolean,
 | 
			
		||||
            cv.Required(CONF_RF_SWITCH): cv.boolean,
 | 
			
		||||
 
 | 
			
		||||
@@ -164,8 +164,8 @@ def validate_config(config):
 | 
			
		||||
            raise cv.Invalid(f"{config[CONF_BANDWIDTH]} is not available with LORA")
 | 
			
		||||
        if CONF_DIO0_PIN not in config:
 | 
			
		||||
            raise cv.Invalid("Cannot use LoRa without dio0_pin")
 | 
			
		||||
        if 0 < config[CONF_PREAMBLE_SIZE] < 6:
 | 
			
		||||
            raise cv.Invalid("Minimum preamble size is 6 with LORA")
 | 
			
		||||
        if config[CONF_PREAMBLE_SIZE] < 6:
 | 
			
		||||
            raise cv.Invalid("Minimum 'preamble_size' is 6 with LORA")
 | 
			
		||||
        if config[CONF_SPREADING_FACTOR] == 6 and config[CONF_PAYLOAD_LENGTH] == 0:
 | 
			
		||||
            raise cv.Invalid("Payload length must be set when spreading factor is 6")
 | 
			
		||||
    else:
 | 
			
		||||
 
 | 
			
		||||
@@ -70,7 +70,7 @@ static void usbh_print_cfg_desc(const usb_config_desc_t *cfg_desc) {
 | 
			
		||||
  ESP_LOGV(TAG, "bMaxPower %dmA", cfg_desc->bMaxPower * 2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void usb_client_print_device_descriptor(const usb_device_desc_t *devc_desc) {
 | 
			
		||||
static void usb_client_print_device_descriptor(const usb_device_desc_t *devc_desc) {
 | 
			
		||||
  if (devc_desc == NULL) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
@@ -92,8 +92,8 @@ void usb_client_print_device_descriptor(const usb_device_desc_t *devc_desc) {
 | 
			
		||||
  ESP_LOGV(TAG, "bNumConfigurations %d", devc_desc->bNumConfigurations);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void usb_client_print_config_descriptor(const usb_config_desc_t *cfg_desc,
 | 
			
		||||
                                        print_class_descriptor_cb class_specific_cb) {
 | 
			
		||||
static void usb_client_print_config_descriptor(const usb_config_desc_t *cfg_desc,
 | 
			
		||||
                                               print_class_descriptor_cb class_specific_cb) {
 | 
			
		||||
  if (cfg_desc == nullptr) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
@@ -128,9 +128,9 @@ void usb_client_print_config_descriptor(const usb_config_desc_t *cfg_desc,
 | 
			
		||||
static std::string get_descriptor_string(const usb_str_desc_t *desc) {
 | 
			
		||||
  char buffer[256];
 | 
			
		||||
  if (desc == nullptr)
 | 
			
		||||
    return "(unknown)";
 | 
			
		||||
    return "(unspecified)";
 | 
			
		||||
  char *p = buffer;
 | 
			
		||||
  for (size_t i = 0; i != desc->bLength / 2; i++) {
 | 
			
		||||
  for (int i = 0; i != desc->bLength / 2; i++) {
 | 
			
		||||
    auto c = desc->wData[i];
 | 
			
		||||
    if (c < 0x100)
 | 
			
		||||
      *p++ = static_cast<char>(c);
 | 
			
		||||
@@ -169,7 +169,7 @@ void USBClient::setup() {
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  for (auto trq : this->trq_pool_) {
 | 
			
		||||
  for (auto *trq : this->trq_pool_) {
 | 
			
		||||
    usb_host_transfer_alloc(64, 0, &trq->transfer);
 | 
			
		||||
    trq->client = this;
 | 
			
		||||
  }
 | 
			
		||||
@@ -197,7 +197,8 @@ void USBClient::loop() {
 | 
			
		||||
        ESP_LOGD(TAG, "Device descriptor: vid %X pid %X", desc->idVendor, desc->idProduct);
 | 
			
		||||
        if (desc->idVendor == this->vid_ && desc->idProduct == this->pid_ || this->vid_ == 0 && this->pid_ == 0) {
 | 
			
		||||
          usb_device_info_t dev_info;
 | 
			
		||||
          if ((err = usb_host_device_info(this->device_handle_, &dev_info)) != ESP_OK) {
 | 
			
		||||
          err = usb_host_device_info(this->device_handle_, &dev_info);
 | 
			
		||||
          if (err != ESP_OK) {
 | 
			
		||||
            ESP_LOGW(TAG, "Device info failed: %s", esp_err_to_name(err));
 | 
			
		||||
            this->disconnect();
 | 
			
		||||
            break;
 | 
			
		||||
@@ -336,7 +337,7 @@ static void transfer_callback(usb_transfer_t *xfer) {
 | 
			
		||||
 * @throws None.
 | 
			
		||||
 */
 | 
			
		||||
void USBClient::transfer_in(uint8_t ep_address, const transfer_cb_t &callback, uint16_t length) {
 | 
			
		||||
  auto trq = this->get_trq_();
 | 
			
		||||
  auto *trq = this->get_trq_();
 | 
			
		||||
  if (trq == nullptr) {
 | 
			
		||||
    ESP_LOGE(TAG, "Too many requests queued");
 | 
			
		||||
    return;
 | 
			
		||||
@@ -349,7 +350,6 @@ void USBClient::transfer_in(uint8_t ep_address, const transfer_cb_t &callback, u
 | 
			
		||||
  if (err != ESP_OK) {
 | 
			
		||||
    ESP_LOGE(TAG, "Failed to submit transfer, address=%x, length=%d, err=%x", ep_address, length, err);
 | 
			
		||||
    this->release_trq(trq);
 | 
			
		||||
    this->disconnect();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -364,7 +364,7 @@ void USBClient::transfer_in(uint8_t ep_address, const transfer_cb_t &callback, u
 | 
			
		||||
 * @throws None.
 | 
			
		||||
 */
 | 
			
		||||
void USBClient::transfer_out(uint8_t ep_address, const transfer_cb_t &callback, const uint8_t *data, uint16_t length) {
 | 
			
		||||
  auto trq = this->get_trq_();
 | 
			
		||||
  auto *trq = this->get_trq_();
 | 
			
		||||
  if (trq == nullptr) {
 | 
			
		||||
    ESP_LOGE(TAG, "Too many requests queued");
 | 
			
		||||
    return;
 | 
			
		||||
 
 | 
			
		||||
@@ -43,7 +43,7 @@ static constexpr uint8_t SET_BAUDRATE = 0x1E;     // Set the baud rate.
 | 
			
		||||
static constexpr uint8_t SET_CHARS = 0x19;        // Set special characters.
 | 
			
		||||
static constexpr uint8_t VENDOR_SPECIFIC = 0xFF;  // Vendor specific command.
 | 
			
		||||
 | 
			
		||||
std::vector<CdcEps> USBUartTypeCP210X::parse_descriptors_(usb_device_handle_t dev_hdl) {
 | 
			
		||||
std::vector<CdcEps> USBUartTypeCP210X::parse_descriptors(usb_device_handle_t dev_hdl) {
 | 
			
		||||
  const usb_config_desc_t *config_desc;
 | 
			
		||||
  const usb_device_desc_t *device_desc;
 | 
			
		||||
  int conf_offset = 0, ep_offset;
 | 
			
		||||
 
 | 
			
		||||
@@ -18,52 +18,48 @@ namespace usb_uart {
 | 
			
		||||
 */
 | 
			
		||||
static optional<CdcEps> get_cdc(const usb_config_desc_t *config_desc, uint8_t intf_idx) {
 | 
			
		||||
  int conf_offset, ep_offset;
 | 
			
		||||
  const usb_ep_desc_t *notify_ep{}, *in_ep{}, *out_ep{};
 | 
			
		||||
  uint8_t interface_number = 0;
 | 
			
		||||
  // look for an interface with one interrupt endpoint (notify), and an interface with two bulk endpoints (data in/out)
 | 
			
		||||
  // look for an interface with an interrupt endpoint (notify), and one with two bulk endpoints (data in/out)
 | 
			
		||||
  CdcEps eps{};
 | 
			
		||||
  eps.bulk_interface_number = 0xFF;
 | 
			
		||||
  for (;;) {
 | 
			
		||||
    auto intf_desc = usb_parse_interface_descriptor(config_desc, intf_idx++, 0, &conf_offset);
 | 
			
		||||
    const auto *intf_desc = usb_parse_interface_descriptor(config_desc, intf_idx++, 0, &conf_offset);
 | 
			
		||||
    if (!intf_desc) {
 | 
			
		||||
      ESP_LOGE(TAG, "usb_parse_interface_descriptor failed");
 | 
			
		||||
      return nullopt;
 | 
			
		||||
    }
 | 
			
		||||
    if (intf_desc->bNumEndpoints == 1) {
 | 
			
		||||
    ESP_LOGD(TAG, "intf_desc: bInterfaceClass=%02X, bInterfaceSubClass=%02X, bInterfaceProtocol=%02X, bNumEndpoints=%d",
 | 
			
		||||
             intf_desc->bInterfaceClass, intf_desc->bInterfaceSubClass, intf_desc->bInterfaceProtocol,
 | 
			
		||||
             intf_desc->bNumEndpoints);
 | 
			
		||||
    for (uint8_t i = 0; i != intf_desc->bNumEndpoints; i++) {
 | 
			
		||||
      ep_offset = conf_offset;
 | 
			
		||||
      notify_ep = usb_parse_endpoint_descriptor_by_index(intf_desc, 0, config_desc->wTotalLength, &ep_offset);
 | 
			
		||||
      if (!notify_ep) {
 | 
			
		||||
        ESP_LOGE(TAG, "notify_ep: usb_parse_endpoint_descriptor_by_index failed");
 | 
			
		||||
      const auto *ep = usb_parse_endpoint_descriptor_by_index(intf_desc, i, config_desc->wTotalLength, &ep_offset);
 | 
			
		||||
      if (!ep) {
 | 
			
		||||
        ESP_LOGE(TAG, "Ran out of interfaces at %d before finding all endpoints", i);
 | 
			
		||||
        return nullopt;
 | 
			
		||||
      }
 | 
			
		||||
      if (notify_ep->bmAttributes != USB_BM_ATTRIBUTES_XFER_INT)
 | 
			
		||||
        notify_ep = nullptr;
 | 
			
		||||
    } else if (USB_CLASS_CDC_DATA && intf_desc->bNumEndpoints == 2) {
 | 
			
		||||
      interface_number = intf_desc->bInterfaceNumber;
 | 
			
		||||
      ep_offset = conf_offset;
 | 
			
		||||
      out_ep = usb_parse_endpoint_descriptor_by_index(intf_desc, 0, config_desc->wTotalLength, &ep_offset);
 | 
			
		||||
      if (!out_ep) {
 | 
			
		||||
        ESP_LOGE(TAG, "out_ep: usb_parse_endpoint_descriptor_by_index failed");
 | 
			
		||||
        return nullopt;
 | 
			
		||||
      ESP_LOGD(TAG, "ep: bEndpointAddress=%02X, bmAttributes=%02X", ep->bEndpointAddress, ep->bmAttributes);
 | 
			
		||||
      if (ep->bmAttributes == USB_BM_ATTRIBUTES_XFER_INT) {
 | 
			
		||||
        eps.notify_ep = ep;
 | 
			
		||||
        eps.interrupt_interface_number = intf_desc->bInterfaceNumber;
 | 
			
		||||
      } else if (ep->bmAttributes == USB_BM_ATTRIBUTES_XFER_BULK && ep->bEndpointAddress & usb_host::USB_DIR_IN &&
 | 
			
		||||
                 (eps.bulk_interface_number == 0xFF || eps.bulk_interface_number == intf_desc->bInterfaceNumber)) {
 | 
			
		||||
        eps.in_ep = ep;
 | 
			
		||||
        eps.bulk_interface_number = intf_desc->bInterfaceNumber;
 | 
			
		||||
      } else if (ep->bmAttributes == USB_BM_ATTRIBUTES_XFER_BULK && !(ep->bEndpointAddress & usb_host::USB_DIR_IN) &&
 | 
			
		||||
                 (eps.bulk_interface_number == 0xFF || eps.bulk_interface_number == intf_desc->bInterfaceNumber)) {
 | 
			
		||||
        eps.out_ep = ep;
 | 
			
		||||
        eps.bulk_interface_number = intf_desc->bInterfaceNumber;
 | 
			
		||||
      } else {
 | 
			
		||||
        ESP_LOGE(TAG, "Unexpected endpoint attributes: %02X", ep->bmAttributes);
 | 
			
		||||
        continue;
 | 
			
		||||
      }
 | 
			
		||||
      if (out_ep->bmAttributes != USB_BM_ATTRIBUTES_XFER_BULK)
 | 
			
		||||
        out_ep = nullptr;
 | 
			
		||||
      ep_offset = conf_offset;
 | 
			
		||||
      in_ep = usb_parse_endpoint_descriptor_by_index(intf_desc, 1, config_desc->wTotalLength, &ep_offset);
 | 
			
		||||
      if (!in_ep) {
 | 
			
		||||
        ESP_LOGE(TAG, "in_ep: usb_parse_endpoint_descriptor_by_index failed");
 | 
			
		||||
        return nullopt;
 | 
			
		||||
      }
 | 
			
		||||
      if (in_ep->bmAttributes != USB_BM_ATTRIBUTES_XFER_BULK)
 | 
			
		||||
        in_ep = nullptr;
 | 
			
		||||
    }
 | 
			
		||||
    if (in_ep != nullptr && out_ep != nullptr && notify_ep != nullptr)
 | 
			
		||||
      break;
 | 
			
		||||
    if (eps.in_ep != nullptr && eps.out_ep != nullptr && eps.notify_ep != nullptr)
 | 
			
		||||
      return eps;
 | 
			
		||||
  }
 | 
			
		||||
  if (in_ep->bEndpointAddress & usb_host::USB_DIR_IN)
 | 
			
		||||
    return CdcEps{notify_ep, in_ep, out_ep, interface_number};
 | 
			
		||||
  return CdcEps{notify_ep, out_ep, in_ep, interface_number};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::vector<CdcEps> USBUartTypeCdcAcm::parse_descriptors_(usb_device_handle_t dev_hdl) {
 | 
			
		||||
std::vector<CdcEps> USBUartTypeCdcAcm::parse_descriptors(usb_device_handle_t dev_hdl) {
 | 
			
		||||
  const usb_config_desc_t *config_desc;
 | 
			
		||||
  const usb_device_desc_t *device_desc;
 | 
			
		||||
  int desc_offset = 0;
 | 
			
		||||
@@ -78,7 +74,7 @@ std::vector<CdcEps> USBUartTypeCdcAcm::parse_descriptors_(usb_device_handle_t de
 | 
			
		||||
    ESP_LOGE(TAG, "get_active_config_descriptor failed");
 | 
			
		||||
    return {};
 | 
			
		||||
  }
 | 
			
		||||
  if (device_desc->bDeviceClass == USB_CLASS_COMM) {
 | 
			
		||||
  if (device_desc->bDeviceClass == USB_CLASS_COMM || device_desc->bDeviceClass == USB_CLASS_VENDOR_SPEC) {
 | 
			
		||||
    // single CDC-ACM device
 | 
			
		||||
    if (auto eps = get_cdc(config_desc, 0)) {
 | 
			
		||||
      ESP_LOGV(TAG, "Found CDC-ACM device");
 | 
			
		||||
@@ -194,7 +190,7 @@ void USBUartComponent::start_input(USBUartChannel *channel) {
 | 
			
		||||
  if (!channel->initialised_ || channel->input_started_ ||
 | 
			
		||||
      channel->input_buffer_.get_free_space() < channel->cdc_dev_.in_ep->wMaxPacketSize)
 | 
			
		||||
    return;
 | 
			
		||||
  auto ep = channel->cdc_dev_.in_ep;
 | 
			
		||||
  const auto *ep = channel->cdc_dev_.in_ep;
 | 
			
		||||
  auto callback = [this, channel](const usb_host::TransferStatus &status) {
 | 
			
		||||
    ESP_LOGV(TAG, "Transfer result: length: %u; status %X", status.data_len, status.error_code);
 | 
			
		||||
    if (!status.success) {
 | 
			
		||||
@@ -227,7 +223,7 @@ void USBUartComponent::start_output(USBUartChannel *channel) {
 | 
			
		||||
  if (channel->output_buffer_.is_empty()) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  auto ep = channel->cdc_dev_.out_ep;
 | 
			
		||||
  const auto *ep = channel->cdc_dev_.out_ep;
 | 
			
		||||
  auto callback = [this, channel](const usb_host::TransferStatus &status) {
 | 
			
		||||
    ESP_LOGV(TAG, "Output Transfer result: length: %u; status %X", status.data_len, status.error_code);
 | 
			
		||||
    channel->output_started_ = false;
 | 
			
		||||
@@ -259,15 +255,15 @@ static void fix_mps(const usb_ep_desc_t *ep) {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
void USBUartTypeCdcAcm::on_connected() {
 | 
			
		||||
  auto cdc_devs = this->parse_descriptors_(this->device_handle_);
 | 
			
		||||
  auto cdc_devs = this->parse_descriptors(this->device_handle_);
 | 
			
		||||
  if (cdc_devs.empty()) {
 | 
			
		||||
    this->status_set_error("No CDC-ACM device found");
 | 
			
		||||
    this->disconnect();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGD(TAG, "Found %zu CDC-ACM devices", cdc_devs.size());
 | 
			
		||||
  auto i = 0;
 | 
			
		||||
  for (auto channel : this->channels_) {
 | 
			
		||||
  size_t i = 0;
 | 
			
		||||
  for (auto *channel : this->channels_) {
 | 
			
		||||
    if (i == cdc_devs.size()) {
 | 
			
		||||
      ESP_LOGE(TAG, "No configuration found for channel %d", channel->index_);
 | 
			
		||||
      this->status_set_warning("No configuration found for channel");
 | 
			
		||||
@@ -277,10 +273,11 @@ void USBUartTypeCdcAcm::on_connected() {
 | 
			
		||||
    fix_mps(channel->cdc_dev_.in_ep);
 | 
			
		||||
    fix_mps(channel->cdc_dev_.out_ep);
 | 
			
		||||
    channel->initialised_ = true;
 | 
			
		||||
    auto err = usb_host_interface_claim(this->handle_, this->device_handle_, channel->cdc_dev_.interface_number, 0);
 | 
			
		||||
    auto err =
 | 
			
		||||
        usb_host_interface_claim(this->handle_, this->device_handle_, channel->cdc_dev_.bulk_interface_number, 0);
 | 
			
		||||
    if (err != ESP_OK) {
 | 
			
		||||
      ESP_LOGE(TAG, "usb_host_interface_claim failed: %s, channel=%d, intf=%d", esp_err_to_name(err), channel->index_,
 | 
			
		||||
               channel->cdc_dev_.interface_number);
 | 
			
		||||
               channel->cdc_dev_.bulk_interface_number);
 | 
			
		||||
      this->status_set_error("usb_host_interface_claim failed");
 | 
			
		||||
      this->disconnect();
 | 
			
		||||
      return;
 | 
			
		||||
@@ -290,7 +287,7 @@ void USBUartTypeCdcAcm::on_connected() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void USBUartTypeCdcAcm::on_disconnected() {
 | 
			
		||||
  for (auto channel : this->channels_) {
 | 
			
		||||
  for (auto *channel : this->channels_) {
 | 
			
		||||
    if (channel->cdc_dev_.in_ep != nullptr) {
 | 
			
		||||
      usb_host_endpoint_halt(this->device_handle_, channel->cdc_dev_.in_ep->bEndpointAddress);
 | 
			
		||||
      usb_host_endpoint_flush(this->device_handle_, channel->cdc_dev_.in_ep->bEndpointAddress);
 | 
			
		||||
@@ -303,7 +300,7 @@ void USBUartTypeCdcAcm::on_disconnected() {
 | 
			
		||||
      usb_host_endpoint_halt(this->device_handle_, channel->cdc_dev_.notify_ep->bEndpointAddress);
 | 
			
		||||
      usb_host_endpoint_flush(this->device_handle_, channel->cdc_dev_.notify_ep->bEndpointAddress);
 | 
			
		||||
    }
 | 
			
		||||
    usb_host_interface_release(this->handle_, this->device_handle_, channel->cdc_dev_.interface_number);
 | 
			
		||||
    usb_host_interface_release(this->handle_, this->device_handle_, channel->cdc_dev_.bulk_interface_number);
 | 
			
		||||
    channel->initialised_ = false;
 | 
			
		||||
    channel->input_started_ = false;
 | 
			
		||||
    channel->output_started_ = false;
 | 
			
		||||
@@ -314,7 +311,7 @@ void USBUartTypeCdcAcm::on_disconnected() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void USBUartTypeCdcAcm::enable_channels() {
 | 
			
		||||
  for (auto channel : this->channels_) {
 | 
			
		||||
  for (auto *channel : this->channels_) {
 | 
			
		||||
    if (!channel->initialised_)
 | 
			
		||||
      continue;
 | 
			
		||||
    channel->input_started_ = false;
 | 
			
		||||
 
 | 
			
		||||
@@ -25,7 +25,8 @@ struct CdcEps {
 | 
			
		||||
  const usb_ep_desc_t *notify_ep;
 | 
			
		||||
  const usb_ep_desc_t *in_ep;
 | 
			
		||||
  const usb_ep_desc_t *out_ep;
 | 
			
		||||
  uint8_t interface_number;
 | 
			
		||||
  uint8_t bulk_interface_number;
 | 
			
		||||
  uint8_t interrupt_interface_number;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum UARTParityOptions {
 | 
			
		||||
@@ -123,7 +124,7 @@ class USBUartTypeCdcAcm : public USBUartComponent {
 | 
			
		||||
  USBUartTypeCdcAcm(uint16_t vid, uint16_t pid) : USBUartComponent(vid, pid) {}
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  virtual std::vector<CdcEps> parse_descriptors_(usb_device_handle_t dev_hdl);
 | 
			
		||||
  virtual std::vector<CdcEps> parse_descriptors(usb_device_handle_t dev_hdl);
 | 
			
		||||
  void on_connected() override;
 | 
			
		||||
  virtual void enable_channels();
 | 
			
		||||
  void on_disconnected() override;
 | 
			
		||||
@@ -134,7 +135,7 @@ class USBUartTypeCP210X : public USBUartTypeCdcAcm {
 | 
			
		||||
  USBUartTypeCP210X(uint16_t vid, uint16_t pid) : USBUartTypeCdcAcm(vid, pid) {}
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  std::vector<CdcEps> parse_descriptors_(usb_device_handle_t dev_hdl) override;
 | 
			
		||||
  std::vector<CdcEps> parse_descriptors(usb_device_handle_t dev_hdl) override;
 | 
			
		||||
  void enable_channels() override;
 | 
			
		||||
};
 | 
			
		||||
class USBUartTypeCH34X : public USBUartTypeCdcAcm {
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,7 @@ from enum import Enum
 | 
			
		||||
 | 
			
		||||
from esphome.enum import StrEnum
 | 
			
		||||
 | 
			
		||||
__version__ = "2025.7.0b1"
 | 
			
		||||
__version__ = "2025.7.0b2"
 | 
			
		||||
 | 
			
		||||
ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_"
 | 
			
		||||
VALID_SUBSTITUTIONS_CHARACTERS = (
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,8 @@
 | 
			
		||||
 | 
			
		||||
#ifdef USE_API
 | 
			
		||||
#include "esphome/components/api/api_server.h"
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
#include "esphome/components/api/user_services.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
@@ -148,7 +150,7 @@ void ComponentIterator::advance() {
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_API
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
    case IteratorState ::SERVICE:
 | 
			
		||||
      if (this->at_ >= api::global_api_server->get_user_services().size()) {
 | 
			
		||||
        advance_platform = true;
 | 
			
		||||
@@ -383,7 +385,7 @@ void ComponentIterator::advance() {
 | 
			
		||||
}
 | 
			
		||||
bool ComponentIterator::on_end() { return true; }
 | 
			
		||||
bool ComponentIterator::on_begin() { return true; }
 | 
			
		||||
#ifdef USE_API
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
bool ComponentIterator::on_service(api::UserServiceDescriptor *service) { return true; }
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_CAMERA
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,7 @@
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
 | 
			
		||||
#ifdef USE_API
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
namespace api {
 | 
			
		||||
class UserServiceDescriptor;
 | 
			
		||||
}  // namespace api
 | 
			
		||||
@@ -45,7 +45,7 @@ class ComponentIterator {
 | 
			
		||||
#ifdef USE_TEXT_SENSOR
 | 
			
		||||
  virtual bool on_text_sensor(text_sensor::TextSensor *text_sensor) = 0;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_API
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
  virtual bool on_service(api::UserServiceDescriptor *service);
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_CAMERA
 | 
			
		||||
@@ -122,7 +122,7 @@ class ComponentIterator {
 | 
			
		||||
#ifdef USE_TEXT_SENSOR
 | 
			
		||||
    TEXT_SENSOR,
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_API
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
    SERVICE,
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_CAMERA
 | 
			
		||||
 
 | 
			
		||||
@@ -108,7 +108,7 @@
 | 
			
		||||
#define USE_API_CLIENT_DISCONNECTED_TRIGGER
 | 
			
		||||
#define USE_API_NOISE
 | 
			
		||||
#define USE_API_PLAINTEXT
 | 
			
		||||
#define USE_API_YAML_SERVICES
 | 
			
		||||
#define USE_API_SERVICES
 | 
			
		||||
#define USE_MD5
 | 
			
		||||
#define USE_MQTT
 | 
			
		||||
#define USE_NETWORK
 | 
			
		||||
 
 | 
			
		||||
@@ -187,6 +187,12 @@ def entity_duplicate_validator(platform: str) -> Callable[[ConfigType], ConfigTy
 | 
			
		||||
            # No name to validate
 | 
			
		||||
            return config
 | 
			
		||||
 | 
			
		||||
        # Skip validation for internal entities
 | 
			
		||||
        # Internal entities are not exposed to Home Assistant and don't use the hash-based
 | 
			
		||||
        # entity state tracking system, so name collisions don't matter for them
 | 
			
		||||
        if config.get(CONF_INTERNAL, False):
 | 
			
		||||
            return config
 | 
			
		||||
 | 
			
		||||
        # Get the entity name
 | 
			
		||||
        entity_name = config[CONF_NAME]
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -66,10 +66,8 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type
 | 
			
		||||
 | 
			
		||||
  if (delay == SCHEDULER_DONT_RUN) {
 | 
			
		||||
    // Still need to cancel existing timer if name is not empty
 | 
			
		||||
    if (this->is_name_valid_(name_cstr)) {
 | 
			
		||||
      LockGuard guard{this->lock_};
 | 
			
		||||
      this->cancel_item_locked_(component, name_cstr, type);
 | 
			
		||||
    }
 | 
			
		||||
    LockGuard guard{this->lock_};
 | 
			
		||||
    this->cancel_item_locked_(component, name_cstr, type);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -125,10 +123,8 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type
 | 
			
		||||
 | 
			
		||||
  LockGuard guard{this->lock_};
 | 
			
		||||
  // If name is provided, do atomic cancel-and-add
 | 
			
		||||
  if (this->is_name_valid_(name_cstr)) {
 | 
			
		||||
    // Cancel existing items
 | 
			
		||||
    this->cancel_item_locked_(component, name_cstr, type);
 | 
			
		||||
  }
 | 
			
		||||
  // Cancel existing items
 | 
			
		||||
  this->cancel_item_locked_(component, name_cstr, type);
 | 
			
		||||
  // Add new item directly to to_add_
 | 
			
		||||
  // since we have the lock held
 | 
			
		||||
  this->to_add_.push_back(std::move(item));
 | 
			
		||||
@@ -442,10 +438,6 @@ bool HOT Scheduler::cancel_item_(Component *component, bool is_static_string, co
 | 
			
		||||
  // Get the name as const char*
 | 
			
		||||
  const char *name_cstr = this->get_name_cstr_(is_static_string, name_ptr);
 | 
			
		||||
 | 
			
		||||
  // Handle null or empty names
 | 
			
		||||
  if (!this->is_name_valid_(name_cstr))
 | 
			
		||||
    return false;
 | 
			
		||||
 | 
			
		||||
  // obtain lock because this function iterates and can be called from non-loop task context
 | 
			
		||||
  LockGuard guard{this->lock_};
 | 
			
		||||
  return this->cancel_item_locked_(component, name_cstr, type);
 | 
			
		||||
@@ -453,6 +445,11 @@ bool HOT Scheduler::cancel_item_(Component *component, bool is_static_string, co
 | 
			
		||||
 | 
			
		||||
// Helper to cancel items by name - must be called with lock held
 | 
			
		||||
bool HOT Scheduler::cancel_item_locked_(Component *component, const char *name_cstr, SchedulerItem::Type type) {
 | 
			
		||||
  // Early return if name is invalid - no items to cancel
 | 
			
		||||
  if (name_cstr == nullptr || name_cstr[0] == '\0') {
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  size_t total_cancelled = 0;
 | 
			
		||||
 | 
			
		||||
  // Check all containers for matching items
 | 
			
		||||
 
 | 
			
		||||
@@ -150,9 +150,6 @@ class Scheduler {
 | 
			
		||||
    return is_static_string ? static_cast<const char *>(name_ptr) : static_cast<const std::string *>(name_ptr)->c_str();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Helper to check if a name is valid (not null and not empty)
 | 
			
		||||
  inline bool is_name_valid_(const char *name) { return name != nullptr && name[0] != '\0'; }
 | 
			
		||||
 | 
			
		||||
  // Common implementation for cancel operations
 | 
			
		||||
  bool cancel_item_(Component *component, bool is_static_string, const void *name_ptr, SchedulerItem::Type type);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -411,7 +411,7 @@ def wizard(path):
 | 
			
		||||
    safe_print("Options:")
 | 
			
		||||
    for board_id, board_data in boards_list:
 | 
			
		||||
        safe_print(f" - {board_id} - {board_data['name']}")
 | 
			
		||||
        boards.append(board_id)
 | 
			
		||||
        boards.append(board_id.lower())
 | 
			
		||||
 | 
			
		||||
    while True:
 | 
			
		||||
        board = safe_input(color(AnsiFore.BOLD_WHITE, "(board): "))
 | 
			
		||||
 
 | 
			
		||||
@@ -61,6 +61,7 @@ src_filter =
 | 
			
		||||
    +<../tests/dummy_main.cpp>
 | 
			
		||||
    +<../.temp/all-include.cpp>
 | 
			
		||||
lib_ldf_mode = off
 | 
			
		||||
lib_compat_mode = strict
 | 
			
		||||
 | 
			
		||||
; This are common settings for all Arduino-framework based environments.
 | 
			
		||||
[common:arduino]
 | 
			
		||||
@@ -211,6 +212,7 @@ build_unflags =
 | 
			
		||||
extends = common:arduino
 | 
			
		||||
platform = libretiny@1.9.1
 | 
			
		||||
framework = arduino
 | 
			
		||||
lib_compat_mode = soft
 | 
			
		||||
lib_deps =
 | 
			
		||||
    droscy/esp_wireguard@0.4.2    ; wireguard
 | 
			
		||||
build_flags =
 | 
			
		||||
 
 | 
			
		||||
@@ -13,7 +13,7 @@ platformio==6.1.18  # When updating platformio, also update /docker/Dockerfile
 | 
			
		||||
esptool==4.9.0
 | 
			
		||||
click==8.1.7
 | 
			
		||||
esphome-dashboard==20250514.0
 | 
			
		||||
aioesphomeapi==34.2.0
 | 
			
		||||
aioesphomeapi==34.2.1
 | 
			
		||||
zeroconf==0.147.0
 | 
			
		||||
puremagic==1.30
 | 
			
		||||
ruamel.yaml==0.18.14 # dashboard_import
 | 
			
		||||
 
 | 
			
		||||
@@ -249,6 +249,42 @@ class TypeInfo(ABC):
 | 
			
		||||
            return 4  # 28 bits
 | 
			
		||||
        return 5  # 32 bits (maximum for uint32_t)
 | 
			
		||||
 | 
			
		||||
    def _get_simple_size_calculation(
 | 
			
		||||
        self, name: str, force: bool, base_method: str, value_expr: str = None
 | 
			
		||||
    ) -> str:
 | 
			
		||||
        """Helper for simple size calculations.
 | 
			
		||||
 | 
			
		||||
        Args:
 | 
			
		||||
            name: Field name
 | 
			
		||||
            force: Whether this is for a repeated field
 | 
			
		||||
            base_method: Base method name (e.g., "add_int32_field")
 | 
			
		||||
            value_expr: Optional value expression (defaults to name)
 | 
			
		||||
        """
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        method = f"{base_method}_repeated" if force else base_method
 | 
			
		||||
        value = value_expr if value_expr else name
 | 
			
		||||
        return f"ProtoSize::{method}(total_size, {field_id_size}, {value});"
 | 
			
		||||
 | 
			
		||||
    def _get_fixed_size_calculation(
 | 
			
		||||
        self, name: str, force: bool, num_bytes: int, zero_check: str
 | 
			
		||||
    ) -> str:
 | 
			
		||||
        """Helper for fixed-size field calculations.
 | 
			
		||||
 | 
			
		||||
        Args:
 | 
			
		||||
            name: Field name
 | 
			
		||||
            force: Whether this is for a repeated field
 | 
			
		||||
            num_bytes: Number of bytes (4 or 8)
 | 
			
		||||
            zero_check: Expression to check for zero value (e.g., "!= 0.0f")
 | 
			
		||||
        """
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        # Fixed-size repeated fields are handled differently in RepeatedTypeInfo
 | 
			
		||||
        # so we should never get force=True here
 | 
			
		||||
        assert not force, (
 | 
			
		||||
            "Fixed-size repeated fields should be handled by RepeatedTypeInfo"
 | 
			
		||||
        )
 | 
			
		||||
        method = f"add_fixed_field<{num_bytes}>"
 | 
			
		||||
        return f"ProtoSize::{method}(total_size, {field_id_size}, {name} {zero_check});"
 | 
			
		||||
 | 
			
		||||
    @abstractmethod
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        """Calculate the size needed for encoding this field.
 | 
			
		||||
@@ -258,6 +294,14 @@ class TypeInfo(ABC):
 | 
			
		||||
            force: Whether to force encoding the field even if it has a default value
 | 
			
		||||
        """
 | 
			
		||||
 | 
			
		||||
    def get_fixed_size_bytes(self) -> int | None:
 | 
			
		||||
        """Get the number of bytes for fixed-size fields (float, double, fixed32, etc).
 | 
			
		||||
 | 
			
		||||
        Returns:
 | 
			
		||||
            The number of bytes (4 or 8) for fixed-size fields, None for variable-size fields.
 | 
			
		||||
        """
 | 
			
		||||
        return None
 | 
			
		||||
 | 
			
		||||
    @abstractmethod
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        """Get estimated size in bytes for this field with typical values.
 | 
			
		||||
@@ -295,9 +339,10 @@ class DoubleType(TypeInfo):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_fixed_field<8>(total_size, {field_id_size}, {name} != 0.0, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_fixed_size_calculation(name, force, 8, "!= 0.0")
 | 
			
		||||
 | 
			
		||||
    def get_fixed_size_bytes(self) -> int:
 | 
			
		||||
        return 8
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 8  # field ID + 8 bytes for double
 | 
			
		||||
@@ -317,9 +362,10 @@ class FloatType(TypeInfo):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_fixed_field<4>(total_size, {field_id_size}, {name} != 0.0f, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_fixed_size_calculation(name, force, 4, "!= 0.0f")
 | 
			
		||||
 | 
			
		||||
    def get_fixed_size_bytes(self) -> int:
 | 
			
		||||
        return 4
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 4  # field ID + 4 bytes for float
 | 
			
		||||
@@ -339,9 +385,7 @@ class Int64Type(TypeInfo):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_int64_field(total_size, {field_id_size}, {name}, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_simple_size_calculation(name, force, "add_int64_field")
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 3  # field ID + 3 bytes typical varint
 | 
			
		||||
@@ -361,9 +405,7 @@ class UInt64Type(TypeInfo):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_uint64_field(total_size, {field_id_size}, {name}, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_simple_size_calculation(name, force, "add_uint64_field")
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 3  # field ID + 3 bytes typical varint
 | 
			
		||||
@@ -383,9 +425,7 @@ class Int32Type(TypeInfo):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_int32_field(total_size, {field_id_size}, {name}, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_simple_size_calculation(name, force, "add_int32_field")
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 3  # field ID + 3 bytes typical varint
 | 
			
		||||
@@ -405,9 +445,10 @@ class Fixed64Type(TypeInfo):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_fixed_field<8>(total_size, {field_id_size}, {name} != 0, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_fixed_size_calculation(name, force, 8, "!= 0")
 | 
			
		||||
 | 
			
		||||
    def get_fixed_size_bytes(self) -> int:
 | 
			
		||||
        return 8
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 8  # field ID + 8 bytes fixed
 | 
			
		||||
@@ -427,9 +468,10 @@ class Fixed32Type(TypeInfo):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_fixed_field<4>(total_size, {field_id_size}, {name} != 0, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_fixed_size_calculation(name, force, 4, "!= 0")
 | 
			
		||||
 | 
			
		||||
    def get_fixed_size_bytes(self) -> int:
 | 
			
		||||
        return 4
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 4  # field ID + 4 bytes fixed
 | 
			
		||||
@@ -448,9 +490,7 @@ class BoolType(TypeInfo):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_bool_field(total_size, {field_id_size}, {name}, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_simple_size_calculation(name, force, "add_bool_field")
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 1  # field ID + 1 byte
 | 
			
		||||
@@ -471,9 +511,7 @@ class StringType(TypeInfo):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_string_field(total_size, {field_id_size}, {name}, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_simple_size_calculation(name, force, "add_string_field")
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 8  # field ID + 8 bytes typical string
 | 
			
		||||
@@ -498,20 +536,33 @@ class MessageType(TypeInfo):
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def encode_func(self) -> str:
 | 
			
		||||
        return f"encode_message<{self.cpp_type}>"
 | 
			
		||||
        return "encode_message"
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def decode_length(self) -> str:
 | 
			
		||||
        return f"value.as_message<{self.cpp_type}>()"
 | 
			
		||||
        # Override to return None for message types because we can't use template-based
 | 
			
		||||
        # decoding when the specific message type isn't known at compile time.
 | 
			
		||||
        # Instead, we use the non-template decode_to_message() method which allows
 | 
			
		||||
        # runtime polymorphism through virtual function calls.
 | 
			
		||||
        return None
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def decode_length_content(self) -> str:
 | 
			
		||||
        # Custom decode that doesn't use templates
 | 
			
		||||
        return dedent(
 | 
			
		||||
            f"""\
 | 
			
		||||
        case {self.number}: {{
 | 
			
		||||
          value.decode_to_message(this->{self.field_name});
 | 
			
		||||
          return true;
 | 
			
		||||
        }}"""
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    def dump(self, name: str) -> str:
 | 
			
		||||
        o = f"{name}.dump_to(out);"
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_message_object(total_size, {field_id_size}, {name}, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_simple_size_calculation(name, force, "add_message_object")
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return (
 | 
			
		||||
@@ -538,9 +589,7 @@ class BytesType(TypeInfo):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_string_field(total_size, {field_id_size}, {name}, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_simple_size_calculation(name, force, "add_string_field")
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 8  # field ID + 8 bytes typical bytes
 | 
			
		||||
@@ -560,9 +609,7 @@ class UInt32Type(TypeInfo):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_uint32_field(total_size, {field_id_size}, {name}, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_simple_size_calculation(name, force, "add_uint32_field")
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 3  # field ID + 3 bytes typical varint
 | 
			
		||||
@@ -576,23 +623,27 @@ class EnumType(TypeInfo):
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def decode_varint(self) -> str:
 | 
			
		||||
        return f"value.as_enum<{self.cpp_type}>()"
 | 
			
		||||
        return f"static_cast<{self.cpp_type}>(value.as_uint32())"
 | 
			
		||||
 | 
			
		||||
    default_value = ""
 | 
			
		||||
    wire_type = WireType.VARINT  # Uses wire type 0
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def encode_func(self) -> str:
 | 
			
		||||
        return f"encode_enum<{self.cpp_type}>"
 | 
			
		||||
        return "encode_uint32"
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def encode_content(self) -> str:
 | 
			
		||||
        return f"buffer.{self.encode_func}({self.number}, static_cast<uint32_t>(this->{self.field_name}));"
 | 
			
		||||
 | 
			
		||||
    def dump(self, name: str) -> str:
 | 
			
		||||
        o = f"out.append(proto_enum_to_string<{self.cpp_type}>({name}));"
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_enum_field(total_size, {field_id_size}, static_cast<uint32_t>({name}), {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_simple_size_calculation(
 | 
			
		||||
            name, force, "add_enum_field", f"static_cast<uint32_t>({name})"
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 1  # field ID + 1 byte typical enum
 | 
			
		||||
@@ -612,9 +663,10 @@ class SFixed32Type(TypeInfo):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_fixed_field<4>(total_size, {field_id_size}, {name} != 0, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_fixed_size_calculation(name, force, 4, "!= 0")
 | 
			
		||||
 | 
			
		||||
    def get_fixed_size_bytes(self) -> int:
 | 
			
		||||
        return 4
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 4  # field ID + 4 bytes fixed
 | 
			
		||||
@@ -634,9 +686,10 @@ class SFixed64Type(TypeInfo):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_fixed_field<8>(total_size, {field_id_size}, {name} != 0, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_fixed_size_calculation(name, force, 8, "!= 0")
 | 
			
		||||
 | 
			
		||||
    def get_fixed_size_bytes(self) -> int:
 | 
			
		||||
        return 8
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 8  # field ID + 8 bytes fixed
 | 
			
		||||
@@ -656,9 +709,7 @@ class SInt32Type(TypeInfo):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_sint32_field(total_size, {field_id_size}, {name}, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_simple_size_calculation(name, force, "add_sint32_field")
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 3  # field ID + 3 bytes typical varint
 | 
			
		||||
@@ -678,9 +729,7 @@ class SInt64Type(TypeInfo):
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
    def get_size_calculation(self, name: str, force: bool = False) -> str:
 | 
			
		||||
        field_id_size = self.calculate_field_id_size()
 | 
			
		||||
        o = f"ProtoSize::add_sint64_field(total_size, {field_id_size}, {name}, {force_str(force)});"
 | 
			
		||||
        return o
 | 
			
		||||
        return self._get_simple_size_calculation(name, force, "add_sint64_field")
 | 
			
		||||
 | 
			
		||||
    def get_estimated_size(self) -> int:
 | 
			
		||||
        return self.calculate_field_id_size() + 3  # field ID + 3 bytes typical varint
 | 
			
		||||
@@ -727,6 +776,16 @@ class RepeatedTypeInfo(TypeInfo):
 | 
			
		||||
    @property
 | 
			
		||||
    def decode_length_content(self) -> str:
 | 
			
		||||
        content = self._ti.decode_length
 | 
			
		||||
        if content is None and isinstance(self._ti, MessageType):
 | 
			
		||||
            # Special handling for non-template message decoding
 | 
			
		||||
            return dedent(
 | 
			
		||||
                f"""\
 | 
			
		||||
        case {self.number}: {{
 | 
			
		||||
          this->{self.field_name}.emplace_back();
 | 
			
		||||
          value.decode_to_message(this->{self.field_name}.back());
 | 
			
		||||
          return true;
 | 
			
		||||
        }}"""
 | 
			
		||||
            )
 | 
			
		||||
        if content is None:
 | 
			
		||||
            return None
 | 
			
		||||
        return dedent(
 | 
			
		||||
@@ -771,7 +830,10 @@ class RepeatedTypeInfo(TypeInfo):
 | 
			
		||||
    @property
 | 
			
		||||
    def encode_content(self) -> str:
 | 
			
		||||
        o = f"for (auto {'' if self._ti_is_bool else '&'}it : this->{self.field_name}) {{\n"
 | 
			
		||||
        o += f"  buffer.{self._ti.encode_func}({self.number}, it, true);\n"
 | 
			
		||||
        if isinstance(self._ti, EnumType):
 | 
			
		||||
            o += f"  buffer.{self._ti.encode_func}({self.number}, static_cast<uint32_t>(it), true);\n"
 | 
			
		||||
        else:
 | 
			
		||||
            o += f"  buffer.{self._ti.encode_func}({self.number}, it, true);\n"
 | 
			
		||||
        o += "}"
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
@@ -795,11 +857,23 @@ class RepeatedTypeInfo(TypeInfo):
 | 
			
		||||
            field_id_size = self._ti.calculate_field_id_size()
 | 
			
		||||
            o = f"ProtoSize::add_repeated_message(total_size, {field_id_size}, {name});"
 | 
			
		||||
            return o
 | 
			
		||||
 | 
			
		||||
        # For other repeated types, use the underlying type's size calculation with force=True
 | 
			
		||||
        o = f"if (!{name}.empty()) {{\n"
 | 
			
		||||
        o += f"  for (const auto {'' if self._ti_is_bool else '&'}it : {name}) {{\n"
 | 
			
		||||
        o += f"    {self._ti.get_size_calculation('it', True)}\n"
 | 
			
		||||
        o += "  }\n"
 | 
			
		||||
 | 
			
		||||
        # Check if this is a fixed-size type by seeing if it has a fixed byte count
 | 
			
		||||
        num_bytes = self._ti.get_fixed_size_bytes()
 | 
			
		||||
        if num_bytes is not None:
 | 
			
		||||
            # Fixed types have constant size per element, so we can multiply
 | 
			
		||||
            field_id_size = self._ti.calculate_field_id_size()
 | 
			
		||||
            # Pre-calculate the total bytes per element
 | 
			
		||||
            bytes_per_element = field_id_size + num_bytes
 | 
			
		||||
            o += f"  total_size += {name}.size() * {bytes_per_element};\n"
 | 
			
		||||
        else:
 | 
			
		||||
            # Other types need the actual value
 | 
			
		||||
            o += f"  for (const auto {'' if self._ti_is_bool else '&'}it : {name}) {{\n"
 | 
			
		||||
            o += f"    {self._ti.get_size_calculation('it', True)}\n"
 | 
			
		||||
            o += "  }\n"
 | 
			
		||||
        o += "}"
 | 
			
		||||
        return o
 | 
			
		||||
 | 
			
		||||
@@ -985,15 +1059,31 @@ def build_message_type(
 | 
			
		||||
    # Get message ID if it's a service message
 | 
			
		||||
    message_id: int | None = get_opt(desc, pb.id)
 | 
			
		||||
 | 
			
		||||
    # Get source direction to determine if we need decode/encode methods
 | 
			
		||||
    source: int = get_opt(desc, pb.source, SOURCE_BOTH)
 | 
			
		||||
    needs_decode = source in (SOURCE_BOTH, SOURCE_CLIENT)
 | 
			
		||||
    needs_encode = source in (SOURCE_BOTH, SOURCE_SERVER)
 | 
			
		||||
 | 
			
		||||
    # Add MESSAGE_TYPE method if this is a service message
 | 
			
		||||
    if message_id is not None:
 | 
			
		||||
        # Validate that message_id fits in uint8_t
 | 
			
		||||
        if message_id > 255:
 | 
			
		||||
            raise ValueError(
 | 
			
		||||
                f"Message ID {message_id} for {desc.name} exceeds uint8_t maximum (255)"
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
        # Add static constexpr for message type
 | 
			
		||||
        public_content.append(f"static constexpr uint16_t MESSAGE_TYPE = {message_id};")
 | 
			
		||||
        public_content.append(f"static constexpr uint8_t MESSAGE_TYPE = {message_id};")
 | 
			
		||||
 | 
			
		||||
        # Add estimated size constant
 | 
			
		||||
        estimated_size = calculate_message_estimated_size(desc)
 | 
			
		||||
        # Validate that estimated_size fits in uint8_t
 | 
			
		||||
        if estimated_size > 255:
 | 
			
		||||
            raise ValueError(
 | 
			
		||||
                f"Estimated size {estimated_size} for {desc.name} exceeds uint8_t maximum (255)"
 | 
			
		||||
            )
 | 
			
		||||
        public_content.append(
 | 
			
		||||
            f"static constexpr uint16_t ESTIMATED_SIZE = {estimated_size};"
 | 
			
		||||
            f"static constexpr uint8_t ESTIMATED_SIZE = {estimated_size};"
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        # Add message_name method inline in header
 | 
			
		||||
@@ -1016,18 +1106,21 @@ def build_message_type(
 | 
			
		||||
            protected_content.extend(ti.protected_content)
 | 
			
		||||
            public_content.extend(ti.public_content)
 | 
			
		||||
 | 
			
		||||
        # Always include encode/decode logic for all fields
 | 
			
		||||
        encode.append(ti.encode_content)
 | 
			
		||||
        size_calc.append(ti.get_size_calculation(f"this->{ti.field_name}"))
 | 
			
		||||
        # Only collect encode logic if this message needs it
 | 
			
		||||
        if needs_encode:
 | 
			
		||||
            encode.append(ti.encode_content)
 | 
			
		||||
            size_calc.append(ti.get_size_calculation(f"this->{ti.field_name}"))
 | 
			
		||||
 | 
			
		||||
        if ti.decode_varint_content:
 | 
			
		||||
            decode_varint.append(ti.decode_varint_content)
 | 
			
		||||
        if ti.decode_length_content:
 | 
			
		||||
            decode_length.append(ti.decode_length_content)
 | 
			
		||||
        if ti.decode_32bit_content:
 | 
			
		||||
            decode_32bit.append(ti.decode_32bit_content)
 | 
			
		||||
        if ti.decode_64bit_content:
 | 
			
		||||
            decode_64bit.append(ti.decode_64bit_content)
 | 
			
		||||
        # Only collect decode methods if this message needs them
 | 
			
		||||
        if needs_decode:
 | 
			
		||||
            if ti.decode_varint_content:
 | 
			
		||||
                decode_varint.append(ti.decode_varint_content)
 | 
			
		||||
            if ti.decode_length_content:
 | 
			
		||||
                decode_length.append(ti.decode_length_content)
 | 
			
		||||
            if ti.decode_32bit_content:
 | 
			
		||||
                decode_32bit.append(ti.decode_32bit_content)
 | 
			
		||||
            if ti.decode_64bit_content:
 | 
			
		||||
                decode_64bit.append(ti.decode_64bit_content)
 | 
			
		||||
        if ti.dump_content:
 | 
			
		||||
            dump.append(ti.dump_content)
 | 
			
		||||
 | 
			
		||||
@@ -1073,8 +1166,8 @@ def build_message_type(
 | 
			
		||||
        prot = "bool decode_64bit(uint32_t field_id, Proto64Bit value) override;"
 | 
			
		||||
        protected_content.insert(0, prot)
 | 
			
		||||
 | 
			
		||||
    # Only generate encode method if there are fields to encode
 | 
			
		||||
    if encode:
 | 
			
		||||
    # Only generate encode method if this message needs encoding and has fields
 | 
			
		||||
    if needs_encode and encode:
 | 
			
		||||
        o = f"void {desc.name}::encode(ProtoWriteBuffer buffer) const {{"
 | 
			
		||||
        if len(encode) == 1 and len(encode[0]) + len(o) + 3 < 120:
 | 
			
		||||
            o += f" {encode[0]} "
 | 
			
		||||
@@ -1085,10 +1178,10 @@ def build_message_type(
 | 
			
		||||
        cpp += o
 | 
			
		||||
        prot = "void encode(ProtoWriteBuffer buffer) const override;"
 | 
			
		||||
        public_content.append(prot)
 | 
			
		||||
    # If no fields to encode, the default implementation in ProtoMessage will be used
 | 
			
		||||
    # If no fields to encode or message doesn't need encoding, the default implementation in ProtoMessage will be used
 | 
			
		||||
 | 
			
		||||
    # Add calculate_size method only if there are fields
 | 
			
		||||
    if size_calc:
 | 
			
		||||
    # Add calculate_size method only if this message needs encoding and has fields
 | 
			
		||||
    if needs_encode and size_calc:
 | 
			
		||||
        o = f"void {desc.name}::calculate_size(uint32_t &total_size) const {{"
 | 
			
		||||
        # For a single field, just inline it for simplicity
 | 
			
		||||
        if len(size_calc) == 1 and len(size_calc[0]) + len(o) + 3 < 120:
 | 
			
		||||
@@ -1101,7 +1194,7 @@ def build_message_type(
 | 
			
		||||
        cpp += o
 | 
			
		||||
        prot = "void calculate_size(uint32_t &total_size) const override;"
 | 
			
		||||
        public_content.append(prot)
 | 
			
		||||
    # If no fields to calculate size for, the default implementation in ProtoMessage will be used
 | 
			
		||||
    # If no fields to calculate size for or message doesn't need encoding, the default implementation in ProtoMessage will be used
 | 
			
		||||
 | 
			
		||||
    # dump_to method declaration in header
 | 
			
		||||
    prot = "#ifdef HAS_PROTO_MESSAGE_DUMP\n"
 | 
			
		||||
@@ -1366,7 +1459,6 @@ def main() -> None:
 | 
			
		||||
#include "esphome/core/defines.h"
 | 
			
		||||
 | 
			
		||||
#include "proto.h"
 | 
			
		||||
#include "api_pb2_size.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace api {
 | 
			
		||||
@@ -1376,7 +1468,6 @@ namespace api {
 | 
			
		||||
    cpp = FILE_HEADER
 | 
			
		||||
    cpp += """\
 | 
			
		||||
    #include "api_pb2.h"
 | 
			
		||||
    #include "api_pb2_size.h"
 | 
			
		||||
    #include "esphome/core/log.h"
 | 
			
		||||
    #include "esphome/core/helpers.h"
 | 
			
		||||
 | 
			
		||||
@@ -1701,7 +1792,6 @@ static const char *const TAG = "api.service";
 | 
			
		||||
        exec_clang_format(root / "api_pb2_service.cpp")
 | 
			
		||||
        exec_clang_format(root / "api_pb2.h")
 | 
			
		||||
        exec_clang_format(root / "api_pb2.cpp")
 | 
			
		||||
        exec_clang_format(root / "api_pb2_dump.h")
 | 
			
		||||
        exec_clang_format(root / "api_pb2_dump.cpp")
 | 
			
		||||
    except ImportError:
 | 
			
		||||
        pass
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										24
									
								
								tests/integration/fixtures/api_custom_services.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								tests/integration/fixtures/api_custom_services.yaml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
esphome:
 | 
			
		||||
  name: api-custom-services-test
 | 
			
		||||
host:
 | 
			
		||||
 | 
			
		||||
# This is required for CustomAPIDevice to work
 | 
			
		||||
api:
 | 
			
		||||
  custom_services: true
 | 
			
		||||
  # Also test that YAML services still work
 | 
			
		||||
  actions:
 | 
			
		||||
    - action: test_yaml_service
 | 
			
		||||
      then:
 | 
			
		||||
        - logger.log: "YAML service called"
 | 
			
		||||
 | 
			
		||||
logger:
 | 
			
		||||
  level: DEBUG
 | 
			
		||||
 | 
			
		||||
# External component that uses CustomAPIDevice
 | 
			
		||||
external_components:
 | 
			
		||||
  - source:
 | 
			
		||||
      type: local
 | 
			
		||||
      path: EXTERNAL_COMPONENT_PATH
 | 
			
		||||
    components: [custom_api_device_component]
 | 
			
		||||
 | 
			
		||||
custom_api_device_component:
 | 
			
		||||
@@ -0,0 +1,19 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.const import CONF_ID
 | 
			
		||||
 | 
			
		||||
custom_api_device_component_ns = cg.esphome_ns.namespace("custom_api_device_component")
 | 
			
		||||
CustomAPIDeviceComponent = custom_api_device_component_ns.class_(
 | 
			
		||||
    "CustomAPIDeviceComponent", cg.Component
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(): cv.declare_id(CustomAPIDeviceComponent),
 | 
			
		||||
    }
 | 
			
		||||
).extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
@@ -0,0 +1,53 @@
 | 
			
		||||
#include "custom_api_device_component.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_API
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace custom_api_device_component {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "custom_api";
 | 
			
		||||
 | 
			
		||||
void CustomAPIDeviceComponent::setup() {
 | 
			
		||||
  // Register services using CustomAPIDevice
 | 
			
		||||
  register_service(&CustomAPIDeviceComponent::on_test_service, "custom_test_service");
 | 
			
		||||
 | 
			
		||||
  register_service(&CustomAPIDeviceComponent::on_service_with_args, "custom_service_with_args",
 | 
			
		||||
                   {"arg_string", "arg_int", "arg_bool", "arg_float"});
 | 
			
		||||
 | 
			
		||||
  // Test array types
 | 
			
		||||
  register_service(&CustomAPIDeviceComponent::on_service_with_arrays, "custom_service_with_arrays",
 | 
			
		||||
                   {"bool_array", "int_array", "float_array", "string_array"});
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CustomAPIDeviceComponent::on_test_service() { ESP_LOGI(TAG, "Custom test service called!"); }
 | 
			
		||||
 | 
			
		||||
// NOLINTNEXTLINE(performance-unnecessary-value-param)
 | 
			
		||||
void CustomAPIDeviceComponent::on_service_with_args(std::string arg_string, int32_t arg_int, bool arg_bool,
 | 
			
		||||
                                                    float arg_float) {
 | 
			
		||||
  ESP_LOGI(TAG, "Custom service called with: %s, %d, %d, %.2f", arg_string.c_str(), arg_int, arg_bool, arg_float);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CustomAPIDeviceComponent::on_service_with_arrays(std::vector<bool> bool_array, std::vector<int32_t> int_array,
 | 
			
		||||
                                                      std::vector<float> float_array,
 | 
			
		||||
                                                      std::vector<std::string> string_array) {
 | 
			
		||||
  ESP_LOGI(TAG, "Array service called with %zu bools, %zu ints, %zu floats, %zu strings", bool_array.size(),
 | 
			
		||||
           int_array.size(), float_array.size(), string_array.size());
 | 
			
		||||
 | 
			
		||||
  // Log first element of each array if not empty
 | 
			
		||||
  if (!bool_array.empty()) {
 | 
			
		||||
    ESP_LOGI(TAG, "First bool: %s", bool_array[0] ? "true" : "false");
 | 
			
		||||
  }
 | 
			
		||||
  if (!int_array.empty()) {
 | 
			
		||||
    ESP_LOGI(TAG, "First int: %d", int_array[0]);
 | 
			
		||||
  }
 | 
			
		||||
  if (!float_array.empty()) {
 | 
			
		||||
    ESP_LOGI(TAG, "First float: %.2f", float_array[0]);
 | 
			
		||||
  }
 | 
			
		||||
  if (!string_array.empty()) {
 | 
			
		||||
    ESP_LOGI(TAG, "First string: %s", string_array[0].c_str());
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace custom_api_device_component
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
#endif  // USE_API
 | 
			
		||||
@@ -0,0 +1,29 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/components/api/custom_api_device.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_API
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace custom_api_device_component {
 | 
			
		||||
 | 
			
		||||
using namespace api;
 | 
			
		||||
 | 
			
		||||
class CustomAPIDeviceComponent : public Component, public CustomAPIDevice {
 | 
			
		||||
 public:
 | 
			
		||||
  void setup() override;
 | 
			
		||||
 | 
			
		||||
  void on_test_service();
 | 
			
		||||
 | 
			
		||||
  // NOLINTNEXTLINE(performance-unnecessary-value-param)
 | 
			
		||||
  void on_service_with_args(std::string arg_string, int32_t arg_int, bool arg_bool, float arg_float);
 | 
			
		||||
 | 
			
		||||
  void on_service_with_arrays(std::vector<bool> bool_array, std::vector<int32_t> int_array,
 | 
			
		||||
                              std::vector<float> float_array, std::vector<std::string> string_array);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace custom_api_device_component
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
#endif  // USE_API
 | 
			
		||||
@@ -23,19 +23,6 @@ void SchedulerStringLifetimeComponent::run_string_lifetime_test() {
 | 
			
		||||
  test_vector_reallocation();
 | 
			
		||||
  test_string_move_semantics();
 | 
			
		||||
  test_lambda_capture_lifetime();
 | 
			
		||||
 | 
			
		||||
  // Schedule final check
 | 
			
		||||
  this->set_timeout("final_check", 200, [this]() {
 | 
			
		||||
    ESP_LOGI(TAG, "Tests passed: %d", this->tests_passed_);
 | 
			
		||||
    ESP_LOGI(TAG, "Tests failed: %d", this->tests_failed_);
 | 
			
		||||
 | 
			
		||||
    if (this->tests_failed_ == 0) {
 | 
			
		||||
      ESP_LOGI(TAG, "SUCCESS: All string lifetime tests passed!");
 | 
			
		||||
    } else {
 | 
			
		||||
      ESP_LOGE(TAG, "FAILURE: %d string lifetime tests failed!", this->tests_failed_);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGI(TAG, "String lifetime tests complete");
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SchedulerStringLifetimeComponent::run_test1() {
 | 
			
		||||
@@ -69,7 +56,6 @@ void SchedulerStringLifetimeComponent::run_test5() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SchedulerStringLifetimeComponent::run_final_check() {
 | 
			
		||||
  ESP_LOGI(TAG, "String lifetime tests complete");
 | 
			
		||||
  ESP_LOGI(TAG, "Tests passed: %d", this->tests_passed_);
 | 
			
		||||
  ESP_LOGI(TAG, "Tests failed: %d", this->tests_failed_);
 | 
			
		||||
 | 
			
		||||
@@ -78,6 +64,7 @@ void SchedulerStringLifetimeComponent::run_final_check() {
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGE(TAG, "FAILURE: %d string lifetime tests failed!", this->tests_failed_);
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGI(TAG, "String lifetime tests complete");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SchedulerStringLifetimeComponent::test_temporary_string_lifetime() {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										43
									
								
								tests/integration/fixtures/scheduler_null_name.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								tests/integration/fixtures/scheduler_null_name.yaml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
esphome:
 | 
			
		||||
  name: scheduler-null-name
 | 
			
		||||
 | 
			
		||||
host:
 | 
			
		||||
 | 
			
		||||
logger:
 | 
			
		||||
  level: DEBUG
 | 
			
		||||
 | 
			
		||||
api:
 | 
			
		||||
  services:
 | 
			
		||||
    - service: test_null_name
 | 
			
		||||
      then:
 | 
			
		||||
        - lambda: |-
 | 
			
		||||
            // First, create a scenario that would trigger the crash
 | 
			
		||||
            // The crash happens when defer() is called with a name that would be cancelled
 | 
			
		||||
 | 
			
		||||
            // Test 1: Create a defer with a valid name
 | 
			
		||||
            App.scheduler.set_timeout(nullptr, "test_defer", 0, []() {
 | 
			
		||||
              ESP_LOGI("TEST", "First defer should be cancelled");
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            // Test 2: Create another defer with the same name - this triggers cancel_item_locked_
 | 
			
		||||
            // In the unfixed code, this would crash if the name was NULL
 | 
			
		||||
            App.scheduler.set_timeout(nullptr, "test_defer", 0, []() {
 | 
			
		||||
              ESP_LOGI("TEST", "Second defer executed");
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            // Test 3: Now test with nullptr - this is the actual crash scenario
 | 
			
		||||
            // Create a defer item without a name (like voice assistant does)
 | 
			
		||||
            const char* null_name = nullptr;
 | 
			
		||||
            App.scheduler.set_timeout(nullptr, null_name, 0, []() {
 | 
			
		||||
              ESP_LOGI("TEST", "Defer with null name executed");
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            // Test 4: Create another defer with null name - this would trigger the crash
 | 
			
		||||
            App.scheduler.set_timeout(nullptr, null_name, 0, []() {
 | 
			
		||||
              ESP_LOGI("TEST", "Second null defer executed");
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            // Test 5: Verify scheduler still works
 | 
			
		||||
            App.scheduler.set_timeout(nullptr, "valid_timeout", 50, []() {
 | 
			
		||||
              ESP_LOGI("TEST", "Test completed successfully");
 | 
			
		||||
            });
 | 
			
		||||
							
								
								
									
										144
									
								
								tests/integration/test_api_custom_services.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										144
									
								
								tests/integration/test_api_custom_services.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,144 @@
 | 
			
		||||
"""Integration test for API custom services using CustomAPIDevice."""
 | 
			
		||||
 | 
			
		||||
from __future__ import annotations
 | 
			
		||||
 | 
			
		||||
import asyncio
 | 
			
		||||
from pathlib import Path
 | 
			
		||||
import re
 | 
			
		||||
 | 
			
		||||
from aioesphomeapi import UserService, UserServiceArgType
 | 
			
		||||
import pytest
 | 
			
		||||
 | 
			
		||||
from .types import APIClientConnectedFactory, RunCompiledFunction
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.mark.asyncio
 | 
			
		||||
async def test_api_custom_services(
 | 
			
		||||
    yaml_config: str,
 | 
			
		||||
    run_compiled: RunCompiledFunction,
 | 
			
		||||
    api_client_connected: APIClientConnectedFactory,
 | 
			
		||||
) -> None:
 | 
			
		||||
    """Test CustomAPIDevice services work correctly with custom_services: true."""
 | 
			
		||||
    # Get the path to the external components directory
 | 
			
		||||
    external_components_path = str(
 | 
			
		||||
        Path(__file__).parent / "fixtures" / "external_components"
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    # Replace the placeholder in the YAML config with the actual path
 | 
			
		||||
    yaml_config = yaml_config.replace(
 | 
			
		||||
        "EXTERNAL_COMPONENT_PATH", external_components_path
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    loop = asyncio.get_running_loop()
 | 
			
		||||
 | 
			
		||||
    # Track log messages
 | 
			
		||||
    yaml_service_future = loop.create_future()
 | 
			
		||||
    custom_service_future = loop.create_future()
 | 
			
		||||
    custom_args_future = loop.create_future()
 | 
			
		||||
    custom_arrays_future = loop.create_future()
 | 
			
		||||
 | 
			
		||||
    # Patterns to match in logs
 | 
			
		||||
    yaml_service_pattern = re.compile(r"YAML service called")
 | 
			
		||||
    custom_service_pattern = re.compile(r"Custom test service called!")
 | 
			
		||||
    custom_args_pattern = re.compile(
 | 
			
		||||
        r"Custom service called with: test_string, 456, 1, 78\.90"
 | 
			
		||||
    )
 | 
			
		||||
    custom_arrays_pattern = re.compile(
 | 
			
		||||
        r"Array service called with 2 bools, 3 ints, 2 floats, 2 strings"
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    def check_output(line: str) -> None:
 | 
			
		||||
        """Check log output for expected messages."""
 | 
			
		||||
        if not yaml_service_future.done() and yaml_service_pattern.search(line):
 | 
			
		||||
            yaml_service_future.set_result(True)
 | 
			
		||||
        elif not custom_service_future.done() and custom_service_pattern.search(line):
 | 
			
		||||
            custom_service_future.set_result(True)
 | 
			
		||||
        elif not custom_args_future.done() and custom_args_pattern.search(line):
 | 
			
		||||
            custom_args_future.set_result(True)
 | 
			
		||||
        elif not custom_arrays_future.done() and custom_arrays_pattern.search(line):
 | 
			
		||||
            custom_arrays_future.set_result(True)
 | 
			
		||||
 | 
			
		||||
    # Run with log monitoring
 | 
			
		||||
    async with run_compiled(yaml_config, line_callback=check_output):
 | 
			
		||||
        async with api_client_connected() as client:
 | 
			
		||||
            # Verify device info
 | 
			
		||||
            device_info = await client.device_info()
 | 
			
		||||
            assert device_info is not None
 | 
			
		||||
            assert device_info.name == "api-custom-services-test"
 | 
			
		||||
 | 
			
		||||
            # List services
 | 
			
		||||
            _, services = await client.list_entities_services()
 | 
			
		||||
 | 
			
		||||
            # Should have 4 services: 1 YAML + 3 CustomAPIDevice
 | 
			
		||||
            assert len(services) == 4, f"Expected 4 services, found {len(services)}"
 | 
			
		||||
 | 
			
		||||
            # Find our services
 | 
			
		||||
            yaml_service: UserService | None = None
 | 
			
		||||
            custom_service: UserService | None = None
 | 
			
		||||
            custom_args_service: UserService | None = None
 | 
			
		||||
            custom_arrays_service: UserService | None = None
 | 
			
		||||
 | 
			
		||||
            for service in services:
 | 
			
		||||
                if service.name == "test_yaml_service":
 | 
			
		||||
                    yaml_service = service
 | 
			
		||||
                elif service.name == "custom_test_service":
 | 
			
		||||
                    custom_service = service
 | 
			
		||||
                elif service.name == "custom_service_with_args":
 | 
			
		||||
                    custom_args_service = service
 | 
			
		||||
                elif service.name == "custom_service_with_arrays":
 | 
			
		||||
                    custom_arrays_service = service
 | 
			
		||||
 | 
			
		||||
            assert yaml_service is not None, "test_yaml_service not found"
 | 
			
		||||
            assert custom_service is not None, "custom_test_service not found"
 | 
			
		||||
            assert custom_args_service is not None, "custom_service_with_args not found"
 | 
			
		||||
            assert custom_arrays_service is not None, (
 | 
			
		||||
                "custom_service_with_arrays not found"
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
            # Test YAML service
 | 
			
		||||
            client.execute_service(yaml_service, {})
 | 
			
		||||
            await asyncio.wait_for(yaml_service_future, timeout=5.0)
 | 
			
		||||
 | 
			
		||||
            # Test simple CustomAPIDevice service
 | 
			
		||||
            client.execute_service(custom_service, {})
 | 
			
		||||
            await asyncio.wait_for(custom_service_future, timeout=5.0)
 | 
			
		||||
 | 
			
		||||
            # Verify custom_args_service arguments
 | 
			
		||||
            assert len(custom_args_service.args) == 4
 | 
			
		||||
            arg_types = {arg.name: arg.type for arg in custom_args_service.args}
 | 
			
		||||
            assert arg_types["arg_string"] == UserServiceArgType.STRING
 | 
			
		||||
            assert arg_types["arg_int"] == UserServiceArgType.INT
 | 
			
		||||
            assert arg_types["arg_bool"] == UserServiceArgType.BOOL
 | 
			
		||||
            assert arg_types["arg_float"] == UserServiceArgType.FLOAT
 | 
			
		||||
 | 
			
		||||
            # Test CustomAPIDevice service with arguments
 | 
			
		||||
            client.execute_service(
 | 
			
		||||
                custom_args_service,
 | 
			
		||||
                {
 | 
			
		||||
                    "arg_string": "test_string",
 | 
			
		||||
                    "arg_int": 456,
 | 
			
		||||
                    "arg_bool": True,
 | 
			
		||||
                    "arg_float": 78.9,
 | 
			
		||||
                },
 | 
			
		||||
            )
 | 
			
		||||
            await asyncio.wait_for(custom_args_future, timeout=5.0)
 | 
			
		||||
 | 
			
		||||
            # Verify array service arguments
 | 
			
		||||
            assert len(custom_arrays_service.args) == 4
 | 
			
		||||
            array_arg_types = {arg.name: arg.type for arg in custom_arrays_service.args}
 | 
			
		||||
            assert array_arg_types["bool_array"] == UserServiceArgType.BOOL_ARRAY
 | 
			
		||||
            assert array_arg_types["int_array"] == UserServiceArgType.INT_ARRAY
 | 
			
		||||
            assert array_arg_types["float_array"] == UserServiceArgType.FLOAT_ARRAY
 | 
			
		||||
            assert array_arg_types["string_array"] == UserServiceArgType.STRING_ARRAY
 | 
			
		||||
 | 
			
		||||
            # Test CustomAPIDevice service with arrays
 | 
			
		||||
            client.execute_service(
 | 
			
		||||
                custom_arrays_service,
 | 
			
		||||
                {
 | 
			
		||||
                    "bool_array": [True, False],
 | 
			
		||||
                    "int_array": [1, 2, 3],
 | 
			
		||||
                    "float_array": [1.1, 2.2],
 | 
			
		||||
                    "string_array": ["hello", "world"],
 | 
			
		||||
                },
 | 
			
		||||
            )
 | 
			
		||||
            await asyncio.wait_for(custom_arrays_future, timeout=5.0)
 | 
			
		||||
							
								
								
									
										59
									
								
								tests/integration/test_scheduler_null_name.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								tests/integration/test_scheduler_null_name.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,59 @@
 | 
			
		||||
"""Test that scheduler handles NULL names safely without crashing."""
 | 
			
		||||
 | 
			
		||||
import asyncio
 | 
			
		||||
import re
 | 
			
		||||
 | 
			
		||||
import pytest
 | 
			
		||||
 | 
			
		||||
from .types import APIClientConnectedFactory, RunCompiledFunction
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.mark.asyncio
 | 
			
		||||
async def test_scheduler_null_name(
 | 
			
		||||
    yaml_config: str,
 | 
			
		||||
    run_compiled: RunCompiledFunction,
 | 
			
		||||
    api_client_connected: APIClientConnectedFactory,
 | 
			
		||||
) -> None:
 | 
			
		||||
    """Test that scheduler handles NULL names safely without crashing."""
 | 
			
		||||
 | 
			
		||||
    loop = asyncio.get_running_loop()
 | 
			
		||||
    test_complete_future: asyncio.Future[bool] = loop.create_future()
 | 
			
		||||
 | 
			
		||||
    # Pattern to match test completion
 | 
			
		||||
    test_complete_pattern = re.compile(r"Test completed successfully")
 | 
			
		||||
 | 
			
		||||
    def check_output(line: str) -> None:
 | 
			
		||||
        """Check log output for test completion."""
 | 
			
		||||
        if not test_complete_future.done() and test_complete_pattern.search(line):
 | 
			
		||||
            test_complete_future.set_result(True)
 | 
			
		||||
 | 
			
		||||
    async with run_compiled(yaml_config, line_callback=check_output):
 | 
			
		||||
        async with api_client_connected() as client:
 | 
			
		||||
            # Verify we can connect
 | 
			
		||||
            device_info = await client.device_info()
 | 
			
		||||
            assert device_info is not None
 | 
			
		||||
            assert device_info.name == "scheduler-null-name"
 | 
			
		||||
 | 
			
		||||
            # List services
 | 
			
		||||
            _, services = await asyncio.wait_for(
 | 
			
		||||
                client.list_entities_services(), timeout=5.0
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
            # Find our test service
 | 
			
		||||
            test_null_name_service = next(
 | 
			
		||||
                (s for s in services if s.name == "test_null_name"), None
 | 
			
		||||
            )
 | 
			
		||||
            assert test_null_name_service is not None, (
 | 
			
		||||
                "test_null_name service not found"
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
            # Execute the test
 | 
			
		||||
            client.execute_service(test_null_name_service, {})
 | 
			
		||||
 | 
			
		||||
            # Wait for test completion
 | 
			
		||||
            try:
 | 
			
		||||
                await asyncio.wait_for(test_complete_future, timeout=10.0)
 | 
			
		||||
            except asyncio.TimeoutError:
 | 
			
		||||
                pytest.fail(
 | 
			
		||||
                    "Test did not complete within timeout - likely crashed due to NULL name"
 | 
			
		||||
                )
 | 
			
		||||
@@ -8,9 +8,19 @@ from typing import Any
 | 
			
		||||
import pytest
 | 
			
		||||
 | 
			
		||||
from esphome.config_validation import Invalid
 | 
			
		||||
from esphome.const import CONF_DEVICE_ID, CONF_DISABLED_BY_DEFAULT, CONF_ICON, CONF_NAME
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_DEVICE_ID,
 | 
			
		||||
    CONF_DISABLED_BY_DEFAULT,
 | 
			
		||||
    CONF_ICON,
 | 
			
		||||
    CONF_INTERNAL,
 | 
			
		||||
    CONF_NAME,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core import CORE, ID, entity_helpers
 | 
			
		||||
from esphome.core.entity_helpers import get_base_entity_object_id, setup_entity
 | 
			
		||||
from esphome.core.entity_helpers import (
 | 
			
		||||
    entity_duplicate_validator,
 | 
			
		||||
    get_base_entity_object_id,
 | 
			
		||||
    setup_entity,
 | 
			
		||||
)
 | 
			
		||||
from esphome.cpp_generator import MockObj
 | 
			
		||||
from esphome.helpers import sanitize, snake_case
 | 
			
		||||
 | 
			
		||||
@@ -493,11 +503,6 @@ async def test_setup_entity_disabled_by_default(
 | 
			
		||||
 | 
			
		||||
def test_entity_duplicate_validator() -> None:
 | 
			
		||||
    """Test the entity_duplicate_validator function."""
 | 
			
		||||
    from esphome.core.entity_helpers import entity_duplicate_validator
 | 
			
		||||
 | 
			
		||||
    # Reset CORE unique_ids for clean test
 | 
			
		||||
    CORE.unique_ids.clear()
 | 
			
		||||
 | 
			
		||||
    # Create validator for sensor platform
 | 
			
		||||
    validator = entity_duplicate_validator("sensor")
 | 
			
		||||
 | 
			
		||||
@@ -523,11 +528,6 @@ def test_entity_duplicate_validator() -> None:
 | 
			
		||||
 | 
			
		||||
def test_entity_duplicate_validator_with_devices() -> None:
 | 
			
		||||
    """Test entity_duplicate_validator with devices."""
 | 
			
		||||
    from esphome.core.entity_helpers import entity_duplicate_validator
 | 
			
		||||
 | 
			
		||||
    # Reset CORE unique_ids for clean test
 | 
			
		||||
    CORE.unique_ids.clear()
 | 
			
		||||
 | 
			
		||||
    # Create validator for sensor platform
 | 
			
		||||
    validator = entity_duplicate_validator("sensor")
 | 
			
		||||
 | 
			
		||||
@@ -605,3 +605,36 @@ def test_entity_different_platforms_yaml_validation(
 | 
			
		||||
    )
 | 
			
		||||
    # This should succeed
 | 
			
		||||
    assert result is not None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_entity_duplicate_validator_internal_entities() -> None:
 | 
			
		||||
    """Test that internal entities are excluded from duplicate name validation."""
 | 
			
		||||
    # Create validator for sensor platform
 | 
			
		||||
    validator = entity_duplicate_validator("sensor")
 | 
			
		||||
 | 
			
		||||
    # First entity should pass
 | 
			
		||||
    config1 = {CONF_NAME: "Temperature"}
 | 
			
		||||
    validated1 = validator(config1)
 | 
			
		||||
    assert validated1 == config1
 | 
			
		||||
    assert ("sensor", "temperature") in CORE.unique_ids
 | 
			
		||||
 | 
			
		||||
    # Internal entity with same name should pass (not added to unique_ids)
 | 
			
		||||
    config2 = {CONF_NAME: "Temperature", CONF_INTERNAL: True}
 | 
			
		||||
    validated2 = validator(config2)
 | 
			
		||||
    assert validated2 == config2
 | 
			
		||||
    # Internal entity should not be added to unique_ids
 | 
			
		||||
    assert len([k for k in CORE.unique_ids if k == ("sensor", "temperature")]) == 1
 | 
			
		||||
 | 
			
		||||
    # Another internal entity with same name should also pass
 | 
			
		||||
    config3 = {CONF_NAME: "Temperature", CONF_INTERNAL: True}
 | 
			
		||||
    validated3 = validator(config3)
 | 
			
		||||
    assert validated3 == config3
 | 
			
		||||
    # Still only one entry in unique_ids (from the non-internal entity)
 | 
			
		||||
    assert len([k for k in CORE.unique_ids if k == ("sensor", "temperature")]) == 1
 | 
			
		||||
 | 
			
		||||
    # Non-internal entity with same name should fail
 | 
			
		||||
    config4 = {CONF_NAME: "Temperature"}
 | 
			
		||||
    with pytest.raises(
 | 
			
		||||
        Invalid, match=r"Duplicate sensor entity with name 'Temperature' found"
 | 
			
		||||
    ):
 | 
			
		||||
        validator(config4)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user