ColorItem extends DimmerItem { @Override public void setState(State state) { if (isAcceptedState(acceptedDataTypes, state)) { State currentState = this.state; if (currentState instanceof HSBType) { DecimalType hue = ((HSBType) currentState).getHue(); PercentType saturation = ((HSBType) currentState).getSaturation(); if (state == OnOffType.OFF) { applyState(new HSBType(hue, saturation, PercentType.ZERO)); } else if (state == OnOffType.ON) { applyState(new HSBType(hue, saturation, PercentType.HUNDRED)); } else if (state instanceof PercentType && !(state instanceof HSBType)) { applyState(new HSBType(hue, saturation, (PercentType) state)); } else if (state instanceof DecimalType && !(state instanceof HSBType)) { applyState(new HSBType(hue, saturation, new PercentType(((DecimalType) state).toBigDecimal().multiply(BigDecimal.valueOf(100))))); } else { applyState(state); } } else { State convertedState = state.as(HSBType.class); if (convertedState != null) { applyState(convertedState); } else { applyState(state); } } } else { logSetTypeError(state); } } ColorItem(String name); void send(HSBType command); @Override List<Class<? extends State>> getAcceptedDataTypes(); @Override List<Class<? extends Command>> getAcceptedCommandTypes(); @Override void setState(State state);  }
@Test public void testSetStateWithHSBType() { ColorItem item = new ColorItem("test"); item.setState(new HSBType(new DecimalType(75), new PercentType(75), new PercentType(75))); assertEquals(new HSBType(new DecimalType(75), new PercentType(75), new PercentType(75)), item.getState()); }
@Test public void testSetStateWithPercentType() { ColorItem item = new ColorItem("test"); item.setState(new PercentType(50)); assertEquals(new HSBType(new DecimalType(0), new PercentType(0), new PercentType(50)), item.getState()); }
@Test public void testSetStateWithOnOffType() { ColorItem item = new ColorItem("test"); item.setState(OnOffType.ON); assertEquals(new HSBType("0,0,100"), item.getState()); item.setState(OnOffType.OFF); assertEquals(new HSBType("0,0,0"), item.getState()); }
@Test public void testUpdateStateWithPercentType() { ColorItem item = new ColorItem("test"); item.setState(new HSBType(new DecimalType(75), new PercentType(75), new PercentType(75))); item.setState(new PercentType(50)); assertEquals(new HSBType(new DecimalType(75), new PercentType(75), new PercentType(50)), item.getState()); }