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.

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