Skip to content

Commit b4aed24

Browse files
authored
Merge pull request #44 from MichaelDvP/ft_webcallcmd
mqtt-HA-config dynamically
2 parents b77d9d4 + 015ab64 commit b4aed24

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

src/emsdevice.cpp

+25-11
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ void EMSdevice::register_device_value(uint8_t tag,
455455
};
456456
}
457457

458-
devicevalues_.emplace_back(device_type_, tag, value_p, type, options, options_size, short_name, full_name, uom, has_cmd);
458+
devicevalues_.emplace_back(device_type_, tag, value_p, type, options, options_size, short_name, full_name, uom, 0, has_cmd);
459459
}
460460

461461
void EMSdevice::register_device_value(uint8_t tag, void * value_p, uint8_t type, const __FlashStringHelper * const * options, const __FlashStringHelper * const * name, uint8_t uom, cmdfunction_p f) {
@@ -617,11 +617,12 @@ bool EMSdevice::generate_values_json_web(JsonObject & json) {
617617
// return false if empty
618618
// this is used to create both the MQTT payloads and Console messages (console = true)
619619
bool EMSdevice::generate_values_json(JsonObject & root, const uint8_t tag_filter, const bool nested, const bool console) {
620-
bool has_value = false; // to see if we've added a value. it's faster than doing a json.size() at the end
621-
uint8_t old_tag = 255; // NAN
622-
JsonObject json = root;
620+
bool has_values = false; // to see if we've added a value. it's faster than doing a json.size() at the end
621+
uint8_t old_tag = 255; // NAN
622+
JsonObject json = root;
623623

624-
for (const auto & dv : devicevalues_) {
624+
for (auto & dv : devicevalues_) {
625+
bool has_value = false;
625626
// only show if tag is either empty (TAG_NONE) or matches a value
626627
// and don't show if full_name is empty unless we're outputing for mqtt payloads
627628
// for nested we use all values
@@ -753,20 +754,33 @@ bool EMSdevice::generate_values_json(JsonObject & root, const uint8_t tag_filter
753754
}
754755
}
755756
}
757+
dv.ha |= has_value ? DeviceValueHA::HA_VALUE : DeviceValueHA::HA_NONE;
758+
has_values |= has_value;
756759
}
757760

758-
return has_value;
761+
return has_values;
759762
}
760763

761764
// create the Home Assistant configs for each value
762-
// this is called when an MQTT publish is done via an EMS Device, and only done once
765+
// this is called when an MQTT publish is done via an EMS Device
763766
void EMSdevice::publish_mqtt_ha_sensor() {
764-
for (const auto & dv : devicevalues_) {
765-
Mqtt::publish_mqtt_ha_sensor(dv.type, dv.tag, dv.full_name, device_type_, dv.short_name, dv.uom);
767+
for (auto & dv : devicevalues_) {
768+
if (dv.ha == DeviceValueHA::HA_VALUE) {
769+
Mqtt::publish_mqtt_ha_sensor(dv.type, dv.tag, dv.full_name, device_type_, dv.short_name, dv.uom);
770+
dv.ha |= DeviceValueHA::HA_DONE;
771+
}
766772
}
773+
if (!ha_config_done()) {
774+
bool ok = publish_ha_config();
775+
ha_config_done(ok); // see if it worked
776+
}
777+
}
767778

768-
bool ok = publish_ha_config();
769-
ha_config_done(ok); // see if it worked
779+
void EMSdevice::ha_config_clear() {
780+
for (auto & dv : devicevalues_) {
781+
dv.ha &= ~DeviceValueHA::HA_DONE;
782+
}
783+
ha_config_done(false);
770784
}
771785

772786
// return the name of the telegram type

src/emsdevice.h

+8
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ enum DeviceValueTAG : uint8_t {
141141
// mqtt flags for command subscriptions
142142
enum MqttSubFlag : uint8_t { FLAG_NORMAL = 0, FLAG_HC, FLAG_WWC, FLAG_NOSUB };
143143

144+
// mqtt-HA flags
145+
enum DeviceValueHA : uint8_t { HA_NONE = 0, HA_VALUE, HA_DONE};
146+
144147
class EMSdevice {
145148
public:
146149
virtual ~EMSdevice() = default; // destructor of base class must always be virtual because it's a polymorphic class
@@ -297,6 +300,8 @@ class EMSdevice {
297300
ha_config_done_ = v;
298301
}
299302

303+
void ha_config_clear();
304+
300305
enum Brand : uint8_t {
301306
NO_BRAND = 0, // 0
302307
BOSCH, // 1
@@ -407,6 +412,7 @@ class EMSdevice {
407412
const __FlashStringHelper * short_name; // used in MQTT
408413
const __FlashStringHelper * full_name; // used in Web and Console
409414
uint8_t uom; // DeviceValueUOM::*
415+
uint8_t ha; // DevcieValueHA::
410416
bool has_cmd; // true if there is a Console/MQTT command which matches the short_name
411417

412418
DeviceValue(uint8_t device_type,
@@ -418,6 +424,7 @@ class EMSdevice {
418424
const __FlashStringHelper * short_name,
419425
const __FlashStringHelper * full_name,
420426
uint8_t uom,
427+
uint8_t ha,
421428
bool has_cmd)
422429
: device_type(device_type)
423430
, tag(tag)
@@ -428,6 +435,7 @@ class EMSdevice {
428435
, short_name(short_name)
429436
, full_name(full_name)
430437
, uom(uom)
438+
, ha(ha)
431439
, has_cmd(has_cmd) {
432440
}
433441
};

src/emsesp.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ void EMSESP::reset_mqtt_ha() {
423423
}
424424

425425
for (const auto & emsdevice : emsdevices) {
426-
emsdevice->ha_config_done(false);
426+
emsdevice->ha_config_clear();
427427
}
428428
dallassensor_.reload();
429429
}
@@ -440,8 +440,8 @@ void EMSESP::publish_device_values(uint8_t device_type) {
440440
// group by device type
441441
for (const auto & emsdevice : emsdevices) {
442442
if (emsdevice && (emsdevice->device_type() == device_type)) {
443-
// if we're using HA and it's not already done, send the config topics first. only do this once
444-
if (Mqtt::ha_enabled() && (!emsdevice->ha_config_done())) {
443+
// if we're using HA, if done is checked for each sensor in devices
444+
if (Mqtt::ha_enabled()) {
445445
emsdevice->publish_mqtt_ha_sensor(); // create the configs for each value as a sensor
446446
}
447447

0 commit comments

Comments
 (0)