nested mqtt mode - (ESP32) Bring back MQTT single topics for Thermostat (and possible others) #738

This commit is contained in:
proddy
2021-03-09 23:44:48 +01:00
parent 39336eecd6
commit 438c2ba223
8 changed files with 34 additions and 25 deletions

View File

@@ -141,6 +141,16 @@ class MqttSettingsForm extends React.Component<MqttSettingsFormProps> {
<Typography variant="h6" color="primary" > <Typography variant="h6" color="primary" >
Formatting Formatting
</Typography> </Typography>
<BlockFormControlLabel
control={
<Checkbox
checked={data.nested_format}
onChange={handleValueChange('nested_format')}
value="nested_format"
/>
}
label="Nested format (Thermostat & Mixer only)"
/>
<SelectValidator name="dallas_format" <SelectValidator name="dallas_format"
label="Dallas Sensor Payload Grouping" label="Dallas Sensor Payload Grouping"
value={data.dallas_format} value={data.dallas_format}

View File

@@ -40,4 +40,5 @@ export interface MqttSettings {
mqtt_retain: boolean; mqtt_retain: boolean;
ha_enabled: boolean; ha_enabled: boolean;
ha_climate_format: number; ha_climate_format: number;
nested_format: boolean;
} }

View File

@@ -181,6 +181,7 @@ void MqttSettings::read(MqttSettings & settings, JsonObject & root) {
root["bool_format"] = settings.bool_format; root["bool_format"] = settings.bool_format;
root["ha_climate_format"] = settings.ha_climate_format; root["ha_climate_format"] = settings.ha_climate_format;
root["ha_enabled"] = settings.ha_enabled; root["ha_enabled"] = settings.ha_enabled;
root["nested_format"] = settings.nested_format;
} }
StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & settings) { StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & settings) {
@@ -211,6 +212,7 @@ StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & setting
newSettings.bool_format = root["bool_format"] | EMSESP_DEFAULT_BOOL_FORMAT; newSettings.bool_format = root["bool_format"] | EMSESP_DEFAULT_BOOL_FORMAT;
newSettings.ha_climate_format = root["ha_climate_format"] | EMSESP_DEFAULT_HA_CLIMATE_FORMAT; newSettings.ha_climate_format = root["ha_climate_format"] | EMSESP_DEFAULT_HA_CLIMATE_FORMAT;
newSettings.ha_enabled = root["ha_enabled"] | EMSESP_DEFAULT_HA_ENABLED; newSettings.ha_enabled = root["ha_enabled"] | EMSESP_DEFAULT_HA_ENABLED;
newSettings.nested_format = root["nested_format"] | EMSESP_DEFAULT_NESTED_FORMAT;
if (newSettings.mqtt_qos != settings.mqtt_qos) { if (newSettings.mqtt_qos != settings.mqtt_qos) {
emsesp::EMSESP::mqtt_.set_qos(newSettings.mqtt_qos); emsesp::EMSESP::mqtt_.set_qos(newSettings.mqtt_qos);
@@ -222,6 +224,10 @@ StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & setting
changed = true; changed = true;
} }
if (newSettings.nested_format != settings.nested_format) {
changed = true;
}
if (newSettings.ha_climate_format != settings.ha_climate_format) { if (newSettings.ha_climate_format != settings.ha_climate_format) {
emsesp::EMSESP::mqtt_.ha_climate_format(newSettings.ha_climate_format); emsesp::EMSESP::mqtt_.ha_climate_format(newSettings.ha_climate_format);
changed = true; changed = true;

View File

@@ -68,6 +68,7 @@ static String generateClientId() {
#define EMSESP_DEFAULT_MQTT_RETAIN false #define EMSESP_DEFAULT_MQTT_RETAIN false
#define EMSESP_DEFAULT_HA_ENABLED false #define EMSESP_DEFAULT_HA_ENABLED false
#define EMSESP_DEFAULT_PUBLISH_TIME 10 #define EMSESP_DEFAULT_PUBLISH_TIME 10
#define EMSESP_DEFAULT_NESTED_FORMAT true
class MqttSettings { class MqttSettings {
public: public:
@@ -102,6 +103,7 @@ class MqttSettings {
uint8_t bool_format; uint8_t bool_format;
uint8_t ha_climate_format; uint8_t ha_climate_format;
bool ha_enabled; bool ha_enabled;
bool nested_format;
static void read(MqttSettings & settings, JsonObject & root); static void read(MqttSettings & settings, JsonObject & root);
static StateUpdateResult update(JsonObject & root, MqttSettings & settings); static StateUpdateResult update(JsonObject & root, MqttSettings & settings);

View File

@@ -32,6 +32,7 @@ class DummySettings {
bool mqtt_retain = false; bool mqtt_retain = false;
bool enabled = true; bool enabled = true;
uint8_t dallas_format = 1; uint8_t dallas_format = 1;
bool nested_format = true;
uint8_t ha_climate_format = 1; uint8_t ha_climate_format = 1;
bool ha_enabled = true; bool ha_enabled = true;
std::string base = "ems-esp"; std::string base = "ems-esp";

View File

@@ -39,6 +39,7 @@ uint8_t Mqtt::dallas_format_;
uint8_t Mqtt::bool_format_; uint8_t Mqtt::bool_format_;
uint8_t Mqtt::ha_climate_format_; uint8_t Mqtt::ha_climate_format_;
bool Mqtt::ha_enabled_; bool Mqtt::ha_enabled_;
bool Mqtt::nested_format_;
std::deque<Mqtt::QueuedMqttMessage> Mqtt::mqtt_messages_; std::deque<Mqtt::QueuedMqttMessage> Mqtt::mqtt_messages_;
std::vector<Mqtt::MQTTSubFunction> Mqtt::mqtt_subfunctions_; std::vector<Mqtt::MQTTSubFunction> Mqtt::mqtt_subfunctions_;
@@ -512,6 +513,7 @@ void Mqtt::on_connect() {
ha_climate_format_ = mqttSettings.ha_climate_format; ha_climate_format_ = mqttSettings.ha_climate_format;
dallas_format_ = mqttSettings.dallas_format; dallas_format_ = mqttSettings.dallas_format;
bool_format_ = mqttSettings.bool_format; bool_format_ = mqttSettings.bool_format;
nested_format_ = mqttSettings.nested_format;
}); });
// send info topic appended with the version information as JSON // send info topic appended with the version information as JSON
@@ -862,19 +864,10 @@ void Mqtt::publish_mqtt_ha_sensor(uint8_t type, // EMSdevice
snprintf_P(topic, sizeof(topic), PSTR("homeassistant/binary_sensor/%s/%s/config"), mqtt_base_.c_str(), uniq.c_str()); // topic snprintf_P(topic, sizeof(topic), PSTR("homeassistant/binary_sensor/%s/%s/config"), mqtt_base_.c_str(), uniq.c_str()); // topic
// how to render boolean // how to render boolean
if (bool_format_ == BOOL_FORMAT_ONOFF) { // HA only accepts String values
doc[F("payload_on")] = FJSON("on"); char result[10];
doc[F("payload_off")] = FJSON("off"); doc[F("payload_on")] = Helpers::render_boolean(result, true);
} else if (bool_format_ == BOOL_FORMAT_ONOFF_CAP) { doc[F("payload_off")] = Helpers::render_boolean(result, false);
doc[F("payload_on")] = FJSON("ON");
doc[F("payload_off")] = FJSON("OFF");
} else if (bool_format_ == BOOL_FORMAT_TRUEFALSE) {
doc[F("payload_on")] = true;
doc[F("payload_off")] = false;
} else {
doc[F("payload_on")] = 1;
doc[F("payload_off")] = 0;
}
} else { } else {
// normal HA sensor, not a boolean one // normal HA sensor, not a boolean one
snprintf_P(topic, sizeof(topic), PSTR("homeassistant/sensor/%s/%s/config"), mqtt_base_.c_str(), uniq.c_str()); // topic snprintf_P(topic, sizeof(topic), PSTR("homeassistant/sensor/%s/%s/config"), mqtt_base_.c_str(), uniq.c_str()); // topic

View File

@@ -156,6 +156,10 @@ class Mqtt {
return bool_format_; return bool_format_;
} }
static bool nested_format() {
return nested_format_;
}
static bool ha_enabled() { static bool ha_enabled() {
return ha_enabled_; return ha_enabled_;
} }
@@ -184,15 +188,6 @@ class Mqtt {
return mqtt_messages_.empty(); return mqtt_messages_.empty();
} }
/*
struct QueuedMqttMessage {
uint16_t id_;
std::shared_ptr<const MqttMessage> content_;
uint8_t retry_count_;
uint16_t packet_id_;
};
*/
struct QueuedMqttMessage { struct QueuedMqttMessage {
const uint16_t id_; const uint16_t id_;
const std::shared_ptr<const MqttMessage> content_; const std::shared_ptr<const MqttMessage> content_;
@@ -270,6 +265,7 @@ class Mqtt {
static uint8_t bool_format_; static uint8_t bool_format_;
static uint8_t ha_climate_format_; static uint8_t ha_climate_format_;
static bool ha_enabled_; static bool ha_enabled_;
static bool nested_format_;
}; };
} // namespace emsesp } // namespace emsesp