HA autodiscovery enabled with run-time variable

This commit is contained in:
Jaakko Haakana 2025-07-05 10:49:18 +03:00
parent efdbbff916
commit 00fb213cc1
2 changed files with 65 additions and 63 deletions

View file

@ -21,6 +21,14 @@ const bool mqtt_enabled_default = false;
bool mqtt_enabled = mqtt_enabled_default; 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_config_t mqtt_cfg;
esp_mqtt_client_handle_t client; esp_mqtt_client_handle_t client;
char mqtt_msg[MQTT_MSG_BUFFER_SIZE]; char mqtt_msg[MQTT_MSG_BUFFER_SIZE];
@ -60,7 +68,6 @@ static void publish_values(void) {
#endif #endif
} }
#ifdef HA_AUTODISCOVERY
static bool ha_common_info_published = false; static bool ha_common_info_published = false;
static bool ha_cell_voltages_published = false; static bool ha_cell_voltages_published = false;
static bool ha_events_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["unit_of_measurement"] = "V";
doc["value_template"] = "{{ value_json.cell_voltages[" + String(i) + "] }}"; doc["value_template"] = "{{ value_json.cell_voltages[" + String(i) + "] }}";
} }
#endif // HA_AUTODISCOVERY
static String generateButtonTopic(const char* subtype) { static String generateButtonTopic(const char* subtype) {
return topic_name + "/command/" + String(subtype); return topic_name + "/command/" + String(subtype);
@ -219,8 +225,10 @@ static std::vector<EventData> order_events;
static bool publish_common_info(void) { static bool publish_common_info(void) {
static JsonDocument doc; static JsonDocument doc;
static String state_topic = topic_name + "/info"; 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) { for (auto& config : sensorConfigs) {
if (!config.condition(battery)) { if (!config.condition(battery)) {
continue; continue;
@ -249,7 +257,6 @@ static bool publish_common_info(void) {
} }
} else { } else {
#endif // HA_AUTODISCOVERY
doc["bms_status"] = getBMSStatus(datalayer.battery.status.bms_status); doc["bms_status"] = getBMSStatus(datalayer.battery.status.bms_status);
doc["pause_status"] = get_emulator_pause_status(); doc["pause_status"] = get_emulator_pause_status();
@ -272,9 +279,7 @@ static bool publish_common_info(void) {
return false; return false;
} }
doc.clear(); doc.clear();
#ifdef HA_AUTODISCOVERY
} }
#endif // HA_AUTODISCOVERY
return true; return true;
} }
@ -283,7 +288,7 @@ static bool publish_cell_voltages(void) {
static String state_topic = topic_name + "/spec_data"; static String state_topic = topic_name + "/spec_data";
static String state_topic_2 = topic_name + "/spec_data_2"; static String state_topic_2 = topic_name + "/spec_data_2";
#ifdef HA_AUTODISCOVERY if (ha_autodiscovery_enabled) {
bool failed_to_publish = false; bool failed_to_publish = false;
if (ha_cell_voltages_published == false) { if (ha_cell_voltages_published == false) {
@ -327,7 +332,7 @@ static bool publish_cell_voltages(void) {
if (failed_to_publish == false) { if (failed_to_publish == false) {
ha_cell_voltages_published = true; ha_cell_voltages_published = true;
} }
#endif // HA_AUTODISCOVERY }
// If cell voltages have been populated... // If cell voltages have been populated...
if (datalayer.battery.info.number_of_cells != 0u && if (datalayer.battery.info.number_of_cells != 0u &&
@ -376,8 +381,7 @@ static bool publish_cell_voltages(void) {
bool publish_events() { bool publish_events() {
static JsonDocument doc; static JsonDocument doc;
static String state_topic = topic_name + "/events"; static String state_topic = topic_name + "/events";
#ifdef HA_AUTODISCOVERY if (ha_autodiscovery_enabled && !ha_events_published) {
if (ha_events_published == false) {
doc["name"] = "Event"; doc["name"] = "Event";
doc["state_topic"] = state_topic; doc["state_topic"] = state_topic;
@ -398,8 +402,6 @@ bool publish_events() {
doc.clear(); doc.clear();
} else { } else {
#endif // HA_AUTODISCOVERY
const EVENTS_STRUCT_TYPE* event_pointer; const EVENTS_STRUCT_TYPE* event_pointer;
//clear the vector //clear the vector
@ -439,14 +441,12 @@ bool publish_events() {
//clear the vector //clear the vector
order_events.clear(); order_events.clear();
} }
#ifdef HA_AUTODISCOVERY
} }
#endif // HA_AUTODISCOVERY
return true; return true;
} }
static bool publish_buttons_discovery(void) { static bool publish_buttons_discovery(void) {
#ifdef HA_AUTODISCOVERY if (ha_autodiscovery_enabled) {
if (ha_buttons_published == false) { if (ha_buttons_published == false) {
#ifdef DEBUG_LOG #ifdef DEBUG_LOG
logging.println("Publishing buttons discovery"); logging.println("Publishing buttons discovery");
@ -468,7 +468,7 @@ static bool publish_buttons_discovery(void) {
doc.clear(); doc.clear();
} }
} }
#endif // HA_AUTODISCOVERY }
return true; return true;
} }
@ -551,10 +551,11 @@ static void mqtt_event_handler(void* handler_args, esp_event_base_t base, int32_
} }
void init_mqtt(void) { void init_mqtt(void) {
#ifdef HA_AUTODISCOVERY if (ha_autodiscovery_enabled) {
create_battery_sensor_configs(); create_battery_sensor_configs();
create_global_sensor_configs(); create_global_sensor_configs();
#endif // HA_AUTODISCOVERY }
#ifdef MQTT_MANUAL_TOPIC_OBJECT_NAME #ifdef MQTT_MANUAL_TOPIC_OBJECT_NAME
// Use custom topic name, object ID prefix, and device name from user settings // Use custom topic name, object ID prefix, and device name from user settings
topic_name = mqtt_topic_name; topic_name = mqtt_topic_name;

View file

@ -56,5 +56,6 @@ void mqtt_loop(void);
bool mqtt_publish(const char* topic, const char* mqtt_msg, bool retain); bool mqtt_publish(const char* topic, const char* mqtt_msg, bool retain);
extern bool mqtt_enabled; extern bool mqtt_enabled;
extern bool ha_autodiscovery_enabled;
#endif #endif