From e6714b21f92650b484666b36c124b54d0d321a57 Mon Sep 17 00:00:00 2001 From: LordGrey <48840279+Lord-Grey@users.noreply.github.com> Date: Tue, 21 May 2024 19:37:43 +0200 Subject: [PATCH] Fix Philip Hue APIv2 support without Entertainment group defined (#1743) --- CHANGELOG.md | 1 + .../js/wizards/LedDevice_philipshue.js | 20 +++++++++----- libsrc/leddevice/LedDeviceWrapper.cpp | 2 +- .../leddevice/dev_net/LedDevicePhilipsHue.cpp | 26 +++++++++---------- libsrc/leddevice/dev_net/ProviderRestApi.cpp | 12 ++++++++- 5 files changed, 40 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 755775513..4ea84fc11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Workaround to address Web UI keeps forcing browser to download the html instead (#1692) - Fixed: Kodi Color Calibration, Refactor Wizards (#1674) - Fixed: Token Dialog not closing +- Fixed: Philip Hue APIv2 support without Entertainment group defined (#1742) **JSON-API** - Refactored JSON-API to ensure consistent authorization behaviour across sessions and single requests with token authorization. diff --git a/assets/webconfig/js/wizards/LedDevice_philipshue.js b/assets/webconfig/js/wizards/LedDevice_philipshue.js index 92f1c1737..bfc33bd8b 100644 --- a/assets/webconfig/js/wizards/LedDevice_philipshue.js +++ b/assets/webconfig/js/wizards/LedDevice_philipshue.js @@ -502,7 +502,9 @@ const philipshueWizard = (() => { let serviceID; if (isAPIv2Ready) { - serviceID = lightLocation.service.rid; + if (lightLocation) { + serviceID = lightLocation.service.rid; + } } if (position.startsWith("entertainment")) { @@ -531,7 +533,7 @@ const philipshueWizard = (() => { // Layout per manual settings let maxSegments = 1; - if (isAPIv2Ready) { + if (isAPIv2Ready && serviceID) { const service = hueEntertainmentServices.find(service => service.id === serviceID); maxSegments = service.segments.max_segments; } @@ -593,10 +595,10 @@ const philipshueWizard = (() => { d.enableAttempts = parseInt(conf_editor.getEditor("root.generalOptions.enableAttempts").getValue()); d.enableAttemptsInterval = parseInt(conf_editor.getEditor("root.generalOptions.enableAttemptsInterval").getValue()); - d.useEntertainmentAPI = isEntertainmentReady; + d.useEntertainmentAPI = isEntertainmentReady && (d.groupId !== ""); d.useAPIv2 = isAPIv2Ready; - if (isEntertainmentReady) { + if (d.useEntertainmentAPI) { d.hardwareLedCount = channelNumber; if (window.serverConfig.device.type !== d.type) { //smoothing on, if new device @@ -803,12 +805,18 @@ const philipshueWizard = (() => { "lightPosBottomLeft112", "lightPosBottomLeftNewMid", "lightPosBottomLeft121" ]; - if (isEntertainmentReady) { + if (isEntertainmentReady && hueEntertainmentConfigs.length > 0) { lightOptions.unshift("entertainment_center"); lightOptions.unshift("entertainment"); } else { lightOptions.unshift("disabled"); - groupLights = Object.keys(hueLights); + if (isAPIv2Ready) { + for (const light in hueLights) { + groupLights.push(hueLights[light].id); + } + } else { + groupLights = Object.keys(hueLights); + } } $('.lidsb').html(""); diff --git a/libsrc/leddevice/LedDeviceWrapper.cpp b/libsrc/leddevice/LedDeviceWrapper.cpp index 838a5b37b..535320745 100644 --- a/libsrc/leddevice/LedDeviceWrapper.cpp +++ b/libsrc/leddevice/LedDeviceWrapper.cpp @@ -65,7 +65,7 @@ void LedDeviceWrapper::createLedDevice(const QJsonObject& config) connect(thread, &QThread::started, _ledDevice, &LedDevice::start); // further signals - connect(this, &LedDeviceWrapper::updateLeds, _ledDevice, &LedDevice::updateLeds, Qt::QueuedConnection); + connect(this, &LedDeviceWrapper::updateLeds, _ledDevice, &LedDevice::updateLeds, Qt::BlockingQueuedConnection); connect(this, &LedDeviceWrapper::switchOn, _ledDevice, &LedDevice::switchOn, Qt::BlockingQueuedConnection); connect(this, &LedDeviceWrapper::switchOff, _ledDevice, &LedDevice::switchOff, Qt::BlockingQueuedConnection); diff --git a/libsrc/leddevice/dev_net/LedDevicePhilipsHue.cpp b/libsrc/leddevice/dev_net/LedDevicePhilipsHue.cpp index f5f8d24b9..e3df5c7de 100644 --- a/libsrc/leddevice/dev_net/LedDevicePhilipsHue.cpp +++ b/libsrc/leddevice/dev_net/LedDevicePhilipsHue.cpp @@ -895,6 +895,7 @@ void LedDevicePhilipsHueBridge::setBridgeDetails(const QJsonDocument &doc, bool log( "API-Version", "%u.%u.%u", _api_major, _api_minor, _api_patch ); log( "API v2 ready", "%s", _isAPIv2Ready ? "Yes" : "No" ); log( "Entertainment ready", "%s", _isHueEntertainmentReady ? "Yes" : "No" ); + log( "Use Entertainment API", "%s", _useEntertainmentAPI ? "Yes" : "No" ); log( "DIYHue", "%s", _isDiyHue ? "Yes" : "No" ); } } @@ -1799,11 +1800,11 @@ bool LedDevicePhilipsHue::init(const QJsonObject &deviceConfig) if (LedDevicePhilipsHueBridge::init(_devConfig)) { - log( "Off on Black", "%s", _switchOffOnBlack ? "Yes" : "No" ); - log( "Brightness Factor", "%f", _brightnessFactor ); - log( "Transition Time", "%d", _transitionTime ); - log( "Restore Original State", "%s", _isRestoreOrigState ? "Yes" : "No" ); - log( "Use Hue Entertainment API", "%s", _useEntertainmentAPI ? "Yes" : "No" ); + log("Off on Black", "%s", _switchOffOnBlack ? "Yes" : "No" ); + log("Brightness Factor", "%f", _brightnessFactor ); + log("Transition Time", "%d", _transitionTime ); + log("Restore Original State", "%s", _isRestoreOrigState ? "Yes" : "No" ); + log("Use Hue Entertainment API", "%s", _useEntertainmentAPI ? "Yes" : "No" ); log("Brightness Threshold", "%f", _blackLevel); log("CandyGamma", "%s", _candyGamma ? "Yes" : "No" ); log("Time powering off when black", "%s", _onBlackTimeToPowerOff ? "Yes" : "No" ); @@ -1864,7 +1865,7 @@ bool LedDevicePhilipsHue::setLights() Debug(_log, "Lights configured: %d", configuredLightsCount ); if (updateLights( getLightMap())) { - if (_useApiV2) + if (_useApiV2 && _useEntertainmentAPI) { _channelsCount = getGroupChannelsCount (_groupId); @@ -2208,15 +2209,14 @@ int LedDevicePhilipsHue::write(const std::vector & ledValues) int rc {0}; if (_isOn) { - if (!_useApiV2) - { - rc = writeSingleLights( ledValues ); - } - if (_useEntertainmentAPI && _isInitLeds) { rc= writeStreamData(ledValues); } + else + { + rc = writeSingleLights( ledValues ); + } } return rc; } @@ -2482,7 +2482,7 @@ void LedDevicePhilipsHue::setColor(PhilipsHueLight& light, CiColor& color) QJsonObject colorXY; colorXY[API_X_COORDINATE] = color.x; colorXY[API_Y_COORDINATE] = color.y; - cmd.insert(API_COLOR, QJsonObject {{API_DURATION, colorXY }}); + cmd.insert(API_COLOR, QJsonObject {{API_XY_COORDINATES, colorXY }}); cmd.insert(API_DIMMING, QJsonObject {{API_BRIGHTNESS, bri }}); } else @@ -2556,7 +2556,7 @@ void LedDevicePhilipsHue::setState(PhilipsHueLight& light, bool on, const CiColo QJsonObject colorXY; colorXY[API_X_COORDINATE] = color.x; colorXY[API_Y_COORDINATE] = color.y; - cmd.insert(API_COLOR, QJsonObject {{API_DURATION, colorXY }}); + cmd.insert(API_COLOR, QJsonObject {{API_XY_COORDINATES, colorXY }}); cmd.insert(API_DIMMING, QJsonObject {{API_BRIGHTNESS, bri }}); } else diff --git a/libsrc/leddevice/dev_net/ProviderRestApi.cpp b/libsrc/leddevice/dev_net/ProviderRestApi.cpp index 7321810f5..e981d00ae 100644 --- a/libsrc/leddevice/dev_net/ProviderRestApi.cpp +++ b/libsrc/leddevice/dev_net/ProviderRestApi.cpp @@ -30,7 +30,8 @@ enum HttpStatusCode { BadRequest = 400, UnAuthorized = 401, Forbidden = 403, - NotFound = 404 + NotFound = 404, + TooManyRequests = 429 }; } //End of constants @@ -336,6 +337,15 @@ httpResponse ProviderRestApi::getResponse(QNetworkReply* const& reply) case HttpStatusCode::NotFound: advise = "Check Resource given"; break; + case HttpStatusCode::TooManyRequests: + { + QString retryAfterTime = response.getHeader("Retry-After"); + if (!retryAfterTime.isEmpty()) + { + advise = "Retry-After: " + response.getHeader("Retry-After"); + } + } + break; default: advise = httpReason; break;