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.

68 lines
1.5 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.onSetState([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. alexa.onGetState([relays](unsigned char device_id, const char * name) {
  36. return relayStatus(device_id);
  37. });
  38. }
  39. void alexaLoop() {
  40. alexa.handle();
  41. if (_alexa_change) {
  42. DEBUG_MSG_P(PSTR("[ALEXA] Device #%d state: %s\n"), _alexa_device_id, _alexa_state ? "ON" : "OFF");
  43. _alexa_change = false;
  44. relayStatus(_alexa_device_id, _alexa_state);
  45. }
  46. }
  47. #endif