custom entities: bool write, dashboard show and count

This commit is contained in:
MichaelDvP
2023-04-27 09:07:38 +02:00
parent 57cc39c087
commit f15fb9a5d1

View File

@@ -51,7 +51,7 @@ void WebEntity::read(WebEntity & webEntity, JsonObject & root) {
} }
} }
// call on initialization and also when the Schedule web page is updated // call on initialization and also when the Entity web page is updated
// this loads the data into the internal class // this loads the data into the internal class
StateUpdateResult WebEntity::update(JsonObject & root, WebEntity & webEntity) { StateUpdateResult WebEntity::update(JsonObject & root, WebEntity & webEntity) {
for (EntityItem & entityItem : webEntity.entityItems) { for (EntityItem & entityItem : webEntity.entityItems) {
@@ -111,7 +111,7 @@ bool WebEntityService::command_setvalue(const char * value, const std::string na
if (!Helpers::value2bool(value, v)) { if (!Helpers::value2bool(value, v)) {
return false; return false;
} }
EMSESP::send_write_request(entityItem.type_id, entityItem.device_id, entityItem.offset, v ? 0xFF : 0, 0); EMSESP::send_write_request(entityItem.type_id, entityItem.device_id, entityItem.offset, v ? entityItem.type_id > 0xFF ? 1 : 0xFF : 0, 0);
} else { } else {
float f; float f;
if (!Helpers::value2float(value, f)) { if (!Helpers::value2float(value, f)) {
@@ -326,7 +326,7 @@ void WebEntityService::publish(const bool force) {
// EMSESP::logger().debug("publish %d custom entities", output.size()); // EMSESP::logger().debug("publish %d custom entities", output.size());
} }
// count only entities with valid value // count only entities with valid value or command to show in dashboard
uint8_t WebEntityService::count_entities() { uint8_t WebEntityService::count_entities() {
EMSESP::webEntityService.read([&](WebEntity & webEntity) { entityItems = &webEntity.entityItems; }); EMSESP::webEntityService.read([&](WebEntity & webEntity) { entityItems = &webEntity.entityItems; });
if (entityItems->size() == 0) { if (entityItems->size() == 0) {
@@ -334,10 +334,12 @@ uint8_t WebEntityService::count_entities() {
} }
DynamicJsonDocument doc(EMSESP_JSON_SIZE_XLARGE); DynamicJsonDocument doc(EMSESP_JSON_SIZE_XLARGE);
JsonObject output = doc.to<JsonObject>(); JsonObject output = doc.to<JsonObject>();
uint8_t count = 0;
for (const EntityItem & entity : *entityItems) { for (const EntityItem & entity : *entityItems) {
render_value(output, entity); render_value(output, entity);
count += (output.containsKey(entity.name) || entity.writeable) ? 1 : 0;
} }
return output.size(); return count;
} }
// send to dashboard, msgpack don't like serialized, use number // send to dashboard, msgpack don't like serialized, use number
@@ -345,6 +347,7 @@ void WebEntityService::generate_value_web(JsonObject & output) {
EMSESP::webEntityService.read([&](WebEntity & webEntity) { entityItems = &webEntity.entityItems; }); EMSESP::webEntityService.read([&](WebEntity & webEntity) { entityItems = &webEntity.entityItems; });
output["label"] = (std::string) "Custom Entities"; output["label"] = (std::string) "Custom Entities";
JsonArray data = output.createNestedArray("data"); JsonArray data = output.createNestedArray("data");
uint8_t index = 0;
for (const EntityItem & entity : *entityItems) { for (const EntityItem & entity : *entityItems) {
JsonObject obj = data.createNestedObject(); // create the object, we know there is a value JsonObject obj = data.createNestedObject(); // create the object, we know there is a value
obj["id"] = "00" + entity.name; obj["id"] = "00" + entity.name;
@@ -390,6 +393,11 @@ void WebEntityService::generate_value_web(JsonObject & output) {
default: default:
break; break;
} }
if (!obj.containsKey("v") && !obj.containsKey("c")) {
data.remove(index);
} else {
index++;
}
} }
} }