mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	[web_server] Reduce flash usage by consolidating defer calls in switch and lock handlers (#10297)
This commit is contained in:
		| @@ -507,14 +507,37 @@ void WebServer::handle_switch_request(AsyncWebServerRequest *request, const UrlM | |||||||
|       auto detail = get_request_detail(request); |       auto detail = get_request_detail(request); | ||||||
|       std::string data = this->switch_json(obj, obj->state, detail); |       std::string data = this->switch_json(obj, obj->state, detail); | ||||||
|       request->send(200, "application/json", data.c_str()); |       request->send(200, "application/json", data.c_str()); | ||||||
|     } else if (match.method_equals("toggle")) { |       return; | ||||||
|       this->defer([obj]() { obj->toggle(); }); |     } | ||||||
|       request->send(200); |  | ||||||
|  |     // Handle action methods with single defer and response | ||||||
|  |     enum SwitchAction { NONE, TOGGLE, TURN_ON, TURN_OFF }; | ||||||
|  |     SwitchAction action = NONE; | ||||||
|  |  | ||||||
|  |     if (match.method_equals("toggle")) { | ||||||
|  |       action = TOGGLE; | ||||||
|     } else if (match.method_equals("turn_on")) { |     } else if (match.method_equals("turn_on")) { | ||||||
|       this->defer([obj]() { obj->turn_on(); }); |       action = TURN_ON; | ||||||
|       request->send(200); |  | ||||||
|     } else if (match.method_equals("turn_off")) { |     } else if (match.method_equals("turn_off")) { | ||||||
|       this->defer([obj]() { obj->turn_off(); }); |       action = TURN_OFF; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if (action != NONE) { | ||||||
|  |       this->defer([obj, action]() { | ||||||
|  |         switch (action) { | ||||||
|  |           case TOGGLE: | ||||||
|  |             obj->toggle(); | ||||||
|  |             break; | ||||||
|  |           case TURN_ON: | ||||||
|  |             obj->turn_on(); | ||||||
|  |             break; | ||||||
|  |           case TURN_OFF: | ||||||
|  |             obj->turn_off(); | ||||||
|  |             break; | ||||||
|  |           default: | ||||||
|  |             break; | ||||||
|  |         } | ||||||
|  |       }); | ||||||
|       request->send(200); |       request->send(200); | ||||||
|     } else { |     } else { | ||||||
|       request->send(404); |       request->send(404); | ||||||
| @@ -1332,14 +1355,37 @@ void WebServer::handle_lock_request(AsyncWebServerRequest *request, const UrlMat | |||||||
|       auto detail = get_request_detail(request); |       auto detail = get_request_detail(request); | ||||||
|       std::string data = this->lock_json(obj, obj->state, detail); |       std::string data = this->lock_json(obj, obj->state, detail); | ||||||
|       request->send(200, "application/json", data.c_str()); |       request->send(200, "application/json", data.c_str()); | ||||||
|     } else if (match.method_equals("lock")) { |       return; | ||||||
|       this->defer([obj]() { obj->lock(); }); |     } | ||||||
|       request->send(200); |  | ||||||
|  |     // Handle action methods with single defer and response | ||||||
|  |     enum LockAction { NONE, LOCK, UNLOCK, OPEN }; | ||||||
|  |     LockAction action = NONE; | ||||||
|  |  | ||||||
|  |     if (match.method_equals("lock")) { | ||||||
|  |       action = LOCK; | ||||||
|     } else if (match.method_equals("unlock")) { |     } else if (match.method_equals("unlock")) { | ||||||
|       this->defer([obj]() { obj->unlock(); }); |       action = UNLOCK; | ||||||
|       request->send(200); |  | ||||||
|     } else if (match.method_equals("open")) { |     } else if (match.method_equals("open")) { | ||||||
|       this->defer([obj]() { obj->open(); }); |       action = OPEN; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if (action != NONE) { | ||||||
|  |       this->defer([obj, action]() { | ||||||
|  |         switch (action) { | ||||||
|  |           case LOCK: | ||||||
|  |             obj->lock(); | ||||||
|  |             break; | ||||||
|  |           case UNLOCK: | ||||||
|  |             obj->unlock(); | ||||||
|  |             break; | ||||||
|  |           case OPEN: | ||||||
|  |             obj->open(); | ||||||
|  |             break; | ||||||
|  |           default: | ||||||
|  |             break; | ||||||
|  |         } | ||||||
|  |       }); | ||||||
|       request->send(200); |       request->send(200); | ||||||
|     } else { |     } else { | ||||||
|       request->send(404); |       request->send(404); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user