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