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" >
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}

View File

@@ -40,4 +40,5 @@ export interface MqttSettings {
mqtt_retain: boolean;
ha_enabled: boolean;
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["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;

View File

@@ -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);

View File

@@ -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";

View File

@@ -149,7 +149,7 @@ void Mixer::process_IPMStatusMessage(std::shared_ptr<const Telegram> telegram) {
// do we have a mixed circuit
if (ismixed == 2) {
has_update(telegram->read_value(flowTempHc_, 3)); // is * 10
has_update(telegram->read_value(status_, 2)); // valve status
has_update(telegram->read_value(status_, 2)); // valve status
}
has_update(telegram->read_bitvalue(pumpStatus_, 1, 0)); // pump is also in unmixed circuits
@@ -164,7 +164,7 @@ void Mixer::process_MMStatusMessage(std::shared_ptr<const Telegram> telegram) {
// 0x21 is position 2. 0x20 is typically reserved for the WM10 switch module
// see https://github.com/proddy/EMS-ESP/issues/270 and https://github.com/proddy/EMS-ESP/issues/386#issuecomment-629610918
has_update(telegram->read_value(flowTempHc_, 1)); // is * 10
has_update(telegram->read_value(flowTempHc_, 1)); // is * 10
has_update(telegram->read_bitvalue(pumpStatus_, 3, 2)); // is 0 or 0x64 (100%), check only bit 2
has_update(telegram->read_value(flowSetTemp_, 0));
has_update(telegram->read_value(status_, 4)); // valve status -100 to 100

View File

@@ -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

View File

@@ -156,6 +156,10 @@ class Mqtt {
return bool_format_;
}
static bool nested_format() {
return nested_format_;
}
static bool ha_enabled() {
return ha_enabled_;
}
@@ -181,18 +185,9 @@ class Mqtt {
}
static bool is_empty() {
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 {
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