From 9c73896999602ad00edd26472dff15d7c71cae57 Mon Sep 17 00:00:00 2001 From: Florian Date: Mon, 6 Mar 2023 14:20:08 -0600 Subject: [PATCH] [BT Discovery] Deactivate/Activate sliders dynamicaly depending on auto adaptive state (#1502) To avoid having unnecessaries commands in the interface --- main/ZgatewayBT.ino | 11 +++++++++- main/ZmqttDiscovery.ino | 46 +++++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index fae6871..da22c47 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -137,7 +137,7 @@ void stateBTMeasures(bool start) { void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) { // Attempts to connect to eligible devices or not Config_update(BTdata, "bleconnect", BTConfig.bleConnect); - // Identify AdaptiveScan deactivation to pass to continuous mode + // Identify AdaptiveScan deactivation to pass to continuous mode or activation to come back to default settings if (startup == false) { if (BTdata.containsKey("adaptivescan") && BTdata["adaptivescan"] == false && BTConfig.adaptiveScan == true) { BTdata["interval"] = MinTimeBtwScan; @@ -145,9 +145,18 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) { } else if (BTdata.containsKey("adaptivescan") && BTdata["adaptivescan"] == true && BTConfig.adaptiveScan == false) { BTdata["interval"] = TimeBtwRead; BTdata["intervalacts"] = TimeBtwActive; +# ifdef ZmqttDiscovery + // Remove discovered entities + eraseTopic("number", (char*)getUniqueId("interval", "").c_str()); + eraseTopic("number", (char*)getUniqueId("intervalacts", "").c_str()); +# endif } } Config_update(BTdata, "adaptivescan", BTConfig.adaptiveScan); +# ifdef ZmqttDiscovery + // Create discovery entities + btScanParametersDiscovery(); +# endif // Time before before active scan // Scan interval set if (BTdata.containsKey("interval") && BTdata["interval"] != 0) diff --git a/main/ZmqttDiscovery.ino b/main/ZmqttDiscovery.ino index 071ef20..30edfcc 100644 --- a/main/ZmqttDiscovery.ino +++ b/main/ZmqttDiscovery.ino @@ -385,6 +385,31 @@ void eraseTopic(const char* sensor_type, const char* unique_id) { pubMQTT((char*)topic.c_str(), "", true); } +# ifdef ZgatewayBT +void btScanParametersDiscovery() { + if (BTConfig.adaptiveScan == false) { + createDiscovery("number", //set Type + subjectBTtoMQTT, "BT: Interval between scans", (char*)getUniqueId("interval", "").c_str(), //set state_topic,name,uniqueId + will_Topic, "", "{{ value_json.interval/1000 }}", //set availability_topic,device_class,value_template, + "{\"interval\":{{value*1000}},\"save\":true}", "", "s", //set,payload_on,payload_off,unit_of_meas, + 0, //set off_delay + Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_available,payload_not available ,is a gateway entity, command topic + "", "", "", "", false, // device name, device manufacturer, device model, device ID, retain, + stateClassNone //State Class + ); + createDiscovery("number", //set Type + subjectBTtoMQTT, "BT: Interval between active scans", (char*)getUniqueId("intervalacts", "").c_str(), //set state_topic,name,uniqueId + will_Topic, "", "{{ value_json.intervalacts/1000 }}", //set availability_topic,device_class,value_template, + "{\"intervalacts\":{{value*1000}},\"save\":true}", "", "s", //set,payload_on,payload_off,unit_of_meas, + 0, //set off_delay + Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_available,payload_not available ,is a gateway entity, command topic + "", "", "", "", false, // device name, device manufacturer, device model, device ID, retain, + stateClassNone //State Class + ); + } +} +# endif + void pubMqttDiscovery() { Log.trace(F("omgStatusDiscovery" CR)); createDiscovery("binary_sensor", //set Type @@ -988,24 +1013,7 @@ void pubMqttDiscovery() { # ifdef ZgatewayBT # ifdef ESP32 - createDiscovery("number", //set Type - subjectBTtoMQTT, "BT: Interval between scans", (char*)getUniqueId("interval", "").c_str(), //set state_topic,name,uniqueId - will_Topic, "", "{{ value_json.interval/1000 }}", //set availability_topic,device_class,value_template, - "{\"interval\":{{value*1000}},\"save\":true}", "", "s", //set,payload_on,payload_off,unit_of_meas, - 0, //set off_delay - Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_available,payload_not available ,is a gateway entity, command topic - "", "", "", "", false, // device name, device manufacturer, device model, device ID, retain, - stateClassNone //State Class - ); - createDiscovery("number", //set Type - subjectBTtoMQTT, "BT: Interval between active scans", (char*)getUniqueId("intervalacts", "").c_str(), //set state_topic,name,uniqueId - will_Topic, "", "{{ value_json.intervalacts/1000 }}", //set availability_topic,device_class,value_template, - "{\"intervalacts\":{{value*1000}},\"save\":true}", "", "s", //set,payload_on,payload_off,unit_of_meas, - 0, //set off_delay - Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_available,payload_not available ,is a gateway entity, command topic - "", "", "", "", false, // device name, device manufacturer, device model, device ID, retain, - stateClassNone //State Class - ); + createDiscovery("number", //set Type subjectBTtoMQTT, "BT: Connnect interval", (char*)getUniqueId("intervalcnct", "").c_str(), //set state_topic,name,uniqueId will_Topic, "", "{{ value_json.intervalcnct/60000 }}", //set availability_topic,device_class,value_template, @@ -1072,6 +1080,8 @@ void pubMqttDiscovery() { eraseTopic(obsoleteEntities[i][0], (char*)getUniqueId(obsoleteEntities[i][1], "").c_str()); } + btScanParametersDiscovery(); + createDiscovery("switch", //set Type subjectBTtoMQTT, "BT: Publish HASS presence", (char*)getUniqueId("hasspresence", "").c_str(), //set state_topic,name,uniqueId will_Topic, "", "{{ value_json.hasspresence }}", //set availability_topic,device_class,value_template,