add tests for custom entities

This commit is contained in:
proddy
2025-04-23 21:30:02 +02:00
parent 1fa7a6c549
commit 5047f1752e
4 changed files with 87 additions and 20 deletions

View File

@@ -352,6 +352,10 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
#ifdef EMSESP_STANDALONE #ifdef EMSESP_STANDALONE
AsyncWebServerRequest request; AsyncWebServerRequest request;
JsonDocument doc;
JsonVariant json;
// read requests
request.method(HTTP_GET); request.method(HTTP_GET);
request.url("/api/custom"); request.url("/api/custom");
EMSESP::webAPIService.webAPIService(&request); EMSESP::webAPIService.webAPIService(&request);
@@ -363,6 +367,20 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
EMSESP::webAPIService.webAPIService(&request); EMSESP::webAPIService.webAPIService(&request);
shell.invoke_command("call custom info"); shell.invoke_command("call custom info");
// write requests
request.method(HTTP_POST);
char data[] = "{\"value\":\"99\"}";
deserializeJson(doc, data);
json = doc.as<JsonVariant>();
// validate
request.url("/api/custom/test_ram");
EMSESP::webAPIService.webAPIService(&request, json);
request.method(HTTP_GET);
request.url("/api/custom");
EMSESP::webAPIService.webAPIService(&request);
#endif #endif
ok = true; ok = true;
} }
@@ -1091,6 +1109,16 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
// POST COMMANDS // POST COMMANDS
request.method(HTTP_POST); request.method(HTTP_POST);
char data0[] = "{\"value\":12}";
deserializeJson(doc, data0);
request.url("/api/thermostat/seltemp");
EMSESP::webAPIService.webAPIService(&request, doc.as<JsonVariant>());
// request.method(HTTP_GET);
// request.url("/api/thermostat/seltemp/value");
// EMSESP::webAPIService.webAPIService(&request);
// request.method(HTTP_POST);
// char data1[] = "{\"device\":\"system\", \"cmd\":\"restart\",\"id\":-1}"; // char data1[] = "{\"device\":\"system\", \"cmd\":\"restart\",\"id\":-1}";
// deserializeJson(doc, data1); // deserializeJson(doc, data1);
// request.url("/api"); // request.url("/api");
@@ -1114,28 +1142,28 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
// test version checks // test version checks
// use same data as in rest_server.ts // use same data as in rest_server.ts
// log shows first if you can upgrade to dev, and then if you can upgrade to stable // log shows first if you can upgrade to dev, and then if you can upgrade to stable
request.url("/rest/action"); // request.url("/rest/action");
std::string LATEST_STABLE_VERSION = "3.7.2"; // std::string LATEST_STABLE_VERSION = "3.7.2";
std::string LATEST_DEV_VERSION = "3.7.3-dev.3"; // std::string LATEST_DEV_VERSION = "3.7.3-dev.3";
std::string param = LATEST_DEV_VERSION + "," + LATEST_STABLE_VERSION; // std::string param = LATEST_DEV_VERSION + "," + LATEST_STABLE_VERSION;
std::string action = "{\"action\":\"checkUpgrade\", \"param\":\"" + param + "\"}"; // std::string action = "{\"action\":\"checkUpgrade\", \"param\":\"" + param + "\"}";
deserializeJson(doc, action); // deserializeJson(doc, action);
// case 0: on latest stable, can upgrade to dev only. So true, false // // case 0: on latest stable, can upgrade to dev only. So true, false
EMSESP::webStatusService.set_current_version(LATEST_STABLE_VERSION); // EMSESP::webStatusService.set_current_version(LATEST_STABLE_VERSION);
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>()); // EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());
// case 1: on latest dev, no updates to either dev or stable. So false, false // // case 1: on latest dev, no updates to either dev or stable. So false, false
EMSESP::webStatusService.set_current_version(LATEST_DEV_VERSION); // EMSESP::webStatusService.set_current_version(LATEST_DEV_VERSION);
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>()); // EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());
// case 2: upgrade an older stable to latest stable or the latest dev. So true, true // // case 2: upgrade an older stable to latest stable or the latest dev. So true, true
EMSESP::webStatusService.set_current_version("3.6.5"); // EMSESP::webStatusService.set_current_version("3.6.5");
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>()); // EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());
// case 3: upgrade an older dev to latest dev, no stable upgrades available. So true, false // // case 3: upgrade an older dev to latest dev, no stable upgrades available. So true, false
EMSESP::webStatusService.set_current_version("3.7.3-dev.2"); // EMSESP::webStatusService.set_current_version("3.7.3-dev.2");
EMSESP::webStatusService.action(&request, doc.as<JsonVariant>()); // EMSESP::webStatusService.action(&request, doc.as<JsonVariant>());
// char data6[] = "{\"device\":\"system\", \"cmd\":\"read\",\"value\":\"8 2 27 1\"}"; // char data6[] = "{\"device\":\"system\", \"cmd\":\"read\",\"value\":\"8 2 27 1\"}";
// deserializeJson(doc, data6); // deserializeJson(doc, data6);

View File

