mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
nested mqtt mode - (ESP32) Bring back MQTT single topics for Thermostat (and possible others) #738
This commit is contained in:
@@ -141,6 +141,16 @@ class MqttSettingsForm extends React.Component<MqttSettingsFormProps> {
|
||||
<Typography variant="h6" color="primary" >
|
||||
Formatting
|
||||
</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"
|
||||
label="Dallas Sensor Payload Grouping"
|
||||
value={data.dallas_format}
|
||||
|
||||
@@ -40,4 +40,5 @@ export interface MqttSettings {
|
||||
mqtt_retain: boolean;
|
||||
ha_enabled: boolean;
|
||||
ha_climate_format: number;
|
||||
nested_format: boolean;
|
||||
}
|
||||
|
||||
@@ -181,6 +181,7 @@ void MqttSettings::read(MqttSettings & settings, JsonObject & root) {
|
||||
root["bool_format"] = settings.bool_format;
|
||||
root["ha_climate_format"] = settings.ha_climate_format;
|
||||
root["ha_enabled"] = settings.ha_enabled;
|
||||
root["nested_format"] = settings.nested_format;
|
||||
}
|
||||
|
||||
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.ha_climate_format = root["ha_climate_format"] | EMSESP_DEFAULT_HA_CLIMATE_FORMAT;
|
||||
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) {
|
||||
emsesp::EMSESP::mqtt_.set_qos(newSettings.mqtt_qos);
|
||||
@@ -222,6 +224,10 @@ StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & setting
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (newSettings.nested_format != settings.nested_format) {
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (newSettings.ha_climate_format != settings.ha_climate_format) {
|
||||
emsesp::EMSESP::mqtt_.ha_climate_format(newSettings.ha_climate_format);
|
||||
changed = true;
|
||||
|
||||
@@ -68,6 +68,7 @@ static String generateClientId() {
|
||||
#define EMSESP_DEFAULT_MQTT_RETAIN false
|
||||
#define EMSESP_DEFAULT_HA_ENABLED false
|
||||
#define EMSESP_DEFAULT_PUBLISH_TIME 10
|
||||
#define EMSESP_DEFAULT_NESTED_FORMAT true
|
||||
|
||||
class MqttSettings {
|
||||
public:
|
||||
@@ -102,6 +103,7 @@ class MqttSettings {
|
||||
uint8_t bool_format;
|
||||
uint8_t ha_climate_format;
|
||||
bool ha_enabled;
|
||||
bool nested_format;
|
||||
|
||||
static void read(MqttSettings & settings, JsonObject & root);
|
||||
static StateUpdateResult update(JsonObject & root, MqttSettings & settings);
|
||||
|
||||
@@ -32,6 +32,7 @@ class DummySettings {
|
||||
bool mqtt_retain = false;
|
||||
bool enabled = true;
|
||||
uint8_t dallas_format = 1;
|
||||
bool nested_format = true;
|
||||
uint8_t ha_climate_format = 1;
|
||||
bool ha_enabled = true;
|
||||
std::string base = "ems-esp";
|
||||
|
||||
19
src/mqtt.cpp
19
src/mqtt.cpp
@@ -39,6 +39,7 @@ uint8_t Mqtt::dallas_format_;
|
||||
uint8_t Mqtt::bool_format_;
|
||||
uint8_t Mqtt::ha_climate_format_;
|
||||
bool Mqtt::ha_enabled_;
|
||||
bool Mqtt::nested_format_;
|
||||
|
||||
std::deque<Mqtt::QueuedMqttMessage> Mqtt::mqtt_messages_;
|
||||
std::vector<Mqtt::MQTTSubFunction> Mqtt::mqtt_subfunctions_;
|
||||
@@ -512,6 +513,7 @@ void Mqtt::on_connect() {
|
||||
ha_climate_format_ = mqttSettings.ha_climate_format;
|
||||
dallas_format_ = mqttSettings.dallas_format;
|
||||
bool_format_ = mqttSettings.bool_format;
|
||||
nested_format_ = mqttSettings.nested_format;
|
||||
});
|
||||
|
||||
// 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
|
||||
|
||||
// how to render boolean
|
||||
if (bool_format_ == BOOL_FORMAT_ONOFF) {
|
||||
doc[F("payload_on")] = FJSON("on");
|
||||
doc[F("payload_off")] = FJSON("off");
|
||||
} else if (bool_format_ == BOOL_FORMAT_ONOFF_CAP) {
|
||||
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;
|
||||
}
|
||||
// HA only accepts String values
|
||||
char result[10];
|
||||
doc[F("payload_on")] = Helpers::render_boolean(result, true);
|
||||
doc[F("payload_off")] = Helpers::render_boolean(result, false);
|
||||
} else {
|
||||
// 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
|
||||
|
||||
14
src/mqtt.h
14
src/mqtt.h
@@ -156,6 +156,10 @@ class Mqtt {
|
||||
return bool_format_;
|
||||
}
|
||||
|
||||
static bool nested_format() {
|
||||
return nested_format_;
|
||||
}
|
||||
|
||||
static bool ha_enabled() {
|
||||
return ha_enabled_;
|
||||
}
|
||||
@@ -184,15 +188,6 @@ class Mqtt {
|
||||
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 {
|
||||
const uint16_t id_;
|
||||
const std::shared_ptr<const MqttMessage> content_;
|
||||
@@ -270,6 +265,7 @@ class Mqtt {
|
||||
static uint8_t bool_format_;
|
||||
static uint8_t ha_climate_format_;
|
||||
static bool ha_enabled_;
|
||||
static bool nested_format_;
|
||||
};
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
Reference in New Issue
Block a user