diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 317dd91720..308d9f405d 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -252,29 +252,32 @@ 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) -> std::unique_ptr { - auto *bs = static_cast(entity); - auto msg = std::make_unique(); - msg->state = state; - msg->missing_state = !bs->has_state(); - msg->key = bs->get_object_id_hash(); - return msg; - }); + return this->schedule_message_( + binary_sensor, + [state](EntityBase *entity, ProtoWriteBuffer &buffer, uint32_t max_size) -> APIConnection::MessageInfo { + 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 APIConnection::encode_message_to_buffer(msg, buffer, max_size); + }); } void APIConnection::send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor) { this->schedule_message_(binary_sensor, &APIConnection::try_send_binary_sensor_info_); } -std::unique_ptr APIConnection::try_send_binary_sensor_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_binary_sensor_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *binary_sensor = static_cast(entity); - auto msg = std::make_unique(); - msg->device_class = binary_sensor->get_device_class(); - msg->is_status_binary_sensor = binary_sensor->is_status_binary_sensor(); - msg->unique_id = get_default_unique_id("binary_sensor", binary_sensor); + ListEntitiesBinarySensorResponse msg; // Stack allocated! + msg.device_class = binary_sensor->get_device_class(); + msg.is_status_binary_sensor = binary_sensor->is_status_binary_sensor(); + msg.unique_id = get_default_unique_id("binary_sensor", binary_sensor); // Fill common entity fields - fill_entity_info_base_(binary_sensor, *msg); + fill_entity_info_base_(binary_sensor, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } #endif @@ -285,31 +288,33 @@ 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_); } -std::unique_ptr APIConnection::try_send_cover_state_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_cover_state_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *cover = static_cast(entity); - auto msg = std::make_unique(); + CoverStateResponse msg; // Stack allocated! auto traits = cover->get_traits(); - msg->legacy_state = + msg.legacy_state = (cover->position == cover::COVER_OPEN) ? enums::LEGACY_COVER_STATE_OPEN : enums::LEGACY_COVER_STATE_CLOSED; - msg->position = cover->position; + msg.position = cover->position; if (traits.get_supports_tilt()) - msg->tilt = cover->tilt; - msg->current_operation = static_cast(cover->current_operation); - msg->key = cover->get_object_id_hash(); - return msg; + 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, buffer, max_size); } -std::unique_ptr APIConnection::try_send_cover_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_cover_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *cover = static_cast(entity); - auto msg = std::make_unique(); + ListEntitiesCoverResponse msg; // Stack allocated! auto traits = cover->get_traits(); - msg->assumed_state = traits.get_is_assumed_state(); - msg->supports_position = traits.get_supports_position(); - msg->supports_tilt = traits.get_supports_tilt(); - msg->supports_stop = traits.get_supports_stop(); - msg->device_class = cover->get_device_class(); - msg->unique_id = get_default_unique_id("cover", cover); - fill_entity_info_base_(cover, *msg); - return msg; + msg.assumed_state = traits.get_is_assumed_state(); + msg.supports_position = traits.get_supports_position(); + msg.supports_tilt = traits.get_supports_tilt(); + msg.supports_stop = traits.get_supports_stop(); + 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, buffer, max_size); } void APIConnection::cover_command(const CoverCommandRequest &msg) { cover::Cover *cover = App.get_cover_by_key(msg.key); @@ -345,36 +350,38 @@ bool APIConnection::send_fan_state(fan::Fan *fan) { return this->schedule_message_(fan, &APIConnection::try_send_fan_state_); } void APIConnection::send_fan_info(fan::Fan *fan) { this->schedule_message_(fan, &APIConnection::try_send_fan_info_); } -std::unique_ptr APIConnection::try_send_fan_state_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_fan_state_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *fan = static_cast(entity); - auto msg = std::make_unique(); + FanStateResponse msg; // Stack allocated! auto traits = fan->get_traits(); - msg->state = fan->state; + msg.state = fan->state; if (traits.supports_oscillation()) - msg->oscillating = fan->oscillating; + msg.oscillating = fan->oscillating; if (traits.supports_speed()) { - msg->speed_level = fan->speed; + msg.speed_level = fan->speed; } if (traits.supports_direction()) - msg->direction = static_cast(fan->direction); + msg.direction = static_cast(fan->direction); if (traits.supports_preset_modes()) - msg->preset_mode = fan->preset_mode; - msg->key = fan->get_object_id_hash(); - return msg; + msg.preset_mode = fan->preset_mode; + msg.key = fan->get_object_id_hash(); + return encode_message_to_buffer(msg, buffer, max_size); } -std::unique_ptr APIConnection::try_send_fan_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_fan_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *fan = static_cast(entity); - auto msg = std::make_unique(); + ListEntitiesFanResponse msg; // Stack allocated! auto traits = fan->get_traits(); - msg->supports_oscillation = traits.supports_oscillation(); - msg->supports_speed = traits.supports_speed(); - msg->supports_direction = traits.supports_direction(); - msg->supported_speed_count = traits.supported_speed_count(); + msg.supports_oscillation = traits.supports_oscillation(); + msg.supports_speed = traits.supports_speed(); + msg.supports_direction = traits.supports_direction(); + msg.supported_speed_count = traits.supported_speed_count(); for (auto const &preset : traits.supported_preset_modes()) - msg->supported_preset_modes.push_back(preset); - msg->unique_id = get_default_unique_id("fan", fan); - fill_entity_info_base_(fan, *msg); - return msg; + 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, buffer, max_size); } void APIConnection::fan_command(const FanCommandRequest &msg) { fan::Fan *fan = App.get_fan_by_key(msg.key); @@ -405,55 +412,56 @@ 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_); } -std::unique_ptr APIConnection::try_send_light_state_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_light_state_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *light = static_cast(entity); - auto msg = std::make_unique(); + LightStateResponse msg; // Stack allocated! auto traits = light->get_traits(); auto values = light->remote_values; auto color_mode = values.get_color_mode(); - msg->state = values.is_on(); - msg->color_mode = static_cast(color_mode); - msg->brightness = values.get_brightness(); - msg->color_brightness = values.get_color_brightness(); - msg->red = values.get_red(); - msg->green = values.get_green(); - msg->blue = values.get_blue(); - msg->white = values.get_white(); - msg->color_temperature = values.get_color_temperature(); - msg->cold_white = values.get_cold_white(); - msg->warm_white = values.get_warm_white(); + msg.state = values.is_on(); + msg.color_mode = static_cast(color_mode); + msg.brightness = values.get_brightness(); + msg.color_brightness = values.get_color_brightness(); + msg.red = values.get_red(); + msg.green = values.get_green(); + msg.blue = values.get_blue(); + msg.white = values.get_white(); + msg.color_temperature = values.get_color_temperature(); + msg.cold_white = values.get_cold_white(); + msg.warm_white = values.get_warm_white(); if (light->supports_effects()) - msg->effect = light->get_effect_name(); - msg->key = light->get_object_id_hash(); - return msg; + msg.effect = light->get_effect_name(); + msg.key = light->get_object_id_hash(); + return encode_message_to_buffer(msg, buffer, max_size); } -std::unique_ptr APIConnection::try_send_light_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_light_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *light = static_cast(entity); - auto msg = std::make_unique(); + ListEntitiesLightResponse msg; // Stack allocated! auto traits = light->get_traits(); for (auto mode : traits.get_supported_color_modes()) - msg->supported_color_modes.push_back(static_cast(mode)); - msg->legacy_supports_brightness = traits.supports_color_capability(light::ColorCapability::BRIGHTNESS); - msg->legacy_supports_rgb = traits.supports_color_capability(light::ColorCapability::RGB); - msg->legacy_supports_white_value = - msg->legacy_supports_rgb && (traits.supports_color_capability(light::ColorCapability::WHITE) || - traits.supports_color_capability(light::ColorCapability::COLD_WARM_WHITE)); - msg->legacy_supports_color_temperature = - traits.supports_color_capability(light::ColorCapability::COLOR_TEMPERATURE) || - traits.supports_color_capability(light::ColorCapability::COLD_WARM_WHITE); - if (msg->legacy_supports_color_temperature) { - msg->min_mireds = traits.get_min_mireds(); - msg->max_mireds = traits.get_max_mireds(); + msg.supported_color_modes.push_back(static_cast(mode)); + msg.legacy_supports_brightness = traits.supports_color_capability(light::ColorCapability::BRIGHTNESS); + msg.legacy_supports_rgb = traits.supports_color_capability(light::ColorCapability::RGB); + msg.legacy_supports_white_value = + msg.legacy_supports_rgb && (traits.supports_color_capability(light::ColorCapability::WHITE) || + traits.supports_color_capability(light::ColorCapability::COLD_WARM_WHITE)); + msg.legacy_supports_color_temperature = traits.supports_color_capability(light::ColorCapability::COLOR_TEMPERATURE) || + traits.supports_color_capability(light::ColorCapability::COLD_WARM_WHITE); + if (msg.legacy_supports_color_temperature) { + msg.min_mireds = traits.get_min_mireds(); + msg.max_mireds = traits.get_max_mireds(); } if (light->supports_effects()) { - msg->effects.emplace_back("None"); + msg.effects.emplace_back("None"); for (auto *effect : light->get_effects()) { - msg->effects.push_back(effect->get_name()); + msg.effects.push_back(effect->get_name()); } } - msg->unique_id = get_default_unique_id("light", light); - fill_entity_info_base_(light, *msg); - return msg; + msg.unique_id = get_default_unique_id("light", light); + fill_entity_info_base_(light, msg); + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::light_command(const LightCommandRequest &msg) { light::LightState *light = App.get_light_by_key(msg.key); @@ -494,55 +502,59 @@ 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) -> std::unique_ptr { - auto *s = static_cast(entity); - auto msg = std::make_unique(); - msg->state = state; - msg->missing_state = !s->has_state(); - msg->key = s->get_object_id_hash(); - return msg; - }); + return this->schedule_message_( + sensor, [state](EntityBase *entity, ProtoWriteBuffer &buffer, uint32_t max_size) -> APIConnection::MessageInfo { + 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 APIConnection::encode_message_to_buffer(msg, buffer, max_size); + }); } void APIConnection::send_sensor_info(sensor::Sensor *sensor) { this->schedule_message_(sensor, &APIConnection::try_send_sensor_info_); } -std::unique_ptr APIConnection::try_send_sensor_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_sensor_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *sensor = static_cast(entity); - auto msg = std::make_unique(); - msg->unit_of_measurement = sensor->get_unit_of_measurement(); - msg->accuracy_decimals = sensor->get_accuracy_decimals(); - msg->force_update = sensor->get_force_update(); - msg->device_class = sensor->get_device_class(); - msg->state_class = static_cast(sensor->get_state_class()); - msg->unique_id = sensor->unique_id(); + ListEntitiesSensorResponse msg; // Stack allocated! + msg.unit_of_measurement = sensor->get_unit_of_measurement(); + msg.accuracy_decimals = sensor->get_accuracy_decimals(); + msg.force_update = sensor->get_force_update(); + msg.device_class = sensor->get_device_class(); + msg.state_class = static_cast(sensor->get_state_class()); + msg.unique_id = sensor->unique_id(); if (msg->unique_id.empty()) - msg->unique_id = get_default_unique_id("sensor", sensor); - fill_entity_info_base_(sensor, *msg); - return msg; + msg.unique_id = get_default_unique_id("sensor", sensor); + fill_entity_info_base_(sensor, msg); + return encode_message_to_buffer(msg, buffer, max_size); } #endif #ifdef USE_SWITCH bool APIConnection::send_switch_state(switch_::Switch *a_switch, bool state) { - return this->schedule_message_(a_switch, [state](EntityBase *entity) -> std::unique_ptr { - auto *sw = static_cast(entity); - auto msg = std::make_unique(); - msg->state = state; - msg->key = sw->get_object_id_hash(); - return msg; - }); + return this->schedule_message_( + a_switch, [state](EntityBase *entity, ProtoWriteBuffer &buffer, uint32_t max_size) -> APIConnection::MessageInfo { + auto *sw = static_cast(entity); + SwitchStateResponse msg; // Stack allocated! + msg.state = state; + msg.key = sw->get_object_id_hash(); + return APIConnection::encode_message_to_buffer(msg, buffer, max_size); + }); } void APIConnection::send_switch_info(switch_::Switch *a_switch) { this->schedule_message_(a_switch, &APIConnection::try_send_switch_info_); } -std::unique_ptr APIConnection::try_send_switch_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_switch_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *a_switch = static_cast(entity); - auto msg = std::make_unique(); - 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 msg; + 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, buffer, max_size); } void APIConnection::switch_command(const SwitchCommandRequest &msg) { switch_::Switch *a_switch = App.get_switch_by_key(msg.key); @@ -560,30 +572,32 @@ void APIConnection::switch_command(const SwitchCommandRequest &msg) { #ifdef USE_TEXT_SENSOR 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) -> std::unique_ptr { + [state = std::move(state)](EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) -> APIConnection::MessageInfo { auto *ts = static_cast(entity); - auto msg = std::make_unique(); - msg->state = std::move(state); - msg->missing_state = !ts->has_state(); - msg->key = ts->get_object_id_hash(); - return msg; + TextSensorStateResponse msg; // Stack allocated! + msg.state = state; + msg.missing_state = !ts->has_state(); + msg.key = ts->get_object_id_hash(); + return APIConnection::encode_message_to_buffer(msg, buffer, max_size); }); } void APIConnection::send_text_sensor_info(text_sensor::TextSensor *text_sensor) { this->schedule_message_(text_sensor, &APIConnection::try_send_text_sensor_info_); } -std::unique_ptr APIConnection::try_send_text_sensor_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_text_sensor_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *text_sensor = static_cast(entity); - auto msg = std::make_unique(); - msg->device_class = text_sensor->get_device_class(); - msg->unique_id = text_sensor->unique_id(); + ListEntitiesTextSensorResponse msg; // Stack allocated! + msg.device_class = text_sensor->get_device_class(); + msg.unique_id = text_sensor->unique_id(); if (msg->unique_id.empty()) - msg->unique_id = get_default_unique_id("text_sensor", text_sensor); + msg.unique_id = get_default_unique_id("text_sensor", text_sensor); // Fill common entity fields - fill_entity_info_base_(text_sensor, *msg); + fill_entity_info_base_(text_sensor, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } #endif @@ -591,75 +605,77 @@ std::unique_ptr 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_); } -std::unique_ptr APIConnection::try_send_climate_state_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_climate_state_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *climate = static_cast(entity); - auto msg = std::make_unique(); - msg->key = climate->get_object_id_hash(); + ClimateStateResponse msg; // Stack allocated! + msg.key = climate->get_object_id_hash(); auto traits = climate->get_traits(); - msg->mode = static_cast(climate->mode); - msg->action = static_cast(climate->action); + msg.mode = static_cast(climate->mode); + msg.action = static_cast(climate->action); if (traits.get_supports_current_temperature()) - msg->current_temperature = climate->current_temperature; + msg.current_temperature = climate->current_temperature; if (traits.get_supports_two_point_target_temperature()) { - msg->target_temperature_low = climate->target_temperature_low; - msg->target_temperature_high = climate->target_temperature_high; + msg.target_temperature_low = climate->target_temperature_low; + msg.target_temperature_high = climate->target_temperature_high; } else { - msg->target_temperature = climate->target_temperature; + msg.target_temperature = climate->target_temperature; } if (traits.get_supports_fan_modes() && climate->fan_mode.has_value()) - msg->fan_mode = static_cast(climate->fan_mode.value()); + msg.fan_mode = static_cast(climate->fan_mode.value()); if (!traits.get_supported_custom_fan_modes().empty() && climate->custom_fan_mode.has_value()) - msg->custom_fan_mode = climate->custom_fan_mode.value(); + msg.custom_fan_mode = climate->custom_fan_mode.value(); if (traits.get_supports_presets() && climate->preset.has_value()) { - msg->preset = static_cast(climate->preset.value()); + msg.preset = static_cast(climate->preset.value()); } if (!traits.get_supported_custom_presets().empty() && climate->custom_preset.has_value()) - msg->custom_preset = climate->custom_preset.value(); + msg.custom_preset = climate->custom_preset.value(); if (traits.get_supports_swing_modes()) - msg->swing_mode = static_cast(climate->swing_mode); + msg.swing_mode = static_cast(climate->swing_mode); if (traits.get_supports_current_humidity()) - msg->current_humidity = climate->current_humidity; + msg.current_humidity = climate->current_humidity; if (traits.get_supports_target_humidity()) - msg->target_humidity = climate->target_humidity; - return msg; + msg.target_humidity = climate->target_humidity; + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::send_climate_info(climate::Climate *climate) { this->schedule_message_(climate, &APIConnection::try_send_climate_info_); } -std::unique_ptr APIConnection::try_send_climate_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_climate_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *climate = static_cast(entity); - auto msg = std::make_unique(); + ListEntitiesClimateResponse msg; // Stack allocated! auto traits = climate->get_traits(); - msg->supports_current_temperature = traits.get_supports_current_temperature(); - msg->supports_current_humidity = traits.get_supports_current_humidity(); - msg->supports_two_point_target_temperature = traits.get_supports_two_point_target_temperature(); - msg->supports_target_humidity = traits.get_supports_target_humidity(); + msg.supports_current_temperature = traits.get_supports_current_temperature(); + msg.supports_current_humidity = traits.get_supports_current_humidity(); + msg.supports_two_point_target_temperature = traits.get_supports_two_point_target_temperature(); + msg.supports_target_humidity = traits.get_supports_target_humidity(); for (auto mode : traits.get_supported_modes()) - msg->supported_modes.push_back(static_cast(mode)); - msg->visual_min_temperature = traits.get_visual_min_temperature(); - msg->visual_max_temperature = traits.get_visual_max_temperature(); - msg->visual_target_temperature_step = traits.get_visual_target_temperature_step(); - msg->visual_current_temperature_step = traits.get_visual_current_temperature_step(); - msg->visual_min_humidity = traits.get_visual_min_humidity(); - msg->visual_max_humidity = traits.get_visual_max_humidity(); - msg->legacy_supports_away = traits.supports_preset(climate::CLIMATE_PRESET_AWAY); - msg->supports_action = traits.get_supports_action(); + msg.supported_modes.push_back(static_cast(mode)); + msg.visual_min_temperature = traits.get_visual_min_temperature(); + msg.visual_max_temperature = traits.get_visual_max_temperature(); + msg.visual_target_temperature_step = traits.get_visual_target_temperature_step(); + msg.visual_current_temperature_step = traits.get_visual_current_temperature_step(); + msg.visual_min_humidity = traits.get_visual_min_humidity(); + msg.visual_max_humidity = traits.get_visual_max_humidity(); + msg.legacy_supports_away = traits.supports_preset(climate::CLIMATE_PRESET_AWAY); + msg.supports_action = traits.get_supports_action(); for (auto fan_mode : traits.get_supported_fan_modes()) - msg->supported_fan_modes.push_back(static_cast(fan_mode)); + msg.supported_fan_modes.push_back(static_cast(fan_mode)); for (auto const &custom_fan_mode : traits.get_supported_custom_fan_modes()) - msg->supported_custom_fan_modes.push_back(custom_fan_mode); + msg.supported_custom_fan_modes.push_back(custom_fan_mode); for (auto preset : traits.get_supported_presets()) - msg->supported_presets.push_back(static_cast(preset)); + msg.supported_presets.push_back(static_cast(preset)); for (auto const &custom_preset : traits.get_supported_custom_presets()) - msg->supported_custom_presets.push_back(custom_preset); + msg.supported_custom_presets.push_back(custom_preset); for (auto swing_mode : traits.get_supported_swing_modes()) - msg->supported_swing_modes.push_back(static_cast(swing_mode)); - msg->unique_id = get_default_unique_id("climate", climate); + msg.supported_swing_modes.push_back(static_cast(swing_mode)); + msg.unique_id = get_default_unique_id("climate", climate); // Fill common entity fields - fill_entity_info_base_(climate, *msg); + fill_entity_info_base_(climate, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::climate_command(const ClimateCommandRequest &msg) { climate::Climate *climate = App.get_climate_by_key(msg.key); @@ -693,33 +709,35 @@ 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) -> std::unique_ptr { - auto *n = static_cast(entity); - auto msg = std::make_unique(); - msg->state = state; - msg->missing_state = !n->has_state(); - msg->key = n->get_object_id_hash(); - return msg; - }); + return this->schedule_message_( + number, [state](EntityBase *entity, ProtoWriteBuffer &buffer, uint32_t max_size) -> APIConnection::MessageInfo { + 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 APIConnection::encode_message_to_buffer(msg, buffer, max_size); + }); } void APIConnection::send_number_info(number::Number *number) { this->schedule_message_(number, &APIConnection::try_send_number_info_); } -std::unique_ptr APIConnection::try_send_number_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_number_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *number = static_cast(entity); - auto msg = std::make_unique(); - msg->unit_of_measurement = number->traits.get_unit_of_measurement(); - msg->mode = static_cast(number->traits.get_mode()); - msg->device_class = number->traits.get_device_class(); - msg->min_value = number->traits.get_min_value(); - msg->max_value = number->traits.get_max_value(); - msg->step = number->traits.get_step(); - msg->unique_id = get_default_unique_id("number", number); + ListEntitiesNumberResponse msg; // Stack allocated! + msg.unit_of_measurement = number->traits.get_unit_of_measurement(); + msg.mode = static_cast(number->traits.get_mode()); + msg.device_class = number->traits.get_device_class(); + msg.min_value = number->traits.get_min_value(); + msg.max_value = number->traits.get_max_value(); + msg.step = number->traits.get_step(); + msg.unique_id = get_default_unique_id("number", number); // Fill common entity fields - fill_entity_info_base_(number, *msg); + fill_entity_info_base_(number, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::number_command(const NumberCommandRequest &msg) { number::Number *number = App.get_number_by_key(msg.key); @@ -736,28 +754,30 @@ 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_); } -std::unique_ptr APIConnection::try_send_date_state_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_date_state_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *date = static_cast(entity); - auto msg = std::make_unique(); - msg->missing_state = !date->has_state(); - msg->year = date->year; - msg->month = date->month; - msg->day = date->day; - msg->key = date->get_object_id_hash(); - return msg; + DateStateResponse msg; // Stack allocated! + msg.missing_state = !date->has_state(); + msg.year = date->year; + msg.month = date->month; + msg.day = date->day; + msg.key = date->get_object_id_hash(); + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::send_date_info(datetime::DateEntity *date) { this->schedule_message_(date, &APIConnection::try_send_date_info_); } -std::unique_ptr APIConnection::try_send_date_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_date_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *date = static_cast(entity); - auto msg = std::make_unique(); - msg->unique_id = get_default_unique_id("date", date); + ListEntitiesDateResponse msg; // Stack allocated! + msg.unique_id = get_default_unique_id("date", date); // Fill common entity fields - fill_entity_info_base_(date, *msg); + fill_entity_info_base_(date, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::date_command(const DateCommandRequest &msg) { datetime::DateEntity *date = App.get_date_by_key(msg.key); @@ -774,28 +794,30 @@ 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_); } -std::unique_ptr APIConnection::try_send_time_state_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_time_state_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *time = static_cast(entity); - auto msg = std::make_unique(); - msg->missing_state = !time->has_state(); - msg->hour = time->hour; - msg->minute = time->minute; - msg->second = time->second; - msg->key = time->get_object_id_hash(); - return msg; + TimeStateResponse msg; // Stack allocated! + msg.missing_state = !time->has_state(); + msg.hour = time->hour; + msg.minute = time->minute; + msg.second = time->second; + msg.key = time->get_object_id_hash(); + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::send_time_info(datetime::TimeEntity *time) { this->schedule_message_(time, &APIConnection::try_send_time_info_); } -std::unique_ptr APIConnection::try_send_time_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_time_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *time = static_cast(entity); - auto msg = std::make_unique(); - msg->unique_id = get_default_unique_id("time", time); + ListEntitiesTimeResponse msg; // Stack allocated! + msg.unique_id = get_default_unique_id("time", time); // Fill common entity fields - fill_entity_info_base_(time, *msg); + fill_entity_info_base_(time, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::time_command(const TimeCommandRequest &msg) { datetime::TimeEntity *time = App.get_time_by_key(msg.key); @@ -812,29 +834,31 @@ 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_); } -std::unique_ptr APIConnection::try_send_datetime_state_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_datetime_state_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *datetime = static_cast(entity); - auto msg = std::make_unique(); - msg->missing_state = !datetime->has_state(); + DateTimeStateResponse msg; // Stack allocated! + msg.missing_state = !datetime->has_state(); if (datetime->has_state()) { ESPTime state = datetime->state_as_esptime(); - msg->epoch_seconds = state.timestamp; + msg.epoch_seconds = state.timestamp; } - msg->key = datetime->get_object_id_hash(); - return msg; + msg.key = datetime->get_object_id_hash(); + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::send_datetime_info(datetime::DateTimeEntity *datetime) { this->schedule_message_(datetime, &APIConnection::try_send_datetime_info_); } -std::unique_ptr APIConnection::try_send_datetime_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_datetime_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *datetime = static_cast(entity); - auto msg = std::make_unique(); - msg->unique_id = get_default_unique_id("datetime", datetime); + ListEntitiesDateTimeResponse msg; // Stack allocated! + msg.unique_id = get_default_unique_id("datetime", datetime); // Fill common entity fields - fill_entity_info_base_(datetime, *msg); + fill_entity_info_base_(datetime, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::datetime_command(const DateTimeCommandRequest &msg) { datetime::DateTimeEntity *datetime = App.get_datetime_by_key(msg.key); @@ -849,31 +873,34 @@ void APIConnection::datetime_command(const DateTimeCommandRequest &msg) { #ifdef USE_TEXT bool APIConnection::send_text_state(text::Text *text, std::string state) { - return this->schedule_message_(text, [state = std::move(state)](EntityBase *entity) -> std::unique_ptr { - auto *t = static_cast(entity); - auto msg = std::make_unique(); - msg->state = std::move(state); - msg->missing_state = !t->has_state(); - msg->key = t->get_object_id_hash(); - return msg; - }); + return this->schedule_message_(text, + [state = std::move(state)](EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) -> APIConnection::MessageInfo { + 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 APIConnection::encode_message_to_buffer(msg, buffer, max_size); + }); } void APIConnection::send_text_info(text::Text *text) { this->schedule_message_(text, &APIConnection::try_send_text_info_); } -std::unique_ptr APIConnection::try_send_text_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_text_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *text = static_cast(entity); - auto msg = std::make_unique(); - msg->mode = static_cast(text->traits.get_mode()); - msg->min_length = text->traits.get_min_length(); - msg->max_length = text->traits.get_max_length(); - msg->pattern = text->traits.get_pattern(); - msg->unique_id = get_default_unique_id("text", text); + ListEntitiesTextResponse msg; // Stack allocated! + msg.mode = static_cast(text->traits.get_mode()); + msg.min_length = text->traits.get_min_length(); + msg.max_length = text->traits.get_max_length(); + msg.pattern = text->traits.get_pattern(); + msg.unique_id = get_default_unique_id("text", text); // Fill common entity fields - fill_entity_info_base_(text, *msg); + fill_entity_info_base_(text, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::text_command(const TextCommandRequest &msg) { text::Text *text = App.get_text_by_key(msg.key); @@ -889,29 +916,31 @@ void APIConnection::text_command(const TextCommandRequest &msg) { #ifdef USE_SELECT bool APIConnection::send_select_state(select::Select *select, std::string state) { return this->schedule_message_(select, - [state = std::move(state)](EntityBase *entity) -> std::unique_ptr { + [state = std::move(state)](EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) -> APIConnection::MessageInfo { auto *s = static_cast(entity); - auto msg = std::make_unique(); - msg->state = std::move(state); - msg->missing_state = !s->has_state(); - msg->key = s->get_object_id_hash(); - return msg; + SelectStateResponse msg; // Stack allocated! + msg.state = state; + msg.missing_state = !s->has_state(); + msg.key = s->get_object_id_hash(); + return APIConnection::encode_message_to_buffer(msg, buffer, max_size); }); } void APIConnection::send_select_info(select::Select *select) { this->schedule_message_(select, &APIConnection::try_send_select_info_); } -std::unique_ptr APIConnection::try_send_select_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_select_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *select = static_cast(entity); - auto msg = std::make_unique(); + ListEntitiesSelectResponse msg; // Stack allocated! for (const auto &option : select->traits.get_options()) - msg->options.push_back(option); - msg->unique_id = get_default_unique_id("select", select); + msg.options.push_back(option); + msg.unique_id = get_default_unique_id("select", select); // Fill common entity fields - fill_entity_info_base_(select, *msg); + fill_entity_info_base_(select, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::select_command(const SelectCommandRequest &msg) { select::Select *select = App.get_select_by_key(msg.key); @@ -928,16 +957,17 @@ 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_); } -std::unique_ptr APIConnection::try_send_button_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_button_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *button = static_cast(entity); - auto msg = std::make_unique(); - msg->device_class = button->get_device_class(); - msg->unique_id = get_default_unique_id("button", button); + ListEntitiesButtonResponse msg; // Stack allocated! + msg.device_class = button->get_device_class(); + msg.unique_id = get_default_unique_id("button", button); // Fill common entity fields - fill_entity_info_base_(button, *msg); + fill_entity_info_base_(button, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } void esphome::api::APIConnection::button_command(const ButtonCommandRequest &msg) { button::Button *button = App.get_button_by_key(msg.key); @@ -950,29 +980,31 @@ 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) -> std::unique_ptr { - auto *l = static_cast(entity); - auto msg = std::make_unique(); - msg->state = static_cast(state); - msg->key = l->get_object_id_hash(); - return msg; - }); + return this->schedule_message_( + a_lock, [state](EntityBase *entity, ProtoWriteBuffer &buffer, uint32_t max_size) -> APIConnection::MessageInfo { + auto *l = static_cast(entity); + LockStateResponse msg; // Stack allocated! + msg.state = static_cast(state); + msg.key = l->get_object_id_hash(); + return APIConnection::encode_message_to_buffer(msg, buffer, max_size); + }); } void APIConnection::send_lock_info(lock::Lock *a_lock) { this->schedule_message_(a_lock, &APIConnection::try_send_lock_info_); } -std::unique_ptr APIConnection::try_send_lock_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_lock_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *a_lock = static_cast(entity); - auto msg = std::make_unique(); - msg->assumed_state = a_lock->traits.get_assumed_state(); - msg->supports_open = a_lock->traits.get_supports_open(); - msg->requires_code = a_lock->traits.get_requires_code(); - msg->unique_id = get_default_unique_id("lock", a_lock); + ListEntitiesLockResponse msg; // Stack allocated! + msg.assumed_state = a_lock->traits.get_assumed_state(); + msg.supports_open = a_lock->traits.get_supports_open(); + msg.requires_code = a_lock->traits.get_requires_code(); + msg.unique_id = get_default_unique_id("lock", a_lock); // Fill common entity fields - fill_entity_info_base_(a_lock, *msg); + fill_entity_info_base_(a_lock, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::lock_command(const LockCommandRequest &msg) { lock::Lock *a_lock = App.get_lock_by_key(msg.key); @@ -997,31 +1029,33 @@ 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_); } -std::unique_ptr APIConnection::try_send_valve_state_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_valve_state_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *valve = static_cast(entity); - auto msg = std::make_unique(); - msg->position = valve->position; - msg->current_operation = static_cast(valve->current_operation); - msg->key = valve->get_object_id_hash(); - return msg; + 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, buffer, max_size); } void APIConnection::send_valve_info(valve::Valve *valve) { this->schedule_message_(valve, &APIConnection::try_send_valve_info_); } -std::unique_ptr APIConnection::try_send_valve_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_valve_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *valve = static_cast(entity); - auto msg = std::make_unique(); + ListEntitiesValveResponse msg; // Stack allocated! auto traits = valve->get_traits(); - msg->device_class = valve->get_device_class(); - msg->assumed_state = traits.get_is_assumed_state(); - msg->supports_position = traits.get_supports_position(); - msg->supports_stop = traits.get_supports_stop(); - msg->unique_id = get_default_unique_id("valve", valve); + msg.device_class = valve->get_device_class(); + msg.assumed_state = traits.get_is_assumed_state(); + msg.supports_position = traits.get_supports_position(); + msg.supports_stop = traits.get_supports_stop(); + msg.unique_id = get_default_unique_id("valve", valve); // Fill common entity fields - fill_entity_info_base_(valve, *msg); + fill_entity_info_base_(valve, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::valve_command(const ValveCommandRequest &msg) { valve::Valve *valve = App.get_valve_by_key(msg.key); @@ -1041,26 +1075,28 @@ 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_); } -std::unique_ptr APIConnection::try_send_media_player_state_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_media_player_state_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *media_player = static_cast(entity); - auto msg = std::make_unique(); + MediaPlayerStateResponse msg; // Stack allocated! media_player::MediaPlayerState report_state = media_player->state == media_player::MEDIA_PLAYER_STATE_ANNOUNCING ? media_player::MEDIA_PLAYER_STATE_PLAYING : media_player->state; - msg->state = static_cast(report_state); - msg->volume = media_player->volume; - msg->muted = media_player->is_muted(); - msg->key = media_player->get_object_id_hash(); - return msg; + msg.state = static_cast(report_state); + 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, buffer, max_size); } void APIConnection::send_media_player_info(media_player::MediaPlayer *media_player) { this->schedule_message_(media_player, &APIConnection::try_send_media_player_info_); } -std::unique_ptr APIConnection::try_send_media_player_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_media_player_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *media_player = static_cast(entity); - auto msg = std::make_unique(); + ListEntitiesMediaPlayerResponse msg; // Stack allocated! auto traits = media_player->get_traits(); - msg->supports_pause = traits.get_supports_pause(); + msg.supports_pause = traits.get_supports_pause(); for (auto &supported_format : traits.get_supported_formats()) { MediaPlayerSupportedFormat media_format; media_format.format = supported_format.format; @@ -1068,14 +1104,14 @@ std::unique_ptr APIConnection::try_send_media_player_info_(EntityB media_format.num_channels = supported_format.num_channels; media_format.purpose = static_cast(supported_format.purpose); media_format.sample_bytes = supported_format.sample_bytes; - msg->supported_formats.push_back(media_format); + msg.supported_formats.push_back(media_format); } - msg->unique_id = get_default_unique_id("media_player", media_player); + msg.unique_id = get_default_unique_id("media_player", media_player); // Fill common entity fields - fill_entity_info_base_(media_player, *msg); + fill_entity_info_base_(media_player, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::media_player_command(const MediaPlayerCommandRequest &msg) { media_player::MediaPlayer *media_player = App.get_media_player_by_key(msg.key); @@ -1112,15 +1148,16 @@ 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_); } -std::unique_ptr APIConnection::try_send_camera_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_camera_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *camera = static_cast(entity); - auto msg = std::make_unique(); - msg->unique_id = get_default_unique_id("camera", camera); + ListEntitiesCameraResponse msg; // Stack allocated! + msg.unique_id = get_default_unique_id("camera", camera); // Fill common entity fields - fill_entity_info_base_(camera, *msg); + fill_entity_info_base_(camera, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::camera_image(const CameraImageRequest &msg) { if (esp32_camera::global_esp32_camera == nullptr) @@ -1311,28 +1348,32 @@ void APIConnection::voice_assistant_set_configuration(const VoiceAssistantSetCon bool APIConnection::send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) { return this->schedule_message_(a_alarm_control_panel, &APIConnection::try_send_alarm_control_panel_state_); } -std::unique_ptr APIConnection::try_send_alarm_control_panel_state_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_alarm_control_panel_state_(EntityBase *entity, + ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *a_alarm_control_panel = static_cast(entity); - auto msg = std::make_unique(); - msg->state = static_cast(a_alarm_control_panel->get_state()); - msg->key = a_alarm_control_panel->get_object_id_hash(); - return msg; + 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, buffer, max_size); } 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_); } -std::unique_ptr APIConnection::try_send_alarm_control_panel_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_alarm_control_panel_info_(EntityBase *entity, + ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *a_alarm_control_panel = static_cast(entity); - auto msg = std::make_unique(); - msg->supported_features = a_alarm_control_panel->get_supported_features(); - msg->requires_code = a_alarm_control_panel->get_requires_code(); - msg->requires_code_to_arm = a_alarm_control_panel->get_requires_code_to_arm(); - msg->unique_id = get_default_unique_id("alarm_control_panel", a_alarm_control_panel); + ListEntitiesAlarmControlPanelResponse msg; // Stack allocated! + msg.supported_features = a_alarm_control_panel->get_supported_features(); + msg.requires_code = a_alarm_control_panel->get_requires_code(); + msg.requires_code_to_arm = a_alarm_control_panel->get_requires_code_to_arm(); + msg.unique_id = get_default_unique_id("alarm_control_panel", a_alarm_control_panel); // Fill common entity fields - fill_entity_info_base_(a_alarm_control_panel, *msg); + fill_entity_info_base_(a_alarm_control_panel, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } 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); @@ -1373,27 +1414,28 @@ void APIConnection::send_event(event::Event *event, std::string event_type) { this->schedule_message_(event, [event_type = std::move(event_type)](EntityBase *entity) -> std::unique_ptr { auto *e = static_cast(entity); - auto msg = std::make_unique(); - msg->event_type = std::move(event_type); - msg->key = e->get_object_id_hash(); - return msg; + EventResponse msg; // Stack allocated! + msg.event_type = std::move(event_type); + msg.key = e->get_object_id_hash(); + return encode_message_to_buffer(msg, buffer, max_size); }); } void APIConnection::send_event_info(event::Event *event) { this->schedule_message_(event, &APIConnection::try_send_event_info_); } -std::unique_ptr APIConnection::try_send_event_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_event_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *event = static_cast(entity); - auto msg = std::make_unique(); - msg->device_class = event->get_device_class(); + ListEntitiesEventResponse msg; // Stack allocated! + msg.device_class = event->get_device_class(); for (const auto &event_type : event->get_event_types()) - msg->event_types.push_back(event_type); - msg->unique_id = get_default_unique_id("event", event); + msg.event_types.push_back(event_type); + msg.unique_id = get_default_unique_id("event", event); // Fill common entity fields - fill_entity_info_base_(event, *msg); + fill_entity_info_base_(event, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } #endif @@ -1401,38 +1443,40 @@ std::unique_ptr 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_); } -std::unique_ptr APIConnection::try_send_update_state_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_update_state_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *update = static_cast(entity); - auto msg = std::make_unique(); - msg->missing_state = !update->has_state(); + UpdateStateResponse msg; // Stack allocated! + msg.missing_state = !update->has_state(); if (update->has_state()) { - msg->in_progress = update->state == update::UpdateState::UPDATE_STATE_INSTALLING; + msg.in_progress = update->state == update::UpdateState::UPDATE_STATE_INSTALLING; if (update->update_info.has_progress) { - msg->has_progress = true; - msg->progress = update->update_info.progress; + msg.has_progress = true; + msg.progress = update->update_info.progress; } - msg->current_version = update->update_info.current_version; - msg->latest_version = update->update_info.latest_version; - msg->title = update->update_info.title; - msg->release_summary = update->update_info.summary; - msg->release_url = update->update_info.release_url; + msg.current_version = update->update_info.current_version; + msg.latest_version = update->update_info.latest_version; + msg.title = update->update_info.title; + msg.release_summary = update->update_info.summary; + msg.release_url = update->update_info.release_url; } - msg->key = update->get_object_id_hash(); - return msg; + msg.key = update->get_object_id_hash(); + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::send_update_info(update::UpdateEntity *update) { this->schedule_message_(update, &APIConnection::try_send_update_info_); } -std::unique_ptr APIConnection::try_send_update_info_(EntityBase *entity) { +APIConnection::MessageInfo APIConnection::try_send_update_info_(EntityBase *entity, ProtoWriteBuffer &buffer, + uint32_t max_size) { auto *update = static_cast(entity); - auto msg = std::make_unique(); - msg->device_class = update->get_device_class(); - msg->unique_id = get_default_unique_id("update", update); + ListEntitiesUpdateResponse msg; // Stack allocated! + msg.device_class = update->get_device_class(); + msg.unique_id = get_default_unique_id("update", update); // Fill common entity fields - fill_entity_info_base_(update, *msg); + fill_entity_info_base_(update, msg); - return msg; + return encode_message_to_buffer(msg, buffer, max_size); } void APIConnection::update_command(const UpdateCommandRequest &msg) { update::UpdateEntity *update = App.get_update_by_key(msg.key); @@ -1687,91 +1731,62 @@ void APIConnection::process_batch_() { return; } - // Fast path for single message - if (this->deferred_batch_.items.size() == 1) { - const auto &item = this->deferred_batch_.items[0]; - auto message = item.creator(item.entity); - if (message && this->send_message(*message)) { - this->deferred_batch_.clear(); - } else if (!message) { - // Memory allocation failed, remove item - this->deferred_batch_.clear(); - } - // If send fails, keep for retry - return; - } - - // Multiple messages - use batching - // Single pass: create messages, calculate sizes and check what fits - struct MessageData { - std::unique_ptr message; - uint16_t message_type; - uint16_t message_size; - }; - - // Reserve exact space needed - std::vector messages; - messages.reserve(this->deferred_batch_.items.size()); + // Create buffer for all messages + ProtoWriteBuffer batch_buffer = this->create_buffer(MAX_BATCH_SIZE_BYTES); std::vector> packet_info; packet_info.reserve(this->deferred_batch_.items.size()); - uint32_t total_buffer_size = 0; + size_t items_processed = 0; uint32_t current_offset = 0; + uint32_t remaining_size = MAX_BATCH_SIZE_BYTES; - // Single pass: create messages and calculate what fits + // Process items and encode directly to buffer for (size_t i = 0; i < this->deferred_batch_.items.size(); i++) { const auto &item = this->deferred_batch_.items[i]; - // Create message once - auto message = item.creator(item.entity); - if (!message) { - continue; // Skip if allocation failed - } - - // Calculate size and overhead - uint32_t size = 0; - message->calculate_size(size); - uint16_t message_size = static_cast(size); - uint16_t message_type = message->get_message_type(); - uint16_t overhead = this->helper_->calculate_packet_overhead(message_type, message_size); - uint32_t packet_size = message_size + overhead; - - // Check if adding this message would exceed our limit - if (!messages.empty() && total_buffer_size + packet_size > MAX_BATCH_SIZE_BYTES) { - break; // This and remaining messages won't fit - } - - // Store message and packet info - messages.push_back({std::move(message), message_type, message_size}); - packet_info.push_back(std::make_tuple(message_type, current_offset, message_size)); - - // Update offset for next message - if (i < this->deferred_batch_.items.size() - 1) { - current_offset += message_size + this->helper_->frame_header_padding(); - } - - total_buffer_size += packet_size; - } - - size_t items_processed = messages.size(); - if (items_processed == 0) { - this->deferred_batch_.clear(); - return; - } - - // Create buffer with calculated size - ProtoWriteBuffer batch_buffer = this->create_buffer(total_buffer_size); - - // Encode all messages - for (size_t i = 0; i < messages.size(); i++) { // For messages after the first, extend the buffer with padding if (i > 0) { batch_buffer = this->extend_buffer(); + // Account for header padding in remaining size + uint32_t padding = this->helper_->frame_header_padding(); + if (padding >= remaining_size) { + break; // No more space + } + remaining_size -= padding; + current_offset += padding; } - // Encode the message - messages[i].message->encode(batch_buffer); + // Save buffer position in case we need to rollback + size_t buffer_pos_before = this->proto_write_buffer_.size(); + + // Try to encode message directly to buffer + MessageInfo info = item.creator(item.entity, batch_buffer, remaining_size); + + if (!info.encoded) { + // Message didn't fit, rollback buffer + this->proto_write_buffer_.resize(buffer_pos_before); + break; + } + + // Message was encoded successfully + packet_info.push_back(std::make_tuple(info.type, current_offset, info.size)); + current_offset += info.size; + + // Calculate overhead and update remaining size + uint16_t overhead = this->helper_->calculate_packet_overhead(info.type, info.size); + uint32_t total_packet_size = info.size + overhead; + if (total_packet_size >= remaining_size) { + items_processed = i + 1; + break; // Buffer full + } + remaining_size -= total_packet_size; + items_processed++; + } + + if (items_processed == 0) { + this->deferred_batch_.clear(); + return; } // Add final footer space for Noise if needed diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index 4f891462bb..9eb55e1ece 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -328,90 +328,112 @@ class APIConnection : public APIServerConnection { response.disabled_by_default = entity->is_disabled_by_default(); response.entity_category = static_cast(entity->get_entity_category()); } + + // Helper function to encode a message to buffer + template + static MessageInfo encode_message_to_buffer(MessageT &msg, ProtoWriteBuffer &buffer, uint32_t max_size) { + // Calculate size + uint32_t size = 0; + msg.calculate_size(size); + + // Check if it fits + if (size > max_size) { + return {msg.get_message_type(), static_cast(size), false}; + } + + // Encode directly into buffer + msg.encode(buffer); + return {msg.get_message_type(), static_cast(size), true}; + } #ifdef USE_BINARY_SENSOR - static std::unique_ptr try_send_binary_sensor_state_(EntityBase *binary_sensor); - static std::unique_ptr try_send_binary_sensor_info_(EntityBase *binary_sensor); + static MessageInfo try_send_binary_sensor_state_(EntityBase *binary_sensor, ProtoWriteBuffer &buffer, + uint32_t max_size); + static MessageInfo try_send_binary_sensor_info_(EntityBase *binary_sensor, ProtoWriteBuffer &buffer, + uint32_t max_size); #endif #ifdef USE_COVER - static std::unique_ptr try_send_cover_state_(EntityBase *cover); - static std::unique_ptr try_send_cover_info_(EntityBase *cover); + static MessageInfo try_send_cover_state_(EntityBase *cover, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_cover_info_(EntityBase *cover, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_FAN - static std::unique_ptr try_send_fan_state_(EntityBase *fan); - static std::unique_ptr try_send_fan_info_(EntityBase *fan); + static MessageInfo try_send_fan_state_(EntityBase *fan, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_fan_info_(EntityBase *fan, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_LIGHT - static std::unique_ptr try_send_light_state_(EntityBase *light); - static std::unique_ptr try_send_light_info_(EntityBase *light); + static MessageInfo try_send_light_state_(EntityBase *light, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_light_info_(EntityBase *light, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_SENSOR - static std::unique_ptr try_send_sensor_state_(EntityBase *sensor); - static std::unique_ptr try_send_sensor_info_(EntityBase *sensor); + static MessageInfo try_send_sensor_state_(EntityBase *sensor, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_sensor_info_(EntityBase *sensor, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_SWITCH - static std::unique_ptr try_send_switch_state_(EntityBase *a_switch); - static std::unique_ptr try_send_switch_info_(EntityBase *a_switch); + static MessageInfo try_send_switch_state_(EntityBase *a_switch, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_switch_info_(EntityBase *a_switch, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_TEXT_SENSOR - static std::unique_ptr try_send_text_sensor_state_(EntityBase *text_sensor); - static std::unique_ptr try_send_text_sensor_info_(EntityBase *text_sensor); + static MessageInfo try_send_text_sensor_state_(EntityBase *text_sensor, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_text_sensor_info_(EntityBase *text_sensor, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_CLIMATE - static std::unique_ptr try_send_climate_state_(EntityBase *climate); - static std::unique_ptr try_send_climate_info_(EntityBase *climate); + static MessageInfo try_send_climate_state_(EntityBase *climate, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_climate_info_(EntityBase *climate, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_NUMBER - static std::unique_ptr try_send_number_state_(EntityBase *number); - static std::unique_ptr try_send_number_info_(EntityBase *number); + static MessageInfo try_send_number_state_(EntityBase *number, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_number_info_(EntityBase *number, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_DATETIME_DATE - static std::unique_ptr try_send_date_state_(EntityBase *date); - static std::unique_ptr try_send_date_info_(EntityBase *date); + static MessageInfo try_send_date_state_(EntityBase *date, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_date_info_(EntityBase *date, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_DATETIME_TIME - static std::unique_ptr try_send_time_state_(EntityBase *time); - static std::unique_ptr try_send_time_info_(EntityBase *time); + static MessageInfo try_send_time_state_(EntityBase *time, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_time_info_(EntityBase *time, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_DATETIME_DATETIME - static std::unique_ptr try_send_datetime_state_(EntityBase *datetime); - static std::unique_ptr try_send_datetime_info_(EntityBase *datetime); + static MessageInfo try_send_datetime_state_(EntityBase *datetime, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_datetime_info_(EntityBase *datetime, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_TEXT - static std::unique_ptr try_send_text_state_(EntityBase *text); - static std::unique_ptr try_send_text_info_(EntityBase *text); + static MessageInfo try_send_text_state_(EntityBase *text, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_text_info_(EntityBase *text, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_SELECT - static std::unique_ptr try_send_select_state_(EntityBase *select); - static std::unique_ptr try_send_select_info_(EntityBase *select); + static MessageInfo try_send_select_state_(EntityBase *select, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_select_info_(EntityBase *select, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_BUTTON - static std::unique_ptr try_send_button_info_(EntityBase *button); + static MessageInfo try_send_button_info_(EntityBase *button, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_LOCK - static std::unique_ptr try_send_lock_state_(EntityBase *a_lock); - static std::unique_ptr try_send_lock_info_(EntityBase *a_lock); + static MessageInfo try_send_lock_state_(EntityBase *a_lock, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_lock_info_(EntityBase *a_lock, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_VALVE - static std::unique_ptr try_send_valve_state_(EntityBase *valve); - static std::unique_ptr try_send_valve_info_(EntityBase *valve); + static MessageInfo try_send_valve_state_(EntityBase *valve, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_valve_info_(EntityBase *valve, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_MEDIA_PLAYER - static std::unique_ptr try_send_media_player_state_(EntityBase *media_player); - static std::unique_ptr try_send_media_player_info_(EntityBase *media_player); + static MessageInfo try_send_media_player_state_(EntityBase *media_player, ProtoWriteBuffer &buffer, + uint32_t max_size); + static MessageInfo try_send_media_player_info_(EntityBase *media_player, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_ALARM_CONTROL_PANEL - static std::unique_ptr try_send_alarm_control_panel_state_(EntityBase *a_alarm_control_panel); - static std::unique_ptr try_send_alarm_control_panel_info_(EntityBase *a_alarm_control_panel); + static MessageInfo try_send_alarm_control_panel_state_(EntityBase *a_alarm_control_panel, ProtoWriteBuffer &buffer, + uint32_t max_size); + static MessageInfo try_send_alarm_control_panel_info_(EntityBase *a_alarm_control_panel, ProtoWriteBuffer &buffer, + uint32_t max_size); #endif #ifdef USE_EVENT - static std::unique_ptr try_send_event_info_(EntityBase *event); + static MessageInfo try_send_event_info_(EntityBase *event, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_UPDATE - static std::unique_ptr try_send_update_state_(EntityBase *update); - static std::unique_ptr try_send_update_info_(EntityBase *update); + static MessageInfo try_send_update_state_(EntityBase *update, ProtoWriteBuffer &buffer, uint32_t max_size); + static MessageInfo try_send_update_info_(EntityBase *update, ProtoWriteBuffer &buffer, uint32_t max_size); #endif #ifdef USE_ESP32_CAMERA - static std::unique_ptr try_send_camera_info_(EntityBase *camera); + static MessageInfo try_send_camera_info_(EntityBase *camera, ProtoWriteBuffer &buffer, uint32_t max_size); #endif enum class ConnectionState { @@ -449,8 +471,15 @@ class APIConnection : public APIServerConnection { ListEntitiesIterator list_entities_iterator_; int state_subs_at_ = -1; - // Function type that creates a ProtoMessage from an entity - using MessageCreator = std::function(EntityBase *)>; + // Info about an encoded message + struct MessageInfo { + uint16_t type; + uint16_t size; + bool encoded; + }; + + // Function type that encodes a message directly to buffer + using MessageCreator = std::function; // Generic batching mechanism for both state updates and entity info struct DeferredBatch {