diff --git a/esphome/components/api/api_server.cpp b/esphome/components/api/api_server.cpp index b1a5ee5d57..8b0130044e 100644 --- a/esphome/components/api/api_server.cpp +++ b/esphome/components/api/api_server.cpp @@ -421,7 +421,7 @@ void APIServer::handle_action_response(uint32_t call_id, bool success, const std #ifdef USE_API_HOMEASSISTANT_STATES // Helper to add subscription (reduces duplication) void APIServer::add_state_subscription_(const char *entity_id, const char *attribute, - std::function f, bool once) { + std::function f, bool once) { this->state_subs_.push_back(HomeAssistantStateSubscription{ .entity_id = entity_id, .attribute = attribute, .callback = std::move(f), .once = once, // entity_id_dynamic_storage and attribute_dynamic_storage remain nullptr (no heap allocation) @@ -430,7 +430,7 @@ void APIServer::add_state_subscription_(const char *entity_id, const char *attri // Helper to add subscription with heap-allocated strings (reduces duplication) void APIServer::add_state_subscription_(std::string entity_id, optional attribute, - std::function f, bool once) { + std::function f, bool once) { HomeAssistantStateSubscription sub; // Allocate heap storage for the strings sub.entity_id_dynamic_storage = std::make_unique(std::move(entity_id)); @@ -450,23 +450,23 @@ void APIServer::add_state_subscription_(std::string entity_id, optional f) { + std::function f) { this->add_state_subscription_(entity_id, attribute, std::move(f), false); } void APIServer::get_home_assistant_state(const char *entity_id, const char *attribute, - std::function f) { + std::function f) { this->add_state_subscription_(entity_id, attribute, std::move(f), true); } // Existing std::string overload (for custom_api_device.h - heap allocation) void APIServer::subscribe_home_assistant_state(std::string entity_id, optional attribute, - std::function f) { + std::function f) { this->add_state_subscription_(std::move(entity_id), std::move(attribute), std::move(f), false); } void APIServer::get_home_assistant_state(std::string entity_id, optional attribute, - std::function f) { + std::function f) { this->add_state_subscription_(std::move(entity_id), std::move(attribute), std::move(f), true); } diff --git a/esphome/components/api/api_server.h b/esphome/components/api/api_server.h index ad7d8bf63d..dca9fbdffa 100644 --- a/esphome/components/api/api_server.h +++ b/esphome/components/api/api_server.h @@ -192,7 +192,7 @@ class APIServer : public Component, struct HomeAssistantStateSubscription { const char *entity_id; // Pointer to flash (internal) or heap (external) const char *attribute; // Pointer to flash or nullptr (nullptr means no attribute) - std::function callback; + std::function callback; bool once; // Dynamic storage for external components using std::string API (custom_api_device.h) @@ -202,14 +202,16 @@ class APIServer : public Component, }; // New const char* overload (for internal components - zero allocation) - void subscribe_home_assistant_state(const char *entity_id, const char *attribute, std::function f); - void get_home_assistant_state(const char *entity_id, const char *attribute, std::function f); + void subscribe_home_assistant_state(const char *entity_id, const char *attribute, + std::function f); + void get_home_assistant_state(const char *entity_id, const char *attribute, + std::function f); // Existing std::string overload (for custom_api_device.h - heap allocation) void subscribe_home_assistant_state(std::string entity_id, optional attribute, - std::function f); + std::function f); void get_home_assistant_state(std::string entity_id, optional attribute, - std::function f); + std::function f); const std::vector &get_state_subs() const; #endif @@ -233,10 +235,10 @@ class APIServer : public Component, #endif // USE_API_NOISE #ifdef USE_API_HOMEASSISTANT_STATES // Helper methods to reduce code duplication - void add_state_subscription_(const char *entity_id, const char *attribute, std::function f, + void add_state_subscription_(const char *entity_id, const char *attribute, std::function f, bool once); void add_state_subscription_(std::string entity_id, optional attribute, - std::function f, bool once); + std::function f, bool once); #endif // USE_API_HOMEASSISTANT_STATES // Pointers and pointer-like types first (4 bytes each) std::unique_ptr socket_ = nullptr; diff --git a/esphome/components/api/custom_api_device.h b/esphome/components/api/custom_api_device.h index 5e9165326d..ee14063512 100644 --- a/esphome/components/api/custom_api_device.h +++ b/esphome/components/api/custom_api_device.h @@ -122,7 +122,7 @@ class CustomAPIDevice { * subscribe_homeassistant_state(&CustomNativeAPI::on_state_changed, "climate.kitchen", "current_temperature"); * } * - * void on_state_changed(std::string state) { + * void on_state_changed(const std::string &state) { * // State of sensor.weather_forecast is `state` * } * ``` @@ -133,7 +133,7 @@ class CustomAPIDevice { * @param attribute The entity state attribute to track. */ template - void subscribe_homeassistant_state(void (T::*callback)(std::string), const std::string &entity_id, + void subscribe_homeassistant_state(void (T::*callback)(const std::string &), const std::string &entity_id, const std::string &attribute = "") { auto f = std::bind(callback, (T *) this, std::placeholders::_1); global_api_server->subscribe_home_assistant_state(entity_id, optional(attribute), f); @@ -148,7 +148,7 @@ class CustomAPIDevice { * subscribe_homeassistant_state(&CustomNativeAPI::on_state_changed, "sensor.weather_forecast"); * } * - * void on_state_changed(std::string entity_id, std::string state) { + * void on_state_changed(const std::string &entity_id, const std::string &state) { * // State of `entity_id` is `state` * } * ``` @@ -159,14 +159,14 @@ class CustomAPIDevice { * @param attribute The entity state attribute to track. */ template - void subscribe_homeassistant_state(void (T::*callback)(std::string, std::string), const std::string &entity_id, - const std::string &attribute = "") { + void subscribe_homeassistant_state(void (T::*callback)(const std::string &, const std::string &), + const std::string &entity_id, const std::string &attribute = "") { auto f = std::bind(callback, (T *) this, entity_id, std::placeholders::_1); global_api_server->subscribe_home_assistant_state(entity_id, optional(attribute), f); } #else template - void subscribe_homeassistant_state(void (T::*callback)(std::string), const std::string &entity_id, + void subscribe_homeassistant_state(void (T::*callback)(const std::string &), const std::string &entity_id, const std::string &attribute = "") { static_assert(sizeof(T) == 0, "subscribe_homeassistant_state() requires 'homeassistant_states: true' in the 'api:' section " @@ -174,8 +174,8 @@ class CustomAPIDevice { } template - void subscribe_homeassistant_state(void (T::*callback)(std::string, std::string), const std::string &entity_id, - const std::string &attribute = "") { + void subscribe_homeassistant_state(void (T::*callback)(const std::string &, const std::string &), + const std::string &entity_id, const std::string &attribute = "") { static_assert(sizeof(T) == 0, "subscribe_homeassistant_state() requires 'homeassistant_states: true' in the 'api:' section " "of your YAML configuration");