mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 00:09:51 +03:00
fix webEntity commands
This commit is contained in:
@@ -94,12 +94,12 @@ const DashboarDevicesDialog = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
let helperText = '<';
|
let helperText = '<';
|
||||||
if (dv.u !== DeviceValueUOM.NONE) {
|
if (dv.s) {
|
||||||
helperText += 'n';
|
helperText += 'n';
|
||||||
if (dv.m && dv.x) {
|
if (dv.m !== undefined && dv.x !== undefined) {
|
||||||
|
|
||||||
helperText += ' between ' + dv.m + ' and ' + dv.x;
|
helperText += ' between ' + dv.m + ' and ' + dv.x;
|
||||||
}
|
} else {
|
||||||
if (dv.s) {
|
|
||||||
helperText += ' , step ' + dv.s;
|
helperText += ' , step ' + dv.s;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -144,7 +144,7 @@ const DashboarDevicesDialog = ({
|
|||||||
</MenuItem>
|
</MenuItem>
|
||||||
))}
|
))}
|
||||||
</TextField>
|
</TextField>
|
||||||
) : editItem.u !== DeviceValueUOM.NONE ? (
|
) : editItem.s || editItem.u !== DeviceValueUOM.NONE ? (
|
||||||
<ValidatedTextField
|
<ValidatedTextField
|
||||||
fieldErrors={fieldErrors}
|
fieldErrors={fieldErrors}
|
||||||
name="v"
|
name="v"
|
||||||
@@ -155,7 +155,7 @@ const DashboarDevicesDialog = ({
|
|||||||
type="number"
|
type="number"
|
||||||
sx={{ width: '30ch' }}
|
sx={{ width: '30ch' }}
|
||||||
onChange={updateFormValue}
|
onChange={updateFormValue}
|
||||||
inputProps={editItem.u ? { min: editItem.m, max: editItem.x, step: editItem.s } : {}}
|
inputProps={editItem.s ? { min: editItem.m, max: editItem.x, step: editItem.s } : {}}
|
||||||
InputProps={{
|
InputProps={{
|
||||||
startAdornment: <InputAdornment position="start">{setUom(editItem.u)}</InputAdornment>
|
startAdornment: <InputAdornment position="start">{setUom(editItem.u)}</InputAdornment>
|
||||||
}}
|
}}
|
||||||
|
|||||||
@@ -602,6 +602,19 @@ void Command::show_all(uuid::console::Shell & shell) {
|
|||||||
shell.print(COLOR_RESET);
|
shell.print(COLOR_RESET);
|
||||||
show(shell, EMSdevice::DeviceType::SYSTEM, true);
|
show(shell, EMSdevice::DeviceType::SYSTEM, true);
|
||||||
|
|
||||||
|
// show Custom
|
||||||
|
if (EMSESP::webEntityService.has_commands()) {
|
||||||
|
shell.print(COLOR_BOLD_ON);
|
||||||
|
shell.print(COLOR_YELLOW);
|
||||||
|
shell.printf(" %s: ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::CUSTOM));
|
||||||
|
shell.println(COLOR_RESET);
|
||||||
|
shell.printf(" info: %slists all values %s*", COLOR_BRIGHT_CYAN, COLOR_BRIGHT_RED);
|
||||||
|
shell.println(COLOR_RESET);
|
||||||
|
shell.printf(" commands: %slists all commands %s*", COLOR_BRIGHT_CYAN, COLOR_BRIGHT_RED);
|
||||||
|
shell.print(COLOR_RESET);
|
||||||
|
show(shell, EMSdevice::DeviceType::CUSTOM, true);
|
||||||
|
}
|
||||||
|
|
||||||
// show scheduler
|
// show scheduler
|
||||||
if (EMSESP::webSchedulerService.has_commands()) {
|
if (EMSESP::webSchedulerService.has_commands()) {
|
||||||
shell.print(COLOR_BOLD_ON);
|
shell.print(COLOR_BOLD_ON);
|
||||||
|
|||||||
@@ -242,7 +242,7 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar
|
|||||||
// the data could be in any format, but we need string
|
// the data could be in any format, but we need string
|
||||||
// authenticated is always true
|
// authenticated is always true
|
||||||
JsonVariant data = dv["v"]; // the value in any format
|
JsonVariant data = dv["v"]; // the value in any format
|
||||||
uint8_t return_code = CommandRet::OK;
|
uint8_t return_code = CommandRet::NOT_FOUND;
|
||||||
uint8_t device_type = emsdevice->device_type();
|
uint8_t device_type = emsdevice->device_type();
|
||||||
if (data.is<const char *>()) {
|
if (data.is<const char *>()) {
|
||||||
return_code = Command::call(device_type, cmd, data.as<const char *>(), true, id, output);
|
return_code = Command::call(device_type, cmd, data.as<const char *>(), true, id, output);
|
||||||
@@ -279,16 +279,16 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar
|
|||||||
auto * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_SMALL);
|
auto * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_SMALL);
|
||||||
JsonObject output = response->getRoot();
|
JsonObject output = response->getRoot();
|
||||||
JsonVariant data = dv["v"]; // the value in any format
|
JsonVariant data = dv["v"]; // the value in any format
|
||||||
uint8_t return_code = CommandRet::OK;
|
uint8_t return_code = CommandRet::NOT_FOUND;
|
||||||
uint8_t device_type = EMSdevice::DeviceType::CUSTOM;
|
uint8_t device_type = EMSdevice::DeviceType::CUSTOM;
|
||||||
if (data.is<int>()) {
|
if (data.is<const char *>()) {
|
||||||
|
return_code = Command::call(device_type, cmd, data.as<const char *>(), true, id, output);
|
||||||
|
} else if (data.is<int>()) {
|
||||||
char s[10];
|
char s[10];
|
||||||
return_code = Command::call(device_type, cmd, Helpers::render_value(s, data.as<int16_t>(), 0), true, id, output);
|
return_code = Command::call(device_type, cmd, Helpers::render_value(s, data.as<int16_t>(), 0), true, id, output);
|
||||||
} else if (data.is<float>()) {
|
} else if (data.is<float>()) {
|
||||||
char s[10];
|
char s[10];
|
||||||
return_code = Command::call(device_type, cmd, Helpers::render_value(s, data.as<float>(), 1), true, id, output);
|
return_code = Command::call(device_type, cmd, Helpers::render_value(s, data.as<float>(), 1), true, id, output);
|
||||||
} else if (data.is<bool>()) {
|
|
||||||
return_code = Command::call(device_type, cmd, data.as<bool>() ? "true" : "false", true, id, output);
|
|
||||||
}
|
}
|
||||||
if (return_code != CommandRet::OK) {
|
if (return_code != CommandRet::OK) {
|
||||||
EMSESP::logger().err("Write command failed %s (%s)", (const char *)output["message"], Command::return_code_string(return_code).c_str());
|
EMSESP::logger().err("Write command failed %s (%s)", (const char *)output["message"], Command::return_code_string(return_code).c_str());
|
||||||
|
|||||||
@@ -83,10 +83,12 @@ StateUpdateResult WebEntity::update(JsonObject & root, WebEntity & webEntity) {
|
|||||||
entityItem.value = EMS_VALUE_DEFAULT_SHORT;
|
entityItem.value = EMS_VALUE_DEFAULT_SHORT;
|
||||||
} else if (entityItem.value_type == DeviceValueType::USHORT) {
|
} else if (entityItem.value_type == DeviceValueType::USHORT) {
|
||||||
entityItem.value = EMS_VALUE_DEFAULT_USHORT;
|
entityItem.value = EMS_VALUE_DEFAULT_USHORT;
|
||||||
} else { // if (entityItem.value_type == DeviceValueType::ULONG || entityItem.valuetype == DeviceValueType::TIME) {
|
} else { // if (entityItem.value_type == DeviceValueType::ULONG || entityItem.value_type == DeviceValueType::TIME) {
|
||||||
entityItem.value = EMS_VALUE_DEFAULT_ULONG;
|
entityItem.value = EMS_VALUE_DEFAULT_ULONG;
|
||||||
}
|
}
|
||||||
|
if (entityItem.factor == 0) {
|
||||||
|
entityItem.factor = 1;
|
||||||
|
}
|
||||||
webEntity.entityItems.push_back(entityItem); // add to list
|
webEntity.entityItems.push_back(entityItem); // add to list
|
||||||
|
|
||||||
if (entityItem.writeable) {
|
if (entityItem.writeable) {
|
||||||
@@ -108,13 +110,13 @@ StateUpdateResult WebEntity::update(JsonObject & root, WebEntity & webEntity) {
|
|||||||
bool WebEntityService::command_setvalue(const char * value, const std::string name) {
|
bool WebEntityService::command_setvalue(const char * value, const std::string name) {
|
||||||
EMSESP::webEntityService.read([&](WebEntity & webEntity) { entityItems = &webEntity.entityItems; });
|
EMSESP::webEntityService.read([&](WebEntity & webEntity) { entityItems = &webEntity.entityItems; });
|
||||||
for (EntityItem & entityItem : *entityItems) {
|
for (EntityItem & entityItem : *entityItems) {
|
||||||
if (entityItem.name == name) {
|
if (Helpers::toLower(entityItem.name) == Helpers::toLower(name)) {
|
||||||
if (entityItem.value_type == DeviceValueType::BOOL) {
|
if (entityItem.value_type == DeviceValueType::BOOL) {
|
||||||
bool v;
|
bool v;
|
||||||
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 ? entityItem.type_id > 0xFF ? 1 : 0xFF : 0, 0);
|
EMSESP::send_write_request(entityItem.type_id, entityItem.device_id, entityItem.offset, v ? 0xFF : 0, 0);
|
||||||
} else {
|
} else {
|
||||||
float f;
|
float f;
|
||||||
if (!Helpers::value2float(value, f)) {
|
if (!Helpers::value2float(value, f)) {
|
||||||
@@ -226,6 +228,7 @@ bool WebEntityService::get_value_info(JsonObject & output, const char * cmd) {
|
|||||||
if (Helpers::toLower(entity.name) == Helpers::toLower(command_s)) {
|
if (Helpers::toLower(entity.name) == Helpers::toLower(command_s)) {
|
||||||
output["name"] = entity.name;
|
output["name"] = entity.name;
|
||||||
output["uom"] = EMSdevice::uom_to_string(entity.uom);
|
output["uom"] = EMSdevice::uom_to_string(entity.uom);
|
||||||
|
output["type"] = entity.value_type == DeviceValueType::BOOL ? "boolean" : F_(number);
|
||||||
output["readable"] = true;
|
output["readable"] = true;
|
||||||
output["writeable"] = entity.writeable;
|
output["writeable"] = entity.writeable;
|
||||||
output["visible"] = true;
|
output["visible"] = true;
|
||||||
@@ -311,19 +314,47 @@ void WebEntityService::publish(const bool force) {
|
|||||||
config["name"] = entityItem.name.c_str();
|
config["name"] = entityItem.name.c_str();
|
||||||
|
|
||||||
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
||||||
snprintf(topic, sizeof(topic), "sensor/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str());
|
if (entityItem.writeable) {
|
||||||
//char command_topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
if (entityItem.value_type == DeviceValueType::BOOL) {
|
||||||
// snprintf(command_topic, sizeof(command_topic), "%s/custom/%s", Mqtt::basename().c_str(), entityItem.name.c_str());
|
snprintf(topic, sizeof(topic), "switch/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str());
|
||||||
// config["cmd_t"] = command_topic;
|
} else if (Mqtt::discovery_type() == Mqtt::discoveryType::HOMEASSISTANT) {
|
||||||
|
snprintf(topic, sizeof(topic), "number/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str());
|
||||||
|
} else {
|
||||||
|
snprintf(topic, sizeof(topic), "sensor/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str());
|
||||||
|
}
|
||||||
|
char command_topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
||||||
|
snprintf(command_topic, sizeof(command_topic), "%s/custom/%s", Mqtt::basename().c_str(), entityItem.name.c_str());
|
||||||
|
config["cmd_t"] = command_topic;
|
||||||
|
} else {
|
||||||
|
if (entityItem.value_type == DeviceValueType::BOOL) {
|
||||||
|
snprintf(topic, sizeof(topic), "binary_sensor/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str());
|
||||||
|
} else {
|
||||||
|
snprintf(topic, sizeof(topic), "sensor/%s/custom_%s/config", Mqtt::basename().c_str(), entityItem.name.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (entityItem.value_type == DeviceValueType::BOOL) {
|
||||||
|
// applies to both Binary Sensor (read only) and a Switch (for a command)
|
||||||
|
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
||||||
|
config["pl_on"] = true;
|
||||||
|
config["pl_off"] = false;
|
||||||
|
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
|
||||||
|
config["pl_on"] = 1;
|
||||||
|
config["pl_off"] = 0;
|
||||||
|
} else {
|
||||||
|
char result[12];
|
||||||
|
config["pl_on"] = Helpers::render_boolean(result, true);
|
||||||
|
config["pl_off"] = Helpers::render_boolean(result, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
JsonObject dev = config.createNestedObject("dev");
|
JsonObject dev = config.createNestedObject("dev");
|
||||||
JsonArray ids = dev.createNestedArray("ids");
|
JsonArray ids = dev.createNestedArray("ids");
|
||||||
ids.add("ems-esp");
|
ids.add("ems-esp");
|
||||||
|
|
||||||
// add "availability" section
|
// add "availability" section
|
||||||
Mqtt::add_avty_to_doc(stat_t, config.as<JsonObject>(), val_cond);
|
Mqtt::add_avty_to_doc(stat_t, config.as<JsonObject>(), val_cond);
|
||||||
Mqtt::queue_ha(topic, config.as<JsonObject>());
|
if (Mqtt::queue_ha(topic, config.as<JsonObject>())) {
|
||||||
ha_registered_ = true;
|
ha_registered_ = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (output.size() > 0) {
|
if (output.size() > 0) {
|
||||||
@@ -349,6 +380,15 @@ uint8_t WebEntityService::count_entities() {
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t WebEntityService::has_commands() {
|
||||||
|
EMSESP::webEntityService.read([&](WebEntity & webEntity) { entityItems = &webEntity.entityItems; });
|
||||||
|
uint8_t count = 0;
|
||||||
|
for (const EntityItem & entity : *entityItems) {
|
||||||
|
count += entity.writeable ? 1 : 0;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
// send to dashboard, msgpack don't like serialized, use number
|
// send to dashboard, msgpack don't like serialized, use number
|
||||||
void WebEntityService::generate_value_web(JsonObject & output) {
|
void WebEntityService::generate_value_web(JsonObject & output) {
|
||||||
EMSESP::webEntityService.read([&](WebEntity & webEntity) { entityItems = &webEntity.entityItems; });
|
EMSESP::webEntityService.read([&](WebEntity & webEntity) { entityItems = &webEntity.entityItems; });
|
||||||
@@ -362,6 +402,10 @@ void WebEntityService::generate_value_web(JsonObject & output) {
|
|||||||
obj["u"] = entity.uom;
|
obj["u"] = entity.uom;
|
||||||
if (entity.writeable) {
|
if (entity.writeable) {
|
||||||
obj["c"] = entity.name;
|
obj["c"] = entity.name;
|
||||||
|
if (entity.value_type != DeviceValueType::BOOL) {
|
||||||
|
char s[10];
|
||||||
|
obj["s"] = Helpers::render_value(s, entity.factor, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (entity.value_type) {
|
switch (entity.value_type) {
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ class WebEntityService : public StatefulService<WebEntity> {
|
|||||||
void fetch();
|
void fetch();
|
||||||
void render_value(JsonObject & output, EntityItem entity, const bool useVal = false, const bool web = false);
|
void render_value(JsonObject & output, EntityItem entity, const bool useVal = false, const bool web = false);
|
||||||
uint8_t count_entities();
|
uint8_t count_entities();
|
||||||
|
uint8_t has_commands();
|
||||||
void generate_value_web(JsonObject & output);
|
void generate_value_web(JsonObject & output);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user