Auto discovery deactivation at runtime by MQTT command (#856)
and switch automatic creation for it in HASS (by autodiscovery :-)
This commit is contained in:
1 parent
e8b4c63c6c
commit
2d377a1c3f
9 files changed
+79
-11
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
# Integrate Home Assistant
|
# Integrate Home Assistant
|
||||||
## Auto discovery
|
## Auto discovery
|
||||||
So as to enable HASS auto discovery with MQTT you have to uncomment [ZmqttDiscovery](https://github.com/1technophile/OpenMQTTGateway/blob/0180a0dbd55ed8e0799e30ee84f68070a6f478fa/User_config.h#L99) in User_config.h. Note that Home Assistant discovery is enabled by default on all binaries and platformio configurations except for UNO.
|
Home Assistant discovery is enabled by default on all binaries and platformio configurations except for UNO. With Arduino IDE please read the [advanced configuration section](../upload/advanced-configuration#auto-discovery) of the documentation.
|
||||||
|
|
||||||
Once done, enable discovery on your MQTT integration definition in HASS.
|
Once done, enable discovery on your MQTT integration definition in HASS.
|
||||||
|
|
||||||
@@ -14,7 +14,9 @@ The max size of the username is 30 and 60 for the password.
|
|||||||
|
|
||||||
OMG will use the auto discovery functionality of home assistant to create sensors and gateways into your HASS instance automaticaly.
|
OMG will use the auto discovery functionality of home assistant to create sensors and gateways into your HASS instance automaticaly.
|
||||||
|
|
||||||
|
::: tip
|
||||||
The gateway device will be available into Configuration->Devices section of Home Assistant.
|
The gateway device will be available into Configuration->Devices section of Home Assistant.
|
||||||
|
:::
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|||||||
@@ -52,3 +52,30 @@ home/OpenMQTTGateway/SRFBtoMQTT/59365 {"raw":"2B660186042E00E7E5","value":"59365
|
|||||||
```
|
```
|
||||||
|
|
||||||
It can be usefull to avoid warning or errors on certain controllers.
|
It can be usefull to avoid warning or errors on certain controllers.
|
||||||
|
|
||||||
|
## Auto discovery
|
||||||
|
Home Assistant discovery is enabled by default on all binaries and platformio configurations except for UNO. With Arduino IDE you have to uncomment [ZmqttDiscovery](https://github.com/1technophile/OpenMQTTGateway/blob/0180a0dbd55ed8e0799e30ee84f68070a6f478fa/User_config.h#L99) in User_config.h.
|
||||||
|
|
||||||
|
To have a working discovery setup you need to enable it on your MQTT integration definition in HASS.
|
||||||
|
|
||||||
|
::: tip
|
||||||
|
With platformio you can deactivate discovery by adding:
|
||||||
|
``` ini
|
||||||
|
'-UZmqttDiscovery="HADiscovery"'
|
||||||
|
```
|
||||||
|
To your environment definition.
|
||||||
|
:::
|
||||||
|
|
||||||
|
So as to create the MQTT username and password, you have to create a new user(recommended) into Home Assistant->Configuration->Users (available in admin mode) or use an existing username/pwd combination (not recommended). This user doesn't need to be an administrator.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
::: warning
|
||||||
|
The max size of the username is 30 and 60 for the password.
|
||||||
|
:::
|
||||||
|
|
||||||
|
OMG will use the auto discovery functionality of home assistant to create sensors and gateways into your HASS instance automaticaly.
|
||||||
|
|
||||||
|
The gateway device will be available into Configuration->Devices section of Home Assistant.
|
||||||
|
|
||||||
|

|
||||||
@@ -15,6 +15,20 @@ With Home Assistant, this command is directly avalaible through MQTT auto discov
|
|||||||
With Home Assistant, this command is directly avalaible through MQTT auto discovery as a switch into the HASS OpenMQTTGateway device entities list.
|
With Home Assistant, this command is directly avalaible through MQTT auto discovery as a switch into the HASS OpenMQTTGateway device entities list.
|
||||||
:::
|
:::
|
||||||
|
|
||||||
|
## Auto discovery
|
||||||
|
You can deactivate the MQTT auto discovery function, this function enable to create automaticaly devices/entities with Home Assistant convention.
|
||||||
|
### Deactivate
|
||||||
|
`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/config" -m '{"discovery":false}'`
|
||||||
|
|
||||||
|
### Activate
|
||||||
|
`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/config" -m '{"discovery":true}'`
|
||||||
|
|
||||||
|
If you want the settings to be kept upon gateway restart, you can publish the command with the retain flag.
|
||||||
|
|
||||||
|
::: tip
|
||||||
|
Auto discovery is enable by default on release binaries, on platformio (except for UNO). With Arduino IDE please read the [advanced configuration section](../upload/advanced-configuration#auto-discovery) of the documentation.
|
||||||
|
:::
|
||||||
|
|
||||||
# State LED usage
|
# State LED usage
|
||||||
|
|
||||||
The gateway can support up to 3 LED to display its operating state:
|
The gateway can support up to 3 LED to display its operating state:
|
||||||
|
|||||||
@@ -612,6 +612,7 @@ void coreTask(void* pvParameters) {
|
|||||||
// Launching a connect every BLEscanBeforeConnect
|
// Launching a connect every BLEscanBeforeConnect
|
||||||
if (!(scanCount % BLEscanBeforeConnect) || scanCount == 1)
|
if (!(scanCount % BLEscanBeforeConnect) || scanCount == 1)
|
||||||
BLEconnect();
|
BLEconnect();
|
||||||
|
if (disc)
|
||||||
launchDiscovery();
|
launchDiscovery();
|
||||||
dumpDevices();
|
dumpDevices();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,6 +98,7 @@ void RFtoMQTT() {
|
|||||||
|
|
||||||
if (!isAduplicateSignal(MQTTvalue) && MQTTvalue != 0) { // conditions to avoid duplications of RF -->MQTT
|
if (!isAduplicateSignal(MQTTvalue) && MQTTvalue != 0) { // conditions to avoid duplications of RF -->MQTT
|
||||||
# if defined(ZmqttDiscovery) && !defined(RF_DISABLE_TRANSMIT) //component creation for HA
|
# if defined(ZmqttDiscovery) && !defined(RF_DISABLE_TRANSMIT) //component creation for HA
|
||||||
|
if (disc)
|
||||||
RFtoMQTTdiscovery(MQTTvalue);
|
RFtoMQTTdiscovery(MQTTvalue);
|
||||||
# endif
|
# endif
|
||||||
pub(subjectRFtoMQTT, RFdata);
|
pub(subjectRFtoMQTT, RFdata);
|
||||||
|
|||||||
@@ -132,6 +132,7 @@ void RF2toMQTT() {
|
|||||||
RF2data.set("address", (unsigned long)rf2rd.address);
|
RF2data.set("address", (unsigned long)rf2rd.address);
|
||||||
RF2data.set("switchType", (int)rf2rd.switchType);
|
RF2data.set("switchType", (int)rf2rd.switchType);
|
||||||
# ifdef ZmqttDiscovery //component creation for HA
|
# ifdef ZmqttDiscovery //component creation for HA
|
||||||
|
if (disc)
|
||||||
RF2toMQTTdiscovery(RF2data);
|
RF2toMQTTdiscovery(RF2data);
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
|
|||||||
@@ -292,6 +292,14 @@ void pubMqttDiscovery() {
|
|||||||
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoSYSset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
|
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
|
"", "", "", "" // 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
|
# ifdef ZsensorBME280
|
||||||
# define BMEparametersCount 5
|
# define BMEparametersCount 5
|
||||||
|
|||||||
@@ -100,6 +100,8 @@ void setupZsensorDS1820() {
|
|||||||
void pubOneWire_HADiscovery() {
|
void pubOneWire_HADiscovery() {
|
||||||
// If zmqttDiscovery is enabled, create a sensor topic for each DS18b20 sensor found on the bus, using addr as uniqueID
|
// If zmqttDiscovery is enabled, create a sensor topic for each DS18b20 sensor found on the bus, using addr as uniqueID
|
||||||
# ifdef ZmqttDiscovery
|
# ifdef ZmqttDiscovery
|
||||||
|
// 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);
|
Log.notice(F("CreateDiscoverySensor - Found %d" CR), ds1820_count);
|
||||||
for (int index = 0; index < ds1820_count; index++) {
|
for (int index = 0; index < ds1820_count; index++) {
|
||||||
createDiscovery("sensor",
|
createDiscovery("sensor",
|
||||||
@@ -113,6 +115,7 @@ void pubOneWire_HADiscovery() {
|
|||||||
0, "", "", true, "",
|
0, "", "", true, "",
|
||||||
"", "", "", "");
|
"", "", "", "");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
# endif
|
# endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+21
-10
@@ -159,7 +159,9 @@ char gateway_name[parameters_size * 2] = Gateway_Name;
|
|||||||
bool connectedOnce = false; //indicate if we have been connected once to MQTT
|
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_ntwk = 0; // number of failure connecting to network
|
||||||
int failure_number_mqtt = 0; // number of failure connecting to MQTT
|
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;
|
unsigned long timer_led_measures = 0;
|
||||||
|
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
@@ -716,9 +718,6 @@ void setup() {
|
|||||||
setupRS232();
|
setupRS232();
|
||||||
modules.add(ZgatewayRS232);
|
modules.add(ZgatewayRS232);
|
||||||
#endif
|
#endif
|
||||||
#ifdef ZmqttDiscovery
|
|
||||||
modules.add(ZmqttDiscovery);
|
|
||||||
#endif
|
|
||||||
#ifdef ZsensorSHTC3
|
#ifdef ZsensorSHTC3
|
||||||
setupSHTC3();
|
setupSHTC3();
|
||||||
#endif
|
#endif
|
||||||
@@ -1243,15 +1242,17 @@ void loop() {
|
|||||||
#endif
|
#endif
|
||||||
failure_number_ntwk = 0;
|
failure_number_ntwk = 0;
|
||||||
if (client.connected()) {
|
if (client.connected()) {
|
||||||
#ifdef ZmqttDiscovery
|
|
||||||
if (!connectedOnce) pubMqttDiscovery(); // at first connection we publish the discovery payloads
|
|
||||||
#endif
|
|
||||||
digitalWrite(LED_INFO, LED_INFO_ON);
|
digitalWrite(LED_INFO, LED_INFO_ON);
|
||||||
connectedOnce = true;
|
connectedOnce = true;
|
||||||
failure_number_ntwk = 0;
|
failure_number_ntwk = 0;
|
||||||
|
|
||||||
client.loop();
|
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 defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||||
if (now > (timer_sys_measures + (TimeBetweenReadingSYS * 1000)) || !timer_sys_measures) {
|
if (now > (timer_sys_measures + (TimeBetweenReadingSYS * 1000)) || !timer_sys_measures) {
|
||||||
timer_sys_measures = millis();
|
timer_sys_measures = millis();
|
||||||
@@ -1533,7 +1534,7 @@ void receivingMQTT(char* topicOri, char* datacallback) {
|
|||||||
# if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5STACK)
|
# if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5STACK)
|
||||||
MQTTtoM5(topicOri, jsondata);
|
MQTTtoM5(topicOri, jsondata);
|
||||||
# endif
|
# 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);
|
MQTTtoONOFF(topicOri, jsondata);
|
||||||
# endif
|
# endif
|
||||||
# ifdef ZactuatorSomfy
|
# ifdef ZactuatorSomfy
|
||||||
@@ -1596,10 +1597,20 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
|
|||||||
# ifndef ESPWifiManualSetup
|
# ifndef ESPWifiManualSetup
|
||||||
setup_wifimanager(true);
|
setup_wifimanager(true);
|
||||||
# endif
|
# 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
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user