Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.9 KiB

  1. /*
  2. HOME ASSISTANT MODULE
  3. Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if HOMEASSISTANT_SUPPORT
  6. #include <ArduinoJson.h>
  7. void haSend(bool add) {
  8. DEBUG_MSG_P(PSTR("[HA] Sending autodiscovery MQTT message\n"));
  9. String output;
  10. if (add) {
  11. DynamicJsonBuffer jsonBuffer;
  12. JsonObject& root = jsonBuffer.createObject();
  13. root["name"] = getSetting("hostname");
  14. root["platform"] = "mqtt";
  15. if (relayCount()) {
  16. root["state_topic"] = getTopic(MQTT_TOPIC_RELAY, 0, false);
  17. root["command_topic"] = getTopic(MQTT_TOPIC_RELAY, 0, true);
  18. root["payload_on"] = String("1");
  19. root["payload_off"] = String("0");
  20. }
  21. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  22. if (lightHasColor()) {
  23. root["brightness_state_topic"] = getTopic(MQTT_TOPIC_BRIGHTNESS, false);
  24. root["brightness_command_topic"] = getTopic(MQTT_TOPIC_BRIGHTNESS, true);
  25. root["rgb_state_topic"] = getTopic(MQTT_TOPIC_COLOR_RGB, false);
  26. root["rgb_command_topic"] = getTopic(MQTT_TOPIC_COLOR_RGB, true);
  27. root["color_temp_command_topic"] = getTopic(MQTT_TOPIC_MIRED, true);
  28. }
  29. if (lightChannels() > 3) {
  30. root["white_value_state_topic"] = getTopic(MQTT_TOPIC_CHANNEL, 3, false);
  31. root["white_value_command_topic"] = getTopic(MQTT_TOPIC_CHANNEL, 3, true);
  32. }
  33. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  34. root.printTo(output);
  35. }
  36. #if LIGHT_PROVIDER == LIGHT_PROVIDER_NONE
  37. String component = String("switch");
  38. #else
  39. String component = String("light");
  40. #endif
  41. String topic = getSetting("haPrefix", HOMEASSISTANT_PREFIX) +
  42. "/" + component +
  43. "/" + getSetting("hostname") +
  44. "/config";
  45. mqttSendRaw(topic.c_str(), output.c_str());
  46. }
  47. #endif // HOMEASSISTANT_SUPPORT