Make OTA function switchable in web_server component (#2685)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Laszlo Gazdag 2021-11-10 20:31:22 +01:00 committed by GitHub
parent 99c775d8cb
commit 0bdb48bcac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 5 deletions

View file

@ -12,6 +12,7 @@ from esphome.const import (
CONF_AUTH, CONF_AUTH,
CONF_USERNAME, CONF_USERNAME,
CONF_PASSWORD, CONF_PASSWORD,
CONF_OTA,
) )
from esphome.core import CORE, coroutine_with_priority from esphome.core import CORE, coroutine_with_priority
@ -41,6 +42,7 @@ CONFIG_SCHEMA = cv.Schema(
cv.GenerateID(CONF_WEB_SERVER_BASE_ID): cv.use_id( cv.GenerateID(CONF_WEB_SERVER_BASE_ID): cv.use_id(
web_server_base.WebServerBase web_server_base.WebServerBase
), ),
cv.Optional(CONF_OTA, default=True): cv.boolean,
} }
).extend(cv.COMPONENT_SCHEMA) ).extend(cv.COMPONENT_SCHEMA)
@ -57,6 +59,7 @@ async def to_code(config):
cg.add_define("USE_WEBSERVER") cg.add_define("USE_WEBSERVER")
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]))
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

@ -152,6 +152,8 @@ void WebServer::setup() {
#endif #endif
this->base_->add_handler(&this->events_); this->base_->add_handler(&this->events_);
this->base_->add_handler(this); this->base_->add_handler(this);
if (this->allow_ota_)
this->base_->add_ota_handler(); this->base_->add_ota_handler();
this->set_interval(10000, [this]() { this->events_.send("", "ping", millis(), 30000); }); this->set_interval(10000, [this]() { this->events_.send("", "ping", millis(), 30000); });
@ -240,10 +242,14 @@ void WebServer::handle_index_request(AsyncWebServerRequest *request) {
#endif #endif
stream->print(F("</tbody></table><p>See <a href=\"https://esphome.io/web-api/index.html\">ESPHome Web API</a> for " stream->print(F("</tbody></table><p>See <a href=\"https://esphome.io/web-api/index.html\">ESPHome Web API</a> for "
"REST API documentation.</p>" "REST API documentation.</p>"));
"<h2>OTA Update</h2><form method=\"POST\" action=\"/update\" enctype=\"multipart/form-data\"><input " if (this->allow_ota_) {
"type=\"file\" name=\"update\"><input type=\"submit\" value=\"Update\"></form>" stream->print(
"<h2>Debug Log</h2><pre id=\"log\"></pre>")); F("<h2>OTA Update</h2><form method=\"POST\" action=\"/update\" enctype=\"multipart/form-data\"><input "
"type=\"file\" name=\"update\"><input type=\"submit\" value=\"Update\"></form>"));
}
stream->print(F("<h2>Debug Log</h2><pre id=\"log\"></pre>"));
#ifdef WEBSERVER_JS_INCLUDE #ifdef WEBSERVER_JS_INCLUDE
if (this->js_include_ != nullptr) { if (this->js_include_ != nullptr) {
stream->print(F("<script src=\"/0.js\"></script>")); stream->print(F("<script src=\"/0.js\"></script>"));

View file

@ -58,6 +58,12 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
*/ */
void set_js_include(const char *js_include); void set_js_include(const char *js_include);
/** Set whether or not the webserver should expose the OTA form and handler.
*
* @param allow_ota.
*/
void set_allow_ota(bool allow_ota) { this->allow_ota_ = allow_ota; }
// ========== INTERNAL METHODS ========== // ========== INTERNAL METHODS ==========
// (In most use cases you won't need these) // (In most use cases you won't need these)
/// Setup the internal web server and register handlers. /// Setup the internal web server and register handlers.
@ -182,6 +188,7 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
const char *css_include_{nullptr}; const char *css_include_{nullptr};
const char *js_url_{nullptr}; const char *js_url_{nullptr};
const char *js_include_{nullptr}; const char *js_include_{nullptr};
bool allow_ota_{true};
}; };
} // namespace web_server } // namespace web_server

View file

@ -234,6 +234,7 @@ logger:
web_server: web_server:
port: 8080 port: 8080
ota: true
css_url: https://esphome.io/_static/webserver-v1.min.css css_url: https://esphome.io/_static/webserver-v1.min.css
js_url: https://esphome.io/_static/webserver-v1.min.js js_url: https://esphome.io/_static/webserver-v1.min.js

View file

@ -45,6 +45,7 @@ logger:
level: DEBUG level: DEBUG
web_server: web_server:
ota: false
auth: auth:
username: admin username: admin
password: admin password: admin