mirror of
https://github.com/esphome/esphome.git
synced 2025-09-09 14:52:20 +01:00
Add support for button entities (#2824)
This commit is contained in:
@@ -198,6 +198,11 @@ void WebServer::handle_index_request(AsyncWebServerRequest *request) {
|
||||
write_row(stream, obj, "switch", "<button>Toggle</button>");
|
||||
#endif
|
||||
|
||||
#ifdef USE_BUTTON
|
||||
for (auto *obj : App.get_buttons())
|
||||
write_row(stream, obj, "button", "<button>Press</button>");
|
||||
#endif
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
for (auto *obj : App.get_binary_sensors())
|
||||
if (this->include_internal_ || !obj->is_internal())
|
||||
@@ -382,6 +387,26 @@ void WebServer::handle_switch_request(AsyncWebServerRequest *request, const UrlM
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_BUTTON
|
||||
void WebServer::handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match) {
|
||||
for (button::Button *obj : App.get_buttons()) {
|
||||
if (obj->is_internal())
|
||||
continue;
|
||||
if (obj->get_object_id() != match.id)
|
||||
continue;
|
||||
|
||||
if (request->method() == HTTP_POST && match.method == "press") {
|
||||
this->defer([obj]() { obj->press(); });
|
||||
request->send(200);
|
||||
} else {
|
||||
request->send(404);
|
||||
}
|
||||
return;
|
||||
}
|
||||
request->send(404);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
void WebServer::on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) {
|
||||
this->events_.send(this->binary_sensor_json(obj, state).c_str(), "state");
|
||||
@@ -715,6 +740,11 @@ bool WebServer::canHandle(AsyncWebServerRequest *request) {
|
||||
return true;
|
||||
#endif
|
||||
|
||||
#ifdef USE_BUTTON
|
||||
if (request->method() == HTTP_POST && match.domain == "button")
|
||||
return true;
|
||||
#endif
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
if (request->method() == HTTP_GET && match.domain == "binary_sensor")
|
||||
return true;
|
||||
@@ -787,6 +817,13 @@ void WebServer::handleRequest(AsyncWebServerRequest *request) {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_BUTTON
|
||||
if (match.domain == "button") {
|
||||
this->handle_button_request(request, match);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
if (match.domain == "binary_sensor") {
|
||||
this->handle_binary_sensor_request(request, match);
|
||||
|
Reference in New Issue
Block a user