HASS gateway Discovery improvements (#792)

* MQTT discovery improvements

-remove gatewayBT
-remove device class formaldehyde (not existing in HASS)
-add SYStoMQTT new parameters
-attach connectivity parameter to the device
-add BT gateway infos and switches
-refactor modules storage and publication
This commit is contained in:
Florian authored and GitHub committed 2020-11-04 11:53:55 -06:00
1 parent cabc8695eb
commit 9a66c9ef09
7 files changed
+136 -104

No files matched your search

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

+7 -2
View File
@@ -1,10 +1,15 @@
# Integrate Home Assistant
## 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
And enable discovery on your MQTT integration definition in HASS.
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.
Once done, enable discovery on your MQTT integration definition in HASS.
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.
![](../img/OpenMQTTGateway_Home_Assistant_MQTT_discovery.png)
## Manual integration examples
From @123, @finity, @denniz03, @jrockstad
+8
View File
@@ -85,6 +85,10 @@ you can also force a scan to be done by the following command:
`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"interval":0}'`
::: tip
With Home Assistant, this command is directly avalaible through MQTT auto discovery as a switch into the HASS OpenMQTTGateway device entities list.
:::
Once the forced scan has completed, the previous scan interval value will be restored. Forcing a scan command trigger also a BLE connect process after the scan (see below).
The default value `TimeBtwRead` is set into config_BT.h or into your .ini file for platformio users.
@@ -107,6 +111,10 @@ If you want to change this characteristic:
`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"onlysensors":true}'`
::: tip
With Home Assistant, this command is directly avalaible through MQTT auto discovery as a switch into the HASS OpenMQTTGateway device entities list.
:::
The gateway will publish only the detected sensors like Mi Flora, Mi jia, LYWSD03MMC... and not the other BLE devices. This is usefull if you don't use the gateway for presence detection but only to retrieve sensors data.
## Setting the minimum RSSI accepted to publish device data
+8 -1
View File
@@ -1,13 +1,20 @@
# System commands (ESP only)
## Restart the ESP
`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/config" -m '{"cmd":"restart"}'`
::: tip
With Home Assistant, this command is directly avalaible through MQTT auto discovery as a switch into the HASS OpenMQTTGateway device entities list.
:::
## Erase the ESP settings
`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/config" -m '{"cmd":"erase"}'`
::: tip
With Home Assistant, this command is directly avalaible through MQTT auto discovery as a switch into the HASS OpenMQTTGateway device entities list.
:::
# State LED usage
The gateway can support up to 3 LED to display its operating state:
+1 -1
View File
@@ -230,7 +230,7 @@ void FormalDiscovery(char* mac) {
{"sensor", "Formal-batt", mac, "battery", jsonBatt, "", "", "%"},
{"sensor", "Formal-temp", mac, "temperature", jsonTempc, "", "", "°C"},
{"sensor", "Formal-hum", mac, "humidity", jsonHum, "", "", "%"},
{"sensor", "Formal-for", mac, "formaldehyde", jsonFor, "", "", "%"}
{"sensor", "Formal-for", mac, "", jsonFor, "", "", "%"}
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
};
+78 -16
View File
@@ -114,7 +114,11 @@ void createDiscovery(char* sensor_type,
if (child_device) {
StaticJsonBuffer<JSON_MSG_BUFFER> jsonDeviceBuffer;
JsonObject& device = jsonDeviceBuffer.createObject();
char JSONmessageBuffer[JSON_MSG_BUFFER];
modules.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
Log.notice(F("Received json : %s" CR), JSONmessageBuffer);
device.set("name", gateway_name);
device.set("model", JSONmessageBuffer);
device.set("manufacturer", DEVICEMANUFACTURER);
device.set("sw_version", OMG_VERSION);
JsonArray& identifiers = device.createNestedArray("identifiers");
@@ -128,21 +132,63 @@ void createDiscovery(char* sensor_type,
void pubMqttDiscovery() {
Log.trace(F("omgStatusDiscovery" CR));
createDiscovery("binary_sensor", //set Type
will_Topic, Gateway_Name, (char*)getUniqueId("", "").c_str(), //set state_topic,name,uniqueId
will_Topic, "SYS: Connectivity", (char*)getUniqueId("connectivity", "").c_str(), //set state_topic,name,uniqueId
will_Topic, "connectivity", "", //set availability_topic,device_class,value_template,
Gateway_AnnouncementMsg, will_Message, "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
Gateway_AnnouncementMsg, will_Message, false, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
Gateway_AnnouncementMsg, will_Message, true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
);
createDiscovery("sensor", //set Type
subjectSYStoMQTT, "SYS: Uptime", (char*)getUniqueId("uptime", "").c_str(), //set state_topic,name,uniqueId
"", "", "{{ value_json.uptime }}", //set availability_topic,device_class,value_template,
"", "", "s", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
"", "", true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
);
# if defined(ESP8266) || defined(ESP32)
createDiscovery("sensor", //set Type
subjectSYStoMQTT, "SYS: Free memory", (char*)getUniqueId("freemem", "").c_str(), //set state_topic,name,uniqueId
"", "", "{{ value_json.freemem }}", //set availability_topic,device_class,value_template,
"", "", "B", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
"", "", true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
);
createDiscovery("sensor", //set Type
subjectSYStoMQTT, "SYS: IP", (char*)getUniqueId("ip", "").c_str(), //set state_topic,name,uniqueId
"", "", "{{ value_json.ip }}", //set availability_topic,device_class,value_template,
"", "", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
"", "", true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
);
# ifndef ESP32_ETHERNET
createDiscovery("sensor", //set Type
subjectSYStoMQTT, "SYS: Rssi", (char*)getUniqueId("rssi", "").c_str(), //set state_topic,name,uniqueId
"", "", "{{ value_json.rssi }}", //set availability_topic,device_class,value_template,
"", "", "dB", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
"", "", true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
);
# endif
# endif
# ifdef ESP32
createDiscovery("sensor", //set Type
subjectSYStoMQTT, "SYS: Low Power Mode", (char*)getUniqueId("lowpowermode", "").c_str(), //set state_topic,name,uniqueId
"", "", "{{ value_json.lowpowermode }}", //set availability_topic,device_class,value_template,
"", "", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
"", "", true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
);
# endif
createDiscovery("switch", //set Type
will_Topic, "restart OMG", (char*)getUniqueId("restart", "").c_str(), //set state_topic,name,uniqueId
will_Topic, "SYS: Restart gateway", (char*)getUniqueId("restart", "").c_str(), //set state_topic,name,uniqueId
will_Topic, "", "", //set availability_topic,device_class,value_template,
"{\"cmd\":\"restart\"}", "", "", //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 child device, command topic
);
createDiscovery("switch", //set Type
will_Topic, "erase OMG", (char*)getUniqueId("erase", "").c_str(), //set state_topic,name,uniqueId
will_Topic, "SYS: Erase credentials", (char*)getUniqueId("erase", "").c_str(), //set state_topic,name,uniqueId
will_Topic, "", "", //set availability_topic,device_class,value_template,
"{\"cmd\":\"erase\"}", "", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
@@ -476,18 +522,34 @@ void pubMqttDiscovery() {
# endif
# ifdef ZgatewayBT
// Sensor to display BT received value
Log.trace(F("gatewayBTDiscovery" CR));
char* gatewayBT[8] = {"sensor", "gatewayBT", "", "", jsonId, "", "", ""};
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
Log.trace(F("CreateDiscoverySensor" CR));
//trc(gatewayBT[1]);
createDiscovery(gatewayBT[0],
subjectBTtoMQTT, gatewayBT[1], (char*)getUniqueId(gatewayBT[1], gatewayBT[2]).c_str(),
will_Topic, gatewayBT[3], gatewayBT[4],
gatewayBT[5], gatewayBT[6], gatewayBT[7],
0, "", "", true, "");
createDiscovery("sensor", //set Type
subjectSYStoMQTT, "BT: Interval between scans", (char*)getUniqueId("interval", "").c_str(), //set state_topic,name,uniqueId
"", "", "{{ value_json.interval }}", //set availability_topic,device_class,value_template,
"", "", "ms", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
"", "", true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
);
createDiscovery("sensor", //set Type
subjectSYStoMQTT, "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,
"", "", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
"", "", true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
);
createDiscovery("switch", //set Type
will_Topic, "BT: Force scan", (char*)getUniqueId("force_scan", "").c_str(), //set state_topic,name,uniqueId
will_Topic, "", "", //set availability_topic,device_class,value_template,
"{\"interval\":0}", "", "", //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 child device, command topic
);
createDiscovery("switch", //set Type
"", "BT: Publish only sensors", (char*)getUniqueId("only_sensors", "").c_str(), //set state_topic,name,uniqueId
"", "", "", //set availability_topic,device_class,value_template,
"{\"onlysensors\":true}", "{\"onlysensors\":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 child device, command topic
);
# endif
}
+34 -84
View File
@@ -48,10 +48,14 @@ ReceivedSignal receivedSignal[struct_size] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
unsigned long timer_sys_measures = 0;
# define ARDUINOJSON_USE_LONG_LONG 1
#endif
#include <ArduinoJson.h>
#include <ArduinoLog.h>
#include <PubSubClient.h>
StaticJsonBuffer<JSON_MSG_BUFFER> modulesBuffer;
JsonArray& modules = modulesBuffer.createArray();
// Modules config inclusion
#if defined(ZgatewayRF) || defined(ZgatewayRF2) || defined(ZgatewayPilight)
# include "config_RF.h"
@@ -592,81 +596,110 @@ void setup() {
#ifdef ZsensorBME280
setupZsensorBME280();
modules.add(ZsensorBME280);
#endif
#ifdef ZsensorHTU21
setupZsensorHTU21();
modules.add(ZsensorHTU21);
#endif
#ifdef ZsensorAHTx0
setupZsensorAHTx0();
modules.add(ZsensorAHTx0);
#endif
#ifdef ZsensorBH1750
setupZsensorBH1750();
modules.add(ZsensorBH1750);
#endif
#ifdef ZsensorTSL2561
setupZsensorTSL2561();
modules.add(ZsensorTSL2561);
#endif
#ifdef Zgateway2G
setup2G();
modules.add(Zgateway2G);
#endif
#ifdef ZgatewayIR
setupIR();
modules.add(ZgatewayIR);
#endif
#ifdef ZgatewayLORA
setupLORA();
modules.add(ZgatewayLORA);
#endif
#ifdef ZgatewayRF
setupRF();
modules.add(ZgatewayRF);
#endif
#ifdef ZgatewayRF2
setupRF2();
modules.add(ZgatewayRF2);
#endif
#ifdef ZgatewayPilight
setupPilight();
modules.add(ZgatewayPilight);
#endif
#ifdef ZgatewayWeatherStation
setupWeatherStation();
modules.add(ZgatewayWeatherStation);
#endif
#ifdef ZgatewaySRFB
setupSRFB();
modules.add(ZgatewaySRFB);
#endif
#ifdef ZgatewayBT
setupBT();
modules.add(ZgatewayBT);
#endif
#ifdef ZgatewayRFM69
setupRFM69();
modules.add(ZgatewayRFM69);
#endif
#ifdef ZsensorINA226
setupINA226();
modules.add(ZsensorINA226);
#endif
#ifdef ZsensorHCSR501
setupHCSR501();
modules.add(ZsensorHCSR501);
#endif
#ifdef ZsensorHCSR04
setupHCSR04();
modules.add(ZsensorHCSR04);
#endif
#ifdef ZsensorGPIOInput
setupGPIOInput();
modules.add(ZsensorGPIOInput);
#endif
#ifdef ZsensorGPIOKeyCode
setupGPIOKeyCode();
modules.add(ZsensorGPIOKeyCode);
#endif
#ifdef ZactuatorFASTLED
setupFASTLED();
modules.add(ZactuatorFASTLED);
#endif
#ifdef ZactuatorPWM
setupPWM();
modules.add(ZactuatorPWM);
#endif
#ifdef ZsensorDS1820
setupZsensorDS1820();
modules.add(ZsensorDS1820);
#endif
#ifdef ZsensorADC
setupADC();
modules.add(ZsensorADC);
#endif
#ifdef ZsensorDHT
setupDHT();
modules.add(ZsensorDHT);
#endif
#ifdef ZgatewayRS232
setupRS232();
modules.add(ZgatewayRS232);
#endif
#ifdef ZmqttDiscovery
modules.add(ZmqttDiscovery);
#endif
Log.trace(F("mqtt_max_packet_size: %d" CR), mqtt_max_packet_size);
Log.notice(F("Setup OpenMQTTGateway end" CR));
@@ -1337,94 +1370,14 @@ void stateMeasures() {
SYSdata["mac"] = (char*)mac.c_str();
SYSdata["wifiprt"] = (int)wifiProtocol;
# endif
# endif
String modules = "";
# ifdef ZgatewayRF
modules = modules + ZgatewayRF;
# endif
# ifdef ZsensorBME280
modules = modules + ZsensorBME280;
# endif
# ifdef ZsensorHTU21
modules = modules + ZsensorHTU21;
# endif
# ifdef ZsensorAHTx0
modules = modules + ZsensorAHTx0;
# endif
# ifdef ZsensorHCSR04
modules = modules + ZsensorHCSR04;
# endif
# ifdef ZsensorBH1750
modules = modules + ZsensorBH1750;
# endif
# ifdef ZsensorTSL2561
modules = modules + ZsensorTSL2561;
# endif
# ifdef ZsensorDHT
modules = modules + ZsensorDHT;
# endif
# ifdef ZsensorDS1820
modules = modules + ZsensorDS1820;
# endif
# ifdef ZactuatorONOFF
modules = modules + ZactuatorONOFF;
# endif
# ifdef Zgateway2G
modules = modules + Zgateway2G;
# endif
# ifdef ZgatewayIR
modules = modules + ZgatewayIR;
# endif
# ifdef ZgatewayLORA
modules = modules + ZgatewayLORA;
# endif
# ifdef ZgatewayRF2
modules = modules + ZgatewayRF2;
# endif
# ifdef ZgatewayWeatherStation
modules = modules + ZgatewayWeatherStation;
# endif
# ifdef ZgatewayPilight
modules = modules + ZgatewayPilight;
# endif
# ifdef ZgatewaySRFB
modules = modules + ZgatewaySRFB;
# endif
# ifdef ZgatewayBT
modules = modules + ZgatewayBT;
# ifdef ESP32
SYSdata["lowpowermode"] = (int)low_power_mode;
# endif
SYSdata["interval"] = BLEinterval;
SYSdata["scanbcnct"] = BLEscanBeforeConnect;
# endif
# ifdef ZgatewayRFM69
modules = modules + ZgatewayRFM69;
# endif
# ifdef ZsensorINA226
modules = modules + ZsensorINA226;
# endif
# ifdef ZsensorHCSR501
modules = modules + ZsensorHCSR501;
# endif
# ifdef ZsensorGPIOInput
modules = modules + ZsensorGPIOInput;
# endif
# ifdef ZsensorGPIOKeyCode
modules = modules + ZsensorGPIOKeyCode;
# endif
# ifdef ZsensorGPIOKeyCode
modules = modules + ZsensorGPIOKeyCode;
# endif
# ifdef ZmqttDiscovery
modules = modules + ZmqttDiscovery;
# endif
# ifdef ZactuatorFASTLED
modules = modules + ZactuatorFASTLED;
# endif
# ifdef ZactuatorPWM
modules = modules + ZactuatorPWM;
# endif
# ifdef ZboardM5STACK
M5.Power.begin();
SYSdata["m5-batt-level"] = (int8_t)M5.Power.getBatteryLevel();
@@ -1444,10 +1397,7 @@ void stateMeasures() {
SYSdata["m5-bat-chargecurrent"] = (float)M5.Axp.GetBatChargeCurrent();
SYSdata["m5-aps-voltage"] = (float)M5.Axp.GetAPSVoltage();
# endif
# ifdef ZGatewayRS232
modules = modules + ZGatewayRS232;
# endif
SYSdata["modules"] = modules;
SYSdata.set("modules", modules);
pub(subjectSYStoMQTT, SYSdata);
}
#endif