From 411ba5f2886c25155c0f7656e09132611b0116c6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 6 Jun 2025 01:21:31 +0100 Subject: [PATCH] remove batch --- esphome/components/api/api_connection.cpp | 42 ++++++----------------- esphome/components/api/api_connection.h | 6 ++++ 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 5bd1e823d2..0a3f420934 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -282,9 +282,7 @@ std::unique_ptr APIConnection::try_send_binary_sensor_info_(Entity #ifdef USE_COVER bool APIConnection::send_cover_state(cover::Cover *cover) { - // Cover state doesn't need to capture extra values, so we can use function pointer directly - this->deferred_batch_.add_item(cover, &APIConnection::try_send_cover_state_); - return this->schedule_batch_(); + return this->schedule_state_message_(cover, &APIConnection::try_send_cover_state_); } void APIConnection::send_cover_info(cover::Cover *cover) { this->schedule_info_message_(cover, &APIConnection::try_send_cover_info_); @@ -346,8 +344,7 @@ void APIConnection::cover_command(const CoverCommandRequest &msg) { #ifdef USE_FAN bool APIConnection::send_fan_state(fan::Fan *fan) { - this->deferred_batch_.add_item(fan, &APIConnection::try_send_fan_state_); - return this->schedule_batch_(); + return this->schedule_state_message_(fan, &APIConnection::try_send_fan_state_); } void APIConnection::send_fan_info(fan::Fan *fan) { this->schedule_info_message_(fan, &APIConnection::try_send_fan_info_); @@ -407,8 +404,7 @@ void APIConnection::fan_command(const FanCommandRequest &msg) { #ifdef USE_LIGHT bool APIConnection::send_light_state(light::LightState *light) { - this->deferred_batch_.add_item(light, &APIConnection::try_send_light_state_); - return this->schedule_batch_(); + return this->schedule_state_message_(light, &APIConnection::try_send_light_state_); } void APIConnection::send_light_info(light::LightState *light) { this->schedule_info_message_(light, &APIConnection::try_send_light_info_); @@ -603,9 +599,7 @@ std::unique_ptr APIConnection::try_send_text_sensor_info_(EntityBa #ifdef USE_CLIMATE bool APIConnection::send_climate_state(climate::Climate *climate) { - // Climate state doesn't need to capture extra values, so we can use bind - this->deferred_batch_.add_item(climate, &APIConnection::try_send_climate_state_); - return this->schedule_batch_(); + return this->schedule_state_message_(climate, &APIConnection::try_send_climate_state_); } std::unique_ptr APIConnection::try_send_climate_state_(EntityBase *entity) { auto *climate = static_cast(entity); @@ -752,9 +746,7 @@ void APIConnection::number_command(const NumberCommandRequest &msg) { #ifdef USE_DATETIME_DATE bool APIConnection::send_date_state(datetime::DateEntity *date) { - // Date state doesn't need to capture extra values, so we can use bind - this->deferred_batch_.add_item(date, &APIConnection::try_send_date_state_); - return this->schedule_batch_(); + return this->schedule_state_message_(date, &APIConnection::try_send_date_state_); } std::unique_ptr APIConnection::try_send_date_state_(EntityBase *entity) { auto *date = static_cast(entity); @@ -792,9 +784,7 @@ void APIConnection::date_command(const DateCommandRequest &msg) { #ifdef USE_DATETIME_TIME bool APIConnection::send_time_state(datetime::TimeEntity *time) { - // Time state doesn't need to capture extra values, so we can use bind - this->deferred_batch_.add_item(time, &APIConnection::try_send_time_state_); - return this->schedule_batch_(); + return this->schedule_state_message_(time, &APIConnection::try_send_time_state_); } std::unique_ptr APIConnection::try_send_time_state_(EntityBase *entity) { auto *time = static_cast(entity); @@ -832,9 +822,7 @@ void APIConnection::time_command(const TimeCommandRequest &msg) { #ifdef USE_DATETIME_DATETIME bool APIConnection::send_datetime_state(datetime::DateTimeEntity *datetime) { - // DateTime state doesn't need to capture extra values, so we can use bind - this->deferred_batch_.add_item(datetime, &APIConnection::try_send_datetime_state_); - return this->schedule_batch_(); + return this->schedule_state_message_(datetime, &APIConnection::try_send_datetime_state_); } std::unique_ptr APIConnection::try_send_datetime_state_(EntityBase *entity) { auto *datetime = static_cast(entity); @@ -1025,9 +1013,7 @@ void APIConnection::lock_command(const LockCommandRequest &msg) { #ifdef USE_VALVE bool APIConnection::send_valve_state(valve::Valve *valve) { - // Valve state doesn't need to capture extra values, so we can use bind - this->deferred_batch_.add_item(valve, &APIConnection::try_send_valve_state_); - return this->schedule_batch_(); + return this->schedule_state_message_(valve, &APIConnection::try_send_valve_state_); } std::unique_ptr APIConnection::try_send_valve_state_(EntityBase *entity) { auto *valve = static_cast(entity); @@ -1071,9 +1057,7 @@ void APIConnection::valve_command(const ValveCommandRequest &msg) { #ifdef USE_MEDIA_PLAYER bool APIConnection::send_media_player_state(media_player::MediaPlayer *media_player) { - // Media player state doesn't need to capture extra values, so we can use bind - this->deferred_batch_.add_item(media_player, &APIConnection::try_send_media_player_state_); - return this->schedule_batch_(); + return this->schedule_state_message_(media_player, &APIConnection::try_send_media_player_state_); } std::unique_ptr APIConnection::try_send_media_player_state_(EntityBase *entity) { auto *media_player = static_cast(entity); @@ -1343,9 +1327,7 @@ void APIConnection::voice_assistant_set_configuration(const VoiceAssistantSetCon #ifdef USE_ALARM_CONTROL_PANEL bool APIConnection::send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) { - // Alarm control panel state doesn't need to capture extra values, so we can use function pointer directly - this->deferred_batch_.add_item(a_alarm_control_panel, &APIConnection::try_send_alarm_control_panel_state_); - return this->schedule_batch_(); + return this->schedule_state_message_(a_alarm_control_panel, &APIConnection::try_send_alarm_control_panel_state_); } std::unique_ptr APIConnection::try_send_alarm_control_panel_state_(EntityBase *entity) { auto *a_alarm_control_panel = static_cast(entity); @@ -1437,9 +1419,7 @@ std::unique_ptr APIConnection::try_send_event_info_(EntityBase *en #ifdef USE_UPDATE bool APIConnection::send_update_state(update::UpdateEntity *update) { - // Update state doesn't need to capture extra values, so we can use bind - this->deferred_batch_.add_item(update, &APIConnection::try_send_update_state_); - return this->schedule_batch_(); + return this->schedule_state_message_(update, &APIConnection::try_send_update_state_); } std::unique_ptr APIConnection::try_send_update_state_(EntityBase *entity) { auto *update = static_cast(entity); diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index 2bd4fa698b..f646112151 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -320,6 +320,12 @@ class APIConnection : public APIServerConnection { this->schedule_batch_(); } + // Helper function to schedule a deferred state message and return result + bool schedule_state_message_(EntityBase *entity, MessageCreator creator) { + this->deferred_batch_.add_item(entity, creator); + return this->schedule_batch_(); + } + // Helper function to fill common entity fields template static void fill_entity_info_base_(esphome::EntityBase *entity, ResponseT &response) { // Set common fields that are shared by all entity types