[HA discovery] Save the BLE settings and change switch to optimistic false (#1364)

The current way of saving settings is through the retain flag of the MQTT commands, unfortunately if several commands are issues they may overwrite each other. So as to prevent it, we now leverage the memory saving introduced earlier to store the BLE settings.
Also leverage the config status publication to show the state through the button position (optimistic false)
This commit is contained in:
Florian authored and GitHub committed 2022-12-19 08:44:14 -06:00
1 parent 5d3ea0d3ec
commit 25d921580d
2 files changed
+54 -34

No files matched your search

+1 -1
View File
@@ -191,7 +191,7 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) {
Serial.println(); Serial.println();
return; // Do not try to erase/write/send config at startup return; // Do not try to erase/write/send config at startup
} }
pub("/commands/BTtoMQTT/config", jo); pub(subjectBTtoMQTT, jo);
if (BTdata.containsKey("erase") && BTdata["erase"].as<bool>()) { if (BTdata.containsKey("erase") && BTdata["erase"].as<bool>()) {
// Erase config from NVS (non-volatile storage) // Erase config from NVS (non-volatile storage)
+53 -33
View File
@@ -294,10 +294,17 @@ void createDiscovery(const char* sensor_type,
if (state_class[0]) if (state_class[0])
sensor["state_class"] = state_class; //add the state class on the sensors ( https://developers.home-assistant.io/docs/core/entity/sensor/#available-state-classes ) sensor["state_class"] = state_class; //add the state class on the sensors ( https://developers.home-assistant.io/docs/core/entity/sensor/#available-state-classes )
if (state_on != nullptr) if (state_on != nullptr)
sensor["stat_on"] = state_on; if (strcmp(state_on, "true") == 0) {
sensor["stat_on"] = true;
} else {
sensor["stat_on"] = state_on;
}
if (state_off != nullptr) if (state_off != nullptr)
sensor["stat_off"] = state_off; if (strcmp(state_off, "false") == 0) {
sensor["stat_off"] = false;
} else {
sensor["stat_off"] = state_off;
}
if (cmd_topic[0]) { if (cmd_topic[0]) {
char command_topic[mqtt_topic_max_size]; char command_topic[mqtt_topic_max_size];
strcpy(command_topic, mqtt_topic); strcpy(command_topic, mqtt_topic);
@@ -926,19 +933,20 @@ void pubMqttDiscovery() {
# endif # endif
# ifdef ZgatewayBT # ifdef ZgatewayBT
# ifdef ESP32
createDiscovery("number", //set Type createDiscovery("number", //set Type
subjectSYStoMQTT, "BT: Interval between scans", (char*)getUniqueId("interval", "").c_str(), //set state_topic,name,uniqueId subjectBTtoMQTT, "BT: Interval between scans", (char*)getUniqueId("interval", "").c_str(), //set state_topic,name,uniqueId
"", "", "{{ value_json.interval/1000 }}", //set availability_topic,device_class,value_template, "", "", "{{ value_json.interval/1000 }}", //set availability_topic,device_class,value_template,
"{\"interval\":{{value*1000}}}", "", "s", //set,payload_on,payload_off,unit_of_meas, "{\"interval\":{{value*1000}},\"save\":true}", "", "s", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay 0, //set off_delay
"", "", true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic "", "", true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
"", "", "", "", false, // device name, device manufacturer, device model, device MAC, retain, "", "", "", "", false, // device name, device manufacturer, device model, device MAC, retain,
stateClassNone //State Class stateClassNone //State Class
); );
createDiscovery("number", //set Type createDiscovery("number", //set Type
subjectSYStoMQTT, "BT: Connnect every X scan(s)", (char*)getUniqueId("scanbcnct", "").c_str(), //set state_topic,name,uniqueId subjectBTtoMQTT, "BT: Connnect every X scan(s)", (char*)getUniqueId("scanbcnct", "").c_str(), //set state_topic,name,uniqueId
"", "", "{{ value_json.scanbcnct }}", //set availability_topic,device_class,value_template, "", "", "{{ value_json.scanbcnct }}", //set availability_topic,device_class,value_template,
"{\"scanbcnct\":{{value}}}", "", "", //set,payload_on,payload_off,unit_of_meas, "{\"scanbcnct\":{{value}},\"save\":true}", "", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay 0, //set off_delay
"", "", true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible,is a gateway entity, command topic "", "", true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible,is a gateway entity, command topic
"", "", "", "", false, // device name, device manufacturer, device model, device MAC, retain "", "", "", "", false, // device name, device manufacturer, device model, device MAC, retain
@@ -953,36 +961,57 @@ void pubMqttDiscovery() {
"", "", "", "", false, // device name, device manufacturer, device model, device MAC, retain "", "", "", "", false, // device name, device manufacturer, device model, device MAC, retain
stateClassNone //State Class stateClassNone //State Class
); );
createDiscovery("switch", //set Type createDiscovery("button", //set Type
"", "BT: Publish only sensors", (char*)getUniqueId("only_sensors", "").c_str(), //set state_topic,name,uniqueId will_Topic, "BT: Erase config", (char*)getUniqueId("erase_bt_config", "").c_str(), //set state_topic,name,uniqueId
"", "", "", //set availability_topic,device_class,value_template, will_Topic, "", "", //set availability_topic,device_class,value_template,
"{\"onlysensors\":true}", "{\"onlysensors\":false}", "", //set,payload_on,payload_off,unit_of_meas, "{\"erase\":true}", "", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay 0, //set off_delay
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
"", "", "", "", true, // device name, device manufacturer, device model, device MAC, retain "", "", "", "", false, // device name, device manufacturer, device model, device MAC, retain
stateClassNone //State Class stateClassNone //State Class
); );
createDiscovery("switch", //set Type createDiscovery("switch", //set Type
"", "BT: Active scan", (char*)getUniqueId("active_scan", "").c_str(), //set state_topic,name,uniqueId subjectBTtoMQTT, "BT: Publish only sensors", (char*)getUniqueId("only_sensors", "").c_str(), //set state_topic,name,uniqueId
"", "", "", //set availability_topic,device_class,value_template, "", "", "{{ value_json.onlysensors }}", //set availability_topic,device_class,value_template,
"{\"activescan\":true}", "{\"activescan\":false}", "", //set,payload_on,payload_off,unit_of_meas, "{\"onlysensors\":true,\"save\":true}", "{\"onlysensors\":false,\"save\":true}", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay 0, //set off_delay
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
"", "", "", "", true, // device name, device manufacturer, device model, device MAC, retain "", "", "", "", false, // device name, device manufacturer, device model, device MAC, retain
stateClassNone //State Class stateClassNone, //State Class
"false", "true" //state_off, state_on
); );
createDiscovery("switch", //set Type createDiscovery("switch", //set Type
"", "BT: Publish HASS presence", (char*)getUniqueId("hasspresence", "").c_str(), //set state_topic,name,uniqueId subjectBTtoMQTT, "BT: Active scan", (char*)getUniqueId("active_scan", "").c_str(), //set state_topic,name,uniqueId
"", "", "", //set availability_topic,device_class,value_template, "", "", "{{ value_json.activescan }}", //set availability_topic,device_class,value_template,
"{\"hasspresence\":true}", "{\"hasspresence\":false}", "", //set,payload_on,payload_off,unit_of_meas, "{\"activescan\":true,\"save\":true}", "{\"activescan\":false,\"save\":true}", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay 0, //set off_delay
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
"", "", "", "", true, // device name, device manufacturer, device model, device MAC, retain "", "", "", "", false, // device name, device manufacturer, device model, device MAC, retain
stateClassNone //State Class stateClassNone, //State Class
"false", "true" //state_off, state_on
); );
# ifdef ESP32
createDiscovery("switch", //set Type createDiscovery("switch", //set Type
"", "SYS: Low Power Mode command", (char*)getUniqueId("lowpowermode", "").c_str(), //set state_topic,name,uniqueId subjectBTtoMQTT, "BT: Publish HASS presence", (char*)getUniqueId("hasspresence", "").c_str(), //set state_topic,name,uniqueId
"", "", "{{ value_json.hasspresence }}", //set availability_topic,device_class,value_template,
"{\"hasspresence\":true,\"save\":true}", "{\"hasspresence\":false,\"save\":true}", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
"", "", "", "", false, // device name, device manufacturer, device model, device MAC, retain
stateClassNone, //State Class
"false", "true" //state_off, state_on
);
createDiscovery("switch", //set Type
subjectBTtoMQTT, "BT: Connect to devices", (char*)getUniqueId("bleconnect", "").c_str(), //set state_topic,name,uniqueId
"", "", "{{ value_json.bleconnect }}", //set availability_topic,device_class,value_template,
"{\"bleconnect\":true,\"save\":true}", "{\"bleconnect\":false,\"save\":true}", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
"", "", "", "", false, // device name, device manufacturer, device model, device MAC, retain
stateClassNone, //State Class
"false", "true" //state_off, state_on
);
createDiscovery("switch", //set Type
subjectBTtoMQTT, "SYS: Low Power Mode command", (char*)getUniqueId("lowpowermode", "").c_str(), //set state_topic,name,uniqueId
"", "", "", //set availability_topic,device_class,value_template, "", "", "", //set availability_topic,device_class,value_template,
"{\"lowpowermode\":2}", "{\"lowpowermode\":0}", "", //set,payload_on,payload_off,unit_of_meas, "{\"lowpowermode\":2}", "{\"lowpowermode\":0}", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay 0, //set off_delay
@@ -990,15 +1019,6 @@ void pubMqttDiscovery() {
"", "", "", "", true, // device name, device manufacturer, device model, device MAC, retain "", "", "", "", true, // device name, device manufacturer, device model, device MAC, retain
stateClassNone //State Class stateClassNone //State Class
); );
createDiscovery("switch", //set Type
"", "BT: Connect to devices", (char*)getUniqueId("bleconnect", "").c_str(), //set state_topic,name,uniqueId
"", "", "", //set availability_topic,device_class,value_template,
"{\"bleconnect\":true}", "{\"bleconnect\":false}", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
"", "", "", "", true, // device name, device manufacturer, device model, device MAC, retain
stateClassNone //State Class
);
# endif # endif
# endif # endif
} }