From 4cac16093f715abd6d4d72251b34bbfd15dbbee0 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sat, 10 Apr 2021 12:19:51 +0200 Subject: [PATCH 1/2] mqtt-HA-config dynamically --- src/emsdevice.cpp | 29 +++++++++++++++++------------ src/emsdevice.h | 6 ++++++ src/emsesp.cpp | 4 ++-- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index d52225bd2..bbdb776fb 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -455,7 +455,7 @@ void EMSdevice::register_device_value(uint8_t tag, }; } - devicevalues_.emplace_back(device_type_, tag, value_p, type, options, options_size, short_name, full_name, uom, has_cmd); + devicevalues_.emplace_back(device_type_, tag, value_p, type, options, options_size, short_name, full_name, uom, 0, has_cmd); } 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) { // return false if empty // this is used to create both the MQTT payloads and Console messages (console = true) bool EMSdevice::generate_values_json(JsonObject & root, const uint8_t tag_filter, const bool nested, const bool console) { - bool has_value = false; // to see if we've added a value. it's faster than doing a json.size() at the end - uint8_t old_tag = 255; // NAN - JsonObject json = root; + bool has_values = false; // to see if we've added a value. it's faster than doing a json.size() at the end + uint8_t old_tag = 255; // NAN + JsonObject json = root; - for (const auto & dv : devicevalues_) { + for (auto & dv : devicevalues_) { + bool has_value = false; // only show if tag is either empty (TAG_NONE) or matches a value // and don't show if full_name is empty unless we're outputing for mqtt payloads // for nested we use all values @@ -753,20 +754,24 @@ bool EMSdevice::generate_values_json(JsonObject & root, const uint8_t tag_filter } } } + dv.ha |= has_value ? DeviceValueHA::HA_VALUE : DeviceValueHA::HA_NONE; + has_values |= has_value; } - return has_value; + return has_values; } // create the Home Assistant configs for each value -// this is called when an MQTT publish is done via an EMS Device, and only done once +// this is called when an MQTT publish is done via an EMS Device void EMSdevice::publish_mqtt_ha_sensor() { - for (const auto & dv : devicevalues_) { - Mqtt::publish_mqtt_ha_sensor(dv.type, dv.tag, dv.full_name, device_type_, dv.short_name, dv.uom); + for (auto & dv : devicevalues_) { + if (dv.ha == DeviceValueHA::HA_VALUE) { + Mqtt::publish_mqtt_ha_sensor(dv.type, dv.tag, dv.full_name, device_type_, dv.short_name, dv.uom); + dv.ha |= DeviceValueHA::HA_DONE; + } } - - bool ok = publish_ha_config(); - ha_config_done(ok); // see if it worked + // bool ok = publish_ha_config(); + // ha_config_done(ok); // see if it worked } // return the name of the telegram type diff --git a/src/emsdevice.h b/src/emsdevice.h index e226eb55a..70479f8e8 100644 --- a/src/emsdevice.h +++ b/src/emsdevice.h @@ -141,6 +141,9 @@ enum DeviceValueTAG : uint8_t { // mqtt flags for command subscriptions enum MqttSubFlag : uint8_t { FLAG_NORMAL = 0, FLAG_HC, FLAG_WWC, FLAG_NOSUB }; +// mqtt-HA flags +enum DeviceValueHA : uint8_t { HA_NONE = 0, HA_VALUE, HA_DONE}; + class EMSdevice { public: virtual ~EMSdevice() = default; // destructor of base class must always be virtual because it's a polymorphic class @@ -407,6 +410,7 @@ class EMSdevice { const __FlashStringHelper * short_name; // used in MQTT const __FlashStringHelper * full_name; // used in Web and Console uint8_t uom; // DeviceValueUOM::* + uint8_t ha; // DevcieValueHA:: bool has_cmd; // true if there is a Console/MQTT command which matches the short_name DeviceValue(uint8_t device_type, @@ -418,6 +422,7 @@ class EMSdevice { const __FlashStringHelper * short_name, const __FlashStringHelper * full_name, uint8_t uom, + uint8_t ha, bool has_cmd) : device_type(device_type) , tag(tag) @@ -428,6 +433,7 @@ class EMSdevice { , short_name(short_name) , full_name(full_name) , uom(uom) + , ha(ha) , has_cmd(has_cmd) { } }; diff --git a/src/emsesp.cpp b/src/emsesp.cpp index aa9fdfb68..6d966bcd1 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -440,8 +440,8 @@ void EMSESP::publish_device_values(uint8_t device_type) { // group by device type for (const auto & emsdevice : emsdevices) { if (emsdevice && (emsdevice->device_type() == device_type)) { - // if we're using HA and it's not already done, send the config topics first. only do this once - if (Mqtt::ha_enabled() && (!emsdevice->ha_config_done())) { + // if we're using HA, if done is checked for each sensor in devices + if (Mqtt::ha_enabled()) { emsdevice->publish_mqtt_ha_sensor(); // create the configs for each value as a sensor } From 015ab649af75e2bb2b59c9ebe984ca1638ccd3c0 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sat, 10 Apr 2021 13:32:09 +0200 Subject: [PATCH 2/2] fix publish_ha_config, and add clear --- src/emsdevice.cpp | 13 +++++++++++-- src/emsdevice.h | 2 ++ src/emsesp.cpp | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index bbdb776fb..28f2ab0cc 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -770,8 +770,17 @@ void EMSdevice::publish_mqtt_ha_sensor() { dv.ha |= DeviceValueHA::HA_DONE; } } - // bool ok = publish_ha_config(); - // ha_config_done(ok); // see if it worked + if (!ha_config_done()) { + bool ok = publish_ha_config(); + ha_config_done(ok); // see if it worked + } +} + +void EMSdevice::ha_config_clear() { + for (auto & dv : devicevalues_) { + dv.ha &= ~DeviceValueHA::HA_DONE; + } + ha_config_done(false); } // return the name of the telegram type diff --git a/src/emsdevice.h b/src/emsdevice.h index 70479f8e8..e77d4e079 100644 --- a/src/emsdevice.h +++ b/src/emsdevice.h @@ -300,6 +300,8 @@ class EMSdevice { ha_config_done_ = v; } + void ha_config_clear(); + enum Brand : uint8_t { NO_BRAND = 0, // 0 BOSCH, // 1 diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 6d966bcd1..0f31a6751 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -423,7 +423,7 @@ void EMSESP::reset_mqtt_ha() { } for (const auto & emsdevice : emsdevices) { - emsdevice->ha_config_done(false); + emsdevice->ha_config_clear(); } dallassensor_.reload(); }