/* OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker Send and receiving command by MQTT This is the Home Assistant mqtt Discovery addon. Copyright: (c) Rafal Herok This file is part of OpenMQTTGateway. OpenMQTTGateway is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenMQTTGateway is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "User_config.h" #ifdef ZmqttDiscovery String getMacAddress() { uint8_t baseMac[6]; char baseMacChr[13] = {0}; #if defined(ESP8266) WiFi.macAddress(baseMac); sprintf(baseMacChr, "%02X%02X%02X%02X%02X%02X", baseMac[0], baseMac[1], baseMac[2], baseMac[3], baseMac[4], baseMac[5]); #elif defined(ESP32) esp_read_mac(baseMac, ESP_MAC_WIFI_STA); sprintf(baseMacChr, "%02X%02X%02X%02X%02X%02X", baseMac[0], baseMac[1], baseMac[2], baseMac[3], baseMac[4], baseMac[5]); #else sprintf(baseMacChr, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); #endif return String(baseMacChr); } String getUniqueId(String name, String sufix) { String uniqueId = (String)getMacAddress() + name + sufix; return String(uniqueId); } void createDiscovery(char * sensor_type, char * state_topic, char * s_name, char * unique_id, char * availability_topic, char * device_class, char * value_template, char * payload_on, char * payload_off, char * unit_of_meas, int off_delay, char * payload_available, char * payload_not_avalaible, bool child_device , char * command_topic) { const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(14) + JSON_OBJECT_SIZE(5)+ JSON_ARRAY_SIZE(1); StaticJsonBuffer jsonBuffer; JsonObject &sensor = jsonBuffer.createObject(); sensor.set("stat_t", state_topic); //state_topic sensor.set("name", s_name); //name sensor.set("uniq_id", unique_id); //unique_id if (device_class[0]) sensor.set("dev_cla", device_class); //device_class if (value_template[0]) sensor.set("val_tpl", value_template); //value_template if (payload_on[0]) sensor.set("pl_on", payload_on); // payload_on if (payload_off[0]) sensor.set("pl_off", payload_off); //payload_off if (unit_of_meas[0]) sensor.set("unit_of_meas", unit_of_meas); //unit_of_measurement*/ if (off_delay != 0) sensor.set("off_delay", off_delay); //off_delay if (payload_available[0]) sensor.set("pl_avail", payload_available); // payload_on if (payload_not_avalaible[0]) sensor.set("pl_not_avail", payload_not_avalaible); //payload_off if (command_topic[0]) sensor.set("cmd_t", command_topic); //command_topic /*if (strcmp(s_name, Gateway_Name) == 0){ JsonArray &json_attributes = sensor.createNestedArray("json_attributes"); json_attributes.add("uptime"); json_attributes.add("freemem"); json_attributes.add("rssi"); json_attributes.add("SSID"); }*/ if (child_device){ const int JSON_MSG_CALC_BUFFER = JSON_OBJECT_SIZE(5) + JSON_ARRAY_SIZE(2); StaticJsonBuffer jsonDeviceBuffer; JsonObject &device = jsonDeviceBuffer.createObject(); device.set("name", Gateway_Name); device.set("manufacturer", DEVICEMANUFACTURER); device.set("sw_version", OMG_VERSION); JsonArray &identifiers = device.createNestedArray("identifiers"); identifiers.add(getMacAddress()); sensor.set("device", device); //device sensor is connected to } String topic = String(discovery_Topic) + "/" + String(sensor_type) + "/" + String(unique_id) + "/config"; pub_custom_topic((char *)topic.c_str(), sensor); } void pubMqttDiscovery() { trc(F("omgStatusDiscovery")); createDiscovery("binary_sensor", //set sensorType will_Topic, Gateway_Name, (char *)getUniqueId("", "").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 optimistic,retain, off_delay Gateway_AnnouncementMsg, will_Message,true,"" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic, state on, state off ); #ifdef ZsensorBME280 #define BMEparametersCount 6 trc(F("bme280Discovery")); char * BMEsensor[BMEparametersCount][8] = { {"sensor", "tempc", "bme","temperature","{{ value_json.tempc }}","", "", "°C"} , {"sensor", "tempf", "bme","temperature","{{ value_json.tempf }}","", "", "°F"} , {"sensor", "pa", "bme","","{{ float(value_json.pa) * 0.01 }}","", "", "hPa"} , {"sensor", "hum", "bme","humidity","{{ value_json.hum }}","", "", "%"} , {"sensor", "altim", "bme","","{{ value_json.altim }}","", "", "m"} , {"sensor", "altift", "bme","","{{ value_json.altift }}","", "", "ft"} //component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement }; for (int i=0;i