From 11518364a1c3e85e99471b2d46a1e85607213b02 Mon Sep 17 00:00:00 2001 From: Gil Peeters Date: Tue, 17 Jan 2023 11:02:54 +1100 Subject: [PATCH] Display the configured esphome:comment on the WebServer (#4246) --- esphome/components/web_server/web_server.cpp | 9 +++++---- esphome/core/application.h | 16 ++++++++++------ esphome/core/config.py | 1 + tests/dummy_main.cpp | 10 +++++----- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/esphome/components/web_server/web_server.cpp b/esphome/components/web_server/web_server.cpp index ad9ced0614..513399e257 100644 --- a/esphome/components/web_server/web_server.cpp +++ b/esphome/components/web_server/web_server.cpp @@ -2,12 +2,12 @@ #include "web_server.h" -#include "esphome/core/log.h" -#include "esphome/core/application.h" -#include "esphome/core/entity_base.h" -#include "esphome/core/util.h" #include "esphome/components/json/json_util.h" #include "esphome/components/network/util.h" +#include "esphome/core/application.h" +#include "esphome/core/entity_base.h" +#include "esphome/core/log.h" +#include "esphome/core/util.h" #include "StreamString.h" @@ -105,6 +105,7 @@ void WebServer::setup() { client->send(json::build_json([this](JsonObject root) { root["title"] = App.get_friendly_name().empty() ? App.get_name() : App.get_friendly_name(); + root["comment"] = App.get_comment(); root["ota"] = this->allow_ota_; root["lang"] = "en"; }).c_str(), diff --git a/esphome/core/application.h b/esphome/core/application.h index eaae34635f..0992a4df39 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -2,11 +2,11 @@ #include #include -#include "esphome/core/defines.h" -#include "esphome/core/preferences.h" #include "esphome/core/component.h" +#include "esphome/core/defines.h" #include "esphome/core/hal.h" #include "esphome/core/helpers.h" +#include "esphome/core/preferences.h" #include "esphome/core/scheduler.h" #ifdef USE_BINARY_SENSOR @@ -53,8 +53,8 @@ namespace esphome { class Application { public: - void pre_setup(const std::string &name, const std::string &friendly_name, const char *compilation_time, - bool name_add_mac_suffix) { + void pre_setup(const std::string &name, const std::string &friendly_name, const std::string &comment, + const char *compilation_time, bool name_add_mac_suffix) { arch_init(); this->name_add_mac_suffix_ = name_add_mac_suffix; if (name_add_mac_suffix) { @@ -68,6 +68,7 @@ class Application { this->name_ = name; this->friendly_name_ = friendly_name; } + this->comment_ = comment; this->compilation_time_ = compilation_time; } @@ -138,11 +139,13 @@ class Application { /// Make a loop iteration. Call this in your loop() function. void loop(); - /// Get the name of this Application set by set_name(). + /// Get the name of this Application set by pre_setup(). const std::string &get_name() const { return this->name_; } - /// Get the friendly name of this Application set by set_friendly_name(). + /// Get the friendly name of this Application set by pre_setup(). const std::string &get_friendly_name() const { return this->friendly_name_; } + /// Get the comment of this Application set by pre_setup(). + const std::string &get_comment() const { return this->comment_; } bool is_name_add_mac_suffix_enabled() const { return this->name_add_mac_suffix_; } @@ -349,6 +352,7 @@ class Application { std::string name_; std::string friendly_name_; + std::string comment_; std::string compilation_time_; bool name_add_mac_suffix_; uint32_t last_loop_{0}; diff --git a/esphome/core/config.py b/esphome/core/config.py index 2057ad356e..5501f484af 100644 --- a/esphome/core/config.py +++ b/esphome/core/config.py @@ -350,6 +350,7 @@ async def to_code(config): cg.App.pre_setup( config[CONF_NAME], config[CONF_FRIENDLY_NAME], + config.get(CONF_COMMENT, ""), cg.RawExpression('__DATE__ ", " __TIME__'), config[CONF_NAME_ADD_MAC_SUFFIX], ) diff --git a/tests/dummy_main.cpp b/tests/dummy_main.cpp index c49a67c9a2..236b9f5fc2 100644 --- a/tests/dummy_main.cpp +++ b/tests/dummy_main.cpp @@ -3,16 +3,16 @@ // matter at all, as long as it compiles). // Not used during runtime nor for CI. -#include -#include -#include -#include #include +#include +#include +#include +#include using namespace esphome; void setup() { - App.pre_setup("livingroom", "LivingRoom", __DATE__ ", " __TIME__, false); + App.pre_setup("livingroom", "LivingRoom", "comment", __DATE__ ", " __TIME__, false); auto *log = new logger::Logger(115200, 512); // NOLINT log->pre_setup(); log->set_uart_selection(logger::UART_SELECTION_UART0);