[web_server] Reduce binary size by using EntityBase and minimizing template instantiations (#10033)

This commit is contained in:
J. Nick Koston 2025-08-03 16:03:37 -10:00 committed by GitHub
parent c9d865a061
commit a75f73dbf0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -376,23 +376,32 @@ void WebServer::handle_js_request(AsyncWebServerRequest *request) {
} }
#endif #endif
#define set_json_id(root, obj, sensor, start_config) \ // Helper functions to reduce code size by avoiding macro expansion
(root)["id"] = sensor; \ static void set_json_id(JsonObject &root, EntityBase *obj, const std::string &id, JsonDetail start_config) {
if (((start_config) == DETAIL_ALL)) { \ root["id"] = id;
(root)["name"] = (obj)->get_name(); \ if (start_config == DETAIL_ALL) {
(root)["icon"] = (obj)->get_icon(); \ root["name"] = obj->get_name();
(root)["entity_category"] = (obj)->get_entity_category(); \ root["icon"] = obj->get_icon();
if ((obj)->is_disabled_by_default()) \ root["entity_category"] = obj->get_entity_category();
(root)["is_disabled_by_default"] = (obj)->is_disabled_by_default(); \ bool is_disabled = obj->is_disabled_by_default();
if (is_disabled)
root["is_disabled_by_default"] = is_disabled;
} }
}
#define set_json_value(root, obj, sensor, value, start_config) \ template<typename T>
set_json_id((root), (obj), sensor, start_config); \ static void set_json_value(JsonObject &root, EntityBase *obj, const std::string &id, const T &value,
(root)["value"] = value; JsonDetail start_config) {
set_json_id(root, obj, id, start_config);
root["value"] = value;
}
#define set_json_icon_state_value(root, obj, sensor, state, value, start_config) \ template<typename T>
set_json_value(root, obj, sensor, value, start_config); \ static void set_json_icon_state_value(JsonObject &root, EntityBase *obj, const std::string &id,
(root)["state"] = state; const std::string &state, const T &value, JsonDetail start_config) {
set_json_value(root, obj, id, value, start_config);
root["state"] = state;
}
// Helper to get request detail parameter // Helper to get request detail parameter
static JsonDetail get_request_detail(AsyncWebServerRequest *request) { static JsonDetail get_request_detail(AsyncWebServerRequest *request) {