MQTT: add Homeassistant discovery message
This commit is contained in:
parent
f81326f3ef
commit
d21db10795
55
src/main.cpp
55
src/main.cpp
|
|
@ -34,6 +34,59 @@ void publishClose()
|
|||
mqtt.publish(FLIPDOT_TEXT_TOPIC "/state", Text.c_str());
|
||||
}
|
||||
|
||||
void publishHomeAssistantDiscovery()
|
||||
{
|
||||
auto mac = WiFi.macAddress();
|
||||
mac.replace(":", "_");
|
||||
|
||||
JsonDocument doc;
|
||||
|
||||
JsonObject device = doc["dev"].to<JsonObject>();
|
||||
device["ids"] = mac;
|
||||
device["name"] = "FlipDotDisplay";
|
||||
device["mf"] = "-mf-";
|
||||
device["mdl"] = "-mdl-";
|
||||
device["sw"] = "-sw-";
|
||||
device["sn"] = "-sn-";
|
||||
device["hw"] = "-hw-";
|
||||
|
||||
JsonObject origin = doc["o"].to<JsonObject>();
|
||||
origin["name"] = "Hobbyhimmel";
|
||||
origin["sw"] = "-sw-";
|
||||
origin["url"] = "https://hobbyhimmel.de";
|
||||
|
||||
JsonObject components = doc["cmps"].to<JsonObject>();
|
||||
JsonObject sw = components["FlipDotDisplaySwitch"].to<JsonObject>();
|
||||
sw["unique_id"] = mac + "FlipDotDisplaySwitch";
|
||||
sw["p"] = "switch";
|
||||
sw["command_topic"] = FLIPDOT_SWITCH_TOPIC;
|
||||
sw["state_topic"] = FLIPDOT_SWITCH_TOPIC "/state";
|
||||
sw["payload_on"] = "open";
|
||||
sw["payload_off"] = "close";
|
||||
sw["qos"] = 0;
|
||||
sw["retain"] = false;
|
||||
|
||||
JsonObject txt = components["FlipDotDisplayText"].to<JsonObject>();
|
||||
sw["unique_id"] = mac + "FlipDotDisplayText";
|
||||
sw["p"] = "text";
|
||||
sw["command_topic"] = FLIPDOT_TEXT_TOPIC;
|
||||
sw["state_topic"] = FLIPDOT_TEXT_TOPIC "/state";
|
||||
sw["qos"] = 0;
|
||||
sw["retain"] = false;
|
||||
|
||||
doc["availability_topic "] = FLIPDOT_AVAILABILITY_TOPIC;
|
||||
|
||||
String jsonString;
|
||||
serializeJson(doc, jsonString);
|
||||
|
||||
String discoveryTopic = "homeassistant/device/";
|
||||
discoveryTopic += mac + "/config";
|
||||
|
||||
mqtt.publish(discoveryTopic.c_str(), jsonString.c_str());
|
||||
Serial.print("Homeassistant Discovery was published: ");
|
||||
Serial.println(jsonString);
|
||||
}
|
||||
|
||||
/* ------------ OTA section and variables -------------- */
|
||||
bool OTAStarted;
|
||||
void onOTAStart()
|
||||
|
|
@ -196,6 +249,8 @@ void loop()
|
|||
mqtt.subscribe(FLIPDOT_SWITCH_TOPIC);
|
||||
mqtt.subscribe(FLIPDOT_TEXT_TOPIC);
|
||||
mqtt.publish(FLIPDOT_AVAILABILITY_TOPIC, "available");
|
||||
|
||||
publishHomeAssistantDiscovery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user