From 3fb10037a953190e095086c8fdd7aecfdb49062f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 13 May 2025 20:36:57 -0500 Subject: [PATCH] Refactor api_connection to avoid constructing protobuf messages if the tx_buffer is full Also do not try to dequeue if the buffer is full --- .../alarm_control_panel.cpp | 2 + .../alarm_control_panel/alarm_control_panel.h | 2 + esphome/components/api/api_connection.cpp | 896 +++++------------- esphome/components/api/api_connection.h | 329 +++++-- .../binary_sensor/binary_sensor.cpp | 2 + .../components/binary_sensor/binary_sensor.h | 3 + esphome/components/button/button.cpp | 2 + esphome/components/button/button.h | 2 + esphome/components/climate/climate.cpp | 2 + esphome/components/climate/climate.h | 2 + esphome/components/cover/cover.cpp | 1 + esphome/components/cover/cover.h | 3 + esphome/components/datetime/date_entity.h | 2 + esphome/components/datetime/datetime_base.h | 2 + esphome/components/datetime/datetime_entity.h | 2 + esphome/components/datetime/time_entity.h | 2 + esphome/components/event/event.cpp | 2 + esphome/components/event/event.h | 2 + esphome/components/fan/fan.cpp | 2 + esphome/components/fan/fan.h | 3 + esphome/components/light/light_state.cpp | 2 + esphome/components/light/light_state.h | 2 + esphome/components/lock/lock.cpp | 2 + esphome/components/lock/lock.h | 2 + .../components/media_player/media_player.cpp | 2 + .../components/media_player/media_player.h | 2 + esphome/components/number/number.cpp | 2 + esphome/components/number/number.h | 2 + esphome/components/select/select.cpp | 2 + esphome/components/select/select.h | 2 + esphome/components/sensor/sensor.cpp | 1 + esphome/components/sensor/sensor.h | 3 + esphome/components/switch/switch.cpp | 1 + esphome/components/switch/switch.h | 3 + esphome/components/text/text.cpp | 2 + esphome/components/text/text.h | 2 + .../components/text_sensor/text_sensor.cpp | 1 + esphome/components/text_sensor/text_sensor.h | 3 + esphome/components/update/update_entity.cpp | 2 + esphome/components/update/update_entity.h | 2 + esphome/components/valve/valve.cpp | 2 + esphome/components/valve/valve.h | 2 + esphome/core/entity_base.h | 8 + 43 files changed, 613 insertions(+), 702 deletions(-) diff --git a/esphome/components/alarm_control_panel/alarm_control_panel.cpp b/esphome/components/alarm_control_panel/alarm_control_panel.cpp index 9f1485ee90..3417def62e 100644 --- a/esphome/components/alarm_control_panel/alarm_control_panel.cpp +++ b/esphome/components/alarm_control_panel/alarm_control_panel.cpp @@ -13,6 +13,8 @@ static const char *const TAG = "alarm_control_panel"; AlarmControlPanelCall AlarmControlPanel::make_call() { return AlarmControlPanelCall(this); } +const char *AlarmControlPanel::get_component_type() const { return "alarm_control_panel"; } + bool AlarmControlPanel::is_state_armed(AlarmControlPanelState state) { switch (state) { case ACP_STATE_ARMED_AWAY: diff --git a/esphome/components/alarm_control_panel/alarm_control_panel.h b/esphome/components/alarm_control_panel/alarm_control_panel.h index 85c2b2148e..32d07a14b6 100644 --- a/esphome/components/alarm_control_panel/alarm_control_panel.h +++ b/esphome/components/alarm_control_panel/alarm_control_panel.h @@ -29,6 +29,8 @@ class AlarmControlPanel : public EntityBase { */ AlarmControlPanelCall make_call(); + const char *get_component_type() const override; + /** Set the state of the alarm_control_panel. * * @param state The AlarmControlPanelState. diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index ee0451f499..8c2a010b3a 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -29,7 +29,7 @@ static const char *const TAG = "api.connection"; static const int ESP32_CAMERA_STOP_STREAM = 5000; // helper for allowing only unique entries in the queue -void DeferredMessageQueue::dmq_push_back_with_dedup_(void *source, send_message_t *send_message) { +void DeferredMessageQueue::dmq_push_back_with_dedup_(void *source, send_message_t send_message) { DeferredMessage item(source, send_message); auto iter = std::find_if(this->deferred_queue_.begin(), this->deferred_queue_.end(), @@ -45,7 +45,7 @@ void DeferredMessageQueue::dmq_push_back_with_dedup_(void *source, send_message_ void DeferredMessageQueue::process_queue() { while (!deferred_queue_.empty()) { DeferredMessage &de = deferred_queue_.front(); - if (de.send_message_(this->api_connection_, de.source_)) { + if ((this->api_connection_->*(de.send_message_))(de.source_)) { // O(n) but memory efficiency is more important than speed here which is why std::vector was chosen deferred_queue_.erase(deferred_queue_.begin()); } else { @@ -54,7 +54,7 @@ void DeferredMessageQueue::process_queue() { } } -void DeferredMessageQueue::defer(void *source, send_message_t *send_message) { +void DeferredMessageQueue::defer(void *source, send_message_t send_message) { this->dmq_push_back_with_dedup_(source, send_message); } @@ -267,96 +267,58 @@ 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) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_binary_sensor_state(this, binary_sensor, state)) { - this->deferred_message_queue_.defer(binary_sensor, try_send_binary_sensor_state); - } - - return true; + return this->send_state_with_value_(binary_sensor, &APIConnection::try_send_binary_sensor_state_, + &APIConnection::try_send_binary_sensor_state_, state); } void APIConnection::send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor) { - if (!APIConnection::try_send_binary_sensor_info(this, binary_sensor)) { - this->deferred_message_queue_.defer(binary_sensor, try_send_binary_sensor_info); - } + this->send_info_(binary_sensor, &APIConnection::try_send_binary_sensor_info_); } -bool APIConnection::try_send_binary_sensor_state(APIConnection *api, void *v_binary_sensor) { - binary_sensor::BinarySensor *binary_sensor = reinterpret_cast(v_binary_sensor); - return APIConnection::try_send_binary_sensor_state(api, binary_sensor, binary_sensor->state); +bool APIConnection::try_send_binary_sensor_state_(binary_sensor::BinarySensor *binary_sensor) { + return this->try_send_binary_sensor_state_(binary_sensor, binary_sensor->state); } -bool APIConnection::try_send_binary_sensor_state(APIConnection *api, binary_sensor::BinarySensor *binary_sensor, - bool state) { - BinarySensorStateResponse resp; - resp.key = binary_sensor->get_object_id_hash(); - resp.state = state; - resp.missing_state = !binary_sensor->has_state(); - return api->send_binary_sensor_state_response(resp); -} -bool APIConnection::try_send_binary_sensor_info(APIConnection *api, void *v_binary_sensor) { - binary_sensor::BinarySensor *binary_sensor = reinterpret_cast(v_binary_sensor); - ListEntitiesBinarySensorResponse msg; - msg.object_id = binary_sensor->get_object_id(); +bool APIConnection::try_send_binary_sensor_state_(binary_sensor::BinarySensor *binary_sensor, bool state) { + BinarySensorStateResponse msg; + msg.state = state; + msg.missing_state = !binary_sensor->has_state(); msg.key = binary_sensor->get_object_id_hash(); - if (binary_sensor->has_own_name()) - msg.name = binary_sensor->get_name(); - msg.unique_id = get_default_unique_id("binary_sensor", binary_sensor); + return this->send_binary_sensor_state_response(msg); +} +bool APIConnection::try_send_binary_sensor_info_(binary_sensor::BinarySensor *binary_sensor) { + ListEntitiesBinarySensorResponse msg; msg.device_class = binary_sensor->get_device_class(); msg.is_status_binary_sensor = binary_sensor->is_status_binary_sensor(); - msg.disabled_by_default = binary_sensor->is_disabled_by_default(); - msg.icon = binary_sensor->get_icon(); - msg.entity_category = static_cast(binary_sensor->get_entity_category()); - return api->send_list_entities_binary_sensor_response(msg); + return this->try_send_entity_info_(binary_sensor, msg, &APIConnection::send_list_entities_binary_sensor_response); } #endif #ifdef USE_COVER bool APIConnection::send_cover_state(cover::Cover *cover) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_cover_state(this, cover)) { - this->deferred_message_queue_.defer(cover, try_send_cover_state); - } - - return true; + return this->send_state_(cover, &APIConnection::try_send_cover_state_); } void APIConnection::send_cover_info(cover::Cover *cover) { - if (!APIConnection::try_send_cover_info(this, cover)) { - this->deferred_message_queue_.defer(cover, try_send_cover_info); - } + this->send_info_(cover, &APIConnection::try_send_cover_info_); } -bool APIConnection::try_send_cover_state(APIConnection *api, void *v_cover) { - cover::Cover *cover = reinterpret_cast(v_cover); +bool APIConnection::try_send_cover_state_(cover::Cover *cover) { + CoverStateResponse msg; auto traits = cover->get_traits(); - CoverStateResponse resp{}; - resp.key = cover->get_object_id_hash(); - resp.legacy_state = + msg.legacy_state = (cover->position == cover::COVER_OPEN) ? enums::LEGACY_COVER_STATE_OPEN : enums::LEGACY_COVER_STATE_CLOSED; - resp.position = cover->position; + msg.position = cover->position; if (traits.get_supports_tilt()) - resp.tilt = cover->tilt; - resp.current_operation = static_cast(cover->current_operation); - return api->send_cover_state_response(resp); -} -bool APIConnection::try_send_cover_info(APIConnection *api, void *v_cover) { - cover::Cover *cover = reinterpret_cast(v_cover); - auto traits = cover->get_traits(); - ListEntitiesCoverResponse msg; + msg.tilt = cover->tilt; + msg.current_operation = static_cast(cover->current_operation); msg.key = cover->get_object_id_hash(); - msg.object_id = cover->get_object_id(); - if (cover->has_own_name()) - msg.name = cover->get_name(); - msg.unique_id = get_default_unique_id("cover", cover); + return this->send_cover_state_response(msg); +} +bool APIConnection::try_send_cover_info_(cover::Cover *cover) { + ListEntitiesCoverResponse msg; + 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.disabled_by_default = cover->is_disabled_by_default(); - msg.icon = cover->get_icon(); - msg.entity_category = static_cast(cover->get_entity_category()); - return api->send_list_entities_cover_response(msg); + return this->try_send_entity_info_(cover, msg, &APIConnection::send_list_entities_cover_response); } void APIConnection::cover_command(const CoverCommandRequest &msg) { cover::Cover *cover = App.get_cover_by_key(msg.key); @@ -389,56 +351,35 @@ void APIConnection::cover_command(const CoverCommandRequest &msg) { #ifdef USE_FAN bool APIConnection::send_fan_state(fan::Fan *fan) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_fan_state(this, fan)) { - this->deferred_message_queue_.defer(fan, try_send_fan_state); - } - - return true; + return this->send_state_(fan, &APIConnection::try_send_fan_state_); } -void APIConnection::send_fan_info(fan::Fan *fan) { - if (!APIConnection::try_send_fan_info(this, fan)) { - this->deferred_message_queue_.defer(fan, try_send_fan_info); - } -} -bool APIConnection::try_send_fan_state(APIConnection *api, void *v_fan) { - fan::Fan *fan = reinterpret_cast(v_fan); +void APIConnection::send_fan_info(fan::Fan *fan) { this->send_info_(fan, &APIConnection::try_send_fan_info_); } +bool APIConnection::try_send_fan_state_(fan::Fan *fan) { + FanStateResponse msg; auto traits = fan->get_traits(); - FanStateResponse resp{}; - resp.key = fan->get_object_id_hash(); - resp.state = fan->state; + msg.state = fan->state; if (traits.supports_oscillation()) - resp.oscillating = fan->oscillating; + msg.oscillating = fan->oscillating; if (traits.supports_speed()) { - resp.speed_level = fan->speed; + msg.speed_level = fan->speed; } if (traits.supports_direction()) - resp.direction = static_cast(fan->direction); + msg.direction = static_cast(fan->direction); if (traits.supports_preset_modes()) - resp.preset_mode = fan->preset_mode; - return api->send_fan_state_response(resp); -} -bool APIConnection::try_send_fan_info(APIConnection *api, void *v_fan) { - fan::Fan *fan = reinterpret_cast(v_fan); - auto traits = fan->get_traits(); - ListEntitiesFanResponse msg; + msg.preset_mode = fan->preset_mode; msg.key = fan->get_object_id_hash(); - msg.object_id = fan->get_object_id(); - if (fan->has_own_name()) - msg.name = fan->get_name(); - msg.unique_id = get_default_unique_id("fan", fan); + return this->send_fan_state_response(msg); +} +bool APIConnection::try_send_fan_info_(fan::Fan *fan) { + ListEntitiesFanResponse msg; + 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(); for (auto const &preset : traits.supported_preset_modes()) msg.supported_preset_modes.push_back(preset); - msg.disabled_by_default = fan->is_disabled_by_default(); - msg.icon = fan->get_icon(); - msg.entity_category = static_cast(fan->get_entity_category()); - return api->send_list_entities_fan_response(msg); + return this->try_send_entity_info_(fan, msg, &APIConnection::send_list_entities_fan_response); } void APIConnection::fan_command(const FanCommandRequest &msg) { fan::Fan *fan = App.get_fan_by_key(msg.key); @@ -464,28 +405,16 @@ void APIConnection::fan_command(const FanCommandRequest &msg) { #ifdef USE_LIGHT bool APIConnection::send_light_state(light::LightState *light) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_light_state(this, light)) { - this->deferred_message_queue_.defer(light, try_send_light_state); - } - - return true; + return this->send_state_(light, &APIConnection::try_send_light_state_); } void APIConnection::send_light_info(light::LightState *light) { - if (!APIConnection::try_send_light_info(this, light)) { - this->deferred_message_queue_.defer(light, try_send_light_info); - } + this->send_info_(light, &APIConnection::try_send_light_info_); } -bool APIConnection::try_send_light_state(APIConnection *api, void *v_light) { - light::LightState *light = reinterpret_cast(v_light); +bool APIConnection::try_send_light_state_(light::LightState *light) { + LightStateResponse resp; auto traits = light->get_traits(); auto values = light->remote_values; auto color_mode = values.get_color_mode(); - LightStateResponse resp{}; - - resp.key = light->get_object_id_hash(); resp.state = values.is_on(); resp.color_mode = static_cast(color_mode); resp.brightness = values.get_brightness(); @@ -499,25 +428,14 @@ bool APIConnection::try_send_light_state(APIConnection *api, void *v_light) { resp.warm_white = values.get_warm_white(); if (light->supports_effects()) resp.effect = light->get_effect_name(); - return api->send_light_state_response(resp); + resp.key = light->get_object_id_hash(); + return this->send_light_state_response(resp); } -bool APIConnection::try_send_light_info(APIConnection *api, void *v_light) { - light::LightState *light = reinterpret_cast(v_light); - auto traits = light->get_traits(); +bool APIConnection::try_send_light_info_(light::LightState *light) { ListEntitiesLightResponse msg; - msg.key = light->get_object_id_hash(); - msg.object_id = light->get_object_id(); - if (light->has_own_name()) - msg.name = light->get_name(); - msg.unique_id = get_default_unique_id("light", light); - - msg.disabled_by_default = light->is_disabled_by_default(); - msg.icon = light->get_icon(); - msg.entity_category = static_cast(light->get_entity_category()); - + 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 = @@ -525,17 +443,17 @@ bool APIConnection::try_send_light_info(APIConnection *api, void *v_light) { 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"); - for (auto *effect : light->get_effects()) + for (auto *effect : light->get_effects()) { msg.effects.push_back(effect->get_name()); + } } - return api->send_list_entities_light_response(msg); + return this->try_send_entity_info_(light, msg, &APIConnection::send_list_entities_light_response); } void APIConnection::light_command(const LightCommandRequest &msg) { light::LightState *light = App.get_light_by_key(msg.key); @@ -576,93 +494,57 @@ void APIConnection::light_command(const LightCommandRequest &msg) { #ifdef USE_SENSOR bool APIConnection::send_sensor_state(sensor::Sensor *sensor, float state) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_sensor_state(this, sensor, state)) { - this->deferred_message_queue_.defer(sensor, try_send_sensor_state); - } - - return true; + return this->send_state_with_value_(sensor, &APIConnection::try_send_sensor_state_, + &APIConnection::try_send_sensor_state_, state); } void APIConnection::send_sensor_info(sensor::Sensor *sensor) { - if (!APIConnection::try_send_sensor_info(this, sensor)) { - this->deferred_message_queue_.defer(sensor, try_send_sensor_info); - } + this->send_info_(sensor, &APIConnection::try_send_sensor_info_); } -bool APIConnection::try_send_sensor_state(APIConnection *api, void *v_sensor) { - sensor::Sensor *sensor = reinterpret_cast(v_sensor); - return APIConnection::try_send_sensor_state(api, sensor, sensor->state); +bool APIConnection::try_send_sensor_state_(sensor::Sensor *sensor) { + return this->try_send_sensor_state_(sensor, sensor->state); } -bool APIConnection::try_send_sensor_state(APIConnection *api, sensor::Sensor *sensor, float state) { - SensorStateResponse resp{}; - resp.key = sensor->get_object_id_hash(); +bool APIConnection::try_send_sensor_state_(sensor::Sensor *sensor, float state) { + SensorStateResponse resp; resp.state = state; resp.missing_state = !sensor->has_state(); - return api->send_sensor_state_response(resp); + + resp.key = sensor->get_object_id_hash(); + return this->send_sensor_state_response(resp); } -bool APIConnection::try_send_sensor_info(APIConnection *api, void *v_sensor) { - sensor::Sensor *sensor = reinterpret_cast(v_sensor); +bool APIConnection::try_send_sensor_info_(sensor::Sensor *sensor) { ListEntitiesSensorResponse msg; - msg.key = sensor->get_object_id_hash(); - msg.object_id = sensor->get_object_id(); - if (sensor->has_own_name()) - msg.name = sensor->get_name(); - msg.unique_id = sensor->unique_id(); - if (msg.unique_id.empty()) - msg.unique_id = get_default_unique_id("sensor", sensor); - msg.icon = sensor->get_icon(); 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.disabled_by_default = sensor->is_disabled_by_default(); - msg.entity_category = static_cast(sensor->get_entity_category()); - return api->send_list_entities_sensor_response(msg); + return this->try_send_entity_info_(sensor, msg, &APIConnection::send_list_entities_sensor_response); } #endif #ifdef USE_SWITCH bool APIConnection::send_switch_state(switch_::Switch *a_switch, bool state) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_switch_state(this, a_switch, state)) { - this->deferred_message_queue_.defer(a_switch, try_send_switch_state); - } - - return true; + return this->send_state_with_value_(a_switch, &APIConnection::try_send_switch_state_, + &APIConnection::try_send_switch_state_, state); } void APIConnection::send_switch_info(switch_::Switch *a_switch) { - if (!APIConnection::try_send_switch_info(this, a_switch)) { - this->deferred_message_queue_.defer(a_switch, try_send_switch_info); - } + this->send_info_(a_switch, &APIConnection::try_send_switch_info_); } -bool APIConnection::try_send_switch_state(APIConnection *api, void *v_a_switch) { - switch_::Switch *a_switch = reinterpret_cast(v_a_switch); - return APIConnection::try_send_switch_state(api, a_switch, a_switch->state); +bool APIConnection::try_send_switch_state_(switch_::Switch *a_switch) { + return this->try_send_switch_state_(a_switch, a_switch->state); } -bool APIConnection::try_send_switch_state(APIConnection *api, switch_::Switch *a_switch, bool state) { - SwitchStateResponse resp{}; - resp.key = a_switch->get_object_id_hash(); +bool APIConnection::try_send_switch_state_(switch_::Switch *a_switch, bool state) { + SwitchStateResponse resp; resp.state = state; - return api->send_switch_state_response(resp); + + resp.key = a_switch->get_object_id_hash(); + return this->send_switch_state_response(resp); } -bool APIConnection::try_send_switch_info(APIConnection *api, void *v_a_switch) { - switch_::Switch *a_switch = reinterpret_cast(v_a_switch); +bool APIConnection::try_send_switch_info_(switch_::Switch *a_switch) { ListEntitiesSwitchResponse msg; - msg.key = a_switch->get_object_id_hash(); - msg.object_id = a_switch->get_object_id(); - if (a_switch->has_own_name()) - msg.name = a_switch->get_name(); - msg.unique_id = get_default_unique_id("switch", a_switch); - msg.icon = a_switch->get_icon(); msg.assumed_state = a_switch->assumed_state(); - msg.disabled_by_default = a_switch->is_disabled_by_default(); - msg.entity_category = static_cast(a_switch->get_entity_category()); msg.device_class = a_switch->get_device_class(); - return api->send_list_entities_switch_response(msg); + return this->try_send_entity_info_(a_switch, msg, &APIConnection::send_list_entities_switch_response); } void APIConnection::switch_command(const SwitchCommandRequest &msg) { switch_::Switch *a_switch = App.get_switch_by_key(msg.key); @@ -679,129 +561,95 @@ 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) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_text_sensor_state(this, text_sensor, std::move(state))) { - this->deferred_message_queue_.defer(text_sensor, try_send_text_sensor_state); - } - - return true; + return this->send_state_with_value_(text_sensor, &APIConnection::try_send_text_sensor_state_, + &APIConnection::try_send_text_sensor_state_, std::move(state)); } void APIConnection::send_text_sensor_info(text_sensor::TextSensor *text_sensor) { - if (!APIConnection::try_send_text_sensor_info(this, text_sensor)) { - this->deferred_message_queue_.defer(text_sensor, try_send_text_sensor_info); - } + this->send_info_(text_sensor, &APIConnection::try_send_text_sensor_info_); } -bool APIConnection::try_send_text_sensor_state(APIConnection *api, void *v_text_sensor) { - text_sensor::TextSensor *text_sensor = reinterpret_cast(v_text_sensor); - return APIConnection::try_send_text_sensor_state(api, text_sensor, text_sensor->state); +bool APIConnection::try_send_text_sensor_state_(text_sensor::TextSensor *text_sensor) { + return this->try_send_text_sensor_state_(text_sensor, text_sensor->state); } -bool APIConnection::try_send_text_sensor_state(APIConnection *api, text_sensor::TextSensor *text_sensor, - std::string state) { - TextSensorStateResponse resp{}; - resp.key = text_sensor->get_object_id_hash(); +bool APIConnection::try_send_text_sensor_state_(text_sensor::TextSensor *text_sensor, std::string state) { + TextSensorStateResponse resp; resp.state = std::move(state); resp.missing_state = !text_sensor->has_state(); - return api->send_text_sensor_state_response(resp); + + resp.key = text_sensor->get_object_id_hash(); + return this->send_text_sensor_state_response(resp); } -bool APIConnection::try_send_text_sensor_info(APIConnection *api, void *v_text_sensor) { - text_sensor::TextSensor *text_sensor = reinterpret_cast(v_text_sensor); +bool APIConnection::try_send_text_sensor_info_(text_sensor::TextSensor *text_sensor) { ListEntitiesTextSensorResponse msg; - msg.key = text_sensor->get_object_id_hash(); - msg.object_id = text_sensor->get_object_id(); - msg.name = text_sensor->get_name(); - msg.unique_id = text_sensor->unique_id(); - if (msg.unique_id.empty()) - msg.unique_id = get_default_unique_id("text_sensor", text_sensor); - msg.icon = text_sensor->get_icon(); - msg.disabled_by_default = text_sensor->is_disabled_by_default(); - msg.entity_category = static_cast(text_sensor->get_entity_category()); msg.device_class = text_sensor->get_device_class(); - return api->send_list_entities_text_sensor_response(msg); + return this->try_send_entity_info_(text_sensor, msg, &APIConnection::send_list_entities_text_sensor_response); } #endif #ifdef USE_CLIMATE bool APIConnection::send_climate_state(climate::Climate *climate) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_climate_state(this, climate)) { - this->deferred_message_queue_.defer(climate, try_send_climate_state); - } - - return true; + return this->send_state_(climate, &APIConnection::try_send_climate_state_); } void APIConnection::send_climate_info(climate::Climate *climate) { - if (!APIConnection::try_send_climate_info(this, climate)) { - this->deferred_message_queue_.defer(climate, try_send_climate_info); - } + this->send_info_(climate, &APIConnection::try_send_climate_info_); } -bool APIConnection::try_send_climate_state(APIConnection *api, void *v_climate) { - climate::Climate *climate = reinterpret_cast(v_climate); +bool APIConnection::try_send_climate_state_(climate::Climate *climate) { + ClimateStateResponse resp; auto traits = climate->get_traits(); - ClimateStateResponse resp{}; - resp.key = climate->get_object_id_hash(); resp.mode = static_cast(climate->mode); resp.action = static_cast(climate->action); + if (traits.get_supports_current_temperature()) resp.current_temperature = climate->current_temperature; + if (traits.get_supports_two_point_target_temperature()) { resp.target_temperature_low = climate->target_temperature_low; resp.target_temperature_high = climate->target_temperature_high; } else { resp.target_temperature = climate->target_temperature; } + if (traits.get_supports_fan_modes() && climate->fan_mode.has_value()) resp.fan_mode = static_cast(climate->fan_mode.value()); + if (!traits.get_supported_custom_fan_modes().empty() && climate->custom_fan_mode.has_value()) resp.custom_fan_mode = climate->custom_fan_mode.value(); + if (traits.get_supports_presets() && climate->preset.has_value()) { resp.preset = static_cast(climate->preset.value()); } + if (!traits.get_supported_custom_presets().empty() && climate->custom_preset.has_value()) resp.custom_preset = climate->custom_preset.value(); + if (traits.get_supports_swing_modes()) resp.swing_mode = static_cast(climate->swing_mode); + if (traits.get_supports_current_humidity()) resp.current_humidity = climate->current_humidity; + if (traits.get_supports_target_humidity()) resp.target_humidity = climate->target_humidity; - return api->send_climate_state_response(resp); + + resp.key = climate->get_object_id_hash(); + return this->send_climate_state_response(resp); } -bool APIConnection::try_send_climate_info(APIConnection *api, void *v_climate) { - climate::Climate *climate = reinterpret_cast(v_climate); - auto traits = climate->get_traits(); +bool APIConnection::try_send_climate_info_(climate::Climate *climate) { ListEntitiesClimateResponse msg; - msg.key = climate->get_object_id_hash(); - msg.object_id = climate->get_object_id(); - if (climate->has_own_name()) - msg.name = climate->get_name(); - msg.unique_id = get_default_unique_id("climate", climate); - - msg.disabled_by_default = climate->is_disabled_by_default(); - msg.icon = climate->get_icon(); - msg.entity_category = static_cast(climate->get_entity_category()); - + 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(); - 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(); - for (auto fan_mode : traits.get_supported_fan_modes()) msg.supported_fan_modes.push_back(static_cast(fan_mode)); for (auto const &custom_fan_mode : traits.get_supported_custom_fan_modes()) @@ -812,7 +660,7 @@ bool APIConnection::try_send_climate_info(APIConnection *api, void *v_climate) { 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)); - return api->send_list_entities_climate_response(msg); + return this->try_send_entity_info_(climate, msg, &APIConnection::send_list_entities_climate_response); } void APIConnection::climate_command(const ClimateCommandRequest &msg) { climate::Climate *climate = App.get_climate_by_key(msg.key); @@ -846,51 +694,32 @@ void APIConnection::climate_command(const ClimateCommandRequest &msg) { #ifdef USE_NUMBER bool APIConnection::send_number_state(number::Number *number, float state) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_number_state(this, number, state)) { - this->deferred_message_queue_.defer(number, try_send_number_state); - } - - return true; + return this->send_state_with_value_(number, &APIConnection::try_send_number_state_, + &APIConnection::try_send_number_state_, state); } void APIConnection::send_number_info(number::Number *number) { - if (!APIConnection::try_send_number_info(this, number)) { - this->deferred_message_queue_.defer(number, try_send_number_info); - } + this->send_info_(number, &APIConnection::try_send_number_info_); } -bool APIConnection::try_send_number_state(APIConnection *api, void *v_number) { - number::Number *number = reinterpret_cast(v_number); - return APIConnection::try_send_number_state(api, number, number->state); +bool APIConnection::try_send_number_state_(number::Number *number) { + return this->try_send_number_state_(number, number->state); } -bool APIConnection::try_send_number_state(APIConnection *api, number::Number *number, float state) { - NumberStateResponse resp{}; - resp.key = number->get_object_id_hash(); +bool APIConnection::try_send_number_state_(number::Number *number, float state) { + NumberStateResponse resp; resp.state = state; resp.missing_state = !number->has_state(); - return api->send_number_state_response(resp); + + resp.key = number->get_object_id_hash(); + return this->send_number_state_response(resp); } -bool APIConnection::try_send_number_info(APIConnection *api, void *v_number) { - number::Number *number = reinterpret_cast(v_number); +bool APIConnection::try_send_number_info_(number::Number *number) { ListEntitiesNumberResponse msg; - msg.key = number->get_object_id_hash(); - msg.object_id = number->get_object_id(); - if (number->has_own_name()) - msg.name = number->get_name(); - msg.unique_id = get_default_unique_id("number", number); - msg.icon = number->get_icon(); - msg.disabled_by_default = number->is_disabled_by_default(); - msg.entity_category = static_cast(number->get_entity_category()); 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(); - - return api->send_list_entities_number_response(msg); + return this->try_send_entity_info_(number, msg, &APIConnection::send_list_entities_number_response); } void APIConnection::number_command(const NumberCommandRequest &msg) { number::Number *number = App.get_number_by_key(msg.key); @@ -905,43 +734,24 @@ void APIConnection::number_command(const NumberCommandRequest &msg) { #ifdef USE_DATETIME_DATE bool APIConnection::send_date_state(datetime::DateEntity *date) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_date_state(this, date)) { - this->deferred_message_queue_.defer(date, try_send_date_state); - } - - return true; + return this->send_state_(date, &APIConnection::try_send_date_state_); } void APIConnection::send_date_info(datetime::DateEntity *date) { - if (!APIConnection::try_send_date_info(this, date)) { - this->deferred_message_queue_.defer(date, try_send_date_info); - } + this->send_info_(date, &APIConnection::try_send_date_info_); } -bool APIConnection::try_send_date_state(APIConnection *api, void *v_date) { - datetime::DateEntity *date = reinterpret_cast(v_date); - DateStateResponse resp{}; - resp.key = date->get_object_id_hash(); +bool APIConnection::try_send_date_state_(datetime::DateEntity *date) { + DateStateResponse resp; resp.missing_state = !date->has_state(); resp.year = date->year; resp.month = date->month; resp.day = date->day; - return api->send_date_state_response(resp); -} -bool APIConnection::try_send_date_info(APIConnection *api, void *v_date) { - datetime::DateEntity *date = reinterpret_cast(v_date); - ListEntitiesDateResponse msg; - msg.key = date->get_object_id_hash(); - msg.object_id = date->get_object_id(); - if (date->has_own_name()) - msg.name = date->get_name(); - msg.unique_id = get_default_unique_id("date", date); - msg.icon = date->get_icon(); - msg.disabled_by_default = date->is_disabled_by_default(); - msg.entity_category = static_cast(date->get_entity_category()); - return api->send_list_entities_date_response(msg); + resp.key = date->get_object_id_hash(); + return this->send_date_state_response(resp); +} +bool APIConnection::try_send_date_info_(datetime::DateEntity *date) { + ListEntitiesDateResponse msg; + return this->try_send_entity_info_(date, msg, &APIConnection::send_list_entities_date_response); } void APIConnection::date_command(const DateCommandRequest &msg) { datetime::DateEntity *date = App.get_date_by_key(msg.key); @@ -956,43 +766,24 @@ void APIConnection::date_command(const DateCommandRequest &msg) { #ifdef USE_DATETIME_TIME bool APIConnection::send_time_state(datetime::TimeEntity *time) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_time_state(this, time)) { - this->deferred_message_queue_.defer(time, try_send_time_state); - } - - return true; + return this->send_state_(time, &APIConnection::try_send_time_state_); } void APIConnection::send_time_info(datetime::TimeEntity *time) { - if (!APIConnection::try_send_time_info(this, time)) { - this->deferred_message_queue_.defer(time, try_send_time_info); - } + this->send_info_(time, &APIConnection::try_send_time_info_); } -bool APIConnection::try_send_time_state(APIConnection *api, void *v_time) { - datetime::TimeEntity *time = reinterpret_cast(v_time); - TimeStateResponse resp{}; - resp.key = time->get_object_id_hash(); +bool APIConnection::try_send_time_state_(datetime::TimeEntity *time) { + TimeStateResponse resp; resp.missing_state = !time->has_state(); resp.hour = time->hour; resp.minute = time->minute; resp.second = time->second; - return api->send_time_state_response(resp); -} -bool APIConnection::try_send_time_info(APIConnection *api, void *v_time) { - datetime::TimeEntity *time = reinterpret_cast(v_time); - ListEntitiesTimeResponse msg; - msg.key = time->get_object_id_hash(); - msg.object_id = time->get_object_id(); - if (time->has_own_name()) - msg.name = time->get_name(); - msg.unique_id = get_default_unique_id("time", time); - msg.icon = time->get_icon(); - msg.disabled_by_default = time->is_disabled_by_default(); - msg.entity_category = static_cast(time->get_entity_category()); - return api->send_list_entities_time_response(msg); + resp.key = time->get_object_id_hash(); + return this->send_time_state_response(resp); +} +bool APIConnection::try_send_time_info_(datetime::TimeEntity *time) { + ListEntitiesTimeResponse msg; + return this->try_send_entity_info_(time, msg, &APIConnection::send_list_entities_time_response); } void APIConnection::time_command(const TimeCommandRequest &msg) { datetime::TimeEntity *time = App.get_time_by_key(msg.key); @@ -1007,44 +798,25 @@ void APIConnection::time_command(const TimeCommandRequest &msg) { #ifdef USE_DATETIME_DATETIME bool APIConnection::send_datetime_state(datetime::DateTimeEntity *datetime) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_datetime_state(this, datetime)) { - this->deferred_message_queue_.defer(datetime, try_send_datetime_state); - } - - return true; + return this->send_state_(datetime, &APIConnection::try_send_datetime_state_); } void APIConnection::send_datetime_info(datetime::DateTimeEntity *datetime) { - if (!APIConnection::try_send_datetime_info(this, datetime)) { - this->deferred_message_queue_.defer(datetime, try_send_datetime_info); - } + this->send_info_(datetime, &APIConnection::try_send_datetime_info_); } -bool APIConnection::try_send_datetime_state(APIConnection *api, void *v_datetime) { - datetime::DateTimeEntity *datetime = reinterpret_cast(v_datetime); - DateTimeStateResponse resp{}; - resp.key = datetime->get_object_id_hash(); +bool APIConnection::try_send_datetime_state_(datetime::DateTimeEntity *datetime) { + DateTimeStateResponse resp; resp.missing_state = !datetime->has_state(); if (datetime->has_state()) { ESPTime state = datetime->state_as_esptime(); resp.epoch_seconds = state.timestamp; } - return api->send_date_time_state_response(resp); -} -bool APIConnection::try_send_datetime_info(APIConnection *api, void *v_datetime) { - datetime::DateTimeEntity *datetime = reinterpret_cast(v_datetime); - ListEntitiesDateTimeResponse msg; - msg.key = datetime->get_object_id_hash(); - msg.object_id = datetime->get_object_id(); - if (datetime->has_own_name()) - msg.name = datetime->get_name(); - msg.unique_id = get_default_unique_id("datetime", datetime); - msg.icon = datetime->get_icon(); - msg.disabled_by_default = datetime->is_disabled_by_default(); - msg.entity_category = static_cast(datetime->get_entity_category()); - return api->send_list_entities_date_time_response(msg); + resp.key = datetime->get_object_id_hash(); + return this->send_date_time_state_response(resp); +} +bool APIConnection::try_send_datetime_info_(datetime::DateTimeEntity *datetime) { + ListEntitiesDateTimeResponse msg; + return this->try_send_entity_info_(datetime, msg, &APIConnection::send_list_entities_date_time_response); } void APIConnection::datetime_command(const DateTimeCommandRequest &msg) { datetime::DateTimeEntity *datetime = App.get_datetime_by_key(msg.key); @@ -1059,47 +831,26 @@ void APIConnection::datetime_command(const DateTimeCommandRequest &msg) { #ifdef USE_TEXT bool APIConnection::send_text_state(text::Text *text, std::string state) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_text_state(this, text, std::move(state))) { - this->deferred_message_queue_.defer(text, try_send_text_state); - } - - return true; + return this->send_state_with_value_(text, &APIConnection::try_send_text_state_, &APIConnection::try_send_text_state_, + state); } -void APIConnection::send_text_info(text::Text *text) { - if (!APIConnection::try_send_text_info(this, text)) { - this->deferred_message_queue_.defer(text, try_send_text_info); - } -} -bool APIConnection::try_send_text_state(APIConnection *api, void *v_text) { - text::Text *text = reinterpret_cast(v_text); - return APIConnection::try_send_text_state(api, text, text->state); -} -bool APIConnection::try_send_text_state(APIConnection *api, text::Text *text, std::string state) { - TextStateResponse resp{}; - resp.key = text->get_object_id_hash(); +void APIConnection::send_text_info(text::Text *text) { this->send_info_(text, &APIConnection::try_send_text_info_); } +bool APIConnection::try_send_text_state_(text::Text *text) { return this->try_send_text_state_(text, text->state); } +bool APIConnection::try_send_text_state_(text::Text *text, std::string state) { + TextStateResponse resp; resp.state = std::move(state); resp.missing_state = !text->has_state(); - return api->send_text_state_response(resp); -} -bool APIConnection::try_send_text_info(APIConnection *api, void *v_text) { - text::Text *text = reinterpret_cast(v_text); - ListEntitiesTextResponse msg; - msg.key = text->get_object_id_hash(); - msg.object_id = text->get_object_id(); - msg.name = text->get_name(); - msg.icon = text->get_icon(); - msg.disabled_by_default = text->is_disabled_by_default(); - msg.entity_category = static_cast(text->get_entity_category()); - msg.mode = static_cast(text->traits.get_mode()); + resp.key = text->get_object_id_hash(); + return this->send_text_state_response(resp); +} +bool APIConnection::try_send_text_info_(text::Text *text) { + ListEntitiesTextResponse msg; + 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(); - - return api->send_list_entities_text_response(msg); + return this->try_send_entity_info_(text, msg, &APIConnection::send_list_entities_text_response); } void APIConnection::text_command(const TextCommandRequest &msg) { text::Text *text = App.get_text_by_key(msg.key); @@ -1114,47 +865,28 @@ void APIConnection::text_command(const TextCommandRequest &msg) { #ifdef USE_SELECT bool APIConnection::send_select_state(select::Select *select, std::string state) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_select_state(this, select, std::move(state))) { - this->deferred_message_queue_.defer(select, try_send_select_state); - } - - return true; + return this->send_state_with_value_(select, &APIConnection::try_send_select_state_, + &APIConnection::try_send_select_state_, state); } void APIConnection::send_select_info(select::Select *select) { - if (!APIConnection::try_send_select_info(this, select)) { - this->deferred_message_queue_.defer(select, try_send_select_info); - } + this->send_info_(select, &APIConnection::try_send_select_info_); } -bool APIConnection::try_send_select_state(APIConnection *api, void *v_select) { - select::Select *select = reinterpret_cast(v_select); - return APIConnection::try_send_select_state(api, select, select->state); +bool APIConnection::try_send_select_state_(select::Select *select) { + return this->try_send_select_state_(select, select->state); } -bool APIConnection::try_send_select_state(APIConnection *api, select::Select *select, std::string state) { - SelectStateResponse resp{}; - resp.key = select->get_object_id_hash(); +bool APIConnection::try_send_select_state_(select::Select *select, std::string state) { + SelectStateResponse resp; resp.state = std::move(state); resp.missing_state = !select->has_state(); - return api->send_select_state_response(resp); -} -bool APIConnection::try_send_select_info(APIConnection *api, void *v_select) { - select::Select *select = reinterpret_cast(v_select); - ListEntitiesSelectResponse msg; - msg.key = select->get_object_id_hash(); - msg.object_id = select->get_object_id(); - if (select->has_own_name()) - msg.name = select->get_name(); - msg.unique_id = get_default_unique_id("select", select); - msg.icon = select->get_icon(); - msg.disabled_by_default = select->is_disabled_by_default(); - msg.entity_category = static_cast(select->get_entity_category()); + resp.key = select->get_object_id_hash(); + return this->send_select_state_response(resp); +} +bool APIConnection::try_send_select_info_(select::Select *select) { + ListEntitiesSelectResponse msg; for (const auto &option : select->traits.get_options()) msg.options.push_back(option); - - return api->send_list_entities_select_response(msg); + return this->try_send_entity_info_(select, msg, &APIConnection::send_list_entities_select_response); } void APIConnection::select_command(const SelectCommandRequest &msg) { select::Select *select = App.get_select_by_key(msg.key); @@ -1168,26 +900,15 @@ void APIConnection::select_command(const SelectCommandRequest &msg) { #endif #ifdef USE_BUTTON -void APIConnection::send_button_info(button::Button *button) { - if (!APIConnection::try_send_button_info(this, button)) { - this->deferred_message_queue_.defer(button, try_send_button_info); - } +void esphome::api::APIConnection::send_button_info(button::Button *button) { + this->send_info_(button, &APIConnection::try_send_button_info_); } -bool APIConnection::try_send_button_info(APIConnection *api, void *v_button) { - button::Button *button = reinterpret_cast(v_button); +bool esphome::api::APIConnection::try_send_button_info_(button::Button *button) { ListEntitiesButtonResponse msg; - msg.key = button->get_object_id_hash(); - msg.object_id = button->get_object_id(); - if (button->has_own_name()) - msg.name = button->get_name(); - msg.unique_id = get_default_unique_id("button", button); - msg.icon = button->get_icon(); - msg.disabled_by_default = button->is_disabled_by_default(); - msg.entity_category = static_cast(button->get_entity_category()); msg.device_class = button->get_device_class(); - return api->send_list_entities_button_response(msg); + return this->try_send_entity_info_(button, msg, &APIConnection::send_list_entities_button_response); } -void APIConnection::button_command(const ButtonCommandRequest &msg) { +void esphome::api::APIConnection::button_command(const ButtonCommandRequest &msg) { button::Button *button = App.get_button_by_key(msg.key); if (button == nullptr) return; @@ -1198,45 +919,28 @@ void APIConnection::button_command(const ButtonCommandRequest &msg) { #ifdef USE_LOCK bool APIConnection::send_lock_state(lock::Lock *a_lock, lock::LockState state) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_lock_state(this, a_lock, state)) { - this->deferred_message_queue_.defer(a_lock, try_send_lock_state); - } - - return true; + return this->send_state_with_value_(a_lock, &APIConnection::try_send_lock_state_, + &APIConnection::try_send_lock_state_, state); } void APIConnection::send_lock_info(lock::Lock *a_lock) { - if (!APIConnection::try_send_lock_info(this, a_lock)) { - this->deferred_message_queue_.defer(a_lock, try_send_lock_info); - } + this->send_info_(a_lock, &APIConnection::try_send_lock_info_); } -bool APIConnection::try_send_lock_state(APIConnection *api, void *v_a_lock) { - lock::Lock *a_lock = reinterpret_cast(v_a_lock); - return APIConnection::try_send_lock_state(api, a_lock, a_lock->state); +bool APIConnection::try_send_lock_state_(lock::Lock *a_lock) { + return this->try_send_lock_state_(a_lock, a_lock->state); } -bool APIConnection::try_send_lock_state(APIConnection *api, lock::Lock *a_lock, lock::LockState state) { - LockStateResponse resp{}; - resp.key = a_lock->get_object_id_hash(); +bool APIConnection::try_send_lock_state_(lock::Lock *a_lock, lock::LockState state) { + LockStateResponse resp; resp.state = static_cast(state); - return api->send_lock_state_response(resp); + + resp.key = a_lock->get_object_id_hash(); + return this->send_lock_state_response(resp); } -bool APIConnection::try_send_lock_info(APIConnection *api, void *v_a_lock) { - lock::Lock *a_lock = reinterpret_cast(v_a_lock); +bool APIConnection::try_send_lock_info_(lock::Lock *a_lock) { ListEntitiesLockResponse msg; - msg.key = a_lock->get_object_id_hash(); - msg.object_id = a_lock->get_object_id(); - if (a_lock->has_own_name()) - msg.name = a_lock->get_name(); - msg.unique_id = get_default_unique_id("lock", a_lock); - msg.icon = a_lock->get_icon(); msg.assumed_state = a_lock->traits.get_assumed_state(); - msg.disabled_by_default = a_lock->is_disabled_by_default(); - msg.entity_category = static_cast(a_lock->get_entity_category()); msg.supports_open = a_lock->traits.get_supports_open(); msg.requires_code = a_lock->traits.get_requires_code(); - return api->send_list_entities_lock_response(msg); + return this->try_send_entity_info_(a_lock, msg, &APIConnection::send_list_entities_lock_response); } void APIConnection::lock_command(const LockCommandRequest &msg) { lock::Lock *a_lock = App.get_lock_by_key(msg.key); @@ -1259,45 +963,27 @@ void APIConnection::lock_command(const LockCommandRequest &msg) { #ifdef USE_VALVE bool APIConnection::send_valve_state(valve::Valve *valve) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_valve_state(this, valve)) { - this->deferred_message_queue_.defer(valve, try_send_valve_state); - } - - return true; + return this->send_state_(valve, &APIConnection::try_send_valve_state_); } void APIConnection::send_valve_info(valve::Valve *valve) { - if (!APIConnection::try_send_valve_info(this, valve)) { - this->deferred_message_queue_.defer(valve, try_send_valve_info); - } + this->send_info_(valve, &APIConnection::try_send_valve_info_); } -bool APIConnection::try_send_valve_state(APIConnection *api, void *v_valve) { - valve::Valve *valve = reinterpret_cast(v_valve); - ValveStateResponse resp{}; - resp.key = valve->get_object_id_hash(); +bool APIConnection::try_send_valve_state_(valve::Valve *valve) { + ValveStateResponse resp; resp.position = valve->position; resp.current_operation = static_cast(valve->current_operation); - return api->send_valve_state_response(resp); + + resp.key = valve->get_object_id_hash(); + return this->send_valve_state_response(resp); } -bool APIConnection::try_send_valve_info(APIConnection *api, void *v_valve) { - valve::Valve *valve = reinterpret_cast(v_valve); - auto traits = valve->get_traits(); +bool APIConnection::try_send_valve_info_(valve::Valve *valve) { ListEntitiesValveResponse msg; - msg.key = valve->get_object_id_hash(); - msg.object_id = valve->get_object_id(); - if (valve->has_own_name()) - msg.name = valve->get_name(); - msg.unique_id = get_default_unique_id("valve", valve); - msg.icon = valve->get_icon(); - msg.disabled_by_default = valve->is_disabled_by_default(); - msg.entity_category = static_cast(valve->get_entity_category()); + 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(); - return api->send_list_entities_valve_response(msg); + return this->try_send_entity_info_(valve, msg, &APIConnection::send_list_entities_valve_response); } void APIConnection::valve_command(const ValveCommandRequest &msg) { valve::Valve *valve = App.get_valve_by_key(msg.key); @@ -1315,48 +1001,27 @@ void APIConnection::valve_command(const ValveCommandRequest &msg) { #ifdef USE_MEDIA_PLAYER bool APIConnection::send_media_player_state(media_player::MediaPlayer *media_player) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_media_player_state(this, media_player)) { - this->deferred_message_queue_.defer(media_player, try_send_media_player_state); - } - - return true; + return this->send_state_(media_player, &APIConnection::try_send_media_player_state_); } void APIConnection::send_media_player_info(media_player::MediaPlayer *media_player) { - if (!APIConnection::try_send_media_player_info(this, media_player)) { - this->deferred_message_queue_.defer(media_player, try_send_media_player_info); - } + this->send_info_(media_player, &APIConnection::try_send_media_player_info_); } -bool APIConnection::try_send_media_player_state(APIConnection *api, void *v_media_player) { - media_player::MediaPlayer *media_player = reinterpret_cast(v_media_player); - MediaPlayerStateResponse resp{}; - resp.key = media_player->get_object_id_hash(); - +bool APIConnection::try_send_media_player_state_(media_player::MediaPlayer *media_player) { + MediaPlayerStateResponse resp; media_player::MediaPlayerState report_state = media_player->state == media_player::MEDIA_PLAYER_STATE_ANNOUNCING ? media_player::MEDIA_PLAYER_STATE_PLAYING : media_player->state; resp.state = static_cast(report_state); resp.volume = media_player->volume; resp.muted = media_player->is_muted(); - return api->send_media_player_state_response(resp); -} -bool APIConnection::try_send_media_player_info(APIConnection *api, void *v_media_player) { - media_player::MediaPlayer *media_player = reinterpret_cast(v_media_player); - ListEntitiesMediaPlayerResponse msg; - msg.key = media_player->get_object_id_hash(); - msg.object_id = media_player->get_object_id(); - if (media_player->has_own_name()) - msg.name = media_player->get_name(); - msg.unique_id = get_default_unique_id("media_player", media_player); - msg.icon = media_player->get_icon(); - msg.disabled_by_default = media_player->is_disabled_by_default(); - msg.entity_category = static_cast(media_player->get_entity_category()); + resp.key = media_player->get_object_id_hash(); + return this->send_media_player_state_response(resp); +} +bool APIConnection::try_send_media_player_info_(media_player::MediaPlayer *media_player) { + ListEntitiesMediaPlayerResponse msg; auto traits = media_player->get_traits(); msg.supports_pause = traits.get_supports_pause(); - for (auto &supported_format : traits.get_supported_formats()) { MediaPlayerSupportedFormat media_format; media_format.format = supported_format.format; @@ -1366,8 +1031,7 @@ bool APIConnection::try_send_media_player_info(APIConnection *api, void *v_media media_format.sample_bytes = supported_format.sample_bytes; msg.supported_formats.push_back(media_format); } - - return api->send_list_entities_media_player_response(msg); + return this->try_send_entity_info_(media_player, msg, &APIConnection::send_list_entities_media_player_response); } void APIConnection::media_player_command(const MediaPlayerCommandRequest &msg) { media_player::MediaPlayer *media_player = App.get_media_player_by_key(msg.key); @@ -1402,22 +1066,11 @@ void APIConnection::set_camera_state(std::shared_ptr this->image_reader_.set_image(std::move(image)); } void APIConnection::send_camera_info(esp32_camera::ESP32Camera *camera) { - if (!APIConnection::try_send_camera_info(this, camera)) { - this->deferred_message_queue_.defer(camera, try_send_camera_info); - } + this->send_info_(camera, &APIConnection::try_send_camera_info_); } -bool APIConnection::try_send_camera_info(APIConnection *api, void *v_camera) { - esp32_camera::ESP32Camera *camera = reinterpret_cast(v_camera); +bool APIConnection::try_send_camera_info_(esp32_camera::ESP32Camera *camera) { ListEntitiesCameraResponse msg; - msg.key = camera->get_object_id_hash(); - msg.object_id = camera->get_object_id(); - if (camera->has_own_name()) - msg.name = camera->get_name(); - msg.unique_id = get_default_unique_id("camera", camera); - msg.disabled_by_default = camera->is_disabled_by_default(); - msg.icon = camera->get_icon(); - msg.entity_category = static_cast(camera->get_entity_category()); - return api->send_list_entities_camera_response(msg); + return this->try_send_entity_info_(camera, msg, &APIConnection::send_list_entities_camera_response); } void APIConnection::camera_image(const CameraImageRequest &msg) { if (esp32_camera::global_esp32_camera == nullptr) @@ -1606,43 +1259,25 @@ 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) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_alarm_control_panel_state(this, a_alarm_control_panel)) { - this->deferred_message_queue_.defer(a_alarm_control_panel, try_send_alarm_control_panel_state); - } - - return true; + return this->send_state_(a_alarm_control_panel, &APIConnection::try_send_alarm_control_panel_state_); } void APIConnection::send_alarm_control_panel_info(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) { - if (!APIConnection::try_send_alarm_control_panel_info(this, a_alarm_control_panel)) { - this->deferred_message_queue_.defer(a_alarm_control_panel, try_send_alarm_control_panel_info); - } + this->send_info_(a_alarm_control_panel, &APIConnection::try_send_alarm_control_panel_info_); } -bool APIConnection::try_send_alarm_control_panel_state(APIConnection *api, void *v_a_alarm_control_panel) { - alarm_control_panel::AlarmControlPanel *a_alarm_control_panel = - reinterpret_cast(v_a_alarm_control_panel); - AlarmControlPanelStateResponse resp{}; - resp.key = a_alarm_control_panel->get_object_id_hash(); +bool APIConnection::try_send_alarm_control_panel_state_(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) { + AlarmControlPanelStateResponse resp; resp.state = static_cast(a_alarm_control_panel->get_state()); - return api->send_alarm_control_panel_state_response(resp); + + resp.key = a_alarm_control_panel->get_object_id_hash(); + return this->send_alarm_control_panel_state_response(resp); } -bool APIConnection::try_send_alarm_control_panel_info(APIConnection *api, void *v_a_alarm_control_panel) { - alarm_control_panel::AlarmControlPanel *a_alarm_control_panel = - reinterpret_cast(v_a_alarm_control_panel); +bool APIConnection::try_send_alarm_control_panel_info_(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) { ListEntitiesAlarmControlPanelResponse msg; - msg.key = a_alarm_control_panel->get_object_id_hash(); - msg.object_id = a_alarm_control_panel->get_object_id(); - msg.name = a_alarm_control_panel->get_name(); - msg.unique_id = get_default_unique_id("alarm_control_panel", a_alarm_control_panel); - msg.icon = a_alarm_control_panel->get_icon(); - msg.disabled_by_default = a_alarm_control_panel->is_disabled_by_default(); - msg.entity_category = static_cast(a_alarm_control_panel->get_entity_category()); 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(); - return api->send_list_entities_alarm_control_panel_response(msg); + return this->try_send_entity_info_(a_alarm_control_panel, msg, + &APIConnection::send_list_entities_alarm_control_panel_response); } 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); @@ -1680,63 +1315,40 @@ void APIConnection::alarm_control_panel_command(const AlarmControlPanelCommandRe #ifdef USE_EVENT void APIConnection::send_event(event::Event *event, std::string event_type) { - if (!APIConnection::try_send_event(this, event, std::move(event_type))) { - this->deferred_message_queue_.defer(event, try_send_event); - } + this->send_state_with_value_(event, &APIConnection::try_send_event_, &APIConnection::try_send_event_, + std::move(event_type)); } void APIConnection::send_event_info(event::Event *event) { - if (!APIConnection::try_send_event_info(this, event)) { - this->deferred_message_queue_.defer(event, try_send_event_info); - } + this->send_info_(event, &APIConnection::try_send_event_info_); } -bool APIConnection::try_send_event(APIConnection *api, void *v_event) { - event::Event *event = reinterpret_cast(v_event); - return APIConnection::try_send_event(api, event, *(event->last_event_type)); +bool APIConnection::try_send_event_(event::Event *event) { + return this->try_send_event_(event, *(event->last_event_type)); } -bool APIConnection::try_send_event(APIConnection *api, event::Event *event, std::string event_type) { - EventResponse resp{}; - resp.key = event->get_object_id_hash(); +bool APIConnection::try_send_event_(event::Event *event, std::string event_type) { + EventResponse resp; resp.event_type = std::move(event_type); - return api->send_event_response(resp); + + resp.key = event->get_object_id_hash(); + return this->send_event_response(resp); } -bool APIConnection::try_send_event_info(APIConnection *api, void *v_event) { - event::Event *event = reinterpret_cast(v_event); +bool APIConnection::try_send_event_info_(event::Event *event) { ListEntitiesEventResponse msg; - msg.key = event->get_object_id_hash(); - msg.object_id = event->get_object_id(); - if (event->has_own_name()) - msg.name = event->get_name(); - msg.unique_id = get_default_unique_id("event", event); - msg.icon = event->get_icon(); - msg.disabled_by_default = event->is_disabled_by_default(); - msg.entity_category = static_cast(event->get_entity_category()); msg.device_class = event->get_device_class(); for (const auto &event_type : event->get_event_types()) msg.event_types.push_back(event_type); - return api->send_list_entities_event_response(msg); + return this->try_send_entity_info_(event, msg, &APIConnection::send_list_entities_event_response); } #endif #ifdef USE_UPDATE bool APIConnection::send_update_state(update::UpdateEntity *update) { - if (!this->state_subscription_) - return false; - - if (!APIConnection::try_send_update_state(this, update)) { - this->deferred_message_queue_.defer(update, try_send_update_state); - } - - return true; + return this->send_state_(update, &APIConnection::try_send_update_state_); } void APIConnection::send_update_info(update::UpdateEntity *update) { - if (!APIConnection::try_send_update_info(this, update)) { - this->deferred_message_queue_.defer(update, try_send_update_info); - } + this->send_info_(update, &APIConnection::try_send_update_info_); } -bool APIConnection::try_send_update_state(APIConnection *api, void *v_update) { - update::UpdateEntity *update = reinterpret_cast(v_update); - UpdateStateResponse resp{}; - resp.key = update->get_object_id_hash(); +bool APIConnection::try_send_update_state_(update::UpdateEntity *update) { + UpdateStateResponse resp; resp.missing_state = !update->has_state(); if (update->has_state()) { resp.in_progress = update->state == update::UpdateState::UPDATE_STATE_INSTALLING; @@ -1751,21 +1363,13 @@ bool APIConnection::try_send_update_state(APIConnection *api, void *v_update) { resp.release_url = update->update_info.release_url; } - return api->send_update_state_response(resp); + resp.key = update->get_object_id_hash(); + return this->send_update_state_response(resp); } -bool APIConnection::try_send_update_info(APIConnection *api, void *v_update) { - update::UpdateEntity *update = reinterpret_cast(v_update); +bool APIConnection::try_send_update_info_(update::UpdateEntity *update) { ListEntitiesUpdateResponse msg; - msg.key = update->get_object_id_hash(); - msg.object_id = update->get_object_id(); - if (update->has_own_name()) - msg.name = update->get_name(); - msg.unique_id = get_default_unique_id("update", update); - msg.icon = update->get_icon(); - msg.disabled_by_default = update->is_disabled_by_default(); - msg.entity_category = static_cast(update->get_entity_category()); msg.device_class = update->get_device_class(); - return api->send_list_entities_update_response(msg); + return this->try_send_entity_info_(update, msg, &APIConnection::send_list_entities_update_response); } void APIConnection::update_command(const UpdateCommandRequest &msg) { update::UpdateEntity *update = App.get_update_by_key(msg.key); diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index 1e47418d90..06981976ff 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -12,9 +12,14 @@ #include namespace esphome { +class EntityBase; namespace api { -using send_message_t = bool(APIConnection *, void *); +// Forward declaration +std::string get_default_unique_id(const std::string &component_type, EntityBase *entity); + +// Function signature for deferred message handlers +using send_message_t = bool (APIConnection::*)(void *); /* This class holds a pointer to the source component that wants to publish a message, and a pointer to a function that @@ -30,10 +35,10 @@ class DeferredMessageQueue { protected: void *source_; - send_message_t *send_message_; + send_message_t send_message_; public: - DeferredMessage(void *source, send_message_t *send_message) : source_(source), send_message_(send_message) {} + DeferredMessage(void *source, send_message_t send_message) : source_(source), send_message_(send_message) {} bool operator==(const DeferredMessage &test) const { return (source_ == test.source_ && send_message_ == test.send_message_); } @@ -46,12 +51,12 @@ class DeferredMessageQueue { APIConnection *api_connection_; // helper for allowing only unique entries in the queue - void dmq_push_back_with_dedup_(void *source, send_message_t *send_message); + void dmq_push_back_with_dedup_(void *source, send_message_t send_message); public: DeferredMessageQueue(APIConnection *api_connection) : api_connection_(api_connection) {} void process_queue(); - void defer(void *source, send_message_t *send_message); + void defer(void *source, send_message_t send_message); }; class APIConnection : public APIServerConnection { @@ -69,137 +74,213 @@ class APIConnection : public APIServerConnection { #ifdef USE_BINARY_SENSOR bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor, bool state); void send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor); - static bool try_send_binary_sensor_state(APIConnection *api, void *v_binary_sensor); - static bool try_send_binary_sensor_state(APIConnection *api, binary_sensor::BinarySensor *binary_sensor, bool state); - static bool try_send_binary_sensor_info(APIConnection *api, void *v_binary_sensor); + + protected: + bool try_send_binary_sensor_state_(binary_sensor::BinarySensor *binary_sensor); + bool try_send_binary_sensor_state_(binary_sensor::BinarySensor *binary_sensor, bool state); + bool try_send_binary_sensor_info_(binary_sensor::BinarySensor *binary_sensor); + + public: #endif #ifdef USE_COVER bool send_cover_state(cover::Cover *cover); void send_cover_info(cover::Cover *cover); - static bool try_send_cover_state(APIConnection *api, void *v_cover); - static bool try_send_cover_info(APIConnection *api, void *v_cover); void cover_command(const CoverCommandRequest &msg) override; + + protected: + bool try_send_cover_state_(cover::Cover *cover); + bool try_send_cover_info_(cover::Cover *cover); + + public: #endif #ifdef USE_FAN bool send_fan_state(fan::Fan *fan); void send_fan_info(fan::Fan *fan); - static bool try_send_fan_state(APIConnection *api, void *v_fan); - static bool try_send_fan_info(APIConnection *api, void *v_fan); void fan_command(const FanCommandRequest &msg) override; + + protected: + bool try_send_fan_state_(fan::Fan *fan); + bool try_send_fan_info_(fan::Fan *fan); + + public: #endif #ifdef USE_LIGHT bool send_light_state(light::LightState *light); void send_light_info(light::LightState *light); - static bool try_send_light_state(APIConnection *api, void *v_light); - static bool try_send_light_info(APIConnection *api, void *v_light); void light_command(const LightCommandRequest &msg) override; + + protected: + bool try_send_light_state_(light::LightState *light); + bool try_send_light_info_(light::LightState *light); + + public: #endif #ifdef USE_SENSOR bool send_sensor_state(sensor::Sensor *sensor, float state); void send_sensor_info(sensor::Sensor *sensor); - static bool try_send_sensor_state(APIConnection *api, void *v_sensor); - static bool try_send_sensor_state(APIConnection *api, sensor::Sensor *sensor, float state); - static bool try_send_sensor_info(APIConnection *api, void *v_sensor); + + protected: + bool try_send_sensor_state_(sensor::Sensor *sensor); + bool try_send_sensor_state_(sensor::Sensor *sensor, float state); + bool try_send_sensor_info_(sensor::Sensor *sensor); + + public: #endif #ifdef USE_SWITCH bool send_switch_state(switch_::Switch *a_switch, bool state); void send_switch_info(switch_::Switch *a_switch); - static bool try_send_switch_state(APIConnection *api, void *v_a_switch); - static bool try_send_switch_state(APIConnection *api, switch_::Switch *a_switch, bool state); - static bool try_send_switch_info(APIConnection *api, void *v_a_switch); void switch_command(const SwitchCommandRequest &msg) override; + + protected: + bool try_send_switch_state_(switch_::Switch *a_switch); + bool try_send_switch_state_(switch_::Switch *a_switch, bool state); + bool try_send_switch_info_(switch_::Switch *a_switch); + + public: #endif #ifdef USE_TEXT_SENSOR bool send_text_sensor_state(text_sensor::TextSensor *text_sensor, std::string state); void send_text_sensor_info(text_sensor::TextSensor *text_sensor); - static bool try_send_text_sensor_state(APIConnection *api, void *v_text_sensor); - static bool try_send_text_sensor_state(APIConnection *api, text_sensor::TextSensor *text_sensor, std::string state); - static bool try_send_text_sensor_info(APIConnection *api, void *v_text_sensor); + + protected: + bool try_send_text_sensor_state_(text_sensor::TextSensor *text_sensor); + bool try_send_text_sensor_state_(text_sensor::TextSensor *text_sensor, std::string state); + bool try_send_text_sensor_info_(text_sensor::TextSensor *text_sensor); + + public: #endif #ifdef USE_ESP32_CAMERA void set_camera_state(std::shared_ptr image); void send_camera_info(esp32_camera::ESP32Camera *camera); - static bool try_send_camera_info(APIConnection *api, void *v_camera); void camera_image(const CameraImageRequest &msg) override; + + protected: + bool try_send_camera_info_(esp32_camera::ESP32Camera *camera); + + public: #endif #ifdef USE_CLIMATE bool send_climate_state(climate::Climate *climate); void send_climate_info(climate::Climate *climate); - static bool try_send_climate_state(APIConnection *api, void *v_climate); - static bool try_send_climate_info(APIConnection *api, void *v_climate); void climate_command(const ClimateCommandRequest &msg) override; + + protected: + bool try_send_climate_state_(climate::Climate *climate); + bool try_send_climate_info_(climate::Climate *climate); + + public: #endif #ifdef USE_NUMBER bool send_number_state(number::Number *number, float state); void send_number_info(number::Number *number); - static bool try_send_number_state(APIConnection *api, void *v_number); - static bool try_send_number_state(APIConnection *api, number::Number *number, float state); - static bool try_send_number_info(APIConnection *api, void *v_number); void number_command(const NumberCommandRequest &msg) override; + + protected: + bool try_send_number_state_(number::Number *number); + bool try_send_number_state_(number::Number *number, float state); + bool try_send_number_info_(number::Number *number); + + public: #endif #ifdef USE_DATETIME_DATE bool send_date_state(datetime::DateEntity *date); void send_date_info(datetime::DateEntity *date); - static bool try_send_date_state(APIConnection *api, void *v_date); - static bool try_send_date_info(APIConnection *api, void *v_date); void date_command(const DateCommandRequest &msg) override; + + protected: + bool try_send_date_state_(datetime::DateEntity *date); + bool try_send_date_info_(datetime::DateEntity *date); + + public: #endif #ifdef USE_DATETIME_TIME bool send_time_state(datetime::TimeEntity *time); void send_time_info(datetime::TimeEntity *time); - static bool try_send_time_state(APIConnection *api, void *v_time); - static bool try_send_time_info(APIConnection *api, void *v_time); void time_command(const TimeCommandRequest &msg) override; + + protected: + bool try_send_time_state_(datetime::TimeEntity *time); + bool try_send_time_info_(datetime::TimeEntity *time); + + public: #endif #ifdef USE_DATETIME_DATETIME bool send_datetime_state(datetime::DateTimeEntity *datetime); void send_datetime_info(datetime::DateTimeEntity *datetime); - static bool try_send_datetime_state(APIConnection *api, void *v_datetime); - static bool try_send_datetime_info(APIConnection *api, void *v_datetime); void datetime_command(const DateTimeCommandRequest &msg) override; + + protected: + bool try_send_datetime_state_(datetime::DateTimeEntity *datetime); + bool try_send_datetime_info_(datetime::DateTimeEntity *datetime); + + public: #endif #ifdef USE_TEXT bool send_text_state(text::Text *text, std::string state); void send_text_info(text::Text *text); - static bool try_send_text_state(APIConnection *api, void *v_text); - static bool try_send_text_state(APIConnection *api, text::Text *text, std::string state); - static bool try_send_text_info(APIConnection *api, void *v_text); void text_command(const TextCommandRequest &msg) override; + + protected: + bool try_send_text_state_(text::Text *text); + bool try_send_text_state_(text::Text *text, std::string state); + bool try_send_text_info_(text::Text *text); + + public: #endif #ifdef USE_SELECT bool send_select_state(select::Select *select, std::string state); void send_select_info(select::Select *select); - static bool try_send_select_state(APIConnection *api, void *v_select); - static bool try_send_select_state(APIConnection *api, select::Select *select, std::string state); - static bool try_send_select_info(APIConnection *api, void *v_select); void select_command(const SelectCommandRequest &msg) override; + + protected: + bool try_send_select_state_(select::Select *select); + bool try_send_select_state_(select::Select *select, std::string state); + bool try_send_select_info_(select::Select *select); + + public: #endif #ifdef USE_BUTTON void send_button_info(button::Button *button); - static bool try_send_button_info(APIConnection *api, void *v_button); void button_command(const ButtonCommandRequest &msg) override; + + protected: + bool try_send_button_info_(button::Button *button); + + public: #endif #ifdef USE_LOCK bool send_lock_state(lock::Lock *a_lock, lock::LockState state); void send_lock_info(lock::Lock *a_lock); - static bool try_send_lock_state(APIConnection *api, void *v_a_lock); - static bool try_send_lock_state(APIConnection *api, lock::Lock *a_lock, lock::LockState state); - static bool try_send_lock_info(APIConnection *api, void *v_a_lock); void lock_command(const LockCommandRequest &msg) override; + + protected: + bool try_send_lock_state_(lock::Lock *a_lock); + bool try_send_lock_state_(lock::Lock *a_lock, lock::LockState state); + bool try_send_lock_info_(lock::Lock *a_lock); + + public: #endif #ifdef USE_VALVE bool send_valve_state(valve::Valve *valve); void send_valve_info(valve::Valve *valve); - static bool try_send_valve_state(APIConnection *api, void *v_valve); - static bool try_send_valve_info(APIConnection *api, void *v_valve); void valve_command(const ValveCommandRequest &msg) override; + + protected: + bool try_send_valve_state_(valve::Valve *valve); + bool try_send_valve_info_(valve::Valve *valve); + + public: #endif #ifdef USE_MEDIA_PLAYER bool send_media_player_state(media_player::MediaPlayer *media_player); void send_media_player_info(media_player::MediaPlayer *media_player); - static bool try_send_media_player_state(APIConnection *api, void *v_media_player); - static bool try_send_media_player_info(APIConnection *api, void *v_media_player); void media_player_command(const MediaPlayerCommandRequest &msg) override; + + protected: + bool try_send_media_player_state_(media_player::MediaPlayer *media_player); + bool try_send_media_player_info_(media_player::MediaPlayer *media_player); + + public: #endif bool try_send_log_message(int level, const char *tag, const char *line); void send_homeassistant_service_call(const HomeassistantServiceResponse &call) { @@ -246,25 +327,37 @@ class APIConnection : public APIServerConnection { #ifdef USE_ALARM_CONTROL_PANEL bool send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel); void send_alarm_control_panel_info(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel); - static bool try_send_alarm_control_panel_state(APIConnection *api, void *v_a_alarm_control_panel); - static bool try_send_alarm_control_panel_info(APIConnection *api, void *v_a_alarm_control_panel); void alarm_control_panel_command(const AlarmControlPanelCommandRequest &msg) override; + + protected: + bool try_send_alarm_control_panel_state_(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel); + bool try_send_alarm_control_panel_info_(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel); + + public: #endif #ifdef USE_EVENT void send_event(event::Event *event, std::string event_type); void send_event_info(event::Event *event); - static bool try_send_event(APIConnection *api, void *v_event); - static bool try_send_event(APIConnection *api, event::Event *event, std::string event_type); - static bool try_send_event_info(APIConnection *api, void *v_event); + + protected: + bool try_send_event_(event::Event *event); + bool try_send_event_(event::Event *event, std::string event_type); + bool try_send_event_info_(event::Event *event); + + public: #endif #ifdef USE_UPDATE bool send_update_state(update::UpdateEntity *update); void send_update_info(update::UpdateEntity *update); - static bool try_send_update_state(APIConnection *api, void *v_update); - static bool try_send_update_info(APIConnection *api, void *v_update); void update_command(const UpdateCommandRequest &msg) override; + + protected: + bool try_send_update_state_(update::UpdateEntity *update); + bool try_send_update_info_(update::UpdateEntity *update); + + public: #endif void on_disconnect_response(const DisconnectResponse &value) override; @@ -320,11 +413,135 @@ class APIConnection : public APIServerConnection { } bool send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) override; + /** + * Check if the connection can write without blocking. + * This should be called before constructing any protobuf messages to avoid + * unnecessary message construction when the buffer is full. + */ + inline bool can_write_without_blocking() { return this->helper_->can_write_without_blocking(); } + std::string get_client_combined_info() const { return this->client_combined_info_; } + // Template helper methods are in the protected section + protected: friend APIServer; + /** + * Helper template that attempts to send a message only if the transmit buffer is available. + * Otherwise, defers the message to the queue. + * + * @tparam EntityT The entity type + * @tparam FuncT The function type (typically a member function pointer) + * @tparam ArgsT Additional argument types (if any) + * @param entity The entity to send the message for + * @param func The function to call for sending the message + * @param defer_func The function to defer if sending fails (may be the same as func) + * @param args Additional arguments to pass to the function + * @return True if successful or message deferred + */ + + /** + * Generic send entity state method to reduce code duplication. + * Only attempts to build and send the message if the transmit buffer is available. + * + * This is the base version for entities that use their current state. + * + * @param entity The entity to send state for + * @param try_send_func The function that tries to send the state + * @return True on success or message deferred, false if subscription check failed + */ + template bool send_state_(EntityT *entity, bool (APIConnection::*try_send_func)(EntityT *)) { + if (!this->state_subscription_) + return false; + if (this->can_write_without_blocking() && (this->*try_send_func)(entity)) { + return true; + } + this->deferred_message_queue_.defer(entity, reinterpret_cast(try_send_func)); + return true; + } + + /** + * Enhanced send entity state method that handles explicit state values. + * Only attempts to build and send the message if the transmit buffer is available. + * + * This overload accepts additional state parameter(s) to be used instead of the entity's current state. + * It works with specialized try_send functions that accept additional parameters. + * + * @tparam EntityT The entity type + * @tparam StateT Type of the state parameter + * @tparam Args Additional argument types (if any) + * @param entity The entity to send state for + * @param try_send_entity_func The function that tries to send the state with entity pointer only + * @param try_send_state_func The function that tries to send the state with entity and state parameters + * @param state The state value to send + * @param args Additional arguments to pass to the try_send_state_func + * @return True on success or message deferred, false if subscription check failed + */ + template + bool send_state_with_value_(EntityT *entity, bool (APIConnection::*try_send_entity_func)(EntityT *), + bool (APIConnection::*try_send_state_func)(EntityT *, StateT, Args...), StateT state, + Args... args) { + if (!this->state_subscription_) + return false; + if (this->can_write_without_blocking() && (this->*try_send_state_func)(entity, state, args...)) { + return true; + } + this->deferred_message_queue_.defer(entity, reinterpret_cast(try_send_entity_func)); + return true; + } + + /** + * Generic send entity info method to reduce code duplication. + * Only attempts to build and send the message if the transmit buffer is available. + * + * @param entity The entity to send info for + * @param try_send_func The function that tries to send the info + */ + template void send_info_(EntityT *entity, bool (APIConnection::*try_send_func)(EntityT *)) { + if (this->can_write_without_blocking() && (this->*try_send_func)(entity)) { + return; + } + this->deferred_message_queue_.defer(entity, reinterpret_cast(try_send_func)); + } + + /** + * Generic template for generating entity info response messages. + * This is used to reduce duplication in the try_send_*_info functions. + * + * @tparam EntityT The entity type + * @tparam ResponseT The protobuf list entities response type + * @param entity The entity to generate info for + * @param response The pre-populated response object with entity-specific fields + * @param send_response_func Function to send the response + * @return True if the message was sent successfully + */ + template + bool try_send_entity_info_(EntityT *entity, ResponseT &response, + bool (APIServerConnectionBase::*send_response_func)(const ResponseT &)) { + // Set common fields that are shared by all entity types + response.key = entity->get_object_id_hash(); + response.object_id = entity->get_object_id(); + + if (entity->has_own_name()) + response.name = entity->get_name(); + + // Get the unique_id - try the entity's implementation first + // This is safe because EntityBase has a default implementation that returns an empty string + response.unique_id = entity->unique_id(); + // Fall back to default if empty + if (response.unique_id.empty()) + response.unique_id = get_default_unique_id(entity->get_component_type(), entity); + + // Set common EntityBase properties + response.icon = entity->get_icon(); + response.disabled_by_default = entity->is_disabled_by_default(); + response.entity_category = static_cast(entity->get_entity_category()); + + // Send the response using the provided send method + return (this->*send_response_func)(response); + } + bool send_(const void *buf, size_t len, bool force); enum class ConnectionState { diff --git a/esphome/components/binary_sensor/binary_sensor.cpp b/esphome/components/binary_sensor/binary_sensor.cpp index 30fbe4f0b4..72842c1f5c 100644 --- a/esphome/components/binary_sensor/binary_sensor.cpp +++ b/esphome/components/binary_sensor/binary_sensor.cpp @@ -59,6 +59,8 @@ void BinarySensor::add_filters(const std::vector &filters) { bool BinarySensor::has_state() const { return this->has_state_; } bool BinarySensor::is_status_binary_sensor() const { return false; } +const char *BinarySensor::get_component_type() const { return "binary_sensor"; } + } // namespace binary_sensor } // namespace esphome diff --git a/esphome/components/binary_sensor/binary_sensor.h b/esphome/components/binary_sensor/binary_sensor.h index 9ba7aeeeff..9b5fb0dbe9 100644 --- a/esphome/components/binary_sensor/binary_sensor.h +++ b/esphome/components/binary_sensor/binary_sensor.h @@ -74,6 +74,9 @@ class BinarySensor : public EntityBase, public EntityBase_DeviceClass { virtual bool is_status_binary_sensor() const; + // Get the component type for this entity + const char *get_component_type() const override; + protected: CallbackManager state_callback_{}; Filter *filter_list_{nullptr}; diff --git a/esphome/components/button/button.cpp b/esphome/components/button/button.cpp index 4c4cb7740c..c4af6660a1 100644 --- a/esphome/components/button/button.cpp +++ b/esphome/components/button/button.cpp @@ -13,5 +13,7 @@ void Button::press() { } void Button::add_on_press_callback(std::function &&callback) { this->press_callback_.add(std::move(callback)); } +const char *Button::get_component_type() const { return "button"; } + } // namespace button } // namespace esphome diff --git a/esphome/components/button/button.h b/esphome/components/button/button.h index 9488eca221..9fbcf67b64 100644 --- a/esphome/components/button/button.h +++ b/esphome/components/button/button.h @@ -40,6 +40,8 @@ class Button : public EntityBase, public EntityBase_DeviceClass { */ void add_on_press_callback(std::function &&callback); + const char *get_component_type() const override; + protected: /** You should implement this virtual method if you want to create your own button. */ diff --git a/esphome/components/climate/climate.cpp b/esphome/components/climate/climate.cpp index bc8d932089..ff6ef22340 100644 --- a/esphome/components/climate/climate.cpp +++ b/esphome/components/climate/climate.cpp @@ -566,6 +566,8 @@ bool Climate::set_custom_preset_(const std::string &preset) { return set_alternative(this->custom_preset, this->preset, preset); } +const char *Climate::get_component_type() const { return "climate"; } + void Climate::dump_traits_(const char *tag) { auto traits = this->get_traits(); ESP_LOGCONFIG(tag, "ClimateTraits:"); diff --git a/esphome/components/climate/climate.h b/esphome/components/climate/climate.h index d81702fb0c..fc31caff3f 100644 --- a/esphome/components/climate/climate.h +++ b/esphome/components/climate/climate.h @@ -169,6 +169,8 @@ class Climate : public EntityBase { public: Climate() {} + const char *get_component_type() const override; + /// The active mode of the climate device. ClimateMode mode{CLIMATE_MODE_OFF}; diff --git a/esphome/components/cover/cover.cpp b/esphome/components/cover/cover.cpp index d139bab8ee..e2083d5e78 100644 --- a/esphome/components/cover/cover.cpp +++ b/esphome/components/cover/cover.cpp @@ -207,6 +207,7 @@ optional Cover::restore_state_() { bool Cover::is_fully_open() const { return this->position == COVER_OPEN; } bool Cover::is_fully_closed() const { return this->position == COVER_CLOSED; } +const char *Cover::get_component_type() const { return "cover"; } CoverCall CoverRestoreState::to_call(Cover *cover) { auto call = cover->make_call(); diff --git a/esphome/components/cover/cover.h b/esphome/components/cover/cover.h index 8b6f5b8a72..2b6e6ea659 100644 --- a/esphome/components/cover/cover.h +++ b/esphome/components/cover/cover.h @@ -163,6 +163,9 @@ class Cover : public EntityBase, public EntityBase_DeviceClass { /// Helper method to check if the cover is fully closed. Equivalent to comparing .position against 0.0 bool is_fully_closed() const; + // Get the component type for this entity + const char *get_component_type() const override; + protected: friend CoverCall; diff --git a/esphome/components/datetime/date_entity.h b/esphome/components/datetime/date_entity.h index ce43c5639d..bc71d4f67b 100644 --- a/esphome/components/datetime/date_entity.h +++ b/esphome/components/datetime/date_entity.h @@ -43,6 +43,8 @@ class DateEntity : public DateTimeBase { void publish_state(); DateCall make_call(); + const char *get_component_type() const override { return "date"; } + ESPTime state_as_esptime() const override { ESPTime obj; obj.year = this->year_; diff --git a/esphome/components/datetime/datetime_base.h b/esphome/components/datetime/datetime_base.h index dea34e6110..d94dfe9e8b 100644 --- a/esphome/components/datetime/datetime_base.h +++ b/esphome/components/datetime/datetime_base.h @@ -20,6 +20,8 @@ class DateTimeBase : public EntityBase { void add_on_state_callback(std::function &&callback) { this->state_callback_.add(std::move(callback)); } + virtual const char *get_component_type() const override = 0; + #ifdef USE_TIME void set_rtc(time::RealTimeClock *rtc) { this->rtc_ = rtc; } time::RealTimeClock *get_rtc() const { return this->rtc_; } diff --git a/esphome/components/datetime/datetime_entity.h b/esphome/components/datetime/datetime_entity.h index 27db84cf7e..5fa87861ad 100644 --- a/esphome/components/datetime/datetime_entity.h +++ b/esphome/components/datetime/datetime_entity.h @@ -49,6 +49,8 @@ class DateTimeEntity : public DateTimeBase { void publish_state(); DateTimeCall make_call(); + const char *get_component_type() const override { return "datetime"; } + ESPTime state_as_esptime() const override; const uint16_t &year = year_; diff --git a/esphome/components/datetime/time_entity.h b/esphome/components/datetime/time_entity.h index f7e0a7ddd9..e638f2f3ea 100644 --- a/esphome/components/datetime/time_entity.h +++ b/esphome/components/datetime/time_entity.h @@ -44,6 +44,8 @@ class TimeEntity : public DateTimeBase { void publish_state(); TimeCall make_call(); + const char *get_component_type() const override { return "time"; } + ESPTime state_as_esptime() const override { ESPTime obj; obj.hour = this->hour_; diff --git a/esphome/components/event/event.cpp b/esphome/components/event/event.cpp index d27b3b378e..561967d4c6 100644 --- a/esphome/components/event/event.cpp +++ b/esphome/components/event/event.cpp @@ -22,5 +22,7 @@ void Event::add_on_event_callback(std::functionevent_callback_.add(std::move(callback)); } +const char *Event::get_component_type() const { return "event"; } + } // namespace event } // namespace esphome diff --git a/esphome/components/event/event.h b/esphome/components/event/event.h index 03c3c8d95a..f4aba6d148 100644 --- a/esphome/components/event/event.h +++ b/esphome/components/event/event.h @@ -30,6 +30,8 @@ class Event : public EntityBase, public EntityBase_DeviceClass { std::set get_event_types() const { return this->types_; } void add_on_event_callback(std::function &&callback); + const char *get_component_type() const override; + protected: CallbackManager event_callback_; std::set types_; diff --git a/esphome/components/fan/fan.cpp b/esphome/components/fan/fan.cpp index 1d560d2fc6..353630420a 100644 --- a/esphome/components/fan/fan.cpp +++ b/esphome/components/fan/fan.cpp @@ -205,5 +205,7 @@ void Fan::dump_traits_(const char *tag, const char *prefix) { } } +const char *Fan::get_component_type() const { return "fan"; } + } // namespace fan } // namespace esphome diff --git a/esphome/components/fan/fan.h b/esphome/components/fan/fan.h index b74187eb4a..6d3b2c9bff 100644 --- a/esphome/components/fan/fan.h +++ b/esphome/components/fan/fan.h @@ -132,6 +132,9 @@ class Fan : public EntityBase { /// Set the restore mode of this fan. void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; } + // Get the component type for this entity + const char *get_component_type() const override; + protected: friend FanCall; diff --git a/esphome/components/light/light_state.cpp b/esphome/components/light/light_state.cpp index 16b78a17bd..8857cf38dd 100644 --- a/esphome/components/light/light_state.cpp +++ b/esphome/components/light/light_state.cpp @@ -11,6 +11,8 @@ static const char *const TAG = "light"; LightState::LightState(LightOutput *output) : output_(output) {} +const char *LightState::get_component_type() const { return "light"; } + LightTraits LightState::get_traits() { return this->output_->get_traits(); } LightCall LightState::turn_on() { return this->make_call().set_state(true); } LightCall LightState::turn_off() { return this->make_call().set_state(false); } diff --git a/esphome/components/light/light_state.h b/esphome/components/light/light_state.h index acba986f24..e84739010f 100644 --- a/esphome/components/light/light_state.h +++ b/esphome/components/light/light_state.h @@ -64,6 +64,8 @@ class LightState : public EntityBase, public Component { public: LightState(LightOutput *output); + const char *get_component_type() const override; + LightTraits get_traits(); /// Make a light state call diff --git a/esphome/components/lock/lock.cpp b/esphome/components/lock/lock.cpp index ddc5445349..46f53b2978 100644 --- a/esphome/components/lock/lock.cpp +++ b/esphome/components/lock/lock.cpp @@ -57,6 +57,8 @@ void Lock::publish_state(LockState state) { void Lock::add_on_state_callback(std::function &&callback) { this->state_callback_.add(std::move(callback)); } +const char *Lock::get_component_type() const { return "lock"; } + void LockCall::perform() { ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str()); this->validate_(); diff --git a/esphome/components/lock/lock.h b/esphome/components/lock/lock.h index 7a98187a4f..91fb631dc9 100644 --- a/esphome/components/lock/lock.h +++ b/esphome/components/lock/lock.h @@ -145,6 +145,8 @@ class Lock : public EntityBase { */ void add_on_state_callback(std::function &&callback); + const char *get_component_type() const override; + protected: friend LockCall; diff --git a/esphome/components/media_player/media_player.cpp b/esphome/components/media_player/media_player.cpp index 32da7ee265..7cd84a7e9a 100644 --- a/esphome/components/media_player/media_player.cpp +++ b/esphome/components/media_player/media_player.cpp @@ -137,5 +137,7 @@ void MediaPlayer::add_on_state_callback(std::function &&callback) { void MediaPlayer::publish_state() { this->state_callback_.call(); } +const char *MediaPlayer::get_component_type() const { return "media_player"; } + } // namespace media_player } // namespace esphome diff --git a/esphome/components/media_player/media_player.h b/esphome/components/media_player/media_player.h index ee5889901c..5398fedf44 100644 --- a/esphome/components/media_player/media_player.h +++ b/esphome/components/media_player/media_player.h @@ -105,6 +105,8 @@ class MediaPlayer : public EntityBase { virtual MediaPlayerTraits get_traits() = 0; + const char *get_component_type() const override; + protected: friend MediaPlayerCall; diff --git a/esphome/components/number/number.cpp b/esphome/components/number/number.cpp index fda4f43e34..22201c5998 100644 --- a/esphome/components/number/number.cpp +++ b/esphome/components/number/number.cpp @@ -17,5 +17,7 @@ void Number::add_on_state_callback(std::function &&callback) { this->state_callback_.add(std::move(callback)); } +const char *Number::get_component_type() const { return "number"; } + } // namespace number } // namespace esphome diff --git a/esphome/components/number/number.h b/esphome/components/number/number.h index d839d12ad1..bcd511458b 100644 --- a/esphome/components/number/number.h +++ b/esphome/components/number/number.h @@ -51,6 +51,8 @@ class Number : public EntityBase { /// Return whether this number has gotten a full state yet. bool has_state() const { return has_state_; } + const char *get_component_type() const override; + protected: friend class NumberCall; diff --git a/esphome/components/select/select.cpp b/esphome/components/select/select.cpp index 806882ad94..46cee71f96 100644 --- a/esphome/components/select/select.cpp +++ b/esphome/components/select/select.cpp @@ -58,5 +58,7 @@ optional Select::at(size_t index) const { } } +const char *Select::get_component_type() const { return "select"; } + } // namespace select } // namespace esphome diff --git a/esphome/components/select/select.h b/esphome/components/select/select.h index 8ca9a69d1c..331ef2a035 100644 --- a/esphome/components/select/select.h +++ b/esphome/components/select/select.h @@ -61,6 +61,8 @@ class Select : public EntityBase { void add_on_state_callback(std::function &&callback); + const char *get_component_type() const override; + protected: friend class SelectCall; diff --git a/esphome/components/sensor/sensor.cpp b/esphome/components/sensor/sensor.cpp index 14a8b3d490..8f56694daa 100644 --- a/esphome/components/sensor/sensor.cpp +++ b/esphome/components/sensor/sensor.cpp @@ -86,6 +86,7 @@ void Sensor::clear_filters() { float Sensor::get_state() const { return this->state; } float Sensor::get_raw_state() const { return this->raw_state; } std::string Sensor::unique_id() { return ""; } +const char *Sensor::get_component_type() const { return "sensor"; } void Sensor::internal_send_state_to_frontend(float state) { this->has_state_ = true; diff --git a/esphome/components/sensor/sensor.h b/esphome/components/sensor/sensor.h index 98356c943d..574daa487a 100644 --- a/esphome/components/sensor/sensor.h +++ b/esphome/components/sensor/sensor.h @@ -145,6 +145,9 @@ class Sensor : public EntityBase, public EntityBase_DeviceClass, public EntityBa */ virtual std::string unique_id(); + // Get the component type for this entity + const char *get_component_type() const override; + void internal_send_state_to_frontend(float state); protected: diff --git a/esphome/components/switch/switch.cpp b/esphome/components/switch/switch.cpp index 96611b0b87..859f70b707 100644 --- a/esphome/components/switch/switch.cpp +++ b/esphome/components/switch/switch.cpp @@ -62,6 +62,7 @@ void Switch::add_on_state_callback(std::function &&callback) { } void Switch::set_inverted(bool inverted) { this->inverted_ = inverted; } bool Switch::is_inverted() const { return this->inverted_; } +const char *Switch::get_component_type() const { return "switch"; } void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj) { if (obj != nullptr) { diff --git a/esphome/components/switch/switch.h b/esphome/components/switch/switch.h index b5395a2c83..d55eceb019 100644 --- a/esphome/components/switch/switch.h +++ b/esphome/components/switch/switch.h @@ -112,6 +112,9 @@ class Switch : public EntityBase, public EntityBase_DeviceClass { void set_restore_mode(SwitchRestoreMode restore_mode) { this->restore_mode = restore_mode; } + // Get the component type for this entity + const char *get_component_type() const override; + protected: /** Write the given state to hardware. You should implement this * abstract method if you want to create your own switch. diff --git a/esphome/components/text/text.cpp b/esphome/components/text/text.cpp index 8f0242e747..195302d385 100644 --- a/esphome/components/text/text.cpp +++ b/esphome/components/text/text.cpp @@ -22,5 +22,7 @@ void Text::add_on_state_callback(std::function &&callback) { this->state_callback_.add(std::move(callback)); } +const char *Text::get_component_type() const { return "text"; } + } // namespace text } // namespace esphome diff --git a/esphome/components/text/text.h b/esphome/components/text/text.h index f71dde69ba..024aec3e5b 100644 --- a/esphome/components/text/text.h +++ b/esphome/components/text/text.h @@ -36,6 +36,8 @@ class Text : public EntityBase { void add_on_state_callback(std::function &&callback); + const char *get_component_type() const override; + protected: friend class TextCall; diff --git a/esphome/components/text_sensor/text_sensor.cpp b/esphome/components/text_sensor/text_sensor.cpp index f10cd50267..160c23ad76 100644 --- a/esphome/components/text_sensor/text_sensor.cpp +++ b/esphome/components/text_sensor/text_sensor.cpp @@ -67,6 +67,7 @@ void TextSensor::internal_send_state_to_frontend(const std::string &state) { std::string TextSensor::unique_id() { return ""; } bool TextSensor::has_state() { return this->has_state_; } +const char *TextSensor::get_component_type() const { return "text_sensor"; } } // namespace text_sensor } // namespace esphome diff --git a/esphome/components/text_sensor/text_sensor.h b/esphome/components/text_sensor/text_sensor.h index bd72ea70e3..3bb1a5ea26 100644 --- a/esphome/components/text_sensor/text_sensor.h +++ b/esphome/components/text_sensor/text_sensor.h @@ -71,6 +71,9 @@ class TextSensor : public EntityBase, public EntityBase_DeviceClass { void internal_send_state_to_frontend(const std::string &state); + // Get the component type for this entity + const char *get_component_type() const override; + protected: CallbackManager raw_callback_; ///< Storage for raw state callbacks. CallbackManager callback_; ///< Storage for filtered state callbacks. diff --git a/esphome/components/update/update_entity.cpp b/esphome/components/update/update_entity.cpp index ed9a0480d8..6d2a8d74b0 100644 --- a/esphome/components/update/update_entity.cpp +++ b/esphome/components/update/update_entity.cpp @@ -34,5 +34,7 @@ void UpdateEntity::publish_state() { this->state_callback_.call(); } +const char *UpdateEntity::get_component_type() const { return "update"; } + } // namespace update } // namespace esphome diff --git a/esphome/components/update/update_entity.h b/esphome/components/update/update_entity.h index cc269e288f..51d3b6577f 100644 --- a/esphome/components/update/update_entity.h +++ b/esphome/components/update/update_entity.h @@ -41,6 +41,8 @@ class UpdateEntity : public EntityBase, public EntityBase_DeviceClass { void add_on_state_callback(std::function &&callback) { this->state_callback_.add(std::move(callback)); } + const char *get_component_type() const override; + protected: UpdateState state_{UPDATE_STATE_UNKNOWN}; UpdateInfo update_info_; diff --git a/esphome/components/valve/valve.cpp b/esphome/components/valve/valve.cpp index d1ec17945a..592237780e 100644 --- a/esphome/components/valve/valve.cpp +++ b/esphome/components/valve/valve.cpp @@ -165,6 +165,8 @@ optional Valve::restore_state_() { bool Valve::is_fully_open() const { return this->position == VALVE_OPEN; } bool Valve::is_fully_closed() const { return this->position == VALVE_CLOSED; } +const char *Valve::get_component_type() const { return "valve"; } + ValveCall ValveRestoreState::to_call(Valve *valve) { auto call = valve->make_call(); call.set_position(this->position); diff --git a/esphome/components/valve/valve.h b/esphome/components/valve/valve.h index 0e14a8d8f0..3c128d81bb 100644 --- a/esphome/components/valve/valve.h +++ b/esphome/components/valve/valve.h @@ -136,6 +136,8 @@ class Valve : public EntityBase, public EntityBase_DeviceClass { /// Helper method to check if the valve is fully closed. Equivalent to comparing .position against 0.0 bool is_fully_closed() const; + const char *get_component_type() const override; + protected: friend ValveCall; diff --git a/esphome/core/entity_base.h b/esphome/core/entity_base.h index 4ca21f9ee5..10c8a73095 100644 --- a/esphome/core/entity_base.h +++ b/esphome/core/entity_base.h @@ -29,6 +29,14 @@ class EntityBase { // Get the unique Object ID of this Entity uint32_t get_object_id_hash(); + // Get the unique ID for this entity, can be overridden by entities + // that need custom unique IDs + virtual std::string unique_id() { return ""; } + + // Get the component type for this entity, must be overridden by entities + // Returns a static string stored in flash memory + virtual const char *get_component_type() const = 0; + // Get/set whether this Entity should be hidden from outside of ESPHome bool is_internal() const; void set_internal(bool internal);