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.

84 lines
1.9 KiB

  1. /*
  2. ALEXA MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if ALEXA_SUPPORT
  6. #include <fauxmoESP.h>
  7. fauxmoESP alexa;
  8. // -----------------------------------------------------------------------------
  9. // ALEXA
  10. // -----------------------------------------------------------------------------
  11. bool _alexa_change = false;
  12. unsigned int _alexa_device_id = 0;
  13. bool _alexa_state = false;
  14. void _alexaWebSocketOnSend(JsonObject& root) {
  15. root["alexaVisible"] = 1;
  16. root["alexaEnabled"] = getSetting("alexaEnabled", ALEXA_ENABLED).toInt() == 1;
  17. }
  18. void _alexaConfigure() {
  19. alexa.enable(getSetting("alexaEnabled", ALEXA_ENABLED).toInt() == 1);
  20. }
  21. // -----------------------------------------------------------------------------
  22. void alexaSetup() {
  23. // Backwards compatibility
  24. moveSetting("fauxmoEnabled", "alexaEnabled");
  25. // Load & cache settings
  26. _alexaConfigure();
  27. #if WEB_SUPPORT
  28. // Websockets
  29. wsOnSendRegister(_alexaWebSocketOnSend);
  30. wsOnAfterParseRegister(_alexaConfigure);
  31. #endif
  32. unsigned int relays = relayCount();
  33. String hostname = getSetting("hostname");
  34. if (relays == 1) {
  35. alexa.addDevice(hostname.c_str());
  36. } else {
  37. for (unsigned int i=0; i<relays; i++) {
  38. alexa.addDevice((hostname + "_" + i).c_str());
  39. }
  40. }
  41. alexa.onSetState([relays](unsigned char device_id, const char * name, bool state) {
  42. _alexa_change = true;
  43. _alexa_device_id = device_id;
  44. _alexa_state = state;
  45. });
  46. alexa.onGetState([relays](unsigned char device_id, const char * name) {
  47. return relayStatus(device_id);
  48. });
  49. }
  50. void alexaLoop() {
  51. alexa.handle();
  52. if (_alexa_change) {
  53. DEBUG_MSG_P(PSTR("[ALEXA] Device #%d state: %s\n"), _alexa_device_id, _alexa_state ? "ON" : "OFF");
  54. _alexa_change = false;
  55. relayStatus(_alexa_device_id, _alexa_state);
  56. }
  57. }
  58. #endif