Auto discovery deactivation at runtime by MQTT command (#856)

and switch automatic creation for it in HASS (by autodiscovery :-)
This commit is contained in:
Florian authored and GitHub committed 2021-01-31 13:41:22 -06:00
1 parent e8b4c63c6c
commit 2d377a1c3f
9 files changed
+94 -26

No files matched your search

+2 -1
View File
@@ -612,7 +612,8 @@ void coreTask(void* pvParameters) {
// Launching a connect every BLEscanBeforeConnect
if (!(scanCount % BLEscanBeforeConnect) || scanCount == 1)
BLEconnect();
launchDiscovery();
if (disc)
launchDiscovery();
dumpDevices();
}
if (low_power_mode) {
+2 -1
View File
@@ -98,7 +98,8 @@ void RFtoMQTT() {
if (!isAduplicateSignal(MQTTvalue) && MQTTvalue != 0) { // conditions to avoid duplications of RF -->MQTT
# if defined(ZmqttDiscovery) && !defined(RF_DISABLE_TRANSMIT) //component creation for HA
RFtoMQTTdiscovery(MQTTvalue);
if (disc)
RFtoMQTTdiscovery(MQTTvalue);
# endif
pub(subjectRFtoMQTT, RFdata);
// Casting "receivedSignal[o].value" to (unsigned long) because ArduinoLog doesn't support uint64_t for ESP's
+2 -1
View File
@@ -132,7 +132,8 @@ void RF2toMQTT() {
RF2data.set("address", (unsigned long)rf2rd.address);
RF2data.set("switchType", (int)rf2rd.switchType);
# ifdef ZmqttDiscovery //component creation for HA
RF2toMQTTdiscovery(RF2data);
if (disc)
RF2toMQTTdiscovery(RF2data);
# endif
pub(subjectRF2toMQTT, RF2data);
+8
View File
@@ -292,6 +292,14 @@ void pubMqttDiscovery() {
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoSYSset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
"", "", "", "" // device name, device manufacturer, device model, device mac
);
createDiscovery("switch", //set Type
"", "SYS: Auto discovery", (char*)getUniqueId("discovery", "").c_str(), //set state_topic,name,uniqueId
will_Topic, "", "", //set availability_topic,device_class,value_template,
"{\"discovery\":true}", "{\"discovery\":false}", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoSYSset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
"", "", "", "" // device name, device manufacturer, device model, device mac
);
# ifdef ZsensorBME280
# define BMEparametersCount 5
+15 -12
View File
@@ -100,18 +100,21 @@ void setupZsensorDS1820() {
void pubOneWire_HADiscovery() {
// If zmqttDiscovery is enabled, create a sensor topic for each DS18b20 sensor found on the bus, using addr as uniqueID
# ifdef ZmqttDiscovery
Log.notice(F("CreateDiscoverySensor - Found %d" CR), ds1820_count);
for (int index = 0; index < ds1820_count; index++) {
createDiscovery("sensor",
(char*)(String(OW_TOPIC) + "/" + ds1820_addr[index]).c_str(),
(char*)("DS12B20_" + String(index + 1) + "_c").c_str(),
(char*)(ds1820_addr[index] + "_c").c_str(),
will_Topic,
"temperature",
jsonTempc,
"", "", "°C",
0, "", "", true, "",
"", "", "", "");
// If zmqtt discovery is enabled, create a sensor topic for each DS18b20 sensor found on the bus, using addr as uniqueID
if (disc) {
Log.notice(F("CreateDiscoverySensor - Found %d" CR), ds1820_count);
for (int index = 0; index < ds1820_count; index++) {
createDiscovery("sensor",
(char*)(String(OW_TOPIC) + "/" + ds1820_addr[index]).c_str(),
(char*)("DS12B20_" + String(index + 1) + "_c").c_str(),
(char*)(ds1820_addr[index] + "_c").c_str(),
will_Topic,
"temperature",
jsonTempc,
"", "", "°C",
0, "", "", true, "",
"", "", "", "");
}
}
# endif
}
+21 -10
View File
@@ -159,7 +159,9 @@ char gateway_name[parameters_size * 2] = Gateway_Name;
bool connectedOnce = false; //indicate if we have been connected once to MQTT
int failure_number_ntwk = 0; // number of failure connecting to network
int failure_number_mqtt = 0; // number of failure connecting to MQTT
#ifdef ZmqttDiscovery
bool disc = true; // Auto discovery with Home Assistant convention
#endif
unsigned long timer_led_measures = 0;
#ifdef ESP32
@@ -716,9 +718,6 @@ void setup() {
setupRS232();
modules.add(ZgatewayRS232);
#endif
#ifdef ZmqttDiscovery
modules.add(ZmqttDiscovery);
#endif
#ifdef ZsensorSHTC3
setupSHTC3();
#endif
@@ -1243,15 +1242,17 @@ void loop() {
#endif
failure_number_ntwk = 0;
if (client.connected()) {
#ifdef ZmqttDiscovery
if (!connectedOnce) pubMqttDiscovery(); // at first connection we publish the discovery payloads
#endif
digitalWrite(LED_INFO, LED_INFO_ON);
connectedOnce = true;
failure_number_ntwk = 0;
client.loop();
#ifdef ZmqttDiscovery
if (disc)
if (!connectedOnce) pubMqttDiscovery(); // at first connection we publish the discovery payloads
#endif
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
if (now > (timer_sys_measures + (TimeBetweenReadingSYS * 1000)) || !timer_sys_measures) {
timer_sys_measures = millis();
@@ -1533,7 +1534,7 @@ void receivingMQTT(char* topicOri, char* datacallback) {
# if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5STACK)
MQTTtoM5(topicOri, jsondata);
# endif
# ifdef ZactuatorONOFF // outside the jsonpublishing macro due to the fact that we need to use simplepublishing with HA discovery
# ifdef ZactuatorONOFF
MQTTtoONOFF(topicOri, jsondata);
# endif
# ifdef ZactuatorSomfy
@@ -1596,10 +1597,20 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
# ifndef ESPWifiManualSetup
setup_wifimanager(true);
# endif
} else {
Log.warning(F("wrong command" CR));
}
}
#endif
#ifdef ZmqttDiscovery
if (SYSdata.containsKey("discovery")) {
if (SYSdata.is<bool>("discovery")) {
disc = SYSdata["discovery"];
if (disc)
pubMqttDiscovery();
} else {
Log.error(F("Discovery command not a boolean" CR));
}
Log.notice(F("Discovery state: %T" CR), disc);
}
#endif
}
}