mirror of
https://github.com/esphome/esphome.git
synced 2025-09-09 06:42:20 +01:00
Add select entities and implement template select (#2067)
Co-authored-by: Otto Winter <otto@otto-winter.com>
This commit is contained in:
@@ -129,6 +129,12 @@ void WebServer::setup() {
|
||||
if (!obj->is_internal())
|
||||
client->send(this->number_json(obj, obj->state).c_str(), "state");
|
||||
#endif
|
||||
|
||||
#ifdef USE_SELECT
|
||||
for (auto *obj : App.get_selects())
|
||||
if (!obj->is_internal())
|
||||
client->send(this->select_json(obj, obj->state).c_str(), "state");
|
||||
#endif
|
||||
});
|
||||
|
||||
#ifdef USE_LOGGER
|
||||
@@ -211,6 +217,11 @@ void WebServer::handle_index_request(AsyncWebServerRequest *request) {
|
||||
write_row(stream, obj, "number", "");
|
||||
#endif
|
||||
|
||||
#ifdef USE_SELECT
|
||||
for (auto *obj : App.get_selects())
|
||||
write_row(stream, obj, "select", "");
|
||||
#endif
|
||||
|
||||
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>"
|
||||
"<h2>OTA Update</h2><form method=\"POST\" action=\"/update\" enctype=\"multipart/form-data\"><input "
|
||||
@@ -626,6 +637,31 @@ std::string WebServer::number_json(number::Number *obj, float value) {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_SELECT
|
||||
void WebServer::on_select_update(select::Select *obj, const std::string &state) {
|
||||
this->events_.send(this->select_json(obj, state).c_str(), "state");
|
||||
}
|
||||
void WebServer::handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match) {
|
||||
for (auto *obj : App.get_selects()) {
|
||||
if (obj->is_internal())
|
||||
continue;
|
||||
if (obj->get_object_id() != match.id)
|
||||
continue;
|
||||
std::string data = this->select_json(obj, obj->state);
|
||||
request->send(200, "text/json", data.c_str());
|
||||
return;
|
||||
}
|
||||
request->send(404);
|
||||
}
|
||||
std::string WebServer::select_json(select::Select *obj, const std::string &value) {
|
||||
return json::build_json([obj, value](JsonObject &root) {
|
||||
root["id"] = "select-" + obj->get_object_id();
|
||||
root["state"] = value;
|
||||
root["value"] = value;
|
||||
});
|
||||
}
|
||||
#endif
|
||||
|
||||
bool WebServer::canHandle(AsyncWebServerRequest *request) {
|
||||
if (request->url() == "/")
|
||||
return true;
|
||||
@@ -683,6 +719,11 @@ bool WebServer::canHandle(AsyncWebServerRequest *request) {
|
||||
return true;
|
||||
#endif
|
||||
|
||||
#ifdef USE_SELECT
|
||||
if (request->method() == HTTP_GET && match.domain == "select")
|
||||
return true;
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
void WebServer::handleRequest(AsyncWebServerRequest *request) {
|
||||
@@ -765,6 +806,13 @@ void WebServer::handleRequest(AsyncWebServerRequest *request) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_SELECT
|
||||
if (match.domain == "select") {
|
||||
this->handle_select_request(request, match);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool WebServer::isRequestHandlerTrivial() { return false; }
|
||||
|
Reference in New Issue
Block a user