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.

61 lines
1.4 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 alexaConfigure() {
  15. alexa.enable(getSetting("alexaEnabled", ALEXA_ENABLED).toInt() == 1);
  16. }
  17. void alexaSetup() {
  18. // Backwards compatibility
  19. moveSetting("fauxmoEnabled", "alexaEnabled");
  20. alexaConfigure();
  21. unsigned int relays = relayCount();
  22. String hostname = getSetting("hostname");
  23. if (relays == 1) {
  24. alexa.addDevice(hostname.c_str());
  25. } else {
  26. for (unsigned int i=0; i<relays; i++) {
  27. alexa.addDevice((hostname + "_" + i).c_str());
  28. }
  29. }
  30. alexa.onMessage([relays](unsigned char device_id, const char * name, bool state) {
  31. _alexa_change = true;
  32. _alexa_device_id = device_id;
  33. _alexa_state = state;
  34. });
  35. }
  36. void alexaLoop() {
  37. alexa.handle();
  38. if (_alexa_change) {
  39. DEBUG_MSG_P(PSTR("[ALEXA] Device #%d state: %s\n"), _alexa_device_id, _alexa_state ? "ON" : "OFF");
  40. _alexa_change = false;
  41. relayStatus(_alexa_device_id, _alexa_state);
  42. }
  43. }
  44. #endif