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.

292 lines
7.7 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. // SENSORS
  11. // -----------------------------------------------------------------------------
  12. #if SENSOR_SUPPORT
  13. void _haSendMagnitude(unsigned char i, JsonObject& config) {
  14. unsigned char type = magnitudeType(i);
  15. config["name"] = getSetting("hostname") + String(" ") + magnitudeTopic(type);
  16. config["platform"] = "mqtt";
  17. config["device_class"] = "sensor";
  18. config["state_topic"] = mqttTopic(magnitudeTopicIndex(i).c_str(), false);
  19. config["unit_of_measurement"] = magnitudeUnits(type);
  20. }
  21. void _haSendMagnitudes() {
  22. for (unsigned char i=0; i<magnitudeCount(); i++) {
  23. String topic = getSetting("haPrefix", HOMEASSISTANT_PREFIX) +
  24. "/sensor/" +
  25. getSetting("hostname") + "_" + String(i) +
  26. "/config";
  27. String output;
  28. if (_haEnabled) {
  29. DynamicJsonBuffer jsonBuffer;
  30. JsonObject& config = jsonBuffer.createObject();
  31. _haSendMagnitude(i, config);
  32. config.printTo(output);
  33. }
  34. mqttSendRaw(topic.c_str(), output.c_str());
  35. mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
  36. }
  37. }
  38. #endif // SENSOR_SUPPORT
  39. // -----------------------------------------------------------------------------
  40. // SWITCHES & LIGHTS
  41. // -----------------------------------------------------------------------------
  42. void _haSendSwitch(unsigned char i, JsonObject& config) {
  43. String name = getSetting("hostname");
  44. if (relayCount() > 1) {
  45. name += String(" #") + String(i);
  46. }
  47. config["name"] = name;
  48. config["platform"] = "mqtt";
  49. if (relayCount()) {
  50. config["state_topic"] = mqttTopic(MQTT_TOPIC_RELAY, i, false);
  51. config["command_topic"] = mqttTopic(MQTT_TOPIC_RELAY, i, true);
  52. config["payload_on"] = String("1");
  53. config["payload_off"] = String("0");
  54. config["availability_topic"] = mqttTopic(MQTT_TOPIC_STATUS, false);
  55. config["payload_available"] = String("1");
  56. config["payload_not_available"] = String("0");
  57. }
  58. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  59. if (i == 0) {
  60. if (lightHasColor()) {
  61. config["brightness_state_topic"] = mqttTopic(MQTT_TOPIC_BRIGHTNESS, false);
  62. config["brightness_command_topic"] = mqttTopic(MQTT_TOPIC_BRIGHTNESS, true);
  63. config["rgb_state_topic"] = mqttTopic(MQTT_TOPIC_COLOR_RGB, false);
  64. config["rgb_command_topic"] = mqttTopic(MQTT_TOPIC_COLOR_RGB, true);
  65. config["color_temp_command_topic"] = mqttTopic(MQTT_TOPIC_MIRED, true);
  66. }
  67. if (lightChannels() > 3) {
  68. config["white_value_state_topic"] = mqttTopic(MQTT_TOPIC_CHANNEL, 3, false);
  69. config["white_value_command_topic"] = mqttTopic(MQTT_TOPIC_CHANNEL, 3, true);
  70. }
  71. }
  72. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  73. }
  74. void _haSendSwitches() {
  75. #if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) || (defined(ITEAD_SLAMPHER))
  76. String type = String("light");
  77. #else
  78. String type = String("switch");
  79. #endif
  80. for (unsigned char i=0; i<relayCount(); i++) {
  81. String topic = getSetting("haPrefix", HOMEASSISTANT_PREFIX) +
  82. "/" + type +
  83. "/" + getSetting("hostname") + "_" + String(i) +
  84. "/config";
  85. String output;
  86. if (_haEnabled) {
  87. DynamicJsonBuffer jsonBuffer;
  88. JsonObject& config = jsonBuffer.createObject();
  89. _haSendSwitch(i, config);
  90. config.printTo(output);
  91. }
  92. mqttSendRaw(topic.c_str(), output.c_str());
  93. mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
  94. }
  95. }
  96. // -----------------------------------------------------------------------------
  97. String _haGetConfig() {
  98. String output;
  99. #if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) || (defined(ITEAD_SLAMPHER))
  100. String type = String("light");
  101. #else
  102. String type = String("switch");
  103. #endif
  104. for (unsigned char i=0; i<relayCount(); i++) {
  105. DynamicJsonBuffer jsonBuffer;
  106. JsonObject& config = jsonBuffer.createObject();
  107. _haSendSwitch(i, config);
  108. output += type + ":\n";
  109. bool first = true;
  110. for (auto kv : config) {
  111. if (first) {
  112. output += " - ";
  113. first = false;
  114. } else {
  115. output += " ";
  116. }
  117. output += kv.key + String(": ") + kv.value.as<String>() + String("\n");
  118. }
  119. output += "\n";
  120. }
  121. #if SENSOR_SUPPORT
  122. for (unsigned char i=0; i<magnitudeCount(); i++) {
  123. DynamicJsonBuffer jsonBuffer;
  124. JsonObject& config = jsonBuffer.createObject();
  125. _haSendMagnitude(i, config);
  126. output += "sensor:\n";
  127. bool first = true;
  128. for (auto kv : config) {
  129. if (first) {
  130. output += " - ";
  131. first = false;
  132. } else {
  133. output += " ";
  134. }
  135. output += kv.key + String(": ") + kv.value.as<String>() + String("\n");
  136. }
  137. output += "\n";
  138. }
  139. #endif
  140. return output;
  141. }
  142. void _haSend() {
  143. // Pending message to send?
  144. if (!_haSendFlag) return;
  145. // Are we connected?
  146. if (!mqttConnected()) return;
  147. DEBUG_MSG_P(PSTR("[HA] Sending autodiscovery MQTT message\n"));
  148. // Send messages
  149. _haSendSwitches();
  150. #if SENSOR_SUPPORT
  151. _haSendMagnitudes();
  152. #endif
  153. _haSendFlag = false;
  154. }
  155. void _haConfigure() {
  156. bool enabled = getSetting("haEnabled", HOMEASSISTANT_ENABLED).toInt() == 1;
  157. _haSendFlag = (enabled != _haEnabled);
  158. _haEnabled = enabled;
  159. _haSend();
  160. }
  161. #if WEB_SUPPORT
  162. void _haWebSocketOnSend(JsonObject& root) {
  163. root["haVisible"] = 1;
  164. root["haPrefix"] = getSetting("haPrefix", HOMEASSISTANT_PREFIX);
  165. root["haEnabled"] = getSetting("haEnabled", HOMEASSISTANT_ENABLED).toInt() == 1;
  166. }
  167. void _haWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  168. if (strcmp(action, "haconfig") == 0) {
  169. String output = _haGetConfig();
  170. output.replace(" ", "&nbsp;");
  171. output.replace("\n", "<br />");
  172. output = String("{\"haConfig\": \"") + output + String("\"}");
  173. wsSend(client_id, output.c_str());
  174. }
  175. }
  176. #endif
  177. #if TERMINAL_SUPPORT
  178. void _haInitCommands() {
  179. settingsRegisterCommand(F("HA.CONFIG"), [](Embedis* e) {
  180. DEBUG_MSG(_haGetConfig().c_str());
  181. DEBUG_MSG_P(PSTR("+OK\n"));
  182. });
  183. settingsRegisterCommand(F("HA.SEND"), [](Embedis* e) {
  184. setSetting("haEnabled", "1");
  185. _haConfigure();
  186. wsSend(_haWebSocketOnSend);
  187. DEBUG_MSG_P(PSTR("+OK\n"));
  188. });
  189. settingsRegisterCommand(F("HA.CLEAR"), [](Embedis* e) {
  190. setSetting("haEnabled", "0");
  191. _haConfigure();
  192. wsSend(_haWebSocketOnSend);
  193. DEBUG_MSG_P(PSTR("+OK\n"));
  194. });
  195. }
  196. #endif
  197. // -----------------------------------------------------------------------------
  198. void haSetup() {
  199. _haConfigure();
  200. #if WEB_SUPPORT
  201. wsOnSendRegister(_haWebSocketOnSend);
  202. wsOnAfterParseRegister(_haConfigure);
  203. wsOnActionRegister(_haWebSocketOnAction);
  204. #endif
  205. // On MQTT connect check if we have something to send
  206. mqttRegister([](unsigned int type, const char * topic, const char * payload) {
  207. if (type == MQTT_CONNECT_EVENT) _haSend();
  208. });
  209. #if TERMINAL_SUPPORT
  210. _haInitCommands();
  211. #endif
  212. }
  213. #endif // HOMEASSISTANT_SUPPORT