diff --git a/code/espurna/alexa.ino b/code/espurna/alexa.ino index 81815d8b..f1d552a3 100644 --- a/code/espurna/alexa.ino +++ b/code/espurna/alexa.ino @@ -11,13 +11,13 @@ Copyright (C) 2016-2018 by Xose PĂ©rez #include fauxmoESP alexa; -struct AlexaDevChange { - AlexaDevChange(unsigned char device_id, bool state) : device_id(device_id), state(state) {}; - unsigned char device_id = 0; - bool state = false; -}; #include -static std::queue _alexa_dev_changes; +typedef struct { + unsigned char device_id; + bool state; + unsigned char value; +} alexa_queue_element_t; +static std::queue _alexa_queue; // ----------------------------------------------------------------------------- // ALEXA @@ -29,15 +29,19 @@ bool _alexaWebSocketOnReceive(const char * key, JsonVariant& value) { void _alexaWebSocketOnSend(JsonObject& root) { root["alexaVisible"] = 1; - root["alexaEnabled"] = getSetting("alexaEnabled", ALEXA_ENABLED).toInt() == 1; + root["alexaEnabled"] = alexaEnabled(); } void _alexaConfigure() { - alexa.enable(getSetting("alexaEnabled", ALEXA_ENABLED).toInt() == 1); + alexa.enable(wifiConnected() && alexaEnabled()); } // ----------------------------------------------------------------------------- +bool alexaEnabled() { + return (getSetting("alexaEnabled", ALEXA_ENABLED).toInt() == 1); +} + void alexaSetup() { // Backwards compatibility @@ -46,32 +50,56 @@ void alexaSetup() { // Load & cache settings _alexaConfigure(); - #if WEB_SUPPORT + // Uses hostname as base name for all devices + // TODO: use custom switch name when available + String hostname = getSetting("hostname"); + + // Lights + #if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT + + // Global switch + alexa.addDevice(hostname.c_str()); + + // For each channel + for (unsigned char i = 1; i <= lightChannels(); i++) { + alexa.addDevice((hostname + " " + i).c_str()); + } + + // Relays + #else + + unsigned int relays = relayCount(); + if (relays == 1) { + alexa.addDevice(hostname.c_str()); + } else { + for (unsigned int i=1; i<=relays; i++) { + alexa.addDevice((hostname + " " + i).c_str()); + } + } - // Websockets + #endif + + // Websockets + #if WEB_SUPPORT wsOnSendRegister(_alexaWebSocketOnSend); wsOnAfterParseRegister(_alexaConfigure); wsOnReceiveRegister(_alexaWebSocketOnReceive); - #endif - unsigned int relays = relayCount(); - String hostname = getSetting("hostname"); - if (relays == 1) { - alexa.addDevice(hostname.c_str()); - } else { - for (unsigned int i=0; i