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.

62 lines
1.7 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() {
  8. DEBUG_MSG_P(PSTR("[HA] Sending autodiscovery MQTT message\n"));
  9. String component = String("light");
  10. DynamicJsonBuffer jsonBuffer;
  11. JsonObject& root = jsonBuffer.createObject();
  12. root["name"] = getSetting("hostname");
  13. root["platform"] = "mqtt";
  14. if (relayCount()) {
  15. root["state_topic"] = getTopic(MQTT_TOPIC_RELAY, 0, false);
  16. root["command_topic"] = getTopic(MQTT_TOPIC_RELAY, 0, true);
  17. }
  18. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  19. if (lightHasColor()) {
  20. root["brightness"] = 1;
  21. root["brightness_state_topic"] = getTopic(MQTT_TOPIC_BRIGHTNESS, false);
  22. root["brightness_command_topic"] = getTopic(MQTT_TOPIC_BRIGHTNESS, true);
  23. root["rgb"] = 1;
  24. root["rgb_state_topic"] = getTopic(MQTT_TOPIC_COLOR, false);
  25. root["rgb_command_topic"] = getTopic(MQTT_TOPIC_COLOR, true);
  26. root["color_temp"] = 1;
  27. root["color_temp_command_topic"] = getTopic(MQTT_TOPIC_MIRED, true);
  28. }
  29. if (lightChannels() > 3) {
  30. root["white_value"] = 1;
  31. root["white_value_state_topic"] = getTopic(MQTT_TOPIC_CHANNEL, 3, false);
  32. root["white_value_command_topic"] = getTopic(MQTT_TOPIC_CHANNEL, 3, true);
  33. }
  34. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  35. String output;
  36. root.printTo(output);
  37. String topic = getSetting("haPrefix", HOMEASSISTANT_PREFIX)
  38. + String("/") + component + "/" + getSetting("hostname");
  39. mqttSendRaw(topic.c_str(), output.c_str());
  40. }
  41. #endif // HOMEASSISTANT_SUPPORT