Skip to content

Commit 4cac160

Browse files
committed
mqtt-HA-config dynamically
1 parent b77d9d4 commit 4cac160

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

src/emsdevice.cpp

+17-12
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,24 @@ 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
}
767-
768-
bool ok = publish_ha_config();
769-
ha_config_done(ok); // see if it worked
773+
// bool ok = publish_ha_config();
774+
// ha_config_done(ok); // see if it worked
770775
}
771776

772777
// return the name of the telegram type

src/emsdevice.h

+6
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
@@ -407,6 +410,7 @@ class EMSdevice {
407410
const __FlashStringHelper * short_name; // used in MQTT
408411
const __FlashStringHelper * full_name; // used in Web and Console
409412
uint8_t uom; // DeviceValueUOM::*
413+
uint8_t ha; // DevcieValueHA::
410414
bool has_cmd; // true if there is a Console/MQTT command which matches the short_name
411415

412416
DeviceValue(uint8_t device_type,
@@ -418,6 +422,7 @@ class EMSdevice {
418422
const __FlashStringHelper * short_name,
419423
const __FlashStringHelper * full_name,
420424
uint8_t uom,
425+
uint8_t ha,
421426
bool has_cmd)
422427
: device_type(device_type)
423428
, tag(tag)
@@ -428,6 +433,7 @@ class EMSdevice {
428433
, short_name(short_name)
429434
, full_name(full_name)
430435
, uom(uom)
436+
, ha(ha)
431437
, has_cmd(has_cmd) {
432438
}
433439
};

src/emsesp.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -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)