Skip to content

Commit

Permalink
openhab#6 Add support for editing dimmer
Browse files Browse the repository at this point in the history
  • Loading branch information
magx2 committed Jun 2, 2019
1 parent 5917be8 commit 7b6b19c
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ protected void handlePercentCommand(final ChannelUID channelUID, final PercentTy
ledCommandExecutor.changeBrightness(channelId, channelUID, command);
}
return;
case DIMMER:
ledCommandExecutor.changeBrightness(channelId, channelUID, command);
default:
logger.warn("Not handling `{}` ({}) on channel `{}`", command, command.getClass().getSimpleName(), channelUID);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,30 @@ public void changeColorBrightness(final int channelId, final ChannelUID channelU
public void changeBrightness(final int channelId, final ChannelUID channelUID, final PercentType command) throws ApiException {
final Optional<LedState> state = findLedState(channelUID);
if (state.isPresent()) {
final LedState ledState = state.get();
sendNewLedValue(channelUID, channelId, ledState.hsb, command);
sendNewLedValue(channelUID, channelId, state.get().hsb, command);
}
}

private void sendNewLedValue(
final ChannelUID channelUID,
final int channelId,
final HSBType hsbType,
@Nullable final HSBType hsbType,
@Nullable final PercentType brightness) throws ApiException {
final int colorBrightness = hsbType.getBrightness().intValue();
final HSBType hsbToConvertToRgb = new HSBType(
hsbType.getHue(),
hsbType.getSaturation(),
PercentType.HUNDRED
);
final String rgb = HsbTypeConverter.INSTANCE.convert(hsbToConvertToRgb);
logger.trace("Changing RGB to {}, color brightness {}%, brightness {}%", rgb, colorBrightness, brightness);
final ChannelExecuteActionRequest actionWithoutBrightness = new ChannelExecuteActionRequest()
.action(SET_RGBW_PARAMETERS)
.color(rgb)
.colorBrightness(colorBrightness);
final ChannelExecuteActionRequest action;
ChannelExecuteActionRequest action = new ChannelExecuteActionRequest().action(SET_RGBW_PARAMETERS);
if (hsbType != null) {
final int colorBrightness = hsbType.getBrightness().intValue();
final HSBType hsbToConvertToRgb = new HSBType(
hsbType.getHue(),
hsbType.getSaturation(),
PercentType.HUNDRED
);
final String rgb = HsbTypeConverter.INSTANCE.convert(hsbToConvertToRgb);
logger.trace("Changing RGB to {}, color brightness {}%", rgb, colorBrightness);
action = action.color(rgb).colorBrightness(colorBrightness);
}
if (brightness != null) {
action = actionWithoutBrightness.brightness(brightness.intValue());
} else {
action = actionWithoutBrightness;
logger.trace("Changing brightness {}%", brightness);
action = action.brightness(brightness.intValue());
}

channelsApi.executeAction(action, channelId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ private Optional<? extends State> onOffType(Channel channel) {

@Override
public Optional<? extends State> onDimmer(Channel channel) {
return of(channel).map(Channel::getState).map(ChannelState::getBrightness)
.map(b -> b / 100.0)
.map(DecimalType::new);
final Optional<PercentType> state = of(channel).map(Channel::getState)
.map(ChannelState::getBrightness)
.map(PercentType::new);
state.ifPresent(s -> ledCommandExecutor.setLedState(channelUID, s));
return state;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.openhab.binding.supla.internal.cloud.executors;

import io.github.glytching.junit.extension.random.Random;
import io.github.glytching.junit.extension.random.RandomBeansExtension;
import org.eclipse.smarthome.core.library.types.HSBType;
import org.eclipse.smarthome.core.library.types.PercentType;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openhab.binding.supla.internal.cloud.api.ChannelsCloudApi;
import pl.grzeslowski.jsupla.api.generated.ApiException;
import pl.grzeslowski.jsupla.api.generated.model.ChannelExecuteActionRequest;

import static org.mockito.Mockito.verify;
import static pl.grzeslowski.jsupla.api.generated.model.ChannelFunctionActionEnum.SET_RGBW_PARAMETERS;

@SuppressWarnings("WeakerAccess")
@ExtendWith(MockitoExtension.class)
@ExtendWith(RandomBeansExtension.class)
class SuplaLedCommandExecutorTest {
@InjectMocks SuplaLedCommandExecutor executor;
@Mock ChannelsCloudApi channelsApi;

ChannelUID channelUID = new ChannelUID("a:b:c:d");

@Test
@DisplayName("should send update about change of brightness for dimmer")
void dimmer(@Random int channelId) throws ApiException {

// given
final int brightnessValue = 55;
final PercentType brightness = new PercentType(brightnessValue);
executor.setLedState(channelUID, PercentType.ZERO);

// when
executor.changeBrightness(channelId, channelUID, brightness);

// then
final ChannelExecuteActionRequest expectedAction = new ChannelExecuteActionRequest()
.action(SET_RGBW_PARAMETERS)
.brightness(brightnessValue);
verify(channelsApi).executeAction(expectedAction, channelId);
}

@Test
@DisplayName("should send update about change of brightness for dimmer and rgb")
void dimmerAndRgbChangeOfBrightness(@Random int channelId) throws ApiException {

// given
final int brightnessValue = 55;
final PercentType brightness = new PercentType(brightnessValue);
executor.setLedState(channelUID, PercentType.ZERO);
executor.setLedState(channelUID, HSBType.BLUE);

// when
executor.changeBrightness(channelId, channelUID, brightness);

// then
final ChannelExecuteActionRequest expectedAction = new ChannelExecuteActionRequest()
.action(SET_RGBW_PARAMETERS)
.color("0x0000FF")
.colorBrightness(100)
.brightness(brightnessValue);
verify(channelsApi).executeAction(expectedAction, channelId);
}

@Test
@DisplayName("should send update about change of color for dimmer and rgb")
void dimmerAndRgbChangeOfColor(@Random int channelId) throws ApiException {

// given
final int brightnessValue = 55;
executor.setLedState(channelUID, new PercentType(brightnessValue));
executor.setLedState(channelUID, HSBType.BLUE);

// when
executor.changeColor(channelId, channelUID, HSBType.RED);

// then
final ChannelExecuteActionRequest expectedAction = new ChannelExecuteActionRequest()
.action(SET_RGBW_PARAMETERS)
.color("0xFF0000")
.colorBrightness(100)
.brightness(brightnessValue);
verify(channelsApi).executeAction(expectedAction, channelId);
}
}

0 comments on commit 7b6b19c

Please sign in to comment.