Adds user setting for Home Assistant autodiscovery

This commit is contained in:
Paul Brand 2024-08-27 13:47:33 +02:00
parent 00e44ccf9a
commit ce93284b51
3 changed files with 19 additions and 5 deletions

View file

@ -32,8 +32,8 @@ 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";
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

View file

@ -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

View file

@ -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 */