@@ -401,8 +401,6 @@ bool LedDeviceHomeAssistant::powerOff()
401401
402402int LedDeviceHomeAssistant::write (const std::vector<ColorRgb>& ledValues)
403403{
404- int retVal = 0 ;
405-
406404 QJsonObject serviceAttributes{ {ENTITY_ID, QJsonArray::fromStringList (_lightEntityIds)} };
407405 ColorRgb ledValue = ledValues.at (0 );
408406
@@ -415,20 +413,18 @@ int LedDeviceHomeAssistant::write(const std::vector<ColorRgb>& ledValues)
415413 _restApi->setPath (API_LIGHT_TURN_ON);
416414 serviceAttributes.insert (RGB_COLOR, QJsonArray{ ledValue.red , ledValue.green , ledValue.blue });
417415
418- int brightness = _brightness;
419-
420- // Some devices cannot deal with a black color and brightness > 0
421- if (ledValue == ColorRgb::BLACK)
416+ int brightness;
417+ if (!_isBrightnessOverwrite)
422418 {
423- brightness = 0 ;
419+ brightness = qBound ( 0 , qRound ( 0.2126 * ledValue. red + 0.7152 * ledValue. green + 0.0722 * ledValue. blue ), 255 ) ;
424420 }
425-
426- // Add brightness attribute if applicable
427- if (brightness == 0 || _isBrightnessOverwrite)
421+ else
428422 {
429- serviceAttributes. insert (BRIGHTNESS, brightness) ;
423+ brightness = _brightness ;
430424 }
431425
426+ serviceAttributes.insert (BRIGHTNESS, brightness);
427+
432428 if (_transitionTime > 0 )
433429 {
434430 serviceAttributes.insert (TRANSITION, _transitionTime);
@@ -438,8 +434,8 @@ int LedDeviceHomeAssistant::write(const std::vector<ColorRgb>& ledValues)
438434 if (response.error ())
439435 {
440436 Warning (_log, " Updating lights failed with error: '%s'" , QSTRING_CSTR (response.getErrorReason ()));
441- retVal = -1 ;
437+ return -1 ;
442438 }
443439
444- return retVal ;
440+ return 0 ;
445441}
0 commit comments