Skip to content

Commit

Permalink
Update mqtt_task.cpp - added Home Assistant discovery schema (#240)
Browse files Browse the repository at this point in the history
now the split flap will automatically add itself to home assistant as a
text entity.
  • Loading branch information
Johnr24 authored Oct 5, 2024
1 parent edee41a commit 938dfbc
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions arduino/splitflap/esp32/splitflap/mqtt_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ void MQTTTask::connectMQTT() {
if (mqtt_client_.connect(HOSTNAME "-" MQTT_USER, MQTT_USER, MQTT_PASSWORD)) {
logger_.log("MQTT connected");
mqtt_client_.subscribe(MQTT_COMMAND_TOPIC);
char buf[256];
snprintf(buf, sizeof(buf), "{\"name\": \"%s\", \"command_topic\": \"%s\", \"state_topic\": \"%s\", \"unique_id\": \"%s\"}", HOSTNAME, MQTT_COMMAND_TOPIC, MQTT_COMMAND_TOPIC, HOSTNAME);
mqtt_client_.publish("homeassistant/text/splitflap/config", buf);
logger_.log("Published MQTT discovery message");
} else {
snprintf(buf, sizeof(buf), "MQTT failed rc=%d will try again in 5 seconds", mqtt_client_.state());
logger_.log(buf);
Expand All @@ -76,4 +80,5 @@ void MQTTTask::run() {
delay(1);
}
}

#endif

0 comments on commit 938dfbc

Please sign in to comment.