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.

83 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. #if WEB_SUPPORT
  15. void _alexaWSSend(JsonObject& root) {
  16. root["alexaVisible"] = 1;
  17. root["alexaEnabled"] = getSetting("alexaEnabled", ALEXA_ENABLED).toInt() == 1;
  18. }
  19. #endif
  20. // -----------------------------------------------------------------------------
  21. void alexaConfigure() {
  22. alexa.enable(getSetting("alexaEnabled", ALEXA_ENABLED).toInt() == 1);
  23. }
  24. void alexaSetup() {
  25. // Backwards compatibility
  26. moveSetting("fauxmoEnabled", "alexaEnabled");
  27. // Load & cache settings
  28. alexaConfigure();
  29. #if WEB_SUPPORT
  30. // Websockets
  31. wsRegister(_alexaWSSend);
  32. #endif
  33. unsigned int relays = relayCount();
  34. String hostname = getSetting("hostname");
  35. if (relays == 1) {
  36. alexa.addDevice(hostname.c_str());
  37. } else {
  38. for (unsigned int i=0; i<relays; i++) {
  39. alexa.addDevice((hostname + "_" + i).c_str());
  40. }
  41. }
  42. alexa.onSetState([relays](unsigned char device_id, const char * name, bool state) {
  43. _alexa_change = true;
  44. _alexa_device_id = device_id;
  45. _alexa_state = state;
  46. });
  47. alexa.onGetState([relays](unsigned char device_id, const char * name) {
  48. return relayStatus(device_id);
  49. });
  50. }
  51. void alexaLoop() {
  52. alexa.handle();
  53. if (_alexa_change) {
  54. DEBUG_MSG_P(PSTR("[ALEXA] Device #%d state: %s\n"), _alexa_device_id, _alexa_state ? "ON" : "OFF");
  55. _alexa_change = false;
  56. relayStatus(_alexa_device_id, _alexa_state);
  57. }
  58. }
  59. #endif