Skip to content

Commit

Permalink
Let 'on' take precedence when dimming is 0
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
  • Loading branch information
jlaur committed Dec 1, 2023
1 parent c2e36cc commit 52ea4f2
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ public State getBatteryLowState() {
}

/**
* Get the brightness as a PercentType. If off the brightness is 0, otherwise use dimming value.
* Get the brightness as a PercentType.
* If off the brightness is 0,
* If on use dimming value when > 0, otherwise 100.
* Otherwise use dimming value.
*
* @return a PercentType with the dimming state, or UNDEF, or NULL
*/
Expand All @@ -202,9 +205,19 @@ public State getBrightnessState() {
if (Objects.nonNull(dimming)) {
try {
// if off the brightness is 0, otherwise it is dimming value
// if on and brightness is 0, it is assumed as 100
// (because of contradiction, on takes precedence)
OnState on = this.on;
double brightness = Objects.nonNull(on) && !on.isOn() ? 0f
: Math.max(0f, Math.min(100f, dimming.getBrightness()));
double brightness = Math.max(0f, Math.min(100f, dimming.getBrightness()));
if (on != null) {
if (on.isOn()) {
if (brightness == 0) {
brightness = 100;
}
} else {
brightness = 0;
}
}
return new PercentType(new BigDecimal(brightness, PERCENT_MATH_CONTEXT));
} catch (DTOPresentButEmptyException e) {
return UnDefType.UNDEF; // indicates the DTO is present but its inner fields are missing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.hue.internal.clip2;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.openhab.binding.hue.internal.api.dto.clip2.Dimming;
import org.openhab.binding.hue.internal.api.dto.clip2.OnState;
import org.openhab.binding.hue.internal.api.dto.clip2.Resource;
import org.openhab.binding.hue.internal.api.dto.clip2.enums.ResourceType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.types.UnDefType;

/**
* Tests for {@link Resource}.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class ResourceTest {

@Test
void getBrightnessStateWhenDimmingMissingReturnNull() {
assertThat(createLightResource(true, null).getBrightnessState(), is(equalTo(UnDefType.NULL)));
}

@Test
void getBrightnessStateWhenOnAndDimming75ReturnBrightness75() {
assertThat(createLightResource(true, 75.0).getBrightnessState(), is(equalTo(new PercentType(75))));
}

@Test
void getBrightnessStateWhenOnAndDimming125ReturnBrightness100() {
assertThat(createLightResource(true, 125.0).getBrightnessState(), is(equalTo(new PercentType(100))));
}

@Test
void getBrightnessStateWhenOffAndDimming100ReturnBrightness0() {
assertThat(createLightResource(false, 100.0).getBrightnessState(), is(equalTo(new PercentType(0))));
}

@Test
void getBrightnessStateWhenOnStateMissingAndDimming0ReturnBrightness0() {
assertThat(createLightResource(null, 0.0).getBrightnessState(), is(equalTo(new PercentType(0))));
}

@Test
void getBrightnessStateWhenOnStateMissingAndDimming100ReturnBrightness100() {
assertThat(createLightResource(null, 100.0).getBrightnessState(), is(equalTo(new PercentType(100))));
}

@Test
void getBrightnessStateWhenOnStateMissingAndDimmingMinus1ReturnBrightness0() {
assertThat(createLightResource(null, -1.0).getBrightnessState(), is(equalTo(new PercentType(0))));
}

@Test
void getBrightnessStateWhenOnAndDimmingMinus1ReturnBrightness100() {
assertThat(createLightResource(true, -1.0).getBrightnessState(), is(equalTo(new PercentType(100))));
}

@Test
void getBrightnessStateWhenOnAndDimming0ReturnBrightness100() {
assertThat(createLightResource(true, 0.0).getBrightnessState(), is(equalTo(new PercentType(100))));
}

private Resource createLightResource(@Nullable Boolean on, @Nullable Double brightness) {
Resource resource = new Resource(ResourceType.LIGHT);

if (on != null) {
OnState onState = new OnState();
onState.setOn(on);
resource.setOnState(onState);
}

if (brightness != null) {
Dimming dimming = new Dimming();
dimming.setBrightness(brightness);
resource.setDimming(dimming);
}

return resource;
}
}

0 comments on commit 52ea4f2

Please sign in to comment.