Merge remote-tracking branch 'debsahu/ha-mqtt-discovery'
This commit is contained in:
commit
4e31fa55ae
2 changed files with 44 additions and 0 deletions
|
@ -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)
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue