mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-03 09:49:32 +02:00
Adds user setting for Home Assistant autodiscovery
This commit is contained in:
parent
00e44ccf9a
commit
ce93284b51
3 changed files with 19 additions and 5 deletions
|
@ -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
|
const uint8_t wifi_channel = 0; // Set to 0 for automatic channel selection
|
||||||
// MQTT
|
// MQTT
|
||||||
#ifdef MQTT
|
#ifdef MQTT
|
||||||
const char* mqtt_user = "REDACTED";
|
const char* mqtt_user = "REDACTED"; // Set NULL for no username
|
||||||
const char* mqtt_password = "REDACTED";
|
const char* mqtt_password = "REDACTED"; // Set NULL for no password
|
||||||
#endif // USE_MQTT
|
#endif // USE_MQTT
|
||||||
#endif // WEBSERVER
|
#endif // WEBSERVER
|
||||||
|
|
||||||
/* Charger settings (Optional, when using generator charging) */
|
/* Charger settings (Optional, when using generator charging) */
|
||||||
volatile float CHARGER_SET_HV = 384; // Reasonably appropriate 4.0v per cell charging of a 96s pack
|
volatile float CHARGER_SET_HV = 384; // Reasonably appropriate 4.0v per cell charging of a 96s pack
|
||||||
|
|
|
@ -64,6 +64,9 @@
|
||||||
#define MQTT_SERVER "192.168.xxx.yyy"
|
#define MQTT_SERVER "192.168.xxx.yyy"
|
||||||
#define MQTT_PORT 1883
|
#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*/
|
/* Event options*/
|
||||||
#define DUMMY_EVENT_ENABLED false //Enable this line to have a dummy event that gets logged to test the interface
|
#define DUMMY_EVENT_ENABLED false //Enable this line to have a dummy event that gets logged to test the interface
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,9 @@ static String generateCellVoltageAutoConfigTopic(int cell_number, const char* ho
|
||||||
}
|
}
|
||||||
|
|
||||||
static void publish_cell_voltages(void) {
|
static void publish_cell_voltages(void) {
|
||||||
|
#ifdef HA_AUTODISCOVERY
|
||||||
static bool mqtt_first_transmission = true;
|
static bool mqtt_first_transmission = true;
|
||||||
|
#endif
|
||||||
static JsonDocument doc;
|
static JsonDocument doc;
|
||||||
static const char* hostname = WiFi.getHostname();
|
static const char* hostname = WiFi.getHostname();
|
||||||
static String state_topic = String("battery-emulator_") + String(hostname) + "/spec_data";
|
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) {
|
if (datalayer.battery.info.number_of_cells == 0u) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
#ifdef HA_AUTODISCOVERY
|
||||||
if (mqtt_first_transmission == true) {
|
if (mqtt_first_transmission == true) {
|
||||||
mqtt_first_transmission = false;
|
mqtt_first_transmission = false;
|
||||||
String topic = "homeassistant/sensor/battery-emulator/cell_voltage";
|
String topic = "homeassistant/sensor/battery-emulator/cell_voltage";
|
||||||
|
@ -71,6 +73,7 @@ static void publish_cell_voltages(void) {
|
||||||
}
|
}
|
||||||
doc.clear(); // clear after sending autoconfig
|
doc.clear(); // clear after sending autoconfig
|
||||||
} else {
|
} else {
|
||||||
|
#endif // HA_AUTODISCOVERY
|
||||||
// If cell voltages haven't been populated...
|
// If cell voltages haven't been populated...
|
||||||
if (datalayer.battery.info.number_of_cells == 0u ||
|
if (datalayer.battery.info.number_of_cells == 0u ||
|
||||||
datalayer.battery.status.cell_voltages_mV[datalayer.battery.info.number_of_cells - 1] == 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
|
#endif
|
||||||
}
|
}
|
||||||
doc.clear();
|
doc.clear();
|
||||||
|
#ifdef HA_AUTODISCOVERY
|
||||||
}
|
}
|
||||||
|
#endif // HA_AUTODISCOVERY
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SensorConfig {
|
struct SensorConfig {
|
||||||
|
@ -120,9 +125,12 @@ static String generateCommonInfoAutoConfigTopic(const char* object_id, const cha
|
||||||
|
|
||||||
static void publish_common_info(void) {
|
static void publish_common_info(void) {
|
||||||
static JsonDocument doc;
|
static JsonDocument doc;
|
||||||
|
#ifdef HA_AUTODISCOVERY
|
||||||
static bool mqtt_first_transmission = true;
|
static bool mqtt_first_transmission = true;
|
||||||
|
#endif // HA_AUTODISCOVERY
|
||||||
static const char* hostname = WiFi.getHostname();
|
static const char* hostname = WiFi.getHostname();
|
||||||
static String state_topic = String("battery-emulator_") + String(hostname) + "/info";
|
static String state_topic = String("battery-emulator_") + String(hostname) + "/info";
|
||||||
|
#ifdef HA_AUTODISCOVERY
|
||||||
if (mqtt_first_transmission == true) {
|
if (mqtt_first_transmission == true) {
|
||||||
mqtt_first_transmission = false;
|
mqtt_first_transmission = false;
|
||||||
for (int i = 0; i < sizeof(sensorConfigs) / sizeof(sensorConfigs[0]); i++) {
|
for (int i = 0; i < sizeof(sensorConfigs) / sizeof(sensorConfigs[0]); i++) {
|
||||||
|
@ -149,6 +157,7 @@ static void publish_common_info(void) {
|
||||||
}
|
}
|
||||||
doc.clear();
|
doc.clear();
|
||||||
} else {
|
} else {
|
||||||
|
#endif // HA_AUTODISCOVERY
|
||||||
doc["SOC"] = ((float)datalayer.battery.status.reported_soc) / 100.0;
|
doc["SOC"] = ((float)datalayer.battery.status.reported_soc) / 100.0;
|
||||||
doc["SOC_real"] = ((float)datalayer.battery.status.real_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;
|
doc["state_of_health"] = ((float)datalayer.battery.status.soh_pptt) / 100.0;
|
||||||
|
@ -170,7 +179,9 @@ static void publish_common_info(void) {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
doc.clear();
|
doc.clear();
|
||||||
|
#ifdef HA_AUTODISCOVERY
|
||||||
}
|
}
|
||||||
|
#endif // HA_AUTODISCOVERY
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If we lose the connection, get it back */
|
/* If we lose the connection, get it back */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue