Feature/nine additional alexa devices for presets (#2787)
* add 9 further alexa devices for calling presets 1-9
* use preset names from WLED for Alexa preset device names instead of hardcoded names
* update readme and version
* call alexaInit() at end of savePreset() to keep Alexa in sync with the preset IDs and names
* This reverts commit f8db06c7c5.
* change order to configured Alexa WLED name first, preset names afterwards
* fix status of devices when shown within Alexa, i.e. switching one preset on switches others off (except for alexaInvocationName)
* re-add getPresetName() after merge with master
* restore original readme for pull request
* restore original platformio.ini for pull request
* Logic simplification
* Pass string by reference
* Added number of presets check
* fix alexaInit() in case of alexaNumPresets==0
Co-authored-by: Christian Schwinne <dev.aircoookie@gmail.com>
This commit is contained in:
@@ -14,17 +14,25 @@ void onAlexaChange(EspalexaDevice* dev);
|
||||
|
||||
void alexaInit()
|
||||
{
|
||||
if (alexaEnabled && WLED_CONNECTED)
|
||||
{
|
||||
if (espalexaDevice == nullptr) //only init once
|
||||
if (!alexaEnabled || !WLED_CONNECTED) return;
|
||||
|
||||
espalexa.removeAllDevices();
|
||||
// the original configured device for on/off or macros (added first, i.e. index 0)
|
||||
espalexaDevice = new EspalexaDevice(alexaInvocationName, onAlexaChange, EspalexaDeviceType::extendedcolor);
|
||||
espalexa.addDevice(espalexaDevice);
|
||||
|
||||
// up to 9 devices (added second, third, ... i.e. index 1 to 9) serve for switching on up to nine presets (preset IDs 1 to 9 in WLED),
|
||||
// names are identical as the preset names, switching off can be done by switching off any of them
|
||||
if (alexaNumPresets) {
|
||||
String name = "";
|
||||
for (byte presetIndex = 1; presetIndex <= alexaNumPresets; presetIndex++)
|
||||
{
|
||||
espalexaDevice = new EspalexaDevice(alexaInvocationName, onAlexaChange, EspalexaDeviceType::extendedcolor);
|
||||
espalexa.addDevice(espalexaDevice);
|
||||
espalexa.begin(&server);
|
||||
} else {
|
||||
espalexaDevice->setName(alexaInvocationName);
|
||||
if (!getPresetName(presetIndex, name)) break; // no more presets
|
||||
EspalexaDevice* dev = new EspalexaDevice(name.c_str(), onAlexaChange, EspalexaDeviceType::extendedcolor);
|
||||
espalexa.addDevice(dev);
|
||||
}
|
||||
}
|
||||
espalexa.begin(&server);
|
||||
}
|
||||
|
||||
void handleAlexa()
|
||||
@@ -35,20 +43,34 @@ void handleAlexa()
|
||||
|
||||
void onAlexaChange(EspalexaDevice* dev)
|
||||
{
|
||||
EspalexaDeviceProperty m = espalexaDevice->getLastChangedProperty();
|
||||
EspalexaDeviceProperty m = dev->getLastChangedProperty();
|
||||
|
||||
if (m == EspalexaDeviceProperty::on)
|
||||
{
|
||||
if (!macroAlexaOn)
|
||||
if (dev->getId() == 0) // Device 0 is for on/off or macros
|
||||
{
|
||||
if (bri == 0)
|
||||
if (!macroAlexaOn)
|
||||
{
|
||||
bri = briLast;
|
||||
stateUpdated(CALL_MODE_ALEXA);
|
||||
if (bri == 0)
|
||||
{
|
||||
bri = briLast;
|
||||
stateUpdated(CALL_MODE_ALEXA);
|
||||
}
|
||||
} else
|
||||
{
|
||||
applyPreset(macroAlexaOn, CALL_MODE_ALEXA);
|
||||
if (bri == 0) dev->setValue(briLast); //stop Alexa from complaining if macroAlexaOn does not actually turn on
|
||||
}
|
||||
} else {
|
||||
applyPreset(macroAlexaOn, CALL_MODE_ALEXA);
|
||||
if (bri == 0) espalexaDevice->setValue(briLast); //stop Alexa from complaining if macroAlexaOn does not actually turn on
|
||||
} else // switch-on behavior for preset devices
|
||||
{
|
||||
// turn off other preset devices
|
||||
for (byte i = 1; i < espalexa.getDeviceCount(); i++)
|
||||
{
|
||||
if (i == dev->getId()) continue;
|
||||
espalexa.getDevice(i)->setValue(0); // turn off other presets
|
||||
}
|
||||
|
||||
applyPreset(dev->getId(), CALL_MODE_ALEXA); // in alexaInit() preset 1 device was added second (index 1), preset 2 third (index 2) etc.
|
||||
}
|
||||
} else if (m == EspalexaDeviceProperty::off)
|
||||
{
|
||||
@@ -60,20 +82,25 @@ void onAlexaChange(EspalexaDevice* dev)
|
||||
bri = 0;
|
||||
stateUpdated(CALL_MODE_ALEXA);
|
||||
}
|
||||
} else {
|
||||
} else
|
||||
{
|
||||
applyPreset(macroAlexaOff, CALL_MODE_ALEXA);
|
||||
if (bri != 0) espalexaDevice->setValue(0); //stop Alexa from complaining if macroAlexaOff does not actually turn off
|
||||
// below for loop stops Alexa from complaining if macroAlexaOff does not actually turn off
|
||||
}
|
||||
for (byte i = 0; i < espalexa.getDeviceCount(); i++)
|
||||
{
|
||||
espalexa.getDevice(i)->setValue(0);
|
||||
}
|
||||
} else if (m == EspalexaDeviceProperty::bri)
|
||||
{
|
||||
bri = espalexaDevice->getValue();
|
||||
bri = dev->getValue();
|
||||
stateUpdated(CALL_MODE_ALEXA);
|
||||
} else //color
|
||||
{
|
||||
if (espalexaDevice->getColorMode() == EspalexaColorMode::ct) //shade of white
|
||||
if (dev->getColorMode() == EspalexaColorMode::ct) //shade of white
|
||||
{
|
||||
byte rgbw[4];
|
||||
uint16_t ct = espalexaDevice->getCt();
|
||||
uint16_t ct = dev->getCt();
|
||||
if (!ct) return;
|
||||
uint16_t k = 1000000 / ct; //mireds to kelvin
|
||||
|
||||
@@ -94,7 +121,7 @@ void onAlexaChange(EspalexaDevice* dev)
|
||||
}
|
||||
strip.setColor(0, RGBW32(rgbw[0], rgbw[1], rgbw[2], rgbw[3]));
|
||||
} else {
|
||||
uint32_t color = espalexaDevice->getRGB();
|
||||
uint32_t color = dev->getRGB();
|
||||
strip.setColor(0, color);
|
||||
}
|
||||
stateUpdated(CALL_MODE_ALEXA);
|
||||
|
||||
Reference in New Issue
Block a user