add custom entity type NVS

This commit is contained in:
MichaelDvP
2026-02-08 13:53:32 +01:00
parent c239658131
commit f83404c216
3 changed files with 40 additions and 14 deletions

View File

@@ -310,13 +310,15 @@ const CustomEntities = () => {
/> />
)} )}
</Cell> </Cell>
<Cell>{ei.ram > 0 ? '' : showHex(ei.device_id as number, 2)}</Cell>
<Cell>{ei.ram > 0 ? '' : showHex(ei.type_id as number, 3)}</Cell>
<Cell>{ei.ram > 0 ? '' : ei.offset}</Cell>
<Cell> <Cell>
{ei.ram === 1 ? '' : showHex(ei.device_id as number, 2)} {ei.ram === 1
</Cell> ? 'RAM'
<Cell>{ei.ram === 1 ? '' : showHex(ei.type_id as number, 3)}</Cell> : ei.ram === 2
<Cell>{ei.ram === 1 ? '' : ei.offset}</Cell> ? 'NVS'
<Cell> : DeviceValueTypeNames[ei.value_type]}
{ei.ram === 1 ? 'RAM' : DeviceValueTypeNames[ei.value_type]}
</Cell> </Cell>
<Cell>{formatValue(ei.value, ei.uom)}</Cell> <Cell>{formatValue(ei.value, ei.uom)}</Cell>
</Row> </Row>

View File

@@ -205,9 +205,10 @@ const CustomEntitiesDialog = ({
> >
<MenuItem value={0}>EMS-{LL.VALUE(1)}</MenuItem> <MenuItem value={0}>EMS-{LL.VALUE(1)}</MenuItem>
<MenuItem value={1}>RAM-{LL.VALUE(1)}</MenuItem> <MenuItem value={1}>RAM-{LL.VALUE(1)}</MenuItem>
<MenuItem value={2}>NVS-{LL.VALUE(1)}</MenuItem>
</TextField> </TextField>
</Grid> </Grid>
{editItem.ram === 1 && ( {editItem.ram > 0 && (
<> <>
<Grid> <Grid>
<TextField <TextField

View File

@@ -74,6 +74,11 @@ StateUpdateResult WebCustomEntity::update(JsonObject root, WebCustomEntity & web
if (entityItem.raw) { if (entityItem.raw) {
delete[] entityItem.raw; delete[] entityItem.raw;
} }
if (entityItem.ram == 2) { // NVS
char key[sizeof(entityItem.name) + 2];
snprintf(key, sizeof(key), "c:%s", entityItem.name);
EMSESP::nvs_.remove(key);
}
if (entityItem.ram) { // save name/value pairs for change checking if (entityItem.ram) { // save name/value pairs for change checking
doc[entityItem.name] = entityItem.value; doc[entityItem.name] = entityItem.value;
} }
@@ -95,9 +100,9 @@ StateUpdateResult WebCustomEntity::update(JsonObject root, WebCustomEntity & web
entityItem.value_type = ei["value_type"]; entityItem.value_type = ei["value_type"];
entityItem.writeable = ei["writeable"]; entityItem.writeable = ei["writeable"];
entityItem.hide = ei["hide"] | false; entityItem.hide = ei["hide"] | false;
entityItem.data = ei["value"].as<std::string>(); entityItem.data = ei["value"].as<std::string>().c_str();
strlcpy(entityItem.name, ei["name"].as<const char *>(), sizeof(entityItem.name)); strlcpy(entityItem.name, ei["name"].as<const char *>(), sizeof(entityItem.name));
if (entityItem.ram == 1) { if (entityItem.ram > 0) {
entityItem.device_id = 0; entityItem.device_id = 0;
entityItem.type_id = 0; entityItem.type_id = 0;
entityItem.value_type = DeviceValueType::STRING; entityItem.value_type = DeviceValueType::STRING;
@@ -127,7 +132,15 @@ StateUpdateResult WebCustomEntity::update(JsonObject root, WebCustomEntity & web
if (entityItem.factor == 0) { if (entityItem.factor == 0) {
entityItem.factor = 1; entityItem.factor = 1;
} }
if (entityItem.ram == 2) { // NVS
char key[sizeof(entityItem.name) + 2];
snprintf(key, sizeof(key), "c:%s", entityItem.name);
if (EMSESP::nvs_.isKey(key)) {
entityItem.data = EMSESP::nvs_.getString(key).c_str();
} else {
EMSESP::nvs_.putString(key, entityItem.data.c_str());
}
}
webCustomEntity.customEntityItems.push_back(entityItem); // add to list webCustomEntity.customEntityItems.push_back(entityItem); // add to list
if (entityItem.writeable && entityItem.name[0] != '\0') { if (entityItem.writeable && entityItem.name[0] != '\0') {
@@ -163,6 +176,13 @@ bool WebCustomEntityService::command_setvalue(const char * value, const int8_t i
if (Helpers::toLower(entityItem.name) == Helpers::toLower(name)) { if (Helpers::toLower(entityItem.name) == Helpers::toLower(name)) {
if (entityItem.ram == 1) { if (entityItem.ram == 1) {
entityItem.data = value; entityItem.data = value;
} else if (entityItem.ram == 2) { // NVS
entityItem.data = value;
char key[sizeof(entityItem.name) + 2];
snprintf(key, sizeof(key), "c:%s", entityItem.name);
if (EMSESP::nvs_.getString(key) != entityItem.data.c_str()) {
EMSESP::nvs_.putString(key, entityItem.data.c_str());
}
} else if (entityItem.value_type == DeviceValueType::STRING) { } else if (entityItem.value_type == DeviceValueType::STRING) {
auto telegram = strdup(value); auto telegram = strdup(value);
uint8_t length = strlen(telegram) / 3 + 1; uint8_t length = strlen(telegram) / 3 + 1;
@@ -335,10 +355,13 @@ bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd)
// build the json for specific entity // build the json for specific entity
void WebCustomEntityService::get_value_json(JsonObject output, CustomEntityItem const & entity) { void WebCustomEntityService::get_value_json(JsonObject output, CustomEntityItem const & entity) {
output["name"] = (const char *)entity.name; output["name"] = (const char *)entity.name;
output["fullname"] = (const char *)entity.name; output["fullname"] = (const char *)entity.name;
output["storage"] = entity.ram ? "ram" : "ems"; output["storage"] = entity.ram == 1 ? "ram" : entity.ram == 2 ? "nvs" : "ems";
output["type"] = entity.value_type == DeviceValueType::BOOL ? "boolean" : entity.value_type == DeviceValueType::STRING ? "string" : F_(number); output["type"] = entity.value_type == DeviceValueType::BOOL ? "boolean" : entity.value_type == DeviceValueType::STRING ? "string" : F_(number);
if (entity.uom) {
output["uom"] = EMSdevice::uom_to_string(entity.uom);
}
output["readable"] = true; output["readable"] = true;
output["writeable"] = entity.writeable; output["writeable"] = entity.writeable;
output["visible"] = true; output["visible"] = true;