Merge branch 'v1.10.0'

This commit is contained in:
Nicu Hodos 2025-09-10 14:10:19 +02:00
commit ad03e147b9
2 changed files with 59 additions and 2 deletions

View File

@ -1,7 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json",
"name": "ha-mqtt",
"version": "1.9.0",
"version": "1.10.0",
"description": "Home Assistant classes for integration with MQTT auto discovery",
"repository": {
"type": "git",
@ -16,7 +16,8 @@
],
"dependencies": {
"bblanchon/ArduinoJson": "6.21.5",
"marvinroger/AsyncMqttClient": "^0.9.0"
"marvinroger/AsyncMqttClient": "^0.9.0",
"esphome/ESPAsyncWebServer-esphome": "^3.4.0"
},
"license": "MIT",
"frameworks": "arduino",

56
src/webserver.h Normal file
View File

@ -0,0 +1,56 @@
#ifdef ESP32
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESPAsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>
#include "ha.h"
AsyncWebServer server(80);
namespace WebServer {
void setup() {
server.on("/commands", HTTP_POST, [](AsyncWebServerRequest *request) {
if (request->hasParam("id", true) && request->hasParam("state", true)) {
AsyncWebParameter* switchId = request->getParam("id", true);
AsyncWebParameter* switchState = request->getParam("state", true);
auto cmd = Command::mapCommandIds[string(switchId->value().c_str())];
if (cmd) {
cmd->onCommand(switchState->value().c_str());
request->send(200, "text/plain", switchState->value().c_str());
} else {
request->send(200, "text/plain", "Command not found");
}
} else {
request->send(200, "text/plain", "No parameters provided");
}
});
server.on("/commands", HTTP_GET, [](AsyncWebServerRequest *request) {
AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonDocument jsonResponse(JSON_SIZE*10);
JsonArray array = jsonResponse.to<JsonArray>();
for (auto it = Command::mapCommandIds.begin(); it != Command::mapCommandIds.end(); ++it) {
StaticJsonDocument<JSON_SIZE/2> jsonDoc;
it->second->toJson(jsonDoc);
array.add(jsonDoc);
}
serializeJson(jsonResponse, *response);
request->send(response);
});
server.onNotFound([](AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
});
server.begin();
}
void stop() {
server.end();
}
}