diff --git a/src/main.cpp b/src/main.cpp index 35a7193..dda2770 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,55 +36,67 @@ void publishClose() void publishHomeAssistantDiscovery() { - auto mac = WiFi.macAddress(); - mac.replace(":", "_"); + String uid = WiFi.macAddress(); + uid.replace(":", ""); + uid.toLowerCase(); + uid = String("0x") + uid; JsonDocument doc; + String jsonString; + String discoveryTopic; - JsonObject device = doc["dev"].to(); - device["ids"] = mac; + doc["name"] = "FlipDotDisplayText"; + doc["unique_id"] = uid + "Text"; + doc["command_topic"] = FLIPDOT_TEXT_TOPIC; + doc["state_topic"] = FLIPDOT_TEXT_TOPIC "/state"; + doc["qos"] = 0; + doc["retain"] = false; + JsonObject device = doc["device"].to(); + JsonArray identifiers = device["identifiers"].to(); + identifiers.add(uid); device["name"] = "FlipDotDisplay"; - device["mf"] = "-mf-"; - device["mdl"] = "-mdl-"; - device["sw"] = "-sw-"; - device["sn"] = "-sn-"; - device["hw"] = "-hw-"; - - JsonObject origin = doc["o"].to(); - origin["name"] = "Hobbyhimmel"; - origin["sw"] = "-sw-"; - origin["url"] = "https://hobbyhimmel.de"; - - JsonObject components = doc["cmps"].to(); - JsonObject sw = components["FlipDotDisplaySwitch"].to(); - 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(); - 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; - + device["manufacturer"] = "Hobbyhimmel"; + device["model"] = "FlipDot Hanover"; doc["availability_topic "] = FLIPDOT_AVAILABILITY_TOPIC; - String jsonString; serializeJson(doc, jsonString); - String discoveryTopic = "homeassistant/device/"; - discoveryTopic += mac + "/config"; - + discoveryTopic = "homeassistant/text/"; + discoveryTopic += uid + "/text/config"; mqtt.publish(discoveryTopic.c_str(), jsonString.c_str()); - Serial.print("Homeassistant Discovery was published: "); - Serial.println(jsonString); + + Serial.println(String("\n\nHomeassistant Discovery was published: ") + jsonString); + Serial.println(String("to: ") + discoveryTopic + String("\n\n")); + + + doc.clear(); + + doc["name"] = "FlipDotDisplaySwitch"; + doc["unique_id"] = uid + "Switch"; + doc["command_topic"] = FLIPDOT_SWITCH_TOPIC; + doc["state_topic"] = FLIPDOT_SWITCH_TOPIC "/state"; + doc["payload_on"] = "open"; + doc["payload_off"] = "close"; + doc["state_on"] = "open"; + doc["state_off"] = "close"; + doc["qos"] = 0; + doc["retain"] = false; + device = doc["device"].to(); + identifiers = device["identifiers"].to(); + identifiers.add(uid); + device["name"] = "FlipDotDisplay"; + device["manufacturer"] = "Hobbyhimmel"; + device["model"] = "FlipDot Hanover"; + doc["availability_topic "] = FLIPDOT_AVAILABILITY_TOPIC; + + serializeJson(doc, jsonString); + + discoveryTopic = "homeassistant/switch/"; + discoveryTopic += uid + "/switch/config"; + mqtt.publish(discoveryTopic.c_str(), jsonString.c_str()); + + Serial.println(String("\n\nHomeassistant Discovery was published: ") + jsonString); + Serial.println(String("to: ") + discoveryTopic + String("\n\n")); } /* ------------ OTA section and variables -------------- */ @@ -188,6 +200,7 @@ void setup() /* prepare MQTT client */ mqtt.setServer(MQTT_SERVER, 1883); mqtt.setCallback(mqttCallback); + mqtt.setBufferSize(512); } void loop() @@ -412,7 +425,7 @@ void mqttCallback(char *topic, unsigned char *message, unsigned int length) /* Convert all char* to Strings for simpler handling */ for (int i = 0; i < length; i++) { - MessageString += message[i]; + MessageString += (char) message[i]; } String ToppicString(topic);