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.

101 lines
3.0 KiB

  1. /*
  2. HOME ASSISTANT MODULE
  3. Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if HOMEASSISTANT_SUPPORT
  6. #include <ArduinoJson.h>
  7. bool _haEnabled = false;
  8. // -----------------------------------------------------------------------------
  9. void _haWebSocketOnSend(JsonObject& root) {
  10. root["haVisible"] = 1;
  11. root["haPrefix"] = getSetting("haPrefix", HOMEASSISTANT_PREFIX);
  12. }
  13. void _haConfigure() {
  14. bool enabled = getSetting("haEnabled", HOMEASSISTANT_ENABLED).toInt() == 1;
  15. if (enabled != _haEnabled) haSend(enabled);
  16. _haEnabled = enabled;
  17. }
  18. // -----------------------------------------------------------------------------
  19. void haSend(bool add) {
  20. DEBUG_MSG_P(PSTR("[HA] Sending autodiscovery MQTT message\n"));
  21. String output;
  22. if (add) {
  23. DynamicJsonBuffer jsonBuffer;
  24. JsonObject& root = jsonBuffer.createObject();
  25. root["name"] = getSetting("hostname");
  26. root["platform"] = "mqtt";
  27. if (relayCount()) {
  28. root["state_topic"] = getTopic(MQTT_TOPIC_RELAY, 0, false);
  29. root["command_topic"] = getTopic(MQTT_TOPIC_RELAY, 0, true);
  30. root["payload_on"] = String("1");
  31. root["payload_off"] = String("0");
  32. root["availability_topic"] = getTopic(MQTT_TOPIC_STATUS, false);
  33. root["payload_available"] = String("1");
  34. root["payload_not_available"] = String("0");
  35. }
  36. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  37. if (lightHasColor()) {
  38. root["brightness_state_topic"] = getTopic(MQTT_TOPIC_BRIGHTNESS, false);
  39. root["brightness_command_topic"] = getTopic(MQTT_TOPIC_BRIGHTNESS, true);
  40. root["rgb_state_topic"] = getTopic(MQTT_TOPIC_COLOR_RGB, false);
  41. root["rgb_command_topic"] = getTopic(MQTT_TOPIC_COLOR_RGB, true);
  42. root["color_temp_command_topic"] = getTopic(MQTT_TOPIC_MIRED, true);
  43. }
  44. if (lightChannels() > 3) {
  45. root["white_value_state_topic"] = getTopic(MQTT_TOPIC_CHANNEL, 3, false);
  46. root["white_value_command_topic"] = getTopic(MQTT_TOPIC_CHANNEL, 3, true);
  47. }
  48. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  49. root.printTo(output);
  50. }
  51. #if LIGHT_PROVIDER == LIGHT_PROVIDER_NONE
  52. String component = String("switch");
  53. #else
  54. String component = String("light");
  55. #endif
  56. String topic = getSetting("haPrefix", HOMEASSISTANT_PREFIX) +
  57. "/" + component +
  58. "/" + getSetting("hostname") +
  59. "/config";
  60. mqttSendRaw(topic.c_str(), output.c_str());
  61. mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
  62. }
  63. void haSetup() {
  64. _haConfigure();
  65. #if WEB_SUPPORT
  66. wsOnSendRegister(_haWebSocketOnSend);
  67. wsOnAfterParseRegister(_haConfigure);
  68. #endif
  69. mqttRegister([](unsigned int type, const char * topic, const char * payload) {
  70. if (type == MQTT_CONNECT_EVENT) haSend(_haEnabled);
  71. });
  72. }
  73. #endif // HOMEASSISTANT_SUPPORT