mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-11 10:19:55 +03:00
@@ -229,7 +229,7 @@ const MqttSettingsForm: FC = () => {
|
|||||||
<Grid container spacing={1} direction="row" justifyContent="flex-start" alignItems="flex-start">
|
<Grid container spacing={1} direction="row" justifyContent="flex-start" alignItems="flex-start">
|
||||||
<Grid item>
|
<Grid item>
|
||||||
<ValidatedTextField
|
<ValidatedTextField
|
||||||
name="entity_format"
|
name="discovery_type"
|
||||||
label={LL.MQTT_PUBLISH_TEXT_5()}
|
label={LL.MQTT_PUBLISH_TEXT_5()}
|
||||||
value={data.discovery_type}
|
value={data.discovery_type}
|
||||||
fullWidth
|
fullWidth
|
||||||
|
|||||||
@@ -443,13 +443,32 @@ void AnalogSensor::publish_values(const bool force) {
|
|||||||
case AnalogType::PWM_2:
|
case AnalogType::PWM_2:
|
||||||
dataSensor["value"] = serialized(Helpers::render_value(s, sensor.value(), 2)); // double
|
dataSensor["value"] = serialized(Helpers::render_value(s, sensor.value(), 2)); // double
|
||||||
break;
|
break;
|
||||||
|
case AnalogType::DIGITAL_IN:
|
||||||
|
case AnalogType::DIGITAL_OUT:
|
||||||
|
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
||||||
|
dataSensor["value"] = sensor.value() != 0;
|
||||||
|
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
|
||||||
|
dataSensor["value"] = sensor.value() != 0 ? 1 : 0;
|
||||||
|
} else {
|
||||||
|
char result[12];
|
||||||
|
dataSensor["value"] = Helpers::render_boolean(result, sensor.value() != 0);
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
dataSensor["value"] = (uint8_t)sensor.value(); // convert to char for 1 or 0
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} else if (sensor.type() == AnalogType::DIGITAL_IN || sensor.type() == AnalogType::DIGITAL_OUT) {
|
||||||
|
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
||||||
|
doc[sensor.name()] = sensor.value() != 0;
|
||||||
|
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
|
||||||
|
doc[sensor.name()] = sensor.value() != 0 ? 1 : 0;
|
||||||
|
} else {
|
||||||
|
char result[12];
|
||||||
|
doc[sensor.name()] = Helpers::render_boolean(result, sensor.value() != 0);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// not nested
|
char s[10];
|
||||||
doc[sensor.name()] = sensor.value();
|
doc[sensor.name()] = serialized(Helpers::render_value(s, sensor.value(), 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
// create HA config
|
// create HA config
|
||||||
@@ -463,15 +482,19 @@ void AnalogSensor::publish_values(const bool force) {
|
|||||||
config["stat_t"] = stat_t;
|
config["stat_t"] = stat_t;
|
||||||
|
|
||||||
char val_obj[50];
|
char val_obj[50];
|
||||||
char val_cond[65];
|
char val_cond[95];
|
||||||
if (Mqtt::is_nested()) {
|
if (Mqtt::is_nested()) {
|
||||||
snprintf(val_obj, sizeof(val_obj), "value_json['%02d'].value", sensor.gpio());
|
snprintf(val_obj, sizeof(val_obj), "value_json['%02d'].value", sensor.gpio());
|
||||||
snprintf(val_cond, sizeof(val_cond), "value_json['%02d'] is defined", sensor.gpio());
|
snprintf(val_cond, sizeof(val_cond), "value_json['%02d'] is defined and %s is defined", sensor.gpio(), val_obj);
|
||||||
} else {
|
} else {
|
||||||
snprintf(val_obj, sizeof(val_obj), "value_json['%s']", sensor.name().c_str());
|
snprintf(val_obj, sizeof(val_obj), "value_json['%s']", sensor.name().c_str());
|
||||||
snprintf(val_cond, sizeof(val_cond), "%s is defined", val_obj);
|
snprintf(val_cond, sizeof(val_cond), "%s is defined", val_obj);
|
||||||
}
|
}
|
||||||
config["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + "}}";
|
char sample_val[12] = "0";
|
||||||
|
if (sensor.type() == AnalogType::DIGITAL_IN || sensor.type() == AnalogType::DIGITAL_OUT) {
|
||||||
|
Helpers::render_boolean(sample_val, false);
|
||||||
|
}
|
||||||
|
config["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + " else " + sample_val + "}}";
|
||||||
|
|
||||||
char uniq_s[70];
|
char uniq_s[70];
|
||||||
if (Mqtt::entity_format() == 2) {
|
if (Mqtt::entity_format() == 2) {
|
||||||
|
|||||||
@@ -565,6 +565,9 @@ bool Command::device_has_commands(const uint8_t device_type) {
|
|||||||
void Command::show_devices(uuid::console::Shell & shell) {
|
void Command::show_devices(uuid::console::Shell & shell) {
|
||||||
shell.printf("%s ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::SYSTEM));
|
shell.printf("%s ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::SYSTEM));
|
||||||
|
|
||||||
|
if (EMSESP::webSchedulerService.has_commands()) {
|
||||||
|
shell.printf("%s ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::SCHEDULER));
|
||||||
|
}
|
||||||
if (EMSESP::dallassensor_.have_sensors()) {
|
if (EMSESP::dallassensor_.have_sensors()) {
|
||||||
shell.printf("%s ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::DALLASSENSOR));
|
shell.printf("%s ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::DALLASSENSOR));
|
||||||
}
|
}
|
||||||
@@ -596,11 +599,17 @@ void Command::show_all(uuid::console::Shell & shell) {
|
|||||||
show(shell, EMSdevice::DeviceType::SYSTEM, true);
|
show(shell, EMSdevice::DeviceType::SYSTEM, true);
|
||||||
|
|
||||||
// show scheduler
|
// show scheduler
|
||||||
shell.print(COLOR_BOLD_ON);
|
if (EMSESP::webSchedulerService.has_commands()) {
|
||||||
shell.print(COLOR_YELLOW);
|
shell.print(COLOR_BOLD_ON);
|
||||||
shell.printf(" %s: ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::SCHEDULER));
|
shell.print(COLOR_YELLOW);
|
||||||
shell.print(COLOR_RESET);
|
shell.printf(" %s: ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::SCHEDULER));
|
||||||
show(shell, EMSdevice::DeviceType::SCHEDULER, true);
|
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::SCHEDULER, true);
|
||||||
|
}
|
||||||
|
|
||||||
// show sensors
|
// show sensors
|
||||||
if (EMSESP::dallassensor_.have_sensors()) {
|
if (EMSESP::dallassensor_.have_sensors()) {
|
||||||
|
|||||||
@@ -517,15 +517,15 @@ void DallasSensor::publish_values(const bool force) {
|
|||||||
config["unit_of_meas"] = EMSdevice::uom_to_string(DeviceValueUOM::DEGREES);
|
config["unit_of_meas"] = EMSdevice::uom_to_string(DeviceValueUOM::DEGREES);
|
||||||
|
|
||||||
char val_obj[50];
|
char val_obj[50];
|
||||||
char val_cond[65];
|
char val_cond[95];
|
||||||
if (Mqtt::is_nested()) {
|
if (Mqtt::is_nested()) {
|
||||||
snprintf(val_obj, sizeof(val_obj), "value_json['%s'].temp", sensor.id().c_str());
|
snprintf(val_obj, sizeof(val_obj), "value_json['%s'].temp", sensor.id().c_str());
|
||||||
snprintf(val_cond, sizeof(val_cond), "value_json['%s'] is defined", sensor.id().c_str());
|
snprintf(val_cond, sizeof(val_cond), "value_json['%s'] is defined and %s is defined", sensor.id().c_str(), val_obj);
|
||||||
} else {
|
} else {
|
||||||
snprintf(val_obj, sizeof(val_obj), "value_json['%s']", sensor.name().c_str());
|
snprintf(val_obj, sizeof(val_obj), "value_json['%s']", sensor.name().c_str());
|
||||||
snprintf(val_cond, sizeof(val_cond), "%s is defined", val_obj);
|
snprintf(val_cond, sizeof(val_cond), "%s is defined", val_obj);
|
||||||
}
|
}
|
||||||
config["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + "}}";
|
config["val_tpl"] = (std::string) "{{" + val_obj + " if " + val_cond + " else -55}}";
|
||||||
|
|
||||||
char uniq_s[70];
|
char uniq_s[70];
|
||||||
if (Mqtt::entity_format() == 2) {
|
if (Mqtt::entity_format() == 2) {
|
||||||
|
|||||||
@@ -653,14 +653,17 @@ bool EMSESP::get_device_value_info(JsonObject & root, const char * cmd, const in
|
|||||||
|
|
||||||
// specific for the dallassensor
|
// specific for the dallassensor
|
||||||
if (devicetype == DeviceType::DALLASSENSOR) {
|
if (devicetype == DeviceType::DALLASSENSOR) {
|
||||||
EMSESP::dallassensor_.get_value_info(root, cmd, id);
|
return EMSESP::dallassensor_.get_value_info(root, cmd, id);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// analog sensor
|
// analog sensor
|
||||||
if (devicetype == DeviceType::ANALOGSENSOR) {
|
if (devicetype == DeviceType::ANALOGSENSOR) {
|
||||||
EMSESP::analogsensor_.get_value_info(root, cmd, id);
|
return EMSESP::analogsensor_.get_value_info(root, cmd, id);
|
||||||
return true;
|
}
|
||||||
|
|
||||||
|
// scheduler
|
||||||
|
if (devicetype == DeviceType::SCHEDULER) {
|
||||||
|
return EMSESP::webSchedulerService.get_value_info(root, cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
char error[100];
|
char error[100];
|
||||||
|
|||||||
20
src/mqtt.cpp
20
src/mqtt.cpp
@@ -424,6 +424,7 @@ void Mqtt::load_settings() {
|
|||||||
send_response_ = mqttSettings.send_response;
|
send_response_ = mqttSettings.send_response;
|
||||||
discovery_prefix_ = mqttSettings.discovery_prefix.c_str();
|
discovery_prefix_ = mqttSettings.discovery_prefix.c_str();
|
||||||
entity_format_ = mqttSettings.entity_format;
|
entity_format_ = mqttSettings.entity_format;
|
||||||
|
discovery_type_ = mqttSettings.discovery_type;
|
||||||
|
|
||||||
// convert to milliseconds
|
// convert to milliseconds
|
||||||
publish_time_boiler_ = mqttSettings.publish_time_boiler * 1000;
|
publish_time_boiler_ = mqttSettings.publish_time_boiler * 1000;
|
||||||
@@ -648,7 +649,7 @@ void Mqtt::ha_status() {
|
|||||||
|
|
||||||
// create the sensors - must match the MQTT payload keys
|
// create the sensors - must match the MQTT payload keys
|
||||||
// these are all from the heartbeat MQTT topic
|
// these are all from the heartbeat MQTT topic
|
||||||
if (!EMSESP::system_.ethernet_connected()) {
|
if (!EMSESP::system_.ethernet_connected() || WiFi.isConnected()) {
|
||||||
publish_system_ha_sensor_config(DeviceValueType::INT, "WiFi RSSI", "rssi", DeviceValueUOM::DBM);
|
publish_system_ha_sensor_config(DeviceValueType::INT, "WiFi RSSI", "rssi", DeviceValueUOM::DBM);
|
||||||
publish_system_ha_sensor_config(DeviceValueType::INT, "WiFi strength", "wifistrength", DeviceValueUOM::PERCENT);
|
publish_system_ha_sensor_config(DeviceValueType::INT, "WiFi strength", "wifistrength", DeviceValueUOM::PERCENT);
|
||||||
}
|
}
|
||||||
@@ -1311,8 +1312,8 @@ void Mqtt::publish_ha_climate_config(const uint8_t tag, const bool has_roomtemp,
|
|||||||
char seltemp_s[30];
|
char seltemp_s[30];
|
||||||
char currtemp_s[30];
|
char currtemp_s[30];
|
||||||
char hc_mode_cond[80];
|
char hc_mode_cond[80];
|
||||||
char seltemp_cond[80];
|
char seltemp_cond[100];
|
||||||
char currtemp_cond[170];
|
char currtemp_cond[100];
|
||||||
char mode_str_tpl[400];
|
char mode_str_tpl[400];
|
||||||
char name_s[10];
|
char name_s[10];
|
||||||
char uniq_id_s[60];
|
char uniq_id_s[60];
|
||||||
@@ -1371,9 +1372,9 @@ void Mqtt::publish_ha_climate_config(const uint8_t tag, const bool has_roomtemp,
|
|||||||
}
|
}
|
||||||
|
|
||||||
snprintf(temp_cmd_s, sizeof(temp_cmd_s), "~/thermostat/hc%d/seltemp", hc_num);
|
snprintf(temp_cmd_s, sizeof(temp_cmd_s), "~/thermostat/hc%d/seltemp", hc_num);
|
||||||
snprintf(mode_cmd_s, sizeof(temp_cmd_s), "~/thermostat/hc%d/mode", hc_num);
|
snprintf(mode_cmd_s, sizeof(mode_cmd_s), "~/thermostat/hc%d/mode", hc_num);
|
||||||
|
|
||||||
StaticJsonDocument<EMSESP_JSON_SIZE_LARGE> doc; // doc is 787 typically so 1024 should be enough
|
StaticJsonDocument<EMSESP_JSON_SIZE_XLARGE> doc; // 1024 is not enough
|
||||||
|
|
||||||
doc["~"] = mqtt_base_;
|
doc["~"] = mqtt_base_;
|
||||||
doc["uniq_id"] = uniq_id_s;
|
doc["uniq_id"] = uniq_id_s;
|
||||||
@@ -1444,22 +1445,23 @@ void Mqtt::add_avty_to_doc(const char * state_t, const JsonObject & doc, const c
|
|||||||
snprintf(tpl, sizeof(tpl), tpl_draft, "value == 'online'");
|
snprintf(tpl, sizeof(tpl), tpl_draft, "value == 'online'");
|
||||||
avty_json["val_tpl"] = tpl;
|
avty_json["val_tpl"] = tpl;
|
||||||
avty.add(avty_json);
|
avty.add(avty_json);
|
||||||
avty.clear();
|
avty_json.clear();
|
||||||
|
|
||||||
avty_json["t"] = state_t;
|
avty_json["t"] = state_t;
|
||||||
snprintf(tpl, sizeof(tpl), tpl_draft, cond1 == nullptr ? "value is defined" : cond1);
|
snprintf(tpl, sizeof(tpl), tpl_draft, cond1 == nullptr ? "value is defined" : cond1);
|
||||||
avty_json["val_tpl"] = tpl;
|
avty_json["val_tpl"] = tpl;
|
||||||
avty.add(avty_json);
|
avty.add(avty_json);
|
||||||
|
|
||||||
if (cond2 != nullptr) {
|
if (cond2 != nullptr) {
|
||||||
avty.clear();
|
avty_json.clear();
|
||||||
|
avty_json["t"] = state_t;
|
||||||
snprintf(tpl, sizeof(tpl), tpl_draft, cond2);
|
snprintf(tpl, sizeof(tpl), tpl_draft, cond2);
|
||||||
avty_json["val_tpl"] = tpl;
|
avty_json["val_tpl"] = tpl;
|
||||||
avty.add(avty_json);
|
avty.add(avty_json);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (negcond != nullptr) {
|
if (negcond != nullptr) {
|
||||||
avty.clear();
|
avty_json.clear();
|
||||||
|
avty_json["t"] = state_t;
|
||||||
snprintf(tpl, sizeof(tpl), "{{'offline' if %s else 'online'}}", negcond);
|
snprintf(tpl, sizeof(tpl), "{{'offline' if %s else 'online'}}", negcond);
|
||||||
avty_json["val_tpl"] = tpl;
|
avty_json["val_tpl"] = tpl;
|
||||||
avty.add(avty_json);
|
avty.add(avty_json);
|
||||||
|
|||||||
@@ -128,6 +128,74 @@ bool WebSchedulerService::command_setvalue(const char * value, const std::string
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// process json output for info/commands and value_info
|
||||||
|
bool WebSchedulerService::get_value_info(JsonObject & output, const char * cmd) {
|
||||||
|
EMSESP::webSchedulerService.read([&](WebScheduler & webScheduler) { scheduleItems = &webScheduler.scheduleItems; });
|
||||||
|
if (scheduleItems->size() == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (Helpers::toLower(cmd) == "commands") {
|
||||||
|
output["info"] = "lists all values";
|
||||||
|
output["commands"] = "lists all commands";
|
||||||
|
for (const ScheduleItem & scheduleItem : *scheduleItems) {
|
||||||
|
if (!scheduleItem.name.empty()) {
|
||||||
|
output[scheduleItem.name] = "activate schedule";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (strlen(cmd) == 0 || Helpers::toLower(cmd) == "values" || Helpers::toLower(cmd) == "info") {
|
||||||
|
// list all names
|
||||||
|
for (const ScheduleItem & scheduleItem : *scheduleItems) {
|
||||||
|
if (!scheduleItem.name.empty()) {
|
||||||
|
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
||||||
|
output[scheduleItem.name] = scheduleItem.active;
|
||||||
|
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
|
||||||
|
output[scheduleItem.name] = scheduleItem.active ? 1 : 0;
|
||||||
|
} else {
|
||||||
|
char result[12];
|
||||||
|
output[scheduleItem.name] = Helpers::render_boolean(result, scheduleItem.active);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (output.size() > 0);
|
||||||
|
}
|
||||||
|
char command_s[30];
|
||||||
|
strlcpy(command_s, cmd, sizeof(command_s));
|
||||||
|
char * attribute_s = nullptr;
|
||||||
|
|
||||||
|
// check specific attribute to fetch instead of the complete record
|
||||||
|
char * breakp = strchr(command_s, '/');
|
||||||
|
if (breakp) {
|
||||||
|
*breakp = '\0';
|
||||||
|
attribute_s = breakp + 1;
|
||||||
|
}
|
||||||
|
JsonVariant data;
|
||||||
|
for (const ScheduleItem & scheduleItem : *scheduleItems) {
|
||||||
|
if (Helpers::toLower(scheduleItem.name) == Helpers::toLower(command_s)) {
|
||||||
|
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
||||||
|
output[scheduleItem.name] = scheduleItem.active;
|
||||||
|
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
|
||||||
|
output[scheduleItem.name] = scheduleItem.active ? 1 : 0;
|
||||||
|
} else {
|
||||||
|
char result[12];
|
||||||
|
output[scheduleItem.name] = Helpers::render_boolean(result, scheduleItem.active);
|
||||||
|
}
|
||||||
|
data = output[scheduleItem.name];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (attribute_s && !strcmp(attribute_s, "value")) {
|
||||||
|
output.clear();
|
||||||
|
output["api_data"] = data;
|
||||||
|
}
|
||||||
|
if (output.size()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
output["message"] = "unknown command";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// publish single value
|
||||||
void WebSchedulerService::publish_single(const char * name, const bool state) {
|
void WebSchedulerService::publish_single(const char * name, const bool state) {
|
||||||
if (!Mqtt::publish_single() || name == nullptr || name[0] == '\0') {
|
if (!Mqtt::publish_single() || name == nullptr || name[0] == '\0') {
|
||||||
return;
|
return;
|
||||||
@@ -213,7 +281,9 @@ void WebSchedulerService::publish(const bool force) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Mqtt::queue_publish("scheduler_data", doc.as<JsonObject>());
|
if (doc.size() > 0) {
|
||||||
|
Mqtt::queue_publish("scheduler_data", doc.as<JsonObject>());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WebSchedulerService::has_commands() {
|
bool WebSchedulerService::has_commands() {
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ class WebSchedulerService : public StatefulService<WebScheduler> {
|
|||||||
void publish(const bool force = false);
|
void publish(const bool force = false);
|
||||||
bool has_commands();
|
bool has_commands();
|
||||||
bool command_setvalue(const char * value, const std::string name);
|
bool command_setvalue(const char * value, const std::string name);
|
||||||
|
bool get_value_info(JsonObject & output, const char * cmd);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool command(const char * cmd, const char * data);
|
bool command(const char * cmd, const char * data);
|
||||||
|
|||||||
Reference in New Issue
Block a user