mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-08 00:39:50 +03:00
Move dallas/bool/enum formats to Settings #76
This commit is contained in:
@@ -57,8 +57,9 @@ void DallasSensor::start() {
|
||||
// load the MQTT settings
|
||||
void DallasSensor::reload() {
|
||||
EMSESP::webSettingsService.read([&](WebSettings & settings) {
|
||||
dallas_gpio_ = settings.dallas_gpio;
|
||||
parasite_ = settings.dallas_parasite;
|
||||
dallas_gpio_ = settings.dallas_gpio;
|
||||
parasite_ = settings.dallas_parasite;
|
||||
dallas_format_ = settings.dallas_format;
|
||||
});
|
||||
|
||||
if (Mqtt::ha_enabled()) {
|
||||
@@ -338,7 +339,7 @@ bool DallasSensor::command_info(const char * value, const int8_t id, JsonObject
|
||||
dataSensor["temp"] = (float)(sensor.temperature_c) / 10;
|
||||
}
|
||||
} else { // show according to format
|
||||
if (Mqtt::dallas_format() == Mqtt::Dallas_Format::SENSORID && Helpers::hasValue(sensor.temperature_c)) {
|
||||
if (dallas_format_ == Dallas_Format::SENSORID && Helpers::hasValue(sensor.temperature_c)) {
|
||||
json[sensor.to_string()] = (float)(sensor.temperature_c) / 10;
|
||||
} else if (Helpers::hasValue(sensor.temperature_c)) {
|
||||
json[sensorID] = (float)(sensor.temperature_c) / 10;
|
||||
@@ -360,19 +361,15 @@ void DallasSensor::publish_values(const bool force) {
|
||||
DynamicJsonDocument doc(100 * num_sensors);
|
||||
uint8_t sensor_no = 1;
|
||||
|
||||
// dallas format is overriden when using Home Assistant
|
||||
// uint8_t dallas_format = Mqtt::ha_enabled() ? Mqtt::Dallas_Format::NUMBER : Mqtt::dallas_format();
|
||||
uint8_t dallas_format = Mqtt::dallas_format();
|
||||
|
||||
for (const auto & sensor : sensors_) {
|
||||
char sensorID[10]; // sensor{1-n}
|
||||
snprintf_P(sensorID, 10, PSTR("sensor%d"), sensor_no);
|
||||
if (dallas_format == Mqtt::Dallas_Format::SENSORID) {
|
||||
if (dallas_format_ == Dallas_Format::SENSORID) {
|
||||
// e.g. dallassensor_data = {"28-EA41-9497-0E03":23.3,"28-233D-9497-0C03":24.0}
|
||||
if (Helpers::hasValue(sensor.temperature_c)) {
|
||||
doc[sensor.to_string()] = (float)(sensor.temperature_c) / 10;
|
||||
}
|
||||
} else if (dallas_format == Mqtt::Dallas_Format::NUMBER) {
|
||||
} else if (dallas_format_ == Dallas_Format::NUMBER) {
|
||||
// e.g. dallassensor_data = {"sensor1":{"id":"28-EA41-9497-0E03","temp":23.3},"sensor2":{"id":"28-233D-9497-0C03","temp":24.0}}
|
||||
JsonObject dataSensor = doc.createNestedObject(sensorID);
|
||||
dataSensor["id"] = sensor.to_string();
|
||||
@@ -395,7 +392,7 @@ void DallasSensor::publish_values(const bool force) {
|
||||
config["unit_of_meas"] = FJSON("°C");
|
||||
|
||||
char str[50];
|
||||
if (dallas_format == Mqtt::Dallas_Format::SENSORID) {
|
||||
if (dallas_format_ == Dallas_Format::SENSORID) {
|
||||
snprintf_P(str, sizeof(str), PSTR("{{value_json['%s']}}"), sensor.to_string().c_str());
|
||||
} else {
|
||||
snprintf_P(str, sizeof(str), PSTR("{{value_json.sensor%d.temp}}"), sensor_no);
|
||||
@@ -403,7 +400,7 @@ void DallasSensor::publish_values(const bool force) {
|
||||
config["val_tpl"] = str;
|
||||
|
||||
// name as sensor number not the long unique ID
|
||||
if (dallas_format == Mqtt::Dallas_Format::SENSORID) {
|
||||
if (dallas_format_ == Dallas_Format::SENSORID) {
|
||||
snprintf_P(str, sizeof(str), PSTR("Dallas Sensor %s"), sensor.to_string().c_str());
|
||||
} else {
|
||||
snprintf_P(str, sizeof(str), PSTR("Dallas Sensor %d"), sensor_no);
|
||||
@@ -418,7 +415,7 @@ void DallasSensor::publish_values(const bool force) {
|
||||
ids.add("ems-esp");
|
||||
|
||||
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
|
||||
if (dallas_format == Mqtt::Dallas_Format::SENSORID) {
|
||||
if (dallas_format_ == Dallas_Format::SENSORID) {
|
||||
// use '_' as HA doesn't like '-' in the topic name
|
||||
std::string topicname = sensor.to_string();
|
||||
std::replace(topicname.begin(), topicname.end(), '-', '_');
|
||||
|
||||
@@ -36,6 +36,8 @@
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
enum Dallas_Format : uint8_t { SENSORID = 1, NUMBER, NAME };
|
||||
|
||||
class DallasSensor {
|
||||
public:
|
||||
class Sensor {
|
||||
@@ -76,6 +78,14 @@ class DallasSensor {
|
||||
return (dallas_gpio_ != 0);
|
||||
}
|
||||
|
||||
uint8_t dallas_format() {
|
||||
return dallas_format_;
|
||||
}
|
||||
|
||||
void dallas_format(uint8_t dallas_format) {
|
||||
dallas_format_ = dallas_format;
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr uint8_t MAX_SENSORS = 20;
|
||||
|
||||
@@ -134,6 +144,7 @@ class DallasSensor {
|
||||
uint32_t sensorfails_ = 0;
|
||||
uint32_t sensorreads_ = 0;
|
||||
int8_t scanretry_ = 0;
|
||||
uint8_t dallas_format_ = 0;
|
||||
};
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
@@ -84,6 +84,10 @@
|
||||
#define EMSESP_DEFAULT_BOOL_FORMAT 1 // on/off
|
||||
#endif
|
||||
|
||||
#ifndef EMSESP_DEFAULT_ENUM_FORMAT
|
||||
#define EMSESP_DEFAULT_ENUM_FORMAT 1 // Text
|
||||
#endif
|
||||
|
||||
#ifndef EMSESP_DEFAULT_ANALOG_ENABLED
|
||||
#define EMSESP_DEFAULT_ANALOG_ENABLED false
|
||||
#endif
|
||||
|
||||
@@ -697,7 +697,7 @@ bool EMSdevice::get_value_info(JsonObject & root, const char * cmd, const int8_t
|
||||
switch (dv.type) {
|
||||
case DeviceValueType::ENUM: {
|
||||
if (*(uint8_t *)(dv.value_p) < dv.options_size) {
|
||||
if (Mqtt::bool_format() == BOOL_FORMAT_10) {
|
||||
if (EMSESP::enum_format() == ENUM_FORMAT_NUMBER) {
|
||||
json[value] = (uint8_t)(*(uint8_t *)(dv.value_p));
|
||||
} else {
|
||||
json[value] = dv.options[*(uint8_t *)(dv.value_p)]; // text
|
||||
@@ -770,7 +770,16 @@ bool EMSdevice::get_value_info(JsonObject & root, const char * cmd, const int8_t
|
||||
|
||||
case DeviceValueType::BOOL: {
|
||||
if (Helpers::hasValue(*(uint8_t *)(dv.value_p), EMS_VALUE_BOOL)) {
|
||||
json[value] = (bool)(*(uint8_t *)(dv.value_p)) ? true : false;
|
||||
uint8_t bool_format = EMSESP::bool_format();
|
||||
if (bool_format == BOOL_FORMAT_ONOFF) {
|
||||
json[value] = (bool)(*(uint8_t *)(dv.value_p)) ? F_(on) : F_(off);
|
||||
} else if (bool_format == BOOL_FORMAT_ONOFF_CAP) {
|
||||
json[value] = (bool)(*(uint8_t *)(dv.value_p)) ? F_(ON) : F_(OFF);
|
||||
} else if (bool_format == BOOL_FORMAT_TRUEFALSE) {
|
||||
json[value] = (bool)(*(uint8_t *)(dv.value_p)) ? true : false;
|
||||
} else {
|
||||
json[value] = (bool)(*(uint8_t *)(dv.value_p)) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
json[type] = F("boolean");
|
||||
break;
|
||||
@@ -858,24 +867,17 @@ bool EMSdevice::generate_values_json(JsonObject & root, const uint8_t tag_filter
|
||||
if ((dv.type == DeviceValueType::BOOL) && Helpers::hasValue(*(uint8_t *)(dv.value_p), EMS_VALUE_BOOL)) {
|
||||
// see how to render the value depending on the setting
|
||||
// when in console mode we always use on and off
|
||||
if ((Mqtt::bool_format() == BOOL_FORMAT_ONOFF) || console) {
|
||||
// on or off as strings
|
||||
uint8_t bool_format = EMSESP::bool_format();
|
||||
if ((bool_format == BOOL_FORMAT_ONOFF) || console) {
|
||||
json[name] = *(uint8_t *)(dv.value_p) ? F_(on) : F_(off);
|
||||
has_value = true;
|
||||
} else if (Mqtt::bool_format() == BOOL_FORMAT_ONOFF_CAP) {
|
||||
// on or off as strings
|
||||
} else if (bool_format == BOOL_FORMAT_ONOFF_CAP) {
|
||||
json[name] = *(uint8_t *)(dv.value_p) ? F_(ON) : F_(OFF);
|
||||
has_value = true;
|
||||
} else if (Mqtt::bool_format() == BOOL_FORMAT_TRUEFALSE) {
|
||||
// true or false values (as real booleans, not strings)
|
||||
} else if (bool_format == BOOL_FORMAT_TRUEFALSE) {
|
||||
json[name] = (bool)(*(uint8_t *)(dv.value_p)) ? true : false;
|
||||
has_value = true;
|
||||
} else {
|
||||
// numerical 1 or 0
|
||||
json[name] = (uint8_t)(*(uint8_t *)(dv.value_p)) ? 1 : 0;
|
||||
has_value = true;
|
||||
}
|
||||
|
||||
has_value = true;
|
||||
}
|
||||
|
||||
// handle TEXT strings
|
||||
@@ -888,7 +890,7 @@ bool EMSdevice::generate_values_json(JsonObject & root, const uint8_t tag_filter
|
||||
else if ((dv.type == DeviceValueType::ENUM) && Helpers::hasValue(*(uint8_t *)(dv.value_p))) {
|
||||
if (*(uint8_t *)(dv.value_p) < dv.options_size) {
|
||||
// check for numeric enum-format, but "hamode" always as text
|
||||
if ((Mqtt::bool_format() == BOOL_FORMAT_10) && (dv.short_name != FL_(hamode)[0])) {
|
||||
if ((EMSESP::enum_format() == ENUM_FORMAT_NUMBER) && (dv.short_name != FL_(hamode)[0])) {
|
||||
json[name] = (uint8_t)(*(uint8_t *)(dv.value_p));
|
||||
} else {
|
||||
json[name] = dv.options[*(uint8_t *)(dv.value_p)];
|
||||
|
||||
@@ -75,6 +75,8 @@ uint8_t EMSESP::publish_all_idx_ = 0;
|
||||
uint8_t EMSESP::unique_id_count_ = 0;
|
||||
bool EMSESP::trace_raw_ = false;
|
||||
uint64_t EMSESP::tx_delay_ = 0;
|
||||
uint8_t EMSESP::bool_format_ = 1;
|
||||
uint8_t EMSESP::enum_format_ = 1;
|
||||
|
||||
// for a specific EMS device go and request data values
|
||||
// or if device_id is 0 it will fetch from all our known and active devices
|
||||
|
||||
18
src/emsesp.h
18
src/emsesp.h
@@ -144,6 +144,22 @@ class EMSESP {
|
||||
return (dallassensor_.dallas_enabled());
|
||||
}
|
||||
|
||||
static uint8_t bool_format() {
|
||||
return bool_format_;
|
||||
}
|
||||
|
||||
static void bool_format(uint8_t format) {
|
||||
bool_format_ = format;
|
||||
}
|
||||
|
||||
static uint8_t enum_format() {
|
||||
return enum_format_;
|
||||
}
|
||||
|
||||
static void enum_format(uint8_t format) {
|
||||
enum_format_ = format;
|
||||
}
|
||||
|
||||
enum Watch : uint8_t { WATCH_OFF, WATCH_ON, WATCH_RAW, WATCH_UNKNOWN };
|
||||
static void watch_id(uint16_t id);
|
||||
static uint16_t watch_id() {
|
||||
@@ -246,6 +262,8 @@ class EMSESP {
|
||||
static uint8_t unique_id_count_;
|
||||
static bool trace_raw_;
|
||||
static uint64_t tx_delay_;
|
||||
static uint8_t bool_format_;
|
||||
static uint8_t enum_format_;
|
||||
};
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
*/
|
||||
|
||||
#include "helpers.h"
|
||||
#include "mqtt.h"
|
||||
#include "emsesp.h"
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
@@ -124,7 +124,7 @@ char * Helpers::smallitoa(char * result, const uint16_t value) {
|
||||
|
||||
// work out how to display booleans
|
||||
char * Helpers::render_boolean(char * result, bool value) {
|
||||
uint8_t bool_format_ = Mqtt::bool_format();
|
||||
uint8_t bool_format_ = EMSESP::bool_format();
|
||||
if (bool_format_ == BOOL_FORMAT_ONOFF) {
|
||||
strlcpy(result, value ? uuid::read_flash_string(F_(on)).c_str() : uuid::read_flash_string(F_(off)).c_str(), 5);
|
||||
} else if (bool_format_ == BOOL_FORMAT_ONOFF_CAP) {
|
||||
|
||||
@@ -35,8 +35,6 @@ uint32_t Mqtt::publish_time_mixer_;
|
||||
uint32_t Mqtt::publish_time_sensor_;
|
||||
uint32_t Mqtt::publish_time_other_;
|
||||
bool Mqtt::mqtt_enabled_;
|
||||
uint8_t Mqtt::dallas_format_;
|
||||
uint8_t Mqtt::bool_format_;
|
||||
uint8_t Mqtt::ha_climate_format_;
|
||||
bool Mqtt::ha_enabled_;
|
||||
uint8_t Mqtt::nested_format_;
|
||||
@@ -485,8 +483,6 @@ void Mqtt::load_settings() {
|
||||
mqtt_enabled_ = mqttSettings.enabled;
|
||||
ha_enabled_ = mqttSettings.ha_enabled;
|
||||
ha_climate_format_ = mqttSettings.ha_climate_format;
|
||||
dallas_format_ = mqttSettings.dallas_format;
|
||||
bool_format_ = mqttSettings.bool_format;
|
||||
nested_format_ = mqttSettings.nested_format;
|
||||
subscribe_format_ = mqttSettings.subscribe_format;
|
||||
|
||||
|
||||
17
src/mqtt.h
17
src/mqtt.h
@@ -44,8 +44,6 @@ using uuid::console::Shell;
|
||||
// size of queue
|
||||
#define MAX_MQTT_MESSAGES 200
|
||||
|
||||
enum { BOOL_FORMAT_ONOFF = 1, BOOL_FORMAT_TRUEFALSE, BOOL_FORMAT_10, BOOL_FORMAT_ONOFF_CAP }; // matches Web UI settings
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
using mqtt_subfunction_p = std::function<bool(const char * message)>;
|
||||
@@ -83,7 +81,6 @@ class Mqtt {
|
||||
|
||||
enum Operation { PUBLISH, SUBSCRIBE };
|
||||
|
||||
enum Dallas_Format : uint8_t { SENSORID = 1, NUMBER };
|
||||
enum HA_Climate_Format : uint8_t { CURRENT = 1, SETPOINT, ZERO };
|
||||
|
||||
static constexpr uint8_t MQTT_TOPIC_MAX_SIZE = 128; // note this should really match the user setting in mqttSettings.maxTopicLength
|
||||
@@ -159,14 +156,6 @@ class Mqtt {
|
||||
return ha_climate_format_;
|
||||
}
|
||||
|
||||
static uint8_t dallas_format() {
|
||||
return dallas_format_;
|
||||
}
|
||||
|
||||
static uint8_t bool_format() {
|
||||
return bool_format_;
|
||||
}
|
||||
|
||||
static uint8_t nested_format() {
|
||||
return nested_format_;
|
||||
}
|
||||
@@ -183,10 +172,6 @@ class Mqtt {
|
||||
ha_climate_format_ = ha_climate_format;
|
||||
}
|
||||
|
||||
static void dallas_format(uint8_t dallas_format) {
|
||||
dallas_format_ = dallas_format;
|
||||
}
|
||||
|
||||
static void ha_enabled(bool ha_enabled) {
|
||||
ha_enabled_ = ha_enabled;
|
||||
}
|
||||
@@ -279,8 +264,6 @@ class Mqtt {
|
||||
static uint32_t publish_time_other_;
|
||||
static uint32_t publish_time_sensor_;
|
||||
static bool mqtt_enabled_;
|
||||
static uint8_t dallas_format_;
|
||||
static uint8_t bool_format_;
|
||||
static uint8_t ha_climate_format_;
|
||||
static bool ha_enabled_;
|
||||
static uint8_t nested_format_;
|
||||
|
||||
@@ -835,8 +835,6 @@ bool System::command_settings(const char * value, const int8_t id, JsonObject &
|
||||
node["publish_time_mixer"] = settings.publish_time_mixer;
|
||||
node["publish_time_other"] = settings.publish_time_other;
|
||||
node["publish_time_sensor"] = settings.publish_time_sensor;
|
||||
node["dallas_format"] = settings.dallas_format;
|
||||
node["bool_format"] = settings.bool_format;
|
||||
node["ha_climate_format"] = settings.ha_climate_format;
|
||||
node["ha_enabled"] = settings.ha_enabled;
|
||||
node["mqtt_qos"] = settings.mqtt_qos;
|
||||
@@ -879,6 +877,9 @@ bool System::command_settings(const char * value, const int8_t id, JsonObject &
|
||||
node["led_gpio"] = settings.led_gpio;
|
||||
node["hide_led"] = settings.hide_led;
|
||||
node["notoken_api"] = settings.notoken_api;
|
||||
node["dallas_format"] = settings.dallas_format;
|
||||
node["bool_format"] = settings.bool_format;
|
||||
node["enum_format"] = settings.enum_format;
|
||||
node["analog_enabled"] = settings.analog_enabled;
|
||||
node["pbutton_gpio"] = settings.pbutton_gpio;
|
||||
node["board_profile"] = settings.board_profile;
|
||||
|
||||
@@ -61,6 +61,9 @@ void WebSettings::read(WebSettings & settings, JsonObject & root) {
|
||||
root["pbutton_gpio"] = settings.pbutton_gpio;
|
||||
root["solar_maxflow"] = settings.solar_maxflow;
|
||||
root["board_profile"] = settings.board_profile;
|
||||
root["dallas_format"] = settings.dallas_format;
|
||||
root["bool_format"] = settings.bool_format;
|
||||
root["enum_format"] = settings.enum_format;
|
||||
}
|
||||
|
||||
// call on initialization and also when settings are updated via web or console
|
||||
@@ -173,6 +176,15 @@ StateUpdateResult WebSettings::update(JsonObject & root, WebSettings & settings)
|
||||
settings.notoken_api = root["notoken_api"] | EMSESP_DEFAULT_NOTOKEN_API;
|
||||
settings.solar_maxflow = root["solar_maxflow"] | EMSESP_DEFAULT_SOLAR_MAXFLOW;
|
||||
|
||||
settings.dallas_format = root["dallas_format"] | EMSESP_DEFAULT_DALLAS_FORMAT;
|
||||
EMSESP::dallassensor_.dallas_format(settings.dallas_format);
|
||||
|
||||
settings.bool_format = root["bool_format"] | EMSESP_DEFAULT_BOOL_FORMAT;
|
||||
EMSESP::bool_format(settings.bool_format);
|
||||
|
||||
settings.enum_format = root["enum_format"] | EMSESP_DEFAULT_ENUM_FORMAT;
|
||||
EMSESP::enum_format(settings.enum_format);
|
||||
|
||||
return StateUpdateResult::CHANGED;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,9 @@
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
enum { BOOL_FORMAT_ONOFF = 1, BOOL_FORMAT_ONOFF_CAP, BOOL_FORMAT_TRUEFALSE, BOOL_FORMAT_10 }; // matches Web UI settings
|
||||
enum { ENUM_FORMAT_TEXT = 1, ENUM_FORMAT_NUMBER }; // matches Web UI settings
|
||||
|
||||
class WebSettings {
|
||||
public:
|
||||
uint8_t tx_mode;
|
||||
@@ -56,6 +59,9 @@ class WebSettings {
|
||||
uint8_t pbutton_gpio;
|
||||
uint8_t solar_maxflow;
|
||||
String board_profile;
|
||||
uint8_t dallas_format;
|
||||
uint8_t bool_format;
|
||||
uint8_t enum_format;
|
||||
|
||||
static void read(WebSettings & settings, JsonObject & root);
|
||||
static StateUpdateResult update(JsonObject & root, WebSettings & settings);
|
||||
|
||||
Reference in New Issue
Block a user