@@ -41,7 +41,7 @@ namespace emsesp {
// #define EMSESP_DEBUG_DEFAULT "310" // #define EMSESP_DEBUG_DEFAULT "310"
// #define EMSESP_DEBUG_DEFAULT "render" // #define EMSESP_DEBUG_DEFAULT "render"
// #define EMSESP_DEBUG_DEFAULT "api" // #define EMSESP_DEBUG_DEFAULT "api"
#define EMSESP_DEBUG_DEFAULT "api3" // #define EMSESP_DEBUG_DEFAULT "api3"
// #define EMSESP_DEBUG_DEFAULT "api4" // #define EMSESP_DEBUG_DEFAULT "api4"
// #define EMSESP_DEBUG_DEFAULT "crash" // #define EMSESP_DEBUG_DEFAULT "crash"
// #define EMSESP_DEBUG_DEFAULT "dv" // #define EMSESP_DEBUG_DEFAULT "dv"
@@ -56,7 +56,7 @@ namespace emsesp {
// #define EMSESP_DEBUG_DEFAULT "telegram_dump" // #define EMSESP_DEBUG_DEFAULT "telegram_dump"
// #define EMSESP_DEBUG_DEFAULT "memory" // #define EMSESP_DEBUG_DEFAULT "memory"
// #define EMSESP_DEBUG_DEFAULT "coldshot" // #define EMSESP_DEBUG_DEFAULT "coldshot"
// #define EMSESP_DEBUG_DEFAULT "custom" #define EMSESP_DEBUG_DEFAULT "custom"
// #define EMSESP_DEBUG_DEFAULT "scheduler" // #define EMSESP_DEBUG_DEFAULT "scheduler"
// #define EMSESP_DEBUG_DEFAULT "heat_exchange" // #define EMSESP_DEBUG_DEFAULT "heat_exchange"
// #define EMSESP_DEBUG_DEFAULT "ls" // #define EMSESP_DEBUG_DEFAULT "ls"

View File

@@ -666,6 +666,7 @@ bool WebCustomEntityService::get_value(std::shared_ptr<const Telegram> telegram)
} }
// hard coded tests // hard coded tests
// add the entity and also add the command for writeable entities
#ifdef EMSESP_TEST #ifdef EMSESP_TEST
void WebCustomEntityService::test() { void WebCustomEntityService::test() {
update([&](WebCustomEntity & webCustomEntity) { update([&](WebCustomEntity & webCustomEntity) {
@@ -685,6 +686,14 @@ void WebCustomEntityService::test() {
entityItem.writeable = true; entityItem.writeable = true;
entityItem.data = "70"; entityItem.data = "70";
webCustomEntity.customEntityItems.push_back(entityItem); webCustomEntity.customEntityItems.push_back(entityItem);
Command::add(
EMSdevice::DeviceType::CUSTOM,
webCustomEntity.customEntityItems.back().name.c_str(),
[webCustomEntity](const char * value, const int8_t id) {
return EMSESP::webCustomEntityService.command_setvalue(value, id, webCustomEntity.customEntityItems.back().name.c_str());
},
FL_(entity_cmd),
CommandFlag::ADMIN_ONLY);
// test 2 // test 2
entityItem.id = 2; entityItem.id = 2;
@@ -713,6 +722,14 @@ void WebCustomEntityService::test() {
entityItem.writeable = true; entityItem.writeable = true;
entityItem.data = "14"; entityItem.data = "14";
webCustomEntity.customEntityItems.push_back(entityItem); webCustomEntity.customEntityItems.push_back(entityItem);
Command::add(
EMSdevice::DeviceType::CUSTOM,
webCustomEntity.customEntityItems.back().name.c_str(),
[webCustomEntity](const char * value, const int8_t id) {
return EMSESP::webCustomEntityService.command_setvalue(value, id, webCustomEntity.customEntityItems.back().name.c_str());
},
FL_(entity_cmd),
CommandFlag::ADMIN_ONLY);
// test 4 // test 4
entityItem.id = 4; entityItem.id = 4;
@@ -728,6 +745,14 @@ void WebCustomEntityService::test() {
entityItem.data = "14"; entityItem.data = "14";
entityItem.value = 12; entityItem.value = 12;
webCustomEntity.customEntityItems.push_back(entityItem); webCustomEntity.customEntityItems.push_back(entityItem);
Command::add(
EMSdevice::DeviceType::CUSTOM,
webCustomEntity.customEntityItems.back().name.c_str(),
[webCustomEntity](const char * value, const int8_t id) {
return EMSESP::webCustomEntityService.command_setvalue(value, id, webCustomEntity.customEntityItems.back().name.c_str());
},
FL_(entity_cmd),
CommandFlag::ADMIN_ONLY);
return StateUpdateResult::CHANGED; // persist the changes return StateUpdateResult::CHANGED; // persist the changes
}); });

View File

@@ -74,6 +74,20 @@ Authorization: Bearer {{token}}
GET {{host}}/api/boiler/commands GET {{host}}/api/boiler/commands
###
POST {{host}}/api/custom/test_custom
Content-Type: application/json
Authorization: Bearer {{token}}
{
"value" : 22.0
}
###
GET {{host}}/api/custom/test_custom
# #
# Test on dev # Test on dev
# #