Skip to content

Commit

Permalink
HASS gateway Discovery improvements (#792)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
1technophile committed Nov 4, 2020
1 parent cabc869 commit 9a66c9e
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 104 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions docs/integrate/home_assistant.md
@@ -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

Expand Down
8 changes: 8 additions & 0 deletions docs/use/ble.md
Expand Up @@ -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.
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion docs/use/gateway.md
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion main/ZgatewayBT.ino
Expand Up @@ -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
};

Expand Down
94 changes: 78 additions & 16 deletions main/ZmqttDiscovery.ino
Expand Up @@ -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");
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 9a66c9e

Please sign in to comment.