use of org.openhab.io.hueemulation.internal.dto.HueStateColorBulb in project openhab-addons by openhab.
the class StateUtils method computeCommandByState.
/**
* Apply the new received state from the REST PUT request.
*
* @param responses Creates a response entry for each success and each error. There is one entry per non-null field
* of {@link HueStateChange} created.
* @param prefix The response entry prefix, for example "/groups/mygroupid/state/"
* @param state The current item state
* @param newState A state change DTO
* @return Return a command computed via the incoming state object.
*/
@Nullable
public static Command computeCommandByState(List<HueResponse> responses, String prefix, AbstractHueState state, HueStateChange newState) {
// Apply new state and collect success, error items
Map<String, Object> successApplied = new TreeMap<>();
List<String> errorApplied = new ArrayList<>();
Command command = null;
if (newState.on != null) {
try {
state.as(HueStatePlug.class).on = newState.on;
command = OnOffType.from(newState.on);
successApplied.put("on", newState.on);
} catch (ClassCastException e) {
errorApplied.add("on");
}
}
if (newState.bri != null) {
try {
state.as(HueStateBulb.class).bri = newState.bri;
command = new PercentType((int) (newState.bri * 100.0 / HueStateBulb.MAX_BRI + 0.5));
successApplied.put("bri", newState.bri);
} catch (ClassCastException e) {
errorApplied.add("bri");
}
}
if (newState.bri_inc != null) {
try {
int newBri = state.as(HueStateBulb.class).bri + newState.bri_inc;
if (newBri < 0 || newBri > HueStateBulb.MAX_BRI) {
throw new IllegalArgumentException();
}
command = new PercentType((int) (newBri * 100.0 / HueStateBulb.MAX_BRI + 0.5));
successApplied.put("bri", newState.bri);
} catch (ClassCastException e) {
errorApplied.add("bri_inc");
} catch (IllegalArgumentException e) {
errorApplied.add("bri_inc");
}
}
if (newState.sat != null) {
try {
HueStateColorBulb c = state.as(HueStateColorBulb.class);
c.sat = newState.sat;
c.colormode = ColorMode.hs;
command = c.toHSBType();
successApplied.put("sat", newState.sat);
} catch (ClassCastException e) {
errorApplied.add("sat");
}
}
if (newState.sat_inc != null) {
try {
HueStateColorBulb c = state.as(HueStateColorBulb.class);
int newV = c.sat + newState.sat_inc;
if (newV < 0 || newV > HueStateColorBulb.MAX_SAT) {
throw new IllegalArgumentException();
}
c.colormode = ColorMode.hs;
c.sat = newV;
command = c.toHSBType();
successApplied.put("sat", newState.sat);
} catch (ClassCastException e) {
errorApplied.add("sat_inc");
} catch (IllegalArgumentException e) {
errorApplied.add("sat_inc");
}
}
if (newState.hue != null) {
try {
HueStateColorBulb c = state.as(HueStateColorBulb.class);
c.colormode = ColorMode.hs;
c.hue = newState.hue;
command = c.toHSBType();
successApplied.put("hue", newState.hue);
} catch (ClassCastException e) {
errorApplied.add("hue");
}
}
if (newState.hue_inc != null) {
try {
HueStateColorBulb c = state.as(HueStateColorBulb.class);
int newV = c.hue + newState.hue_inc;
if (newV < 0 || newV > HueStateColorBulb.MAX_HUE) {
throw new IllegalArgumentException();
}
c.colormode = ColorMode.hs;
c.hue = newV;
command = c.toHSBType();
successApplied.put("hue", newState.hue);
} catch (ClassCastException e) {
errorApplied.add("hue_inc");
} catch (IllegalArgumentException e) {
errorApplied.add("hue_inc");
}
}
if (newState.ct != null) {
try {
// Adjusting the color temperature implies setting the mode to ct
if (state instanceof HueStateColorBulb) {
HueStateColorBulb c = state.as(HueStateColorBulb.class);
c.sat = 0;
c.colormode = ColorMode.ct;
command = c.toHSBType();
}
successApplied.put("colormode", ColorMode.ct);
successApplied.put("sat", 0);
successApplied.put("ct", newState.ct);
} catch (ClassCastException e) {
errorApplied.add("ct");
}
}
if (newState.ct_inc != null) {
try {
// Adjusting the color temperature implies setting the mode to ct
if (state instanceof HueStateColorBulb) {
HueStateColorBulb c = state.as(HueStateColorBulb.class);
if (c.colormode != ColorMode.ct) {
c.sat = 0;
command = c.toHSBType();
successApplied.put("colormode", c.colormode);
}
}
successApplied.put("ct", newState.ct);
} catch (ClassCastException e) {
errorApplied.add("ct_inc");
}
}
if (newState.transitiontime != null) {
// Pretend that worked
successApplied.put("transitiontime", newState.transitiontime);
}
if (newState.alert != null) {
// Pretend that worked
successApplied.put("alert", newState.alert);
}
if (newState.effect != null) {
// Pretend that worked
successApplied.put("effect", newState.effect);
}
if (newState.xy != null) {
try {
HueStateColorBulb c = state.as(HueStateColorBulb.class);
c.colormode = ColorMode.xy;
c.bri = state.as(HueStateBulb.class).bri;
c.xy[0] = newState.xy.get(0);
c.xy[1] = newState.xy.get(1);
command = c.toHSBType();
successApplied.put("xy", newState.xy);
} catch (ClassCastException e) {
errorApplied.add("xy");
}
}
if (newState.xy_inc != null) {
try {
HueStateColorBulb c = state.as(HueStateColorBulb.class);
double newX = c.xy[0] + newState.xy_inc.get(0);
double newY = c.xy[1] + newState.xy_inc.get(1);
if (newX < 0 || newX > 1 || newY < 0 || newY > 1) {
throw new IllegalArgumentException();
}
c.colormode = ColorMode.xy;
c.bri = state.as(HueStateBulb.class).bri;
c.xy[0] = newX;
c.xy[1] = newY;
command = c.toHSBType();
successApplied.put("xy", newState.xy_inc);
} catch (ClassCastException e) {
errorApplied.add("xy_inc");
} catch (IllegalArgumentException e) {
errorApplied.add("xy_inc");
}
}
// Generate the response. The response consists of a list with an entry each for all
// submitted change requests. If for example "on" and "bri" was send, 2 entries in the response are
// expected.
successApplied.forEach((t, v) -> {
responses.add(new HueResponse(new HueSuccessResponseStateChanged(prefix + "/" + t, v)));
});
errorApplied.forEach(v -> {
responses.add(new HueResponse(new HueErrorMessage(HueResponse.NOT_AVAILABLE, prefix + "/" + v, "Could not set")));
});
return command;
}
use of org.openhab.io.hueemulation.internal.dto.HueStateColorBulb in project openhab-addons by openhab.
the class StateUtils method colorStateFromItemState.
/**
* Compute the hue state from a given item state and a device type.
*
* @param itemState The item state
* @param deviceType The device type
* @return A hue light state
*/
public static AbstractHueState colorStateFromItemState(State itemState, @Nullable DeviceType deviceType) {
if (deviceType == null) {
return new HueStatePlug(false);
}
AbstractHueState state;
switch(deviceType) {
case ColorType:
if (itemState instanceof HSBType) {
state = new HueStateColorBulb((HSBType) itemState);
} else if (itemState instanceof PercentType) {
state = new HueStateColorBulb((PercentType) itemState, ((PercentType) itemState).intValue() > 0);
} else if (itemState instanceof OnOffType) {
OnOffType t = (OnOffType) itemState;
state = new HueStateColorBulb(t == OnOffType.ON);
} else {
state = new HueStateColorBulb(new HSBType());
}
break;
case WhiteType:
case WhiteTemperatureType:
if (itemState instanceof HSBType) {
PercentType brightness = ((HSBType) itemState).getBrightness();
state = new HueStateBulb(brightness, brightness.intValue() > 0);
} else if (itemState instanceof PercentType) {
PercentType brightness = (PercentType) itemState;
state = new HueStateBulb(brightness, brightness.intValue() > 0);
} else if (itemState instanceof OnOffType) {
OnOffType t = (OnOffType) itemState;
state = new HueStateBulb(t == OnOffType.ON);
} else {
state = new HueStateBulb(new PercentType(0), false);
}
break;
case SwitchType:
default:
if (itemState instanceof OnOffType) {
OnOffType t = (OnOffType) itemState;
state = new HueStatePlug(t == OnOffType.ON);
} else {
state = new HueStatePlug(false);
}
}
return state;
}
use of org.openhab.io.hueemulation.internal.dto.HueStateColorBulb in project openhab-addons by openhab.
the class LightsAndGroupsTests method changeCtValue.
/**
* Amazon echos are setting ct only, if commanded to turn a light white.
*/
@Test
public void changeCtValue() {
HueLightEntry hueDevice = cs.ds.lights.get("2");
hueDevice.item.setState(OnOffType.ON);
hueDevice.state.as(HueStateColorBulb.class).on = true;
String body = "{'ct':500}";
Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/lights/2/state").request().put(Entity.json(body));
assertEquals(200, response.getStatus());
body = response.readEntity(String.class);
assertThat(body, containsString("success"));
assertThat(body, containsString("ct"));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).on, is(true));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).ct, is(500));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).sat, is(0));
// Saturation is expected to be 0 -> white light
verify(commonSetup.eventPublisher).post(argThat(ce -> assertSatValue((ItemCommandEvent) ce, 0)));
}
use of org.openhab.io.hueemulation.internal.dto.HueStateColorBulb in project openhab-addons by openhab.
the class LightsAndGroupsTests method switchOnWithXY.
@Test
public void switchOnWithXY() {
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).on, is(false));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).bri, is(1));
String body = "{'on':true,'bri':200,'xy':[0.5119,0.4147]}";
Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/lights/2/state").request().put(Entity.json(body));
assertEquals(200, response.getStatus());
assertThat(response.readEntity(String.class), containsString("success"));
assertThat(response.readEntity(String.class), containsString("xy"));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).on, is(true));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).bri, is(200));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).xy[0], is(0.5119));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).xy[1], is(0.4147));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).colormode, is(HueStateColorBulb.ColorMode.xy));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).toHSBType().getHue().intValue(), is((int) 27.47722590981918));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).toHSBType().getSaturation().intValue(), is(88));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).toHSBType().getBrightness().intValue(), is(78));
}
use of org.openhab.io.hueemulation.internal.dto.HueStateColorBulb in project openhab-addons by openhab.
the class LightsAndGroupsTests method changeHueSatValues.
@Test
public void changeHueSatValues() {
HueLightEntry hueDevice = cs.ds.lights.get("2");
hueDevice.item.setState(OnOffType.ON);
hueDevice.state.as(HueStateColorBulb.class).on = true;
String body = "{'hue':1000,'sat':50}";
Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/lights/2/state").request().put(Entity.json(body));
assertEquals(200, response.getStatus());
assertThat(response.readEntity(String.class), containsString("success"));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).on, is(true));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).hue, is(1000));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).sat, is(50));
verify(commonSetup.eventPublisher).post(argThat(ce -> assertHueValue((ItemCommandEvent) ce, 1000)));
}
Aggregations