Skip to content

Commit

Permalink
Add invert dimmer options for blinds channel
Browse files Browse the repository at this point in the history
Add option to invert the dimmer value from blinds.

Signed-off-by: Hans Hazelius <hans@hazelius.se>
  • Loading branch information
hazzeh committed Jul 7, 2016
1 parent 5b04d58 commit 46a6375
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 3 deletions.
@@ -0,0 +1,208 @@
package org.openhab.binding.zwave.test.internal.converter;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.PercentType;
import org.eclipse.smarthome.core.types.State;
import org.junit.Before;
import org.junit.Test;
import org.openhab.binding.zwave.handler.ZWaveControllerHandler;
import org.openhab.binding.zwave.handler.ZWaveThingChannel;
import org.openhab.binding.zwave.handler.ZWaveThingChannel.DataType;
import org.openhab.binding.zwave.internal.converter.ZWaveMultiLevelSwitchConverter;
import org.openhab.binding.zwave.internal.protocol.SerialMessage;
import org.openhab.binding.zwave.internal.protocol.ZWaveNode;
import org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass;
import org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveMultiLevelSwitchCommandClass;
import org.openhab.binding.zwave.internal.protocol.event.ZWaveCommandClassValueEvent;

public class ZWaveMultiLevelSwitchConverterTest {

private ZWaveControllerHandler controller;
private ZWaveThingChannel channel;
private ZWaveCommandClassValueEvent event;
private ZWaveNode node;
private PercentType percentType;
private ZWaveMultiLevelSwitchCommandClass commandClass;

@Before
public void setup() {
controller = mock(ZWaveControllerHandler.class);
channel = mock(ZWaveThingChannel.class);
event = mock(ZWaveCommandClassValueEvent.class);
node = mock(ZWaveNode.class);
percentType = mock(PercentType.class);
commandClass = mock(ZWaveMultiLevelSwitchCommandClass.class);
}

@Test
public void handleEvent_PercentType0invertPercentFalse_returnPercentType0() throws Exception {
ZWaveMultiLevelSwitchConverter sut = new ZWaveMultiLevelSwitchConverter(controller);
Map<String, String> configMap = new HashMap<>();
configMap.put("config_invert_percent", "false");
when(channel.getArguments()).thenReturn(configMap);
when(event.getValue()).thenReturn(0);
when(channel.getDataType()).thenReturn(DataType.PercentType);
State state = sut.handleEvent(channel, event);
assertEquals(new PercentType(0), state);
}

@Test
public void handleEvent_PercentType99invertPercentFalse_returnPercentType100() {
ZWaveMultiLevelSwitchConverter sut = new ZWaveMultiLevelSwitchConverter(controller);
Map<String, String> configMap = new HashMap<>();
configMap.put("config_invert_percent", "false");
when(channel.getArguments()).thenReturn(configMap);
when(event.getValue()).thenReturn(99);
when(channel.getDataType()).thenReturn(DataType.PercentType);
State state = sut.handleEvent(channel, event);
assertEquals(new PercentType(100), state);
}

@Test
public void handleEvent_PercentType0invertPercentTrue_returnPercentType100() throws Exception {
ZWaveMultiLevelSwitchConverter sut = new ZWaveMultiLevelSwitchConverter(controller);
Map<String, String> configMap = new HashMap<>();
configMap.put("config_invert_percent", "true");
when(channel.getArguments()).thenReturn(configMap);
when(event.getValue()).thenReturn(0);
when(channel.getDataType()).thenReturn(DataType.PercentType);
State state = sut.handleEvent(channel, event);
assertEquals(new PercentType(100), state);
}

@Test
public void handleEvent_PercentType1invertPercentTrue_returnPercentType100() {
ZWaveMultiLevelSwitchConverter sut = new ZWaveMultiLevelSwitchConverter(controller);
Map<String, String> configMap = new HashMap<>();
configMap.put("config_invert_percent", "true");
when(channel.getArguments()).thenReturn(configMap);
when(event.getValue()).thenReturn(1);
when(channel.getDataType()).thenReturn(DataType.PercentType);
State state = sut.handleEvent(channel, event);
assertEquals(new PercentType(100), state);
}

@Test
public void handleEvent_OnOffType0invertFalse_returnOnOffTypeOff() {
ZWaveMultiLevelSwitchConverter sut = new ZWaveMultiLevelSwitchConverter(controller);
Map<String, String> configMap = new HashMap<>();
configMap.put("config_invert", "false");
when(channel.getArguments()).thenReturn(configMap);
when(event.getValue()).thenReturn(0);
when(channel.getDataType()).thenReturn(DataType.OnOffType);
State state = sut.handleEvent(channel, event);
assertEquals(OnOffType.OFF, state);
}

@Test
public void handleEvent_OnOffType1invertFalse_returnOnOffTypeOn() {
ZWaveMultiLevelSwitchConverter sut = new ZWaveMultiLevelSwitchConverter(controller);
Map<String, String> configMap = new HashMap<>();
configMap.put("config_invert", "false");
when(channel.getArguments()).thenReturn(configMap);
when(event.getValue()).thenReturn(1);
when(channel.getDataType()).thenReturn(DataType.OnOffType);
State state = sut.handleEvent(channel, event);
assertEquals(OnOffType.ON, state);
}

@Test
public void handleEvent_OnOffType0invertTrue_returnOnOffTypeOn() {
ZWaveMultiLevelSwitchConverter sut = new ZWaveMultiLevelSwitchConverter(controller);
Map<String, String> configMap = new HashMap<>();
configMap.put("config_invert", "true");
when(channel.getArguments()).thenReturn(configMap);
when(event.getValue()).thenReturn(0);
when(channel.getDataType()).thenReturn(DataType.OnOffType);
State state = sut.handleEvent(channel, event);
assertEquals(OnOffType.ON, state);
}

@Test
public void handleEvent_OnOffType1invertTrue_returnOnOffTypeOff() {
ZWaveMultiLevelSwitchConverter sut = new ZWaveMultiLevelSwitchConverter(controller);
Map<String, String> configMap = new HashMap<>();
configMap.put("config_invert", "true");
when(channel.getArguments()).thenReturn(configMap);
when(event.getValue()).thenReturn(1);
when(channel.getDataType()).thenReturn(DataType.OnOffType);
State state = sut.handleEvent(channel, event);
assertEquals(OnOffType.OFF, state);
}

@Test
public void receiveCommand_PercentType0invertPercentFalse_setValue0() throws Exception {
ZWaveMultiLevelSwitchConverter sut = new ZWaveMultiLevelSwitchConverter(controller);
setupReceiveCommand();
Map<String, String> configMap = new HashMap<>();
configMap.put("config_invert_percent", "false");
when(channel.getArguments()).thenReturn(configMap);
when(percentType.intValue()).thenReturn(0);
sut.receiveCommand(channel, node, percentType);
verify(commandClass).setValueMessage(0);
}

@Test
public void receiveCommand_PercentType0invertPercentTrue_setValue99() throws Exception {
ZWaveMultiLevelSwitchConverter sut = new ZWaveMultiLevelSwitchConverter(controller);
setupReceiveCommand();
Map<String, String> configMap = new HashMap<>();
configMap.put("config_invert_percent", "true");
when(channel.getArguments()).thenReturn(configMap);
when(percentType.intValue()).thenReturn(0);
sut.receiveCommand(channel, node, percentType);
verify(commandClass).setValueMessage(99);
}

@Test
public void receiveCommand_PercentType80invertPercentFalse_setValue80() throws Exception {
ZWaveMultiLevelSwitchConverter sut = new ZWaveMultiLevelSwitchConverter(controller);
setupReceiveCommand();
Map<String, String> configMap = new HashMap<>();
configMap.put("config_invert_percent", "false");
when(channel.getArguments()).thenReturn(configMap);
when(percentType.intValue()).thenReturn(80);
sut.receiveCommand(channel, node, percentType);
verify(commandClass).setValueMessage(80);
}

@Test
public void receiveCommand_PercentType80invertPercentTrue_setValue20() throws Exception {
ZWaveMultiLevelSwitchConverter sut = new ZWaveMultiLevelSwitchConverter(controller);
setupReceiveCommand();
Map<String, String> configMap = new HashMap<>();
configMap.put("config_invert_percent", "true");
when(channel.getArguments()).thenReturn(configMap);
when(percentType.intValue()).thenReturn(80);
sut.receiveCommand(channel, node, percentType);
verify(commandClass).setValueMessage(20);
}

@Test
public void receiveCommand_PercentType100invertPercentFalse_setValue99() throws Exception {
ZWaveMultiLevelSwitchConverter sut = new ZWaveMultiLevelSwitchConverter(controller);
setupReceiveCommand();
Map<String, String> configMap = new HashMap<>();
configMap.put("config_invert_percent", "true");
when(channel.getArguments()).thenReturn(configMap);
when(percentType.intValue()).thenReturn(100);
sut.receiveCommand(channel, node, percentType);
verify(commandClass).setValueMessage(99);
}

private void setupReceiveCommand() {
when(channel.getDataType()).thenReturn(DataType.PercentType);
when(channel.getEndpoint()).thenReturn(1);
when(node.resolveCommandClass(CommandClass.SWITCH_MULTILEVEL, channel.getEndpoint())).thenReturn(commandClass);
when(node.encapsulate(any(SerialMessage.class), any(ZWaveMultiLevelSwitchCommandClass.class), anyInt()))
.thenReturn(new SerialMessage());
}

}
Expand Up @@ -221,7 +221,7 @@
<category>Blinds</category>
<config-description>
<parameter name="config_invert" type="boolean">
<label>Invert</label>
<label>Invert control</label>
<description>Invert the blinds control</description>
<default>false</default>
<options>
Expand All @@ -230,6 +230,16 @@
</options>
<limitToOptions>true</limitToOptions>
</parameter>
<parameter name="config_invert_percent" type="boolean">
<label>Invert dimmer value</label>
<description>Invert the blinds dimmer value</description>
<default>false</default>
<options>
<option value="true">Yes</option>
<option value="false">No</option>
</options>
<limitToOptions>true</limitToOptions>
</parameter>
<parameter name="config_duration" type="integer" min="1" max="254">
<label>Step duration</label>
<description><![CDATA[Sets the dim rate speed<br>Values from 1 to 127 are defined in seconds,
Expand Down
Expand Up @@ -73,12 +73,13 @@ public List<SerialMessage> executeRefresh(ZWaveThingChannel channel, ZWaveNode n
@Override
public State handleEvent(ZWaveThingChannel channel, ZWaveCommandClassValueEvent event) {
boolean configInvert = "true".equalsIgnoreCase(channel.getArguments().get("config_invert"));
boolean configInvertPercent = "true".equalsIgnoreCase(channel.getArguments().get("config_invert_percent"));

int value = (int) event.getValue();
State state;
switch (channel.getDataType()) {
case PercentType:
if (configInvert) {
if (configInvertPercent) {
state = new PercentType(100 - value);
} else {
state = new PercentType(value);
Expand Down Expand Up @@ -129,6 +130,7 @@ public List<SerialMessage> receiveCommand(ZWaveThingChannel channel, ZWaveNode n
// boolean restoreLastValue = "true".equalsIgnoreCase(channel.getArguments().get("restoreLastValue"));

boolean configInvert = "true".equalsIgnoreCase(channel.getArguments().get("config_invert"));
boolean configInvertPercent = "true".equalsIgnoreCase(channel.getArguments().get("config_invert_percent"));

if (command instanceof StopMoveType && command == StopMoveType.STOP) {
// Special handling for the STOP command
Expand All @@ -149,7 +151,7 @@ public List<SerialMessage> receiveCommand(ZWaveThingChannel channel, ZWaveNode n
}
} else if (command instanceof PercentType) {
int value;
if (configInvert) {
if (configInvertPercent) {
value = 100 - ((PercentType) command).intValue();
} else {
value = ((PercentType) command).intValue();
Expand Down

0 comments on commit 46a6375

Please sign in to comment.