From 00fb213cc12f5e70b730db610df178475cce1209 Mon Sep 17 00:00:00 2001 From: Jaakko Haakana Date: Sat, 5 Jul 2025 10:49:18 +0300 Subject: [PATCH] HA autodiscovery enabled with run-time variable --- Software/src/devboard/mqtt/mqtt.cpp | 127 ++++++++++++++-------------- Software/src/devboard/mqtt/mqtt.h | 1 + 2 files changed, 65 insertions(+), 63 deletions(-) diff --git a/Software/src/devboard/mqtt/mqtt.cpp b/Software/src/devboard/mqtt/mqtt.cpp index 896ba57f..7a241a93 100644 --- a/Software/src/devboard/mqtt/mqtt.cpp +++ b/Software/src/devboard/mqtt/mqtt.cpp @@ -21,6 +21,14 @@ const bool mqtt_enabled_default = false; bool mqtt_enabled = mqtt_enabled_default; +#ifdef HA_AUTODISCOVERY +const bool ha_autodiscovery_enabled_default = true; +#else +const bool ha_autodiscovery_enabled_default = false; +#endif + +bool ha_autodiscovery_enabled = ha_autodiscovery_enabled_default; + esp_mqtt_client_config_t mqtt_cfg; esp_mqtt_client_handle_t client; char mqtt_msg[MQTT_MSG_BUFFER_SIZE]; @@ -60,7 +68,6 @@ static void publish_values(void) { #endif } -#ifdef HA_AUTODISCOVERY static bool ha_common_info_published = false; static bool ha_cell_voltages_published = false; static bool ha_events_published = false; @@ -176,7 +183,6 @@ void set_battery_voltage_attributes(JsonDocument& doc, int i, int cellNumber, co doc["unit_of_measurement"] = "V"; doc["value_template"] = "{{ value_json.cell_voltages[" + String(i) + "] }}"; } -#endif // HA_AUTODISCOVERY static String generateButtonTopic(const char* subtype) { return topic_name + "/command/" + String(subtype); @@ -219,8 +225,10 @@ static std::vector order_events; static bool publish_common_info(void) { static JsonDocument doc; static String state_topic = topic_name + "/info"; -#ifdef HA_AUTODISCOVERY - if (ha_common_info_published == false) { + + // if(ha_autodiscovery_enabled) { + + if (ha_autodiscovery_enabled && !ha_common_info_published) { for (auto& config : sensorConfigs) { if (!config.condition(battery)) { continue; @@ -249,7 +257,6 @@ static bool publish_common_info(void) { } } else { -#endif // HA_AUTODISCOVERY doc["bms_status"] = getBMSStatus(datalayer.battery.status.bms_status); doc["pause_status"] = get_emulator_pause_status(); @@ -272,9 +279,7 @@ static bool publish_common_info(void) { return false; } doc.clear(); -#ifdef HA_AUTODISCOVERY } -#endif // HA_AUTODISCOVERY return true; } @@ -283,51 +288,51 @@ static bool publish_cell_voltages(void) { static String state_topic = topic_name + "/spec_data"; static String state_topic_2 = topic_name + "/spec_data_2"; -#ifdef HA_AUTODISCOVERY - bool failed_to_publish = false; - if (ha_cell_voltages_published == false) { + if (ha_autodiscovery_enabled) { + bool failed_to_publish = false; + if (ha_cell_voltages_published == false) { - // If the cell voltage number isn't initialized... - if (datalayer.battery.info.number_of_cells != 0u) { - - for (int i = 0; i < datalayer.battery.info.number_of_cells; i++) { - int cellNumber = i + 1; - set_battery_voltage_attributes(doc, i, cellNumber, state_topic, object_id_prefix, ""); - set_common_discovery_attributes(doc); - - serializeJson(doc, mqtt_msg, sizeof(mqtt_msg)); - if (mqtt_publish(generateCellVoltageAutoConfigTopic(cellNumber, "").c_str(), mqtt_msg, true) == false) { - failed_to_publish = true; - return false; - } - } - doc.clear(); // clear after sending autoconfig - } - - if (battery2) { - // TODO: Combine this identical block with the previous one. // If the cell voltage number isn't initialized... - if (datalayer.battery2.info.number_of_cells != 0u) { + if (datalayer.battery.info.number_of_cells != 0u) { for (int i = 0; i < datalayer.battery.info.number_of_cells; i++) { int cellNumber = i + 1; - set_battery_voltage_attributes(doc, i, cellNumber, state_topic_2, object_id_prefix + "2_", " 2"); + set_battery_voltage_attributes(doc, i, cellNumber, state_topic, object_id_prefix, ""); set_common_discovery_attributes(doc); serializeJson(doc, mqtt_msg, sizeof(mqtt_msg)); - if (mqtt_publish(generateCellVoltageAutoConfigTopic(cellNumber, "_2_").c_str(), mqtt_msg, true) == false) { + if (mqtt_publish(generateCellVoltageAutoConfigTopic(cellNumber, "").c_str(), mqtt_msg, true) == false) { failed_to_publish = true; return false; } } doc.clear(); // clear after sending autoconfig } + + if (battery2) { + // TODO: Combine this identical block with the previous one. + // If the cell voltage number isn't initialized... + if (datalayer.battery2.info.number_of_cells != 0u) { + + for (int i = 0; i < datalayer.battery.info.number_of_cells; i++) { + int cellNumber = i + 1; + set_battery_voltage_attributes(doc, i, cellNumber, state_topic_2, object_id_prefix + "2_", " 2"); + set_common_discovery_attributes(doc); + + serializeJson(doc, mqtt_msg, sizeof(mqtt_msg)); + if (mqtt_publish(generateCellVoltageAutoConfigTopic(cellNumber, "_2_").c_str(), mqtt_msg, true) == false) { + failed_to_publish = true; + return false; + } + } + doc.clear(); // clear after sending autoconfig + } + } + } + if (failed_to_publish == false) { + ha_cell_voltages_published = true; } } - if (failed_to_publish == false) { - ha_cell_voltages_published = true; - } -#endif // HA_AUTODISCOVERY // If cell voltages have been populated... if (datalayer.battery.info.number_of_cells != 0u && @@ -376,8 +381,7 @@ static bool publish_cell_voltages(void) { bool publish_events() { static JsonDocument doc; static String state_topic = topic_name + "/events"; -#ifdef HA_AUTODISCOVERY - if (ha_events_published == false) { + if (ha_autodiscovery_enabled && !ha_events_published) { doc["name"] = "Event"; doc["state_topic"] = state_topic; @@ -398,8 +402,6 @@ bool publish_events() { doc.clear(); } else { -#endif // HA_AUTODISCOVERY - const EVENTS_STRUCT_TYPE* event_pointer; //clear the vector @@ -439,36 +441,34 @@ bool publish_events() { //clear the vector order_events.clear(); } -#ifdef HA_AUTODISCOVERY } -#endif // HA_AUTODISCOVERY return true; } static bool publish_buttons_discovery(void) { -#ifdef HA_AUTODISCOVERY - if (ha_buttons_published == false) { + if (ha_autodiscovery_enabled) { + if (ha_buttons_published == false) { #ifdef DEBUG_LOG - logging.println("Publishing buttons discovery"); + logging.println("Publishing buttons discovery"); #endif // DEBUG_LOG - static JsonDocument doc; - for (int i = 0; i < sizeof(buttonConfigs) / sizeof(buttonConfigs[0]); i++) { - SensorConfig& config = buttonConfigs[i]; - doc["name"] = config.name; - doc["unique_id"] = object_id_prefix + config.object_id; - doc["command_topic"] = generateButtonTopic(config.object_id); - set_common_discovery_attributes(doc); - serializeJson(doc, mqtt_msg); - if (mqtt_publish(generateButtonAutoConfigTopic(config.object_id).c_str(), mqtt_msg, true)) { - ha_buttons_published = true; - } else { - return false; + static JsonDocument doc; + for (int i = 0; i < sizeof(buttonConfigs) / sizeof(buttonConfigs[0]); i++) { + SensorConfig& config = buttonConfigs[i]; + doc["name"] = config.name; + doc["unique_id"] = object_id_prefix + config.object_id; + doc["command_topic"] = generateButtonTopic(config.object_id); + set_common_discovery_attributes(doc); + serializeJson(doc, mqtt_msg); + if (mqtt_publish(generateButtonAutoConfigTopic(config.object_id).c_str(), mqtt_msg, true)) { + ha_buttons_published = true; + } else { + return false; + } + doc.clear(); } - doc.clear(); } } -#endif // HA_AUTODISCOVERY return true; } @@ -551,10 +551,11 @@ static void mqtt_event_handler(void* handler_args, esp_event_base_t base, int32_ } void init_mqtt(void) { -#ifdef HA_AUTODISCOVERY - create_battery_sensor_configs(); - create_global_sensor_configs(); -#endif // HA_AUTODISCOVERY + if (ha_autodiscovery_enabled) { + create_battery_sensor_configs(); + create_global_sensor_configs(); + } + #ifdef MQTT_MANUAL_TOPIC_OBJECT_NAME // Use custom topic name, object ID prefix, and device name from user settings topic_name = mqtt_topic_name; diff --git a/Software/src/devboard/mqtt/mqtt.h b/Software/src/devboard/mqtt/mqtt.h index bcd48b7c..2a5fff7d 100644 --- a/Software/src/devboard/mqtt/mqtt.h +++ b/Software/src/devboard/mqtt/mqtt.h @@ -56,5 +56,6 @@ void mqtt_loop(void); bool mqtt_publish(const char* topic, const char* mqtt_msg, bool retain); extern bool mqtt_enabled; +extern bool ha_autodiscovery_enabled; #endif