diff --git a/esphome/components/web_server/web_server.cpp b/esphome/components/web_server/web_server.cpp index 510cc3c2a4..053a3e693a 100644 --- a/esphome/components/web_server/web_server.cpp +++ b/esphome/components/web_server/web_server.cpp @@ -413,9 +413,7 @@ 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(); } @@ -459,9 +457,7 @@ 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 } }); } @@ -513,9 +509,7 @@ 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 } }); } @@ -555,9 +549,7 @@ 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 } }); } @@ -599,9 +591,7 @@ 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 } }); } @@ -686,9 +676,7 @@ 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 } }); } @@ -808,9 +796,7 @@ 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 } }); } @@ -895,9 +881,7 @@ 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 } }); } @@ -962,9 +946,7 @@ 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\""; @@ -1037,9 +1019,7 @@ 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 } }); } @@ -1101,9 +1081,7 @@ 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 } }); } @@ -1166,9 +1144,7 @@ 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 } }); } @@ -1233,9 +1209,7 @@ 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 } }); } @@ -1295,9 +1269,7 @@ 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 } }); } @@ -1418,9 +1390,7 @@ 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; @@ -1517,9 +1487,7 @@ 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 } }); } @@ -1595,9 +1563,7 @@ 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 } }); } @@ -1669,9 +1635,7 @@ 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 } }); } @@ -1720,9 +1684,7 @@ 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 } }); } @@ -1790,9 +1752,7 @@ 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 } }); } @@ -2102,6 +2062,17 @@ void WebServer::handleRequest(AsyncWebServerRequest *request) { bool WebServer::isRequestHandlerTrivial() const { return false; } +void WebServer::add_sorting_info_(JsonObject &root, EntityBase *entity) { +#ifdef USE_WEBSERVER_SORTING + if (this->sorting_entitys_.find(entity) != this->sorting_entitys_.end()) { + root["sorting_weight"] = this->sorting_entitys_[entity].weight; + if (this->sorting_groups_.find(this->sorting_entitys_[entity].group_id) != this->sorting_groups_.end()) { + root["sorting_group"] = this->sorting_groups_[this->sorting_entitys_[entity].group_id].name; + } + } +#endif +} + #ifdef USE_WEBSERVER_SORTING void WebServer::add_entity_config(EntityBase *entity, float weight, uint64_t group) { this->sorting_entitys_[entity] = SortingComponents{weight, group}; @@ -2110,15 +2081,6 @@ void WebServer::add_entity_config(EntityBase *entity, float weight, uint64_t gro void WebServer::add_sorting_group(uint64_t group_id, const std::string &group_name, float weight) { this->sorting_groups_[group_id] = SortingGroup{group_name, weight}; } - -void WebServer::add_sorting_info_(JsonObject &root, EntityBase *entity) { - if (this->sorting_entitys_.find(entity) != this->sorting_entitys_.end()) { - root["sorting_weight"] = this->sorting_entitys_[entity].weight; - if (this->sorting_groups_.find(this->sorting_entitys_[entity].group_id) != this->sorting_groups_.end()) { - root["sorting_group"] = this->sorting_groups_[this->sorting_entitys_[entity].group_id].name; - } - } -} #endif void WebServer::schedule_(std::function &&f) { diff --git a/esphome/components/web_server/web_server.h b/esphome/components/web_server/web_server.h index 3b095e7661..3be99eebae 100644 --- a/esphome/components/web_server/web_server.h +++ b/esphome/components/web_server/web_server.h @@ -487,9 +487,7 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { 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