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.

297 lines
7.9 KiB

6 years ago
6 years ago
  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.set("platform", "mqtt");
  17. config.set("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.set("name", name);
  48. config.set("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. bool _haWebSocketOnReceive(const char * key, JsonVariant& value) {
  163. return (strncmp(key, "ha", 2) == 0);
  164. }
  165. void _haWebSocketOnSend(JsonObject& root) {
  166. root["haVisible"] = 1;
  167. root["haPrefix"] = getSetting("haPrefix", HOMEASSISTANT_PREFIX);
  168. root["haEnabled"] = getSetting("haEnabled", HOMEASSISTANT_ENABLED).toInt() == 1;
  169. }
  170. void _haWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  171. if (strcmp(action, "haconfig") == 0) {
  172. String output = _haGetConfig();
  173. output.replace(" ", "&nbsp;");
  174. output.replace("\n", "<br />");
  175. output = String("{\"haConfig\": \"") + output + String("\"}");
  176. wsSend(client_id, output.c_str());
  177. }
  178. }
  179. #endif
  180. #if TERMINAL_SUPPORT
  181. void _haInitCommands() {
  182. settingsRegisterCommand(F("HA.CONFIG"), [](Embedis* e) {
  183. DEBUG_MSG(_haGetConfig().c_str());
  184. DEBUG_MSG_P(PSTR("+OK\n"));
  185. });
  186. settingsRegisterCommand(F("HA.SEND"), [](Embedis* e) {
  187. setSetting("haEnabled", "1");
  188. _haConfigure();
  189. wsSend(_haWebSocketOnSend);
  190. DEBUG_MSG_P(PSTR("+OK\n"));
  191. });
  192. settingsRegisterCommand(F("HA.CLEAR"), [](Embedis* e) {
  193. setSetting("haEnabled", "0");
  194. _haConfigure();
  195. wsSend(_haWebSocketOnSend);
  196. DEBUG_MSG_P(PSTR("+OK\n"));
  197. });
  198. }
  199. #endif
  200. // -----------------------------------------------------------------------------
  201. void haSetup() {
  202. _haConfigure();
  203. #if WEB_SUPPORT
  204. wsOnSendRegister(_haWebSocketOnSend);
  205. wsOnAfterParseRegister(_haConfigure);
  206. wsOnActionRegister(_haWebSocketOnAction);
  207. wsOnReceiveRegister(_haWebSocketOnReceive);
  208. #endif
  209. // On MQTT connect check if we have something to send
  210. mqttRegister([](unsigned int type, const char * topic, const char * payload) {
  211. if (type == MQTT_CONNECT_EVENT) _haSend();
  212. });
  213. #if TERMINAL_SUPPORT
  214. _haInitCommands();
  215. #endif
  216. }
  217. #endif // HOMEASSISTANT_SUPPORT