Add configuration option to disable the log UI. (#4419)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Dion Hulse 2023-06-22 07:58:49 +10:00 committed by Jesse Hills
parent a40fa22055
commit 74daca668e
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A
4 changed files with 25 additions and 9 deletions

View file

@ -14,6 +14,7 @@ from esphome.const import (
CONF_PASSWORD, CONF_PASSWORD,
CONF_INCLUDE_INTERNAL, CONF_INCLUDE_INTERNAL,
CONF_OTA, CONF_OTA,
CONF_LOG,
CONF_VERSION, CONF_VERSION,
CONF_LOCAL, CONF_LOCAL,
) )
@ -71,6 +72,7 @@ CONFIG_SCHEMA = cv.All(
), ),
cv.Optional(CONF_INCLUDE_INTERNAL, default=False): cv.boolean, cv.Optional(CONF_INCLUDE_INTERNAL, default=False): cv.boolean,
cv.Optional(CONF_OTA, default=True): cv.boolean, cv.Optional(CONF_OTA, default=True): cv.boolean,
cv.Optional(CONF_LOG, default=True): cv.boolean,
cv.Optional(CONF_LOCAL): cv.boolean, cv.Optional(CONF_LOCAL): cv.boolean,
} }
).extend(cv.COMPONENT_SCHEMA), ).extend(cv.COMPONENT_SCHEMA),
@ -132,6 +134,7 @@ async def to_code(config):
cg.add(var.set_css_url(config[CONF_CSS_URL])) cg.add(var.set_css_url(config[CONF_CSS_URL]))
cg.add(var.set_js_url(config[CONF_JS_URL])) cg.add(var.set_js_url(config[CONF_JS_URL]))
cg.add(var.set_allow_ota(config[CONF_OTA])) cg.add(var.set_allow_ota(config[CONF_OTA]))
cg.add(var.set_expose_log(config[CONF_LOG]))
if CONF_AUTH in config: if CONF_AUTH in config:
cg.add(paren.set_auth_username(config[CONF_AUTH][CONF_USERNAME])) cg.add(paren.set_auth_username(config[CONF_AUTH][CONF_USERNAME]))
cg.add(paren.set_auth_password(config[CONF_AUTH][CONF_PASSWORD])) cg.add(paren.set_auth_password(config[CONF_AUTH][CONF_PASSWORD]))

View file

@ -102,6 +102,16 @@ void WebServer::set_css_include(const char *css_include) { this->css_include_ =
void WebServer::set_js_include(const char *js_include) { this->js_include_ = js_include; } void WebServer::set_js_include(const char *js_include) { this->js_include_ = js_include; }
#endif #endif
std::string WebServer::get_config_json() {
return 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["log"] = this->expose_log_;
root["lang"] = "en";
});
}
void WebServer::setup() { void WebServer::setup() {
ESP_LOGCONFIG(TAG, "Setting up web server..."); ESP_LOGCONFIG(TAG, "Setting up web server...");
this->setup_controller(this->include_internal_); this->setup_controller(this->include_internal_);
@ -109,20 +119,13 @@ void WebServer::setup() {
this->events_.onConnect([this](AsyncEventSourceClient *client) { this->events_.onConnect([this](AsyncEventSourceClient *client) {
// Configure reconnect timeout and send config // Configure reconnect timeout and send config
client->send(this->get_config_json().c_str(), "ping", millis(), 30000);
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(),
"ping", millis(), 30000);
this->entities_iterator_.begin(this->include_internal_); this->entities_iterator_.begin(this->include_internal_);
}); });
#ifdef USE_LOGGER #ifdef USE_LOGGER
if (logger::global_logger != nullptr) { if (logger::global_logger != nullptr && this->expose_log_) {
logger::global_logger->add_on_log_callback( logger::global_logger->add_on_log_callback(
[this](int level, const char *tag, const char *message) { this->events_.send(message, "log", millis()); }); [this](int level, const char *tag, const char *message) { this->events_.send(message, "log", millis()); });
} }

View file

@ -99,6 +99,11 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
* @param allow_ota. * @param allow_ota.
*/ */
void set_allow_ota(bool allow_ota) { this->allow_ota_ = allow_ota; } void set_allow_ota(bool allow_ota) { this->allow_ota_ = allow_ota; }
/** Set whether or not the webserver should expose the Log.
*
* @param expose_log.
*/
void set_expose_log(bool expose_log) { this->expose_log_ = expose_log; }
// ========== INTERNAL METHODS ========== // ========== INTERNAL METHODS ==========
// (In most use cases you won't need these) // (In most use cases you won't need these)
@ -114,6 +119,9 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
/// Handle an index request under '/'. /// Handle an index request under '/'.
void handle_index_request(AsyncWebServerRequest *request); void handle_index_request(AsyncWebServerRequest *request);
/// Return the webserver configuration as JSON.
std::string get_config_json();
#ifdef USE_WEBSERVER_CSS_INCLUDE #ifdef USE_WEBSERVER_CSS_INCLUDE
/// Handle included css request under '/0.css'. /// Handle included css request under '/0.css'.
void handle_css_request(AsyncWebServerRequest *request); void handle_css_request(AsyncWebServerRequest *request);
@ -274,6 +282,7 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
#endif #endif
bool include_internal_{false}; bool include_internal_{false};
bool allow_ota_{true}; bool allow_ota_{true};
bool expose_log_{true};
#ifdef USE_ESP32 #ifdef USE_ESP32
std::deque<std::function<void()>> to_schedule_; std::deque<std::function<void()>> to_schedule_;
SemaphoreHandle_t to_schedule_lock_; SemaphoreHandle_t to_schedule_lock_;

View file

@ -368,6 +368,7 @@ CONF_LINE_TYPE = "line_type"
CONF_LOADED_INTEGRATIONS = "loaded_integrations" CONF_LOADED_INTEGRATIONS = "loaded_integrations"
CONF_LOCAL = "local" CONF_LOCAL = "local"
CONF_LOCK_ACTION = "lock_action" CONF_LOCK_ACTION = "lock_action"
CONF_LOG = "log"
CONF_LOG_TOPIC = "log_topic" CONF_LOG_TOPIC = "log_topic"
CONF_LOGGER = "logger" CONF_LOGGER = "logger"
CONF_LOGS = "logs" CONF_LOGS = "logs"