mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	Merge branch 'api_services_once' into integration
This commit is contained in:
		| @@ -258,6 +258,10 @@ async def to_code(config): | ||||
|     if config.get(CONF_ACTIONS) or config[CONF_CUSTOM_SERVICES]: | ||||
|         cg.add_define("USE_API_SERVICES") | ||||
|  | ||||
|     # Set USE_API_CUSTOM_SERVICES if external components need dynamic service registration | ||||
|     if config[CONF_CUSTOM_SERVICES]: | ||||
|         cg.add_define("USE_API_CUSTOM_SERVICES") | ||||
|  | ||||
|     if config[CONF_HOMEASSISTANT_SERVICES]: | ||||
|         cg.add_define("USE_API_HOMEASSISTANT_SERVICES") | ||||
|  | ||||
|   | ||||
| @@ -128,8 +128,11 @@ class APIServer : public Component, public Controller { | ||||
|   void initialize_user_services(std::initializer_list<UserServiceDescriptor *> services) { | ||||
|     this->user_services_.assign(services); | ||||
|   } | ||||
| #ifdef USE_API_CUSTOM_SERVICES | ||||
|   // Only compile push_back method when custom_services: true (external components) | ||||
|   void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); } | ||||
| #endif | ||||
| #endif | ||||
| #ifdef USE_HOMEASSISTANT_TIME | ||||
|   void request_time(); | ||||
| #endif | ||||
|   | ||||
| @@ -53,8 +53,14 @@ class CustomAPIDevice { | ||||
|   template<typename T, typename... Ts> | ||||
|   void register_service(void (T::*callback)(Ts...), const std::string &name, | ||||
|                         const std::array<std::string, sizeof...(Ts)> &arg_names) { | ||||
| #ifdef USE_API_CUSTOM_SERVICES | ||||
|     auto *service = new CustomAPIDeviceService<T, Ts...>(name, arg_names, (T *) this, callback);  // NOLINT | ||||
|     global_api_server->register_user_service(service); | ||||
| #else | ||||
|     static_assert( | ||||
|         sizeof(T) == 0, | ||||
|         "register_service() requires 'custom_services: true' in the 'api:' section of your YAML configuration"); | ||||
| #endif | ||||
|   } | ||||
| #else | ||||
|   template<typename T, typename... Ts> | ||||
| @@ -86,8 +92,14 @@ class CustomAPIDevice { | ||||
|    */ | ||||
| #ifdef USE_API_SERVICES | ||||
|   template<typename T> void register_service(void (T::*callback)(), const std::string &name) { | ||||
| #ifdef USE_API_CUSTOM_SERVICES | ||||
|     auto *service = new CustomAPIDeviceService<T>(name, {}, (T *) this, callback);  // NOLINT | ||||
|     global_api_server->register_user_service(service); | ||||
| #else | ||||
|     static_assert( | ||||
|         sizeof(T) == 0, | ||||
|         "register_service() requires 'custom_services: true' in the 'api:' section of your YAML configuration"); | ||||
| #endif | ||||
|   } | ||||
| #else | ||||
|   template<typename T> void register_service(void (T::*callback)(), const std::string &name) { | ||||
|   | ||||
| @@ -123,6 +123,7 @@ | ||||
| #define USE_API_NOISE | ||||
| #define USE_API_PLAINTEXT | ||||
| #define USE_API_SERVICES | ||||
| #define USE_API_CUSTOM_SERVICES | ||||
| #define API_MAX_SEND_QUEUE 8 | ||||
| #define USE_MD5 | ||||
| #define USE_SHA256 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user