[wled] Fix brightness inconsistency + power on (#16907)

This commit is contained in:
Stefan Triller 2024-08-17 16:15:46 +02:00 committed by GitHub
parent fcef4639ca
commit 97e9f374ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 13 deletions

View File

@ -30,14 +30,17 @@ import org.openhab.core.library.types.PercentType;
*/
@NonNullByDefault
public class WLedHelper {
public static HSBType parseToHSBType(String message) {
public static HSBType parseToHSBType(String message, int brightness) {
// example message rgb in array brackets [255.0, 255.0, 255.0]
List<String> colors = Arrays.asList(message.replaceAll("\\[|\\]", "").split("\\s*,\\s*"));
try {
int r = new BigDecimal(colors.get(0)).intValue();
int g = new BigDecimal(colors.get(1)).intValue();
int b = new BigDecimal(colors.get(2)).intValue();
return HSBType.fromRGB(r, g, b);
HSBType tmp = HSBType.fromRGB(r, g, b);
PercentType brightnessPercent = new PercentType(
new BigDecimal(brightness).divide(BIG_DECIMAL_2_55, RoundingMode.HALF_UP));
return new HSBType(tmp.getHue(), tmp.getSaturation(), brightnessPercent);
} catch (NumberFormatException e) {
return new HSBType();
}

View File

@ -296,12 +296,13 @@ public class WledApiV084 implements WledApi {
// There is no thing setup for this segmentIndex.
return;
}
HSBType tempHSB = WLedHelper.parseToHSBType(state.stateResponse.seg[segmentIndex].col[0].toString());
HSBType tempHSB = WLedHelper.parseToHSBType(state.stateResponse.seg[segmentIndex].col[0].toString(),
state.stateResponse.seg[segmentIndex].bri);
handler.update(segmentIndex, CHANNEL_PRIMARY_COLOR, tempHSB);
handler.update(segmentIndex, CHANNEL_SECONDARY_COLOR,
WLedHelper.parseToHSBType(state.stateResponse.seg[segmentIndex].col[1].toString()));
handler.update(segmentIndex, CHANNEL_THIRD_COLOR,
WLedHelper.parseToHSBType(state.stateResponse.seg[segmentIndex].col[2].toString()));
handler.update(segmentIndex, CHANNEL_SECONDARY_COLOR, WLedHelper.parseToHSBType(
state.stateResponse.seg[segmentIndex].col[1].toString(), state.stateResponse.seg[segmentIndex].bri));
handler.update(segmentIndex, CHANNEL_THIRD_COLOR, WLedHelper.parseToHSBType(
state.stateResponse.seg[segmentIndex].col[2].toString(), state.stateResponse.seg[segmentIndex].bri));
if (state.ledInfo.rgbw) {
handler.update(segmentIndex, CHANNEL_PRIMARY_WHITE,
WLedHelper.parseWhitePercent(state.stateResponse.seg[segmentIndex].col[0].toString()));
@ -378,14 +379,17 @@ public class WledApiV084 implements WledApi {
@Override
public void setMasterHSB(HSBType hsbType, int segmentIndex) throws ApiException {
if (hsbType.getBrightness().toBigDecimal().equals(BigDecimal.ZERO)) {
updateStateFromReply(postState("{\"tt\":2,\"v\":true,\"seg\":[{\"on\":false,\"id\":" + segmentIndex
+ ",\"fx\":0,\"col\":[[" + hsbType.getRed().toBigDecimal().multiply(BIG_DECIMAL_2_55).intValue()
+ "," + hsbType.getGreen().toBigDecimal().multiply(BIG_DECIMAL_2_55).intValue() + ","
+ hsbType.getBlue().toBigDecimal().multiply(BIG_DECIMAL_2_55).intValue() + "]]}]}"));
updateStateFromReply(postState(
"{\"tt\":2,\"v\":true,\"seg\":[{\"on\":false,\"id\":" + segmentIndex + ",\"fx\":0,\"bri\":"
+ hsbType.getBrightness().toBigDecimal().multiply(BIG_DECIMAL_2_55).intValue()
+ ",\"col\":[[" + hsbType.getRed().toBigDecimal().multiply(BIG_DECIMAL_2_55).intValue()
+ "," + hsbType.getGreen().toBigDecimal().multiply(BIG_DECIMAL_2_55).intValue() + ","
+ hsbType.getBlue().toBigDecimal().multiply(BIG_DECIMAL_2_55).intValue() + "]]}]}"));
return;
}
updateStateFromReply(postState("{\"tt\":2,\"v\":true,\"seg\":[{\"on\":true,\"id\":" + segmentIndex
+ ",\"fx\":0,\"col\":[[" + hsbType.getRed().toBigDecimal().multiply(BIG_DECIMAL_2_55).intValue() + ","
+ ",\"fx\":0,\"bri\":" + hsbType.getBrightness().toBigDecimal().multiply(BIG_DECIMAL_2_55).intValue()
+ ",\"col\":[[" + hsbType.getRed().toBigDecimal().multiply(BIG_DECIMAL_2_55).intValue() + ","
+ hsbType.getGreen().toBigDecimal().multiply(BIG_DECIMAL_2_55).intValue() + ","
+ hsbType.getBlue().toBigDecimal().multiply(BIG_DECIMAL_2_55).intValue() + "]]}]}"));
}
@ -472,7 +476,8 @@ public class WledApiV084 implements WledApi {
@Override
public void setWhiteOnly(PercentType percentType, int segmentIndex) throws ApiException {
postState("{\"seg\":[{\"on\":true,\"id\":" + segmentIndex + ",\"fx\":0,\"col\":[[0,0,0,"
postState("{\"seg\":[{\"on\":true,\"id\":" + segmentIndex + ",\"fx\":0,\"bri\":"
+ percentType.toBigDecimal().multiply(BIG_DECIMAL_2_55).intValue() + ",\"col\":[[0,0,0,"
+ percentType.toBigDecimal().multiply(BIG_DECIMAL_2_55).intValue() + "]]}]}");
}

View File

@ -117,6 +117,8 @@ public class WLedSegmentHandler extends BaseThingHandler {
localApi.setMasterOn(false, config.segmentIndex);
return;
}
// do not turn the globalOn in order to allow for configuring this segment for effects that
// start later
localApi.setMasterBrightness(percentCommand, config.segmentIndex);
}
break;
@ -155,6 +157,7 @@ public class WLedSegmentHandler extends BaseThingHandler {
localApi.setMasterOn(OnOffType.ON.equals(command), config.segmentIndex);
} else if (command instanceof IncreaseDecreaseType) {
if (IncreaseDecreaseType.INCREASE.equals(command)) {
localApi.setGlobalOn(true);
if (masterBrightness255.intValue() < 240) {
localApi.sendGetRequest("/win&TT=1000&A=~15"); // 255 divided by 15 = 17 levels
} else {
@ -184,6 +187,11 @@ public class WLedSegmentHandler extends BaseThingHandler {
localApi.setMasterHSB(hsbCommand, config.segmentIndex);
}
} else if (command instanceof PercentType percentCommand) {
if (PercentType.ZERO.equals(percentCommand)) {
localApi.setMasterOn(false, config.segmentIndex);
return;
}
localApi.setGlobalOn(true);
localApi.setMasterBrightness(percentCommand, config.segmentIndex);
}
return;
@ -216,6 +224,7 @@ public class WLedSegmentHandler extends BaseThingHandler {
localApi.setPalette(command.toString(), config.segmentIndex);
break;
case CHANNEL_FX:
localApi.setGlobalOn(true);
localApi.setEffect(command.toString(), config.segmentIndex);
break;
case CHANNEL_SPEED: