From fb7faadd99f8c1f3f87abdee6efbd5e1406c3085 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 27 Jun 2025 09:41:20 -0500 Subject: [PATCH 1/2] reduce memory --- esphome/components/web_server/__init__.py | 2 + esphome/components/web_server/web_server.cpp | 44 ++++++++++++++++++++ esphome/components/web_server/web_server.h | 7 ++++ 3 files changed, 53 insertions(+) diff --git a/esphome/components/web_server/__init__.py b/esphome/components/web_server/__init__.py index d846a3418b..8ff7ce1d16 100644 --- a/esphome/components/web_server/__init__.py +++ b/esphome/components/web_server/__init__.py @@ -211,6 +211,7 @@ async def add_entity_config(entity, config): sorting_weight = config.get(CONF_SORTING_WEIGHT, 50) sorting_group_hash = hash(config.get(CONF_SORTING_GROUP_ID)) + cg.add_define("USE_WEBSERVER_SORTING") cg.add( web_server.add_entity_config( entity, @@ -296,4 +297,5 @@ async def to_code(config): cg.add_define("USE_WEBSERVER_LOCAL") if (sorting_group_config := config.get(CONF_SORTING_GROUPS)) is not None: + cg.add_define("USE_WEBSERVER_SORTING") add_sorting_groups(var, sorting_group_config) diff --git a/esphome/components/web_server/web_server.cpp b/esphome/components/web_server/web_server.cpp index 7e9e0ae80e..510cc3c2a4 100644 --- a/esphome/components/web_server/web_server.cpp +++ b/esphome/components/web_server/web_server.cpp @@ -184,6 +184,7 @@ void DeferredUpdateEventSourceList::on_client_connect_(WebServer *ws, DeferredUp std::string message = ws->get_config_json(); source->try_send_nodefer(message.c_str(), "ping", millis(), 30000); +#ifdef USE_WEBSERVER_SORTING for (auto &group : ws->sorting_groups_) { message = json::build_json([group](JsonObject root) { root["name"] = group.second.name; @@ -193,6 +194,7 @@ void DeferredUpdateEventSourceList::on_client_connect_(WebServer *ws, DeferredUp // up to 31 groups should be able to be queued initially without defer source->try_send_nodefer(message.c_str(), "sorting_group"); } +#endif source->entities_iterator_.begin(ws->include_internal_); @@ -411,7 +413,9 @@ std::string WebServer::sensor_json(sensor::Sensor *obj, float value, JsonDetail } set_json_icon_state_value(root, obj, "sensor-" + obj->get_object_id(), state, value, start_config); if (start_config == DETAIL_ALL) { +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif if (!obj->get_unit_of_measurement().empty()) root["uom"] = obj->get_unit_of_measurement(); } @@ -455,7 +459,9 @@ std::string WebServer::text_sensor_json(text_sensor::TextSensor *obj, const std: return json::build_json([this, obj, value, start_config](JsonObject root) { set_json_icon_state_value(root, obj, "text_sensor-" + obj->get_object_id(), value, value, start_config); if (start_config == DETAIL_ALL) { +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -507,7 +513,9 @@ std::string WebServer::switch_json(switch_::Switch *obj, bool value, JsonDetail set_json_icon_state_value(root, obj, "switch-" + obj->get_object_id(), value ? "ON" : "OFF", value, start_config); if (start_config == DETAIL_ALL) { root["assumed_state"] = obj->assumed_state(); +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -547,7 +555,9 @@ std::string WebServer::button_json(button::Button *obj, JsonDetail start_config) return json::build_json([this, obj, start_config](JsonObject root) { set_json_id(root, obj, "button-" + obj->get_object_id(), start_config); if (start_config == DETAIL_ALL) { +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -589,7 +599,9 @@ std::string WebServer::binary_sensor_json(binary_sensor::BinarySensor *obj, bool set_json_icon_state_value(root, obj, "binary_sensor-" + obj->get_object_id(), value ? "ON" : "OFF", value, start_config); if (start_config == DETAIL_ALL) { +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -674,7 +686,9 @@ std::string WebServer::fan_json(fan::Fan *obj, JsonDetail start_config) { if (obj->get_traits().supports_oscillation()) root["oscillation"] = obj->oscillating; if (start_config == DETAIL_ALL) { +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -794,7 +808,9 @@ std::string WebServer::light_json(light::LightState *obj, JsonDetail start_confi for (auto const &option : obj->get_effects()) { opt.add(option->get_name()); } +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -879,7 +895,9 @@ std::string WebServer::cover_json(cover::Cover *obj, JsonDetail start_config) { if (obj->get_traits().get_supports_tilt()) root["tilt"] = obj->tilt; if (start_config == DETAIL_ALL) { +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -944,7 +962,9 @@ std::string WebServer::number_json(number::Number *obj, float value, JsonDetail root["mode"] = (int) obj->traits.get_mode(); if (!obj->traits.get_unit_of_measurement().empty()) root["uom"] = obj->traits.get_unit_of_measurement(); +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } if (std::isnan(value)) { root["value"] = "\"NaN\""; @@ -1017,7 +1037,9 @@ std::string WebServer::date_json(datetime::DateEntity *obj, JsonDetail start_con root["value"] = value; root["state"] = value; if (start_config == DETAIL_ALL) { +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -1079,7 +1101,9 @@ std::string WebServer::time_json(datetime::TimeEntity *obj, JsonDetail start_con root["value"] = value; root["state"] = value; if (start_config == DETAIL_ALL) { +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -1142,7 +1166,9 @@ std::string WebServer::datetime_json(datetime::DateTimeEntity *obj, JsonDetail s root["value"] = value; root["state"] = value; if (start_config == DETAIL_ALL) { +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -1207,7 +1233,9 @@ std::string WebServer::text_json(text::Text *obj, const std::string &value, Json root["value"] = value; if (start_config == DETAIL_ALL) { root["mode"] = (int) obj->traits.get_mode(); +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -1267,7 +1295,9 @@ std::string WebServer::select_json(select::Select *obj, const std::string &value for (auto &option : obj->traits.get_options()) { opt.add(option); } +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -1388,7 +1418,9 @@ std::string WebServer::climate_json(climate::Climate *obj, JsonDetail start_conf for (auto const &custom_preset : traits.get_supported_custom_presets()) opt.add(custom_preset); } +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } bool has_state = false; @@ -1485,7 +1517,9 @@ std::string WebServer::lock_json(lock::Lock *obj, lock::LockState value, JsonDet set_json_icon_state_value(root, obj, "lock-" + obj->get_object_id(), lock::lock_state_to_string(value), value, start_config); if (start_config == DETAIL_ALL) { +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -1561,7 +1595,9 @@ std::string WebServer::valve_json(valve::Valve *obj, JsonDetail start_config) { if (obj->get_traits().get_supports_position()) root["position"] = obj->position; if (start_config == DETAIL_ALL) { +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -1633,7 +1669,9 @@ std::string WebServer::alarm_control_panel_json(alarm_control_panel::AlarmContro set_json_icon_state_value(root, obj, "alarm-control-panel-" + obj->get_object_id(), PSTR_LOCAL(alarm_control_panel_state_to_string(value)), value, start_config); if (start_config == DETAIL_ALL) { +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -1682,7 +1720,9 @@ std::string WebServer::event_json(event::Event *obj, const std::string &event_ty event_types.add(event_type); } root["device_class"] = obj->get_device_class(); +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -1750,7 +1790,9 @@ std::string WebServer::update_json(update::UpdateEntity *obj, JsonDetail start_c root["title"] = obj->update_info.title; root["summary"] = obj->update_info.summary; root["release_url"] = obj->update_info.release_url; +#ifdef USE_WEBSERVER_SORTING this->add_sorting_info_(root, obj); +#endif } }); } @@ -2060,6 +2102,7 @@ void WebServer::handleRequest(AsyncWebServerRequest *request) { bool WebServer::isRequestHandlerTrivial() const { return false; } +#ifdef USE_WEBSERVER_SORTING void WebServer::add_entity_config(EntityBase *entity, float weight, uint64_t group) { this->sorting_entitys_[entity] = SortingComponents{weight, group}; } @@ -2076,6 +2119,7 @@ void WebServer::add_sorting_info_(JsonObject &root, EntityBase *entity) { } } } +#endif void WebServer::schedule_(std::function &&f) { #ifdef USE_ESP32 diff --git a/esphome/components/web_server/web_server.h b/esphome/components/web_server/web_server.h index 25797c654b..3b095e7661 100644 --- a/esphome/components/web_server/web_server.h +++ b/esphome/components/web_server/web_server.h @@ -46,6 +46,7 @@ struct UrlMatch { bool valid; ///< Whether this match is valid }; +#ifdef USE_WEBSERVER_SORTING struct SortingComponents { float weight; uint64_t group_id; @@ -55,6 +56,7 @@ struct SortingGroup { std::string name; float weight; }; +#endif enum JsonDetail { DETAIL_ALL, DETAIL_STATE }; @@ -474,15 +476,20 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// This web handle is not trivial. bool isRequestHandlerTrivial() const override; // NOLINT(readability-identifier-naming) +#ifdef USE_WEBSERVER_SORTING void add_entity_config(EntityBase *entity, float weight, uint64_t group); void add_sorting_group(uint64_t group_id, const std::string &group_name, float weight); std::map sorting_entitys_; std::map sorting_groups_; +#endif + bool include_internal_{false}; protected: +#ifdef USE_WEBSERVER_SORTING void add_sorting_info_(JsonObject &root, EntityBase *entity); +#endif void schedule_(std::function &&f); web_server_base::WebServerBase *base_; #ifdef USE_ARDUINO From 88f857a2f01bfb88c48c941f6c0ebccf7a3ccd79 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 27 Jun 2025 09:44:50 -0500 Subject: [PATCH 2/2] defines --- esphome/core/defines.h | 1 + 1 file changed, 1 insertion(+) diff --git a/esphome/core/defines.h b/esphome/core/defines.h index 8abd6598f7..22454249aa 100644 --- a/esphome/core/defines.h +++ b/esphome/core/defines.h @@ -151,6 +151,7 @@ #define USE_VOICE_ASSISTANT #define USE_WEBSERVER #define USE_WEBSERVER_PORT 80 // NOLINT +#define USE_WEBSERVER_SORTING #define USE_WIFI_11KV_SUPPORT #ifdef USE_ARDUINO