From 04757766a4b0adc088bf5cf486a112cf83a439af Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 6 Jun 2025 05:20:09 +0100 Subject: [PATCH] cleanup --- esphome/components/api/api_connection.cpp | 314 ++++++++++------------ esphome/components/api/api_connection.h | 182 ++++++------- 2 files changed, 225 insertions(+), 271 deletions(-) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 7d50cfc02c..fd1734252e 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -254,24 +254,23 @@ void APIConnection::on_disconnect_response(const DisconnectResponse &value) { #ifdef USE_BINARY_SENSOR bool APIConnection::send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor, bool state) { - return this->schedule_message_(binary_sensor, - [state](EntityBase *entity, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc) -> APIConnection::EncodedMessage { - auto *bs = static_cast(entity); - BinarySensorStateResponse msg; // Stack allocated! - msg.state = state; - msg.missing_state = !bs->has_state(); - msg.key = bs->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); - }); + return this->schedule_message_( + binary_sensor, + [state](EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single) -> EncodedMessage { + auto *bs = static_cast(entity); + BinarySensorStateResponse msg; // Stack allocated! + msg.state = state; + msg.missing_state = !bs->has_state(); + msg.key = bs->get_object_id_hash(); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); + }); } void APIConnection::send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor) { this->schedule_message_(binary_sensor, &APIConnection::try_send_binary_sensor_info_, ListEntitiesBinarySensorResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_binary_sensor_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_binary_sensor_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *binary_sensor = static_cast(entity); ListEntitiesBinarySensorResponse msg; // Stack allocated! msg.device_class = binary_sensor->get_device_class(); @@ -281,7 +280,7 @@ APIConnection::EncodedMessage APIConnection::try_send_binary_sensor_info_(Entity // Fill common entity fields fill_entity_info_base_(binary_sensor, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } #endif @@ -292,9 +291,8 @@ bool APIConnection::send_cover_state(cover::Cover *cover) { void APIConnection::send_cover_info(cover::Cover *cover) { this->schedule_message_(cover, &APIConnection::try_send_cover_info_, ListEntitiesCoverResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_cover_state_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_cover_state_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *cover = static_cast(entity); CoverStateResponse msg; // Stack allocated! auto traits = cover->get_traits(); @@ -305,11 +303,10 @@ APIConnection::EncodedMessage APIConnection::try_send_cover_state_(EntityBase *e msg.tilt = cover->tilt; msg.current_operation = static_cast(cover->current_operation); msg.key = cover->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } -APIConnection::EncodedMessage APIConnection::try_send_cover_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_cover_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *cover = static_cast(entity); ListEntitiesCoverResponse msg; // Stack allocated! auto traits = cover->get_traits(); @@ -320,7 +317,7 @@ APIConnection::EncodedMessage APIConnection::try_send_cover_info_(EntityBase *en msg.device_class = cover->get_device_class(); msg.unique_id = get_default_unique_id("cover", cover); fill_entity_info_base_(cover, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::cover_command(const CoverCommandRequest &msg) { cover::Cover *cover = App.get_cover_by_key(msg.key); @@ -358,9 +355,8 @@ bool APIConnection::send_fan_state(fan::Fan *fan) { void APIConnection::send_fan_info(fan::Fan *fan) { this->schedule_message_(fan, &APIConnection::try_send_fan_info_, ListEntitiesFanResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_fan_state_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_fan_state_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *fan = static_cast(entity); FanStateResponse msg; // Stack allocated! auto traits = fan->get_traits(); @@ -375,11 +371,10 @@ APIConnection::EncodedMessage APIConnection::try_send_fan_state_(EntityBase *ent if (traits.supports_preset_modes()) msg.preset_mode = fan->preset_mode; msg.key = fan->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } -APIConnection::EncodedMessage APIConnection::try_send_fan_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_fan_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *fan = static_cast(entity); ListEntitiesFanResponse msg; // Stack allocated! auto traits = fan->get_traits(); @@ -391,7 +386,7 @@ APIConnection::EncodedMessage APIConnection::try_send_fan_info_(EntityBase *enti msg.supported_preset_modes.push_back(preset); msg.unique_id = get_default_unique_id("fan", fan); fill_entity_info_base_(fan, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::fan_command(const FanCommandRequest &msg) { fan::Fan *fan = App.get_fan_by_key(msg.key); @@ -422,9 +417,8 @@ bool APIConnection::send_light_state(light::LightState *light) { void APIConnection::send_light_info(light::LightState *light) { this->schedule_message_(light, &APIConnection::try_send_light_info_, ListEntitiesLightResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_light_state_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_light_state_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *light = static_cast(entity); LightStateResponse msg; // Stack allocated! auto traits = light->get_traits(); @@ -444,11 +438,10 @@ APIConnection::EncodedMessage APIConnection::try_send_light_state_(EntityBase *e if (light->supports_effects()) msg.effect = light->get_effect_name(); msg.key = light->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } -APIConnection::EncodedMessage APIConnection::try_send_light_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_light_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *light = static_cast(entity); ListEntitiesLightResponse msg; // Stack allocated! auto traits = light->get_traits(); @@ -473,7 +466,7 @@ APIConnection::EncodedMessage APIConnection::try_send_light_info_(EntityBase *en } msg.unique_id = get_default_unique_id("light", light); fill_entity_info_base_(light, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::light_command(const LightCommandRequest &msg) { light::LightState *light = App.get_light_by_key(msg.key); @@ -515,22 +508,21 @@ void APIConnection::light_command(const LightCommandRequest &msg) { #ifdef USE_SENSOR bool APIConnection::send_sensor_state(sensor::Sensor *sensor, float state) { return this->schedule_message_(sensor, - [state](EntityBase *entity, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc) -> APIConnection::EncodedMessage { + [state](EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single) -> APIConnection::EncodedMessage { auto *s = static_cast(entity); SensorStateResponse msg; // Stack allocated! msg.state = state; msg.missing_state = !s->has_state(); msg.key = s->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); }); } void APIConnection::send_sensor_info(sensor::Sensor *sensor) { this->schedule_message_(sensor, &APIConnection::try_send_sensor_info_, ListEntitiesSensorResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_sensor_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_sensor_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *sensor = static_cast(entity); ListEntitiesSensorResponse msg; // Stack allocated! msg.unit_of_measurement = sensor->get_unit_of_measurement(); @@ -542,35 +534,34 @@ APIConnection::EncodedMessage APIConnection::try_send_sensor_info_(EntityBase *e if (msg.unique_id.empty()) msg.unique_id = get_default_unique_id("sensor", sensor); fill_entity_info_base_(sensor, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } #endif #ifdef USE_SWITCH bool APIConnection::send_switch_state(switch_::Switch *a_switch, bool state) { return this->schedule_message_(a_switch, - [state](EntityBase *entity, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc) -> APIConnection::EncodedMessage { + [state](EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single) -> APIConnection::EncodedMessage { auto *sw = static_cast(entity); SwitchStateResponse msg; // Stack allocated! msg.state = state; msg.key = sw->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); }); } void APIConnection::send_switch_info(switch_::Switch *a_switch) { this->schedule_message_(a_switch, &APIConnection::try_send_switch_info_, ListEntitiesSwitchResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_switch_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_switch_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *a_switch = static_cast(entity); ListEntitiesSwitchResponse msg; // Stack allocated! msg.assumed_state = a_switch->assumed_state(); msg.device_class = a_switch->get_device_class(); msg.unique_id = get_default_unique_id("switch", a_switch); fill_entity_info_base_(a_switch, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::switch_command(const SwitchCommandRequest &msg) { switch_::Switch *a_switch = App.get_switch_by_key(msg.key); @@ -589,23 +580,22 @@ void APIConnection::switch_command(const SwitchCommandRequest &msg) { bool APIConnection::send_text_sensor_state(text_sensor::TextSensor *text_sensor, std::string state) { return this->schedule_message_( text_sensor, - [state = std::move(state)](EntityBase *entity, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc) -> APIConnection::EncodedMessage { + [state = std::move(state)](EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single) -> APIConnection::EncodedMessage { auto *ts = static_cast(entity); TextSensorStateResponse msg; // Stack allocated! msg.state = state; msg.missing_state = !ts->has_state(); msg.key = ts->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); }); } void APIConnection::send_text_sensor_info(text_sensor::TextSensor *text_sensor) { this->schedule_message_(text_sensor, &APIConnection::try_send_text_sensor_info_, ListEntitiesTextSensorResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_text_sensor_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_text_sensor_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *text_sensor = static_cast(entity); ListEntitiesTextSensorResponse msg; // Stack allocated! msg.device_class = text_sensor->get_device_class(); @@ -616,7 +606,7 @@ APIConnection::EncodedMessage APIConnection::try_send_text_sensor_info_(EntityBa // Fill common entity fields fill_entity_info_base_(text_sensor, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } #endif @@ -624,9 +614,8 @@ APIConnection::EncodedMessage APIConnection::try_send_text_sensor_info_(EntityBa bool APIConnection::send_climate_state(climate::Climate *climate) { return this->schedule_message_(climate, &APIConnection::try_send_climate_state_); } -APIConnection::EncodedMessage APIConnection::try_send_climate_state_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_climate_state_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *climate = static_cast(entity); ClimateStateResponse msg; // Stack allocated! msg.key = climate->get_object_id_hash(); @@ -656,14 +645,13 @@ APIConnection::EncodedMessage APIConnection::try_send_climate_state_(EntityBase msg.current_humidity = climate->current_humidity; if (traits.get_supports_target_humidity()) msg.target_humidity = climate->target_humidity; - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::send_climate_info(climate::Climate *climate) { this->schedule_message_(climate, &APIConnection::try_send_climate_info_, ListEntitiesClimateResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_climate_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_climate_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *climate = static_cast(entity); ListEntitiesClimateResponse msg; // Stack allocated! auto traits = climate->get_traits(); @@ -696,7 +684,7 @@ APIConnection::EncodedMessage APIConnection::try_send_climate_info_(EntityBase * // Fill common entity fields fill_entity_info_base_(climate, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::climate_command(const ClimateCommandRequest &msg) { climate::Climate *climate = App.get_climate_by_key(msg.key); @@ -731,22 +719,21 @@ void APIConnection::climate_command(const ClimateCommandRequest &msg) { #ifdef USE_NUMBER bool APIConnection::send_number_state(number::Number *number, float state) { return this->schedule_message_(number, - [state](EntityBase *entity, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc) -> APIConnection::EncodedMessage { + [state](EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single) -> APIConnection::EncodedMessage { auto *n = static_cast(entity); NumberStateResponse msg; // Stack allocated! msg.state = state; msg.missing_state = !n->has_state(); msg.key = n->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); }); } void APIConnection::send_number_info(number::Number *number) { this->schedule_message_(number, &APIConnection::try_send_number_info_, ListEntitiesNumberResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_number_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_number_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *number = static_cast(entity); ListEntitiesNumberResponse msg; // Stack allocated! msg.unit_of_measurement = number->traits.get_unit_of_measurement(); @@ -760,7 +747,7 @@ APIConnection::EncodedMessage APIConnection::try_send_number_info_(EntityBase *e // Fill common entity fields fill_entity_info_base_(number, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::number_command(const NumberCommandRequest &msg) { number::Number *number = App.get_number_by_key(msg.key); @@ -777,9 +764,8 @@ void APIConnection::number_command(const NumberCommandRequest &msg) { bool APIConnection::send_date_state(datetime::DateEntity *date) { return this->schedule_message_(date, &APIConnection::try_send_date_state_); } -APIConnection::EncodedMessage APIConnection::try_send_date_state_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_date_state_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *date = static_cast(entity); DateStateResponse msg; // Stack allocated! msg.missing_state = !date->has_state(); @@ -787,14 +773,13 @@ APIConnection::EncodedMessage APIConnection::try_send_date_state_(EntityBase *en msg.month = date->month; msg.day = date->day; msg.key = date->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::send_date_info(datetime::DateEntity *date) { this->schedule_message_(date, &APIConnection::try_send_date_info_, ListEntitiesDateResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_date_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_date_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *date = static_cast(entity); ListEntitiesDateResponse msg; // Stack allocated! msg.unique_id = get_default_unique_id("date", date); @@ -802,7 +787,7 @@ APIConnection::EncodedMessage APIConnection::try_send_date_info_(EntityBase *ent // Fill common entity fields fill_entity_info_base_(date, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::date_command(const DateCommandRequest &msg) { datetime::DateEntity *date = App.get_date_by_key(msg.key); @@ -819,9 +804,8 @@ void APIConnection::date_command(const DateCommandRequest &msg) { bool APIConnection::send_time_state(datetime::TimeEntity *time) { return this->schedule_message_(time, &APIConnection::try_send_time_state_); } -APIConnection::EncodedMessage APIConnection::try_send_time_state_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_time_state_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *time = static_cast(entity); TimeStateResponse msg; // Stack allocated! msg.missing_state = !time->has_state(); @@ -829,14 +813,13 @@ APIConnection::EncodedMessage APIConnection::try_send_time_state_(EntityBase *en msg.minute = time->minute; msg.second = time->second; msg.key = time->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::send_time_info(datetime::TimeEntity *time) { this->schedule_message_(time, &APIConnection::try_send_time_info_, ListEntitiesTimeResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_time_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_time_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *time = static_cast(entity); ListEntitiesTimeResponse msg; // Stack allocated! msg.unique_id = get_default_unique_id("time", time); @@ -844,7 +827,7 @@ APIConnection::EncodedMessage APIConnection::try_send_time_info_(EntityBase *ent // Fill common entity fields fill_entity_info_base_(time, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::time_command(const TimeCommandRequest &msg) { datetime::TimeEntity *time = App.get_time_by_key(msg.key); @@ -861,9 +844,8 @@ void APIConnection::time_command(const TimeCommandRequest &msg) { bool APIConnection::send_datetime_state(datetime::DateTimeEntity *datetime) { return this->schedule_message_(datetime, &APIConnection::try_send_datetime_state_); } -APIConnection::EncodedMessage APIConnection::try_send_datetime_state_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_datetime_state_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *datetime = static_cast(entity); DateTimeStateResponse msg; // Stack allocated! msg.missing_state = !datetime->has_state(); @@ -872,15 +854,14 @@ APIConnection::EncodedMessage APIConnection::try_send_datetime_state_(EntityBase msg.epoch_seconds = state.timestamp; } msg.key = datetime->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::send_datetime_info(datetime::DateTimeEntity *datetime) { this->schedule_message_(datetime, &APIConnection::try_send_datetime_info_, ListEntitiesDateTimeResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_datetime_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_datetime_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *datetime = static_cast(entity); ListEntitiesDateTimeResponse msg; // Stack allocated! msg.unique_id = get_default_unique_id("datetime", datetime); @@ -888,7 +869,7 @@ APIConnection::EncodedMessage APIConnection::try_send_datetime_info_(EntityBase // Fill common entity fields fill_entity_info_base_(datetime, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::datetime_command(const DateTimeCommandRequest &msg) { datetime::DateTimeEntity *datetime = App.get_datetime_by_key(msg.key); @@ -905,22 +886,21 @@ void APIConnection::datetime_command(const DateTimeCommandRequest &msg) { bool APIConnection::send_text_state(text::Text *text, std::string state) { return this->schedule_message_( text, - [state = std::move(state)](EntityBase *entity, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc) -> APIConnection::EncodedMessage { + [state = std::move(state)](EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single) -> APIConnection::EncodedMessage { auto *t = static_cast(entity); TextStateResponse msg; // Stack allocated! msg.state = state; msg.missing_state = !t->has_state(); msg.key = t->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); }); } void APIConnection::send_text_info(text::Text *text) { this->schedule_message_(text, &APIConnection::try_send_text_info_, ListEntitiesTextResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_text_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_text_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *text = static_cast(entity); ListEntitiesTextResponse msg; // Stack allocated! msg.mode = static_cast(text->traits.get_mode()); @@ -932,7 +912,7 @@ APIConnection::EncodedMessage APIConnection::try_send_text_info_(EntityBase *ent // Fill common entity fields fill_entity_info_base_(text, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::text_command(const TextCommandRequest &msg) { text::Text *text = App.get_text_by_key(msg.key); @@ -949,22 +929,21 @@ void APIConnection::text_command(const TextCommandRequest &msg) { bool APIConnection::send_select_state(select::Select *select, std::string state) { return this->schedule_message_( select, - [state = std::move(state)](EntityBase *entity, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc) -> APIConnection::EncodedMessage { + [state = std::move(state)](EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single) -> APIConnection::EncodedMessage { auto *s = static_cast(entity); SelectStateResponse msg; // Stack allocated! msg.state = state; msg.missing_state = !s->has_state(); msg.key = s->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); }); } void APIConnection::send_select_info(select::Select *select) { this->schedule_message_(select, &APIConnection::try_send_select_info_, ListEntitiesSelectResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_select_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_select_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *select = static_cast(entity); ListEntitiesSelectResponse msg; // Stack allocated! for (const auto &option : select->traits.get_options()) @@ -974,7 +953,7 @@ APIConnection::EncodedMessage APIConnection::try_send_select_info_(EntityBase *e // Fill common entity fields fill_entity_info_base_(select, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::select_command(const SelectCommandRequest &msg) { select::Select *select = App.get_select_by_key(msg.key); @@ -991,9 +970,8 @@ void APIConnection::select_command(const SelectCommandRequest &msg) { void esphome::api::APIConnection::send_button_info(button::Button *button) { this->schedule_message_(button, &APIConnection::try_send_button_info_, ListEntitiesButtonResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_button_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_button_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *button = static_cast(entity); ListEntitiesButtonResponse msg; // Stack allocated! msg.device_class = button->get_device_class(); @@ -1002,7 +980,7 @@ APIConnection::EncodedMessage APIConnection::try_send_button_info_(EntityBase *e // Fill common entity fields fill_entity_info_base_(button, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void esphome::api::APIConnection::button_command(const ButtonCommandRequest &msg) { button::Button *button = App.get_button_by_key(msg.key); @@ -1016,21 +994,20 @@ void esphome::api::APIConnection::button_command(const ButtonCommandRequest &msg #ifdef USE_LOCK bool APIConnection::send_lock_state(lock::Lock *a_lock, lock::LockState state) { return this->schedule_message_(a_lock, - [state](EntityBase *entity, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc) -> APIConnection::EncodedMessage { + [state](EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single) -> APIConnection::EncodedMessage { auto *l = static_cast(entity); LockStateResponse msg; // Stack allocated! msg.state = static_cast(state); msg.key = l->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); }); } void APIConnection::send_lock_info(lock::Lock *a_lock) { this->schedule_message_(a_lock, &APIConnection::try_send_lock_info_, ListEntitiesLockResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_lock_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_lock_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *a_lock = static_cast(entity); ListEntitiesLockResponse msg; // Stack allocated! msg.assumed_state = a_lock->traits.get_assumed_state(); @@ -1041,7 +1018,7 @@ APIConnection::EncodedMessage APIConnection::try_send_lock_info_(EntityBase *ent // Fill common entity fields fill_entity_info_base_(a_lock, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::lock_command(const LockCommandRequest &msg) { lock::Lock *a_lock = App.get_lock_by_key(msg.key); @@ -1066,22 +1043,20 @@ void APIConnection::lock_command(const LockCommandRequest &msg) { bool APIConnection::send_valve_state(valve::Valve *valve) { return this->schedule_message_(valve, &APIConnection::try_send_valve_state_); } -APIConnection::EncodedMessage APIConnection::try_send_valve_state_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_valve_state_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *valve = static_cast(entity); ValveStateResponse msg; // Stack allocated! msg.position = valve->position; msg.current_operation = static_cast(valve->current_operation); msg.key = valve->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::send_valve_info(valve::Valve *valve) { this->schedule_message_(valve, &APIConnection::try_send_valve_info_, ListEntitiesValveResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_valve_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_valve_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *valve = static_cast(entity); ListEntitiesValveResponse msg; // Stack allocated! auto traits = valve->get_traits(); @@ -1094,7 +1069,7 @@ APIConnection::EncodedMessage APIConnection::try_send_valve_info_(EntityBase *en // Fill common entity fields fill_entity_info_base_(valve, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::valve_command(const ValveCommandRequest &msg) { valve::Valve *valve = App.get_valve_by_key(msg.key); @@ -1114,9 +1089,8 @@ void APIConnection::valve_command(const ValveCommandRequest &msg) { bool APIConnection::send_media_player_state(media_player::MediaPlayer *media_player) { return this->schedule_message_(media_player, &APIConnection::try_send_media_player_state_); } -APIConnection::EncodedMessage APIConnection::try_send_media_player_state_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_media_player_state_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *media_player = static_cast(entity); MediaPlayerStateResponse msg; // Stack allocated! media_player::MediaPlayerState report_state = media_player->state == media_player::MEDIA_PLAYER_STATE_ANNOUNCING @@ -1126,15 +1100,14 @@ APIConnection::EncodedMessage APIConnection::try_send_media_player_state_(Entity msg.volume = media_player->volume; msg.muted = media_player->is_muted(); msg.key = media_player->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::send_media_player_info(media_player::MediaPlayer *media_player) { this->schedule_message_(media_player, &APIConnection::try_send_media_player_info_, ListEntitiesMediaPlayerResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_media_player_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_media_player_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *media_player = static_cast(entity); ListEntitiesMediaPlayerResponse msg; // Stack allocated! auto traits = media_player->get_traits(); @@ -1153,7 +1126,7 @@ APIConnection::EncodedMessage APIConnection::try_send_media_player_info_(EntityB // Fill common entity fields fill_entity_info_base_(media_player, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::media_player_command(const MediaPlayerCommandRequest &msg) { media_player::MediaPlayer *media_player = App.get_media_player_by_key(msg.key); @@ -1190,9 +1163,8 @@ void APIConnection::set_camera_state(std::shared_ptr void APIConnection::send_camera_info(esp32_camera::ESP32Camera *camera) { this->schedule_message_(camera, &APIConnection::try_send_camera_info_, ListEntitiesCameraResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_camera_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_camera_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *camera = static_cast(entity); ListEntitiesCameraResponse msg; // Stack allocated! msg.unique_id = get_default_unique_id("camera", camera); @@ -1200,7 +1172,7 @@ APIConnection::EncodedMessage APIConnection::try_send_camera_info_(EntityBase *e // Fill common entity fields fill_entity_info_base_(camera, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::camera_image(const CameraImageRequest &msg) { if (esp32_camera::global_esp32_camera == nullptr) @@ -1392,23 +1364,22 @@ bool APIConnection::send_alarm_control_panel_state(alarm_control_panel::AlarmCon return this->schedule_message_(a_alarm_control_panel, &APIConnection::try_send_alarm_control_panel_state_); } APIConnection::EncodedMessage APIConnection::try_send_alarm_control_panel_state_(EntityBase *entity, - BufferAllocator allocator, + APIConnection *conn, uint32_t remaining_size, - OverheadCalculator overhead_calc) { + bool is_single) { auto *a_alarm_control_panel = static_cast(entity); AlarmControlPanelStateResponse msg; // Stack allocated! msg.state = static_cast(a_alarm_control_panel->get_state()); msg.key = a_alarm_control_panel->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::send_alarm_control_panel_info(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) { this->schedule_message_(a_alarm_control_panel, &APIConnection::try_send_alarm_control_panel_info_, ListEntitiesAlarmControlPanelResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_alarm_control_panel_info_(EntityBase *entity, - BufferAllocator allocator, +APIConnection::EncodedMessage APIConnection::try_send_alarm_control_panel_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, - OverheadCalculator overhead_calc) { + bool is_single) { auto *a_alarm_control_panel = static_cast(entity); ListEntitiesAlarmControlPanelResponse msg; // Stack allocated! msg.supported_features = a_alarm_control_panel->get_supported_features(); @@ -1419,7 +1390,7 @@ APIConnection::EncodedMessage APIConnection::try_send_alarm_control_panel_info_( // Fill common entity fields fill_entity_info_base_(a_alarm_control_panel, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::alarm_control_panel_command(const AlarmControlPanelCommandRequest &msg) { alarm_control_panel::AlarmControlPanel *a_alarm_control_panel = App.get_alarm_control_panel_by_key(msg.key); @@ -1459,21 +1430,20 @@ void APIConnection::alarm_control_panel_command(const AlarmControlPanelCommandRe void APIConnection::send_event(event::Event *event, std::string event_type) { this->schedule_message_( event, - [event_type = std::move(event_type)](EntityBase *entity, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc) -> APIConnection::EncodedMessage { + [event_type = std::move(event_type)](EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single) -> APIConnection::EncodedMessage { auto *e = static_cast(entity); EventResponse msg; // Stack allocated! msg.event_type = event_type; msg.key = e->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); }); } void APIConnection::send_event_info(event::Event *event) { this->schedule_message_(event, &APIConnection::try_send_event_info_, ListEntitiesEventResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_event_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_event_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *event = static_cast(entity); ListEntitiesEventResponse msg; // Stack allocated! msg.device_class = event->get_device_class(); @@ -1484,7 +1454,7 @@ APIConnection::EncodedMessage APIConnection::try_send_event_info_(EntityBase *en // Fill common entity fields fill_entity_info_base_(event, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } #endif @@ -1492,9 +1462,8 @@ APIConnection::EncodedMessage APIConnection::try_send_event_info_(EntityBase *en bool APIConnection::send_update_state(update::UpdateEntity *update) { return this->schedule_message_(update, &APIConnection::try_send_update_state_); } -APIConnection::EncodedMessage APIConnection::try_send_update_state_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_update_state_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *update = static_cast(entity); UpdateStateResponse msg; // Stack allocated! msg.missing_state = !update->has_state(); @@ -1511,14 +1480,13 @@ APIConnection::EncodedMessage APIConnection::try_send_update_state_(EntityBase * msg.release_url = update->update_info.release_url; } msg.key = update->get_object_id_hash(); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::send_update_info(update::UpdateEntity *update) { this->schedule_message_(update, &APIConnection::try_send_update_info_, ListEntitiesUpdateResponse::message_type); } -APIConnection::EncodedMessage APIConnection::try_send_update_info_(EntityBase *entity, BufferAllocator allocator, - uint32_t remaining_size, - OverheadCalculator overhead_calc) { +APIConnection::EncodedMessage APIConnection::try_send_update_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single) { auto *update = static_cast(entity); ListEntitiesUpdateResponse msg; // Stack allocated! msg.device_class = update->get_device_class(); @@ -1527,7 +1495,7 @@ APIConnection::EncodedMessage APIConnection::try_send_update_info_(EntityBase *e // Fill common entity fields fill_entity_info_base_(update, msg); - return encode_message_to_buffer(msg, allocator, remaining_size, overhead_calc); + return encode_message_to_buffer(msg, conn, remaining_size, is_single); } void APIConnection::update_command(const UpdateCommandRequest &msg) { update::UpdateEntity *update = App.get_update_by_key(msg.key); @@ -1790,19 +1758,14 @@ void APIConnection::process_batch_() { return; } - // Create overhead calculator once for the entire batch - OverheadCalculator overhead_calc = std::bind(&APIFrameHelper::calculate_packet_overhead, this->helper_.get(), - std::placeholders::_1, std::placeholders::_2); - size_t num_items = this->deferred_batch_.items.size(); // Fast path for single message - allocate exact size needed if (num_items == 1) { const auto &item = this->deferred_batch_.items[0]; - BufferAllocator allocator = std::bind(&APIConnection::allocate_single_message_buffer, this, std::placeholders::_1); // Let the creator calculate size and encode if it fits - EncodedMessage msg = item.creator(item.entity, allocator, std::numeric_limits::max(), overhead_calc); + EncodedMessage msg = item.creator(item.entity, this, std::numeric_limits::max(), true); if (msg && this->send_buffer(ProtoWriteBuffer{&this->proto_write_buffer_}, item.message_type)) { this->deferred_batch_.clear(); @@ -1832,17 +1795,14 @@ void APIConnection::process_batch_() { const uint8_t header_padding = this->helper_->frame_header_padding(); const uint8_t footer_size = this->helper_->frame_footer_size(); - // Create batch allocator - BufferAllocator allocator = std::bind(&APIConnection::allocate_batch_message_buffer, this, std::placeholders::_1); - // Track current position in buffer as we build it uint32_t current_offset = header_padding; // Process items and encode directly to buffer for (const auto &item : this->deferred_batch_.items) { - // Try to encode message with allocator and overhead calculator - // The creator will use the overhead calculator to determine if the message fits - EncodedMessage msg = item.creator(item.entity, allocator, remaining_size, overhead_calc); + // Try to encode message + // The creator will calculate overhead to determine if the message fits + EncodedMessage msg = item.creator(item.entity, this, remaining_size, false); if (!msg) { // Message won't fit, stop processing diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index 2ed8f86719..e403cb862f 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -327,12 +327,6 @@ class APIConnection : public APIServerConnection { ProtoWriteBuffer allocate_single_message_buffer(uint32_t size); ProtoWriteBuffer allocate_batch_message_buffer(uint32_t size); - // Function to allocate buffer space for a message - using BufferAllocator = std::function; - - // Function to calculate packet overhead for a given message type and size - using OverheadCalculator = std::function; - // Result of message encoding struct EncodedMessage { uint16_t payload_size; // Size of the message payload only @@ -360,14 +354,14 @@ class APIConnection : public APIServerConnection { // Helper function to encode a message to buffer template - static EncodedMessage encode_message_to_buffer(MessageT &msg, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc) { + static EncodedMessage encode_message_to_buffer(MessageT &msg, APIConnection *conn, uint32_t remaining_size, + bool is_single) { // Calculate size uint32_t size = 0; msg.calculate_size(size); // Calculate overhead for this message - uint16_t overhead = overhead_calc(MessageT::message_type, static_cast(size)); + uint16_t overhead = conn->helper_->calculate_packet_overhead(MessageT::message_type, static_cast(size)); uint32_t total_size = size + overhead; // Check if it fits @@ -376,138 +370,138 @@ class APIConnection : public APIServerConnection { } // Allocate exact buffer space needed (just the payload, not the overhead) - ProtoWriteBuffer buffer = allocator(size); + ProtoWriteBuffer buffer = + is_single ? conn->allocate_single_message_buffer(size) : conn->allocate_batch_message_buffer(size); // Encode directly into buffer msg.encode(buffer); return {static_cast(size), static_cast(total_size)}; } #ifdef USE_BINARY_SENSOR - static EncodedMessage try_send_binary_sensor_state_(EntityBase *binary_sensor, BufferAllocator allocator, - uint32_t remaining_size, OverheadCalculator overhead_calc); - static EncodedMessage try_send_binary_sensor_info_(EntityBase *binary_sensor, BufferAllocator allocator, - uint32_t remaining_size, OverheadCalculator overhead_calc); + static EncodedMessage try_send_binary_sensor_state_(EntityBase *binary_sensor, APIConnection *conn, + uint32_t remaining_size, bool is_single); + static EncodedMessage try_send_binary_sensor_info_(EntityBase *binary_sensor, APIConnection *conn, + uint32_t remaining_size, bool is_single); #endif #ifdef USE_COVER - static EncodedMessage try_send_cover_state_(EntityBase *cover, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); - static EncodedMessage try_send_cover_info_(EntityBase *cover, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_cover_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_cover_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_FAN - static EncodedMessage try_send_fan_state_(EntityBase *fan, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); - static EncodedMessage try_send_fan_info_(EntityBase *fan, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_fan_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_fan_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_LIGHT - static EncodedMessage try_send_light_state_(EntityBase *light, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); - static EncodedMessage try_send_light_info_(EntityBase *light, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_light_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_light_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_SENSOR - static EncodedMessage try_send_sensor_state_(EntityBase *sensor, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); - static EncodedMessage try_send_sensor_info_(EntityBase *sensor, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_sensor_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_sensor_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_SWITCH - static EncodedMessage try_send_switch_state_(EntityBase *a_switch, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); - static EncodedMessage try_send_switch_info_(EntityBase *a_switch, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_switch_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_switch_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_TEXT_SENSOR - static EncodedMessage try_send_text_sensor_state_(EntityBase *text_sensor, BufferAllocator allocator, - uint32_t remaining_size, OverheadCalculator overhead_calc); - static EncodedMessage try_send_text_sensor_info_(EntityBase *text_sensor, BufferAllocator allocator, - uint32_t remaining_size, OverheadCalculator overhead_calc); + static EncodedMessage try_send_text_sensor_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_text_sensor_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_CLIMATE - static EncodedMessage try_send_climate_state_(EntityBase *climate, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); - static EncodedMessage try_send_climate_info_(EntityBase *climate, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_climate_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_climate_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_NUMBER - static EncodedMessage try_send_number_state_(EntityBase *number, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); - static EncodedMessage try_send_number_info_(EntityBase *number, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_number_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_number_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_DATETIME_DATE - static EncodedMessage try_send_date_state_(EntityBase *date, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); - static EncodedMessage try_send_date_info_(EntityBase *date, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_date_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_date_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_DATETIME_TIME - static EncodedMessage try_send_time_state_(EntityBase *time, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); - static EncodedMessage try_send_time_info_(EntityBase *time, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_time_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_time_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_DATETIME_DATETIME - static EncodedMessage try_send_datetime_state_(EntityBase *datetime, BufferAllocator allocator, - uint32_t remaining_size, OverheadCalculator overhead_calc); - static EncodedMessage try_send_datetime_info_(EntityBase *datetime, BufferAllocator allocator, - uint32_t remaining_size, OverheadCalculator overhead_calc); + static EncodedMessage try_send_datetime_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_datetime_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_TEXT - static EncodedMessage try_send_text_state_(EntityBase *text, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); - static EncodedMessage try_send_text_info_(EntityBase *text, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_text_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_text_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_SELECT - static EncodedMessage try_send_select_state_(EntityBase *select, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); - static EncodedMessage try_send_select_info_(EntityBase *select, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_select_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_select_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_BUTTON - static EncodedMessage try_send_button_info_(EntityBase *button, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_button_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_LOCK - static EncodedMessage try_send_lock_state_(EntityBase *a_lock, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); - static EncodedMessage try_send_lock_info_(EntityBase *a_lock, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_lock_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_lock_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_VALVE - static EncodedMessage try_send_valve_state_(EntityBase *valve, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); - static EncodedMessage try_send_valve_info_(EntityBase *valve, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_valve_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_valve_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_MEDIA_PLAYER - static EncodedMessage try_send_media_player_state_(EntityBase *media_player, BufferAllocator allocator, - uint32_t remaining_size, OverheadCalculator overhead_calc); - static EncodedMessage try_send_media_player_info_(EntityBase *media_player, BufferAllocator allocator, - uint32_t remaining_size, OverheadCalculator overhead_calc); + static EncodedMessage try_send_media_player_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_media_player_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_ALARM_CONTROL_PANEL - static EncodedMessage try_send_alarm_control_panel_state_(EntityBase *a_alarm_control_panel, - BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); - static EncodedMessage try_send_alarm_control_panel_info_(EntityBase *a_alarm_control_panel, BufferAllocator allocator, - uint32_t remaining_size, OverheadCalculator overhead_calc); + static EncodedMessage try_send_alarm_control_panel_state_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single); + static EncodedMessage try_send_alarm_control_panel_info_(EntityBase *entity, APIConnection *conn, + uint32_t remaining_size, bool is_single); #endif #ifdef USE_EVENT - static EncodedMessage try_send_event_info_(EntityBase *event, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_event_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_UPDATE - static EncodedMessage try_send_update_state_(EntityBase *update, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); - static EncodedMessage try_send_update_info_(EntityBase *update, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_update_state_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); + static EncodedMessage try_send_update_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif #ifdef USE_ESP32_CAMERA - static EncodedMessage try_send_camera_info_(EntityBase *camera, BufferAllocator allocator, uint32_t remaining_size, - OverheadCalculator overhead_calc); + static EncodedMessage try_send_camera_info_(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, + bool is_single); #endif enum class ConnectionState { @@ -548,7 +542,7 @@ class APIConnection : public APIServerConnection { // Function type that encodes a message directly to buffer // Returns EncodedMessage with sizes if successful, {0, 0} if it doesn't fit using MessageCreator = - std::function; + std::function; // Generic batching mechanism for both state updates and entity info struct DeferredBatch {