Added MQTT Discovery

#define MQTT_HOME_ASSISTANT_SUPPORT // If defined, use AMQTT and select
Tools -> IwIP Variant -> Higher Bandwidth
This commit is contained in:
debsahu 2018-04-08 00:03:39 -04:00
parent 227afa387e
commit 68fc2bce4c
2 changed files with 44 additions and 0 deletions

View file

@ -15,6 +15,7 @@ const char HOSTNAME[] = "McLighting01"; // Friedly hostname
//#define ENABLE_MQTT // If defined, enable MQTT client code, see: https://github.com/toblum/McLighting/wiki/MQTT-API //#define ENABLE_MQTT // If defined, enable MQTT client code, see: https://github.com/toblum/McLighting/wiki/MQTT-API
#define ENABLE_HOMEASSISTANT // If defined, enable Homeassistant integration, ENABLE_MQTT must be active #define ENABLE_HOMEASSISTANT // If defined, enable Homeassistant integration, ENABLE_MQTT must be active
#define ENABLE_BUTTON // If defined, enable button handling code, see: https://github.com/toblum/McLighting/wiki/Button-control #define ENABLE_BUTTON // If defined, enable button handling code, see: https://github.com/toblum/McLighting/wiki/Button-control
#define MQTT_HOME_ASSISTANT_SUPPORT // If defined, use AMQTT and select Tools -> IwIP Variant -> Higher Bandwidth
#if defined(USE_NEOANIMATIONFX) and defined(USE_WS2812FX) #if defined(USE_NEOANIMATIONFX) and defined(USE_WS2812FX)

View file

@ -1022,6 +1022,27 @@ void checkForRequests() {
#ifdef ENABLE_HOMEASSISTANT #ifdef ENABLE_HOMEASSISTANT
ha_send_data.detach(); ha_send_data.detach();
mqtt_client.subscribe(mqtt_ha_state_in.c_str(), qossub); mqtt_client.subscribe(mqtt_ha_state_in.c_str(), qossub);
#ifdef MQTT_HOME_ASSISTANT_SUPPORT
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(strip.getModeCount()) + JSON_OBJECT_SIZE(11));
JsonObject& json = jsonBuffer.createObject();
json["name"] = HOSTNAME;
json["platform"] = "mqtt_json";
json["state_topic"] = mqtt_ha_state_out;
json["command_topic"] = mqtt_ha_state_in;
json["on_command_type"] = "first";
json["brightness"] = "true";
json["rgb"] = "true";
json["optimistic"] = "false";
json["color_temp"] = "true";
json["effect"] = "true";
JsonArray& effect_list = json.createNestedArray("effect_list");
for (uint8_t i = 0; i < strip.getModeCount(); i++) {
effect_list.add(strip.getModeName(i));
}
char buffer[json.measureLength() + 1];
json.printTo(buffer, sizeof(buffer));
mqtt_client.publish(String("homeassistant/light/" + String(HOSTNAME) + "/config").c_str(), buffer, true);
#endif
#endif #endif
DBG_OUTPUT_PORT.printf("MQTT topic in: %s\n", mqtt_intopic); DBG_OUTPUT_PORT.printf("MQTT topic in: %s\n", mqtt_intopic);
@ -1081,6 +1102,28 @@ void checkForRequests() {
ha_send_data.detach(); ha_send_data.detach();
uint16_t packetIdSub2 = amqttClient.subscribe((char *)mqtt_ha_state_in.c_str(), qossub); uint16_t packetIdSub2 = amqttClient.subscribe((char *)mqtt_ha_state_in.c_str(), qossub);
DBG_OUTPUT_PORT.printf("Subscribing at QoS %d, packetId: ", qossub); DBG_OUTPUT_PORT.println(packetIdSub2); DBG_OUTPUT_PORT.printf("Subscribing at QoS %d, packetId: ", qossub); DBG_OUTPUT_PORT.println(packetIdSub2);
#ifdef MQTT_HOME_ASSISTANT_SUPPORT
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(strip.getModeCount()) + JSON_OBJECT_SIZE(11));
JsonObject& json = jsonBuffer.createObject();
json["name"] = HOSTNAME;
json["platform"] = "mqtt_json";
json["state_topic"] = mqtt_ha_state_out;
json["command_topic"] = mqtt_ha_state_in;
json["on_command_type"] = "first";
json["brightness"] = "true";
json["rgb"] = "true";
json["optimistic"] = "false";
json["color_temp"] = "true";
json["effect"] = "true";
JsonArray& effect_list = json.createNestedArray("effect_list");
for (uint8_t i = 0; i < strip.getModeCount(); i++) {
effect_list.add(strip.getModeName(i));
}
char buffer[json.measureLength() + 1];
json.printTo(buffer, sizeof(buffer));
DBG_OUTPUT_PORT.println(buffer);
amqttClient.publish(String("homeassistant/light/" + String(HOSTNAME) + "/config").c_str(), qospub, true, buffer);
#endif
#endif #endif
} }