mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
Merge pull request #44 from MichaelDvP/ft_webcallcmd
mqtt-HA-config dynamically
This commit is contained in:
@@ -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
|
||||
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,33 @@ 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_) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
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
|
||||
|
||||
@@ -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
|
||||
@@ -297,6 +300,8 @@ class EMSdevice {
|
||||
ha_config_done_ = v;
|
||||
}
|
||||
|
||||
void ha_config_clear();
|
||||
|
||||
enum Brand : uint8_t {
|
||||
NO_BRAND = 0, // 0
|
||||
BOSCH, // 1
|
||||
@@ -407,6 +412,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 +424,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 +435,7 @@ class EMSdevice {
|
||||
, short_name(short_name)
|
||||
, full_name(full_name)
|
||||
, uom(uom)
|
||||
, ha(ha)
|
||||
, has_cmd(has_cmd) {
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user