mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-04 02:09:30 +02:00
HA autodiscovery enabled with run-time variable
This commit is contained in:
parent
efdbbff916
commit
00fb213cc1
2 changed files with 65 additions and 63 deletions
|
@ -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,51 +288,51 @@ 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) {
|
||||||
|
|
||||||
// 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 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++) {
|
for (int i = 0; i < datalayer.battery.info.number_of_cells; i++) {
|
||||||
int cellNumber = i + 1;
|
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);
|
set_common_discovery_attributes(doc);
|
||||||
|
|
||||||
serializeJson(doc, mqtt_msg, sizeof(mqtt_msg));
|
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;
|
failed_to_publish = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
doc.clear(); // clear after sending autoconfig
|
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 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,36 +441,34 @@ 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");
|
||||||
#endif // DEBUG_LOG
|
#endif // DEBUG_LOG
|
||||||
|
|
||||||
static JsonDocument doc;
|
static JsonDocument doc;
|
||||||
for (int i = 0; i < sizeof(buttonConfigs) / sizeof(buttonConfigs[0]); i++) {
|
for (int i = 0; i < sizeof(buttonConfigs) / sizeof(buttonConfigs[0]); i++) {
|
||||||
SensorConfig& config = buttonConfigs[i];
|
SensorConfig& config = buttonConfigs[i];
|
||||||
doc["name"] = config.name;
|
doc["name"] = config.name;
|
||||||
doc["unique_id"] = object_id_prefix + config.object_id;
|
doc["unique_id"] = object_id_prefix + config.object_id;
|
||||||
doc["command_topic"] = generateButtonTopic(config.object_id);
|
doc["command_topic"] = generateButtonTopic(config.object_id);
|
||||||
set_common_discovery_attributes(doc);
|
set_common_discovery_attributes(doc);
|
||||||
serializeJson(doc, mqtt_msg);
|
serializeJson(doc, mqtt_msg);
|
||||||
if (mqtt_publish(generateButtonAutoConfigTopic(config.object_id).c_str(), mqtt_msg, true)) {
|
if (mqtt_publish(generateButtonAutoConfigTopic(config.object_id).c_str(), mqtt_msg, true)) {
|
||||||
ha_buttons_published = true;
|
ha_buttons_published = true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue