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.

115 lines
3.3 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. bool _haSendFlag = false;
  9. // -----------------------------------------------------------------------------
  10. void _haWebSocketOnSend(JsonObject& root) {
  11. root["haVisible"] = 1;
  12. root["haPrefix"] = getSetting("haPrefix", HOMEASSISTANT_PREFIX);
  13. }
  14. void _haSend() {
  15. // Pending message to send?
  16. if (!_haSendFlag) return;
  17. // Are we connected?
  18. if (!mqttConnected()) return;
  19. DEBUG_MSG_P(PSTR("[HA] Sending autodiscovery MQTT message\n"));
  20. String output;
  21. if (_haEnabled) {
  22. DynamicJsonBuffer jsonBuffer;
  23. JsonObject& root = jsonBuffer.createObject();
  24. root["name"] = getSetting("hostname");
  25. root["platform"] = "mqtt";
  26. if (relayCount()) {
  27. root["state_topic"] = mqttGetTopic(MQTT_TOPIC_RELAY, 0, false);
  28. root["command_topic"] = mqttGetTopic(MQTT_TOPIC_RELAY, 0, true);
  29. root["payload_on"] = String("1");
  30. root["payload_off"] = String("0");
  31. root["availability_topic"] = mqttGetTopic(MQTT_TOPIC_STATUS, false);
  32. root["payload_available"] = String("1");
  33. root["payload_not_available"] = String("0");
  34. }
  35. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  36. if (lightHasColor()) {
  37. root["brightness_state_topic"] = mqttGetTopic(MQTT_TOPIC_BRIGHTNESS, false);
  38. root["brightness_command_topic"] = mqttGetTopic(MQTT_TOPIC_BRIGHTNESS, true);
  39. root["rgb_state_topic"] = mqttGetTopic(MQTT_TOPIC_COLOR_RGB, false);
  40. root["rgb_command_topic"] = mqttGetTopic(MQTT_TOPIC_COLOR_RGB, true);
  41. root["color_temp_command_topic"] = mqttGetTopic(MQTT_TOPIC_MIRED, true);
  42. }
  43. if (lightChannels() > 3) {
  44. root["white_value_state_topic"] = mqttGetTopic(MQTT_TOPIC_CHANNEL, 3, false);
  45. root["white_value_command_topic"] = mqttGetTopic(MQTT_TOPIC_CHANNEL, 3, true);
  46. }
  47. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  48. root.printTo(output);
  49. }
  50. #if LIGHT_PROVIDER == LIGHT_PROVIDER_NONE
  51. String component = String("switch");
  52. #else
  53. String component = String("light");
  54. #endif
  55. String topic = getSetting("haPrefix", HOMEASSISTANT_PREFIX) +
  56. "/" + component +
  57. "/" + getSetting("hostname") +
  58. "/config";
  59. mqttSendRaw(topic.c_str(), output.c_str());
  60. mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
  61. _haSendFlag = false;
  62. }
  63. void _haConfigure() {
  64. bool enabled = getSetting("haEnabled", HOMEASSISTANT_ENABLED).toInt() == 1;
  65. _haSendFlag = (enabled != _haEnabled);
  66. _haEnabled = enabled;
  67. _haSend();
  68. }
  69. // -----------------------------------------------------------------------------
  70. void haSetup() {
  71. _haConfigure();
  72. #if WEB_SUPPORT
  73. wsOnSendRegister(_haWebSocketOnSend);
  74. wsOnAfterParseRegister(_haConfigure);
  75. #endif
  76. // On MQTT connect check if we have something to send
  77. mqttRegister([](unsigned int type, const char * topic, const char * payload) {
  78. if (type == MQTT_CONNECT_EVENT) _haSend();
  79. });
  80. }
  81. #endif // HOMEASSISTANT_SUPPORT