mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	Merge branch 'duplicate_webserver_code' into integration
This commit is contained in:
		| @@ -211,6 +211,7 @@ async def add_entity_config(entity, config): | |||||||
|     sorting_weight = config.get(CONF_SORTING_WEIGHT, 50) |     sorting_weight = config.get(CONF_SORTING_WEIGHT, 50) | ||||||
|     sorting_group_hash = hash(config.get(CONF_SORTING_GROUP_ID)) |     sorting_group_hash = hash(config.get(CONF_SORTING_GROUP_ID)) | ||||||
|  |  | ||||||
|  |     cg.add_define("USE_WEBSERVER_SORTING") | ||||||
|     cg.add( |     cg.add( | ||||||
|         web_server.add_entity_config( |         web_server.add_entity_config( | ||||||
|             entity, |             entity, | ||||||
| @@ -296,4 +297,5 @@ async def to_code(config): | |||||||
|         cg.add_define("USE_WEBSERVER_LOCAL") |         cg.add_define("USE_WEBSERVER_LOCAL") | ||||||
|  |  | ||||||
|     if (sorting_group_config := config.get(CONF_SORTING_GROUPS)) is not None: |     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) |         add_sorting_groups(var, sorting_group_config) | ||||||
|   | |||||||
| @@ -184,6 +184,7 @@ void DeferredUpdateEventSourceList::on_client_connect_(WebServer *ws, DeferredUp | |||||||
|   std::string message = ws->get_config_json(); |   std::string message = ws->get_config_json(); | ||||||
|   source->try_send_nodefer(message.c_str(), "ping", millis(), 30000); |   source->try_send_nodefer(message.c_str(), "ping", millis(), 30000); | ||||||
|  |  | ||||||
|  | #ifdef USE_WEBSERVER_SORTING | ||||||
|   for (auto &group : ws->sorting_groups_) { |   for (auto &group : ws->sorting_groups_) { | ||||||
|     message = json::build_json([group](JsonObject root) { |     message = json::build_json([group](JsonObject root) { | ||||||
|       root["name"] = group.second.name; |       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 |     // up to 31 groups should be able to be queued initially without defer | ||||||
|     source->try_send_nodefer(message.c_str(), "sorting_group"); |     source->try_send_nodefer(message.c_str(), "sorting_group"); | ||||||
|   } |   } | ||||||
|  | #endif | ||||||
|  |  | ||||||
|   source->entities_iterator_.begin(ws->include_internal_); |   source->entities_iterator_.begin(ws->include_internal_); | ||||||
|  |  | ||||||
| @@ -2060,6 +2062,18 @@ void WebServer::handleRequest(AsyncWebServerRequest *request) { | |||||||
|  |  | ||||||
| bool WebServer::isRequestHandlerTrivial() const { return false; } | 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) { | void WebServer::add_entity_config(EntityBase *entity, float weight, uint64_t group) { | ||||||
|   this->sorting_entitys_[entity] = SortingComponents{weight, group}; |   this->sorting_entitys_[entity] = SortingComponents{weight, group}; | ||||||
| } | } | ||||||
| @@ -2067,15 +2081,7 @@ 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) { | 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}; |   this->sorting_groups_[group_id] = SortingGroup{group_name, weight}; | ||||||
| } | } | ||||||
|  | #endif | ||||||
| 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; |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| } |  | ||||||
|  |  | ||||||
| void WebServer::schedule_(std::function<void()> &&f) { | void WebServer::schedule_(std::function<void()> &&f) { | ||||||
| #ifdef USE_ESP32 | #ifdef USE_ESP32 | ||||||
|   | |||||||
| @@ -46,6 +46,7 @@ struct UrlMatch { | |||||||
|   bool valid;          ///< Whether this match is valid |   bool valid;          ///< Whether this match is valid | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | #ifdef USE_WEBSERVER_SORTING | ||||||
| struct SortingComponents { | struct SortingComponents { | ||||||
|   float weight; |   float weight; | ||||||
|   uint64_t group_id; |   uint64_t group_id; | ||||||
| @@ -55,6 +56,7 @@ struct SortingGroup { | |||||||
|   std::string name; |   std::string name; | ||||||
|   float weight; |   float weight; | ||||||
| }; | }; | ||||||
|  | #endif | ||||||
|  |  | ||||||
| enum JsonDetail { DETAIL_ALL, DETAIL_STATE }; | enum JsonDetail { DETAIL_ALL, DETAIL_STATE }; | ||||||
|  |  | ||||||
| @@ -474,11 +476,14 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { | |||||||
|   /// This web handle is not trivial. |   /// This web handle is not trivial. | ||||||
|   bool isRequestHandlerTrivial() const override;  // NOLINT(readability-identifier-naming) |   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_entity_config(EntityBase *entity, float weight, uint64_t group); | ||||||
|   void add_sorting_group(uint64_t group_id, const std::string &group_name, float weight); |   void add_sorting_group(uint64_t group_id, const std::string &group_name, float weight); | ||||||
|  |  | ||||||
|   std::map<EntityBase *, SortingComponents> sorting_entitys_; |   std::map<EntityBase *, SortingComponents> sorting_entitys_; | ||||||
|   std::map<uint64_t, SortingGroup> sorting_groups_; |   std::map<uint64_t, SortingGroup> sorting_groups_; | ||||||
|  | #endif | ||||||
|  |  | ||||||
|   bool include_internal_{false}; |   bool include_internal_{false}; | ||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
|   | |||||||
| @@ -338,6 +338,7 @@ AsyncEventSourceResponse::AsyncEventSourceResponse(const AsyncWebServerRequest * | |||||||
|   std::string message = ws->get_config_json(); |   std::string message = ws->get_config_json(); | ||||||
|   this->try_send_nodefer(message.c_str(), "ping", millis(), 30000); |   this->try_send_nodefer(message.c_str(), "ping", millis(), 30000); | ||||||
|  |  | ||||||
|  | #ifdef USE_WEBSERVER_SORTING | ||||||
|   for (auto &group : ws->sorting_groups_) { |   for (auto &group : ws->sorting_groups_) { | ||||||
|     message = json::build_json([group](JsonObject root) { |     message = json::build_json([group](JsonObject root) { | ||||||
|       root["name"] = group.second.name; |       root["name"] = group.second.name; | ||||||
| @@ -348,6 +349,7 @@ AsyncEventSourceResponse::AsyncEventSourceResponse(const AsyncWebServerRequest * | |||||||
|     // since the only thing in the send buffer at this point is the initial ping/config |     // since the only thing in the send buffer at this point is the initial ping/config | ||||||
|     this->try_send_nodefer(message.c_str(), "sorting_group"); |     this->try_send_nodefer(message.c_str(), "sorting_group"); | ||||||
|   } |   } | ||||||
|  | #endif | ||||||
|  |  | ||||||
|   this->entities_iterator_->begin(ws->include_internal_); |   this->entities_iterator_->begin(ws->include_internal_); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -151,6 +151,7 @@ | |||||||
| #define USE_VOICE_ASSISTANT | #define USE_VOICE_ASSISTANT | ||||||
| #define USE_WEBSERVER | #define USE_WEBSERVER | ||||||
| #define USE_WEBSERVER_PORT 80  // NOLINT | #define USE_WEBSERVER_PORT 80  // NOLINT | ||||||
|  | #define USE_WEBSERVER_SORTING | ||||||
| #define USE_WIFI_11KV_SUPPORT | #define USE_WIFI_11KV_SUPPORT | ||||||
|  |  | ||||||
| #ifdef USE_ARDUINO | #ifdef USE_ARDUINO | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user