From ce93284b514635314de04b7d0318c9b38a67a824 Mon Sep 17 00:00:00 2001 From: Paul Brand Date: Tue, 27 Aug 2024 13:47:33 +0200 Subject: [PATCH] Adds user setting for Home Assistant autodiscovery --- Software/USER_SETTINGS.cpp | 8 ++++---- Software/USER_SETTINGS.h | 3 +++ Software/src/devboard/mqtt/mqtt.cpp | 13 ++++++++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Software/USER_SETTINGS.cpp b/Software/USER_SETTINGS.cpp index 71065018..d7817090 100644 --- a/Software/USER_SETTINGS.cpp +++ b/Software/USER_SETTINGS.cpp @@ -32,10 +32,10 @@ const char* passwordAP = "123456789"; // Minimum of 8 characters; set to NULL i const uint8_t wifi_channel = 0; // Set to 0 for automatic channel selection // MQTT #ifdef MQTT -const char* mqtt_user = "REDACTED"; -const char* mqtt_password = "REDACTED"; -#endif // USE_MQTT -#endif // WEBSERVER +const char* mqtt_user = "REDACTED"; // Set NULL for no username +const char* mqtt_password = "REDACTED"; // Set NULL for no password +#endif // USE_MQTT +#endif // WEBSERVER /* Charger settings (Optional, when using generator charging) */ volatile float CHARGER_SET_HV = 384; // Reasonably appropriate 4.0v per cell charging of a 96s pack diff --git a/Software/USER_SETTINGS.h b/Software/USER_SETTINGS.h index 5e82cd5a..f60e1ea2 100644 --- a/Software/USER_SETTINGS.h +++ b/Software/USER_SETTINGS.h @@ -64,6 +64,9 @@ #define MQTT_SERVER "192.168.xxx.yyy" #define MQTT_PORT 1883 +/* Home Assistant options */ +#define HA_AUTODISCOVERY // Enable this line to send Home Assistant autodiscovery messages. If not enabled manual configuration of Home Assitant is required + /* Event options*/ #define DUMMY_EVENT_ENABLED false //Enable this line to have a dummy event that gets logged to test the interface diff --git a/Software/src/devboard/mqtt/mqtt.cpp b/Software/src/devboard/mqtt/mqtt.cpp index 67535cfd..5e4cdf17 100644 --- a/Software/src/devboard/mqtt/mqtt.cpp +++ b/Software/src/devboard/mqtt/mqtt.cpp @@ -31,7 +31,9 @@ static String generateCellVoltageAutoConfigTopic(int cell_number, const char* ho } static void publish_cell_voltages(void) { +#ifdef HA_AUTODISCOVERY static bool mqtt_first_transmission = true; +#endif static JsonDocument doc; static const char* hostname = WiFi.getHostname(); static String state_topic = String("battery-emulator_") + String(hostname) + "/spec_data"; @@ -40,7 +42,7 @@ static void publish_cell_voltages(void) { if (datalayer.battery.info.number_of_cells == 0u) { return; } - +#ifdef HA_AUTODISCOVERY if (mqtt_first_transmission == true) { mqtt_first_transmission = false; String topic = "homeassistant/sensor/battery-emulator/cell_voltage"; @@ -71,6 +73,7 @@ static void publish_cell_voltages(void) { } doc.clear(); // clear after sending autoconfig } else { +#endif // HA_AUTODISCOVERY // If cell voltages haven't been populated... if (datalayer.battery.info.number_of_cells == 0u || datalayer.battery.status.cell_voltages_mV[datalayer.battery.info.number_of_cells - 1] == 0u) { @@ -90,7 +93,9 @@ static void publish_cell_voltages(void) { #endif } doc.clear(); +#ifdef HA_AUTODISCOVERY } +#endif // HA_AUTODISCOVERY } struct SensorConfig { @@ -120,9 +125,12 @@ static String generateCommonInfoAutoConfigTopic(const char* object_id, const cha static void publish_common_info(void) { static JsonDocument doc; +#ifdef HA_AUTODISCOVERY static bool mqtt_first_transmission = true; +#endif // HA_AUTODISCOVERY static const char* hostname = WiFi.getHostname(); static String state_topic = String("battery-emulator_") + String(hostname) + "/info"; +#ifdef HA_AUTODISCOVERY if (mqtt_first_transmission == true) { mqtt_first_transmission = false; for (int i = 0; i < sizeof(sensorConfigs) / sizeof(sensorConfigs[0]); i++) { @@ -149,6 +157,7 @@ static void publish_common_info(void) { } doc.clear(); } else { +#endif // HA_AUTODISCOVERY doc["SOC"] = ((float)datalayer.battery.status.reported_soc) / 100.0; doc["SOC_real"] = ((float)datalayer.battery.status.real_soc) / 100.0; doc["state_of_health"] = ((float)datalayer.battery.status.soh_pptt) / 100.0; @@ -170,7 +179,9 @@ static void publish_common_info(void) { #endif } doc.clear(); +#ifdef HA_AUTODISCOVERY } +#endif // HA_AUTODISCOVERY } /* If we lose the connection, get it back */