Search in sources :

Example 1 with HueStateColorBulb

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;
}
Also used : HueStateColorBulb(org.openhab.io.hueemulation.internal.dto.HueStateColorBulb) ArrayList(java.util.ArrayList) PercentType(org.openhab.core.library.types.PercentType) TreeMap(java.util.TreeMap) HueResponse(org.openhab.io.hueemulation.internal.dto.response.HueResponse) HueErrorMessage(org.openhab.io.hueemulation.internal.dto.response.HueResponse.HueErrorMessage) Command(org.openhab.core.types.Command) HueSuccessResponseStateChanged(org.openhab.io.hueemulation.internal.dto.response.HueSuccessResponseStateChanged) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 2 with HueStateColorBulb

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;
}
Also used : HueStateColorBulb(org.openhab.io.hueemulation.internal.dto.HueStateColorBulb) HueStatePlug(org.openhab.io.hueemulation.internal.dto.HueStatePlug) OnOffType(org.openhab.core.library.types.OnOffType) AbstractHueState(org.openhab.io.hueemulation.internal.dto.AbstractHueState) HueStateBulb(org.openhab.io.hueemulation.internal.dto.HueStateBulb) PercentType(org.openhab.core.library.types.PercentType) HSBType(org.openhab.core.library.types.HSBType)

Example 3 with HueStateColorBulb

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)));
}
Also used : Response(javax.ws.rs.core.Response) CoreMatchers(org.hamcrest.CoreMatchers) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) SwitchItem(org.openhab.core.library.items.SwitchItem) OnOffType(org.openhab.core.library.types.OnOffType) HueStatePlug(org.openhab.io.hueemulation.internal.dto.HueStatePlug) ConfigStore(org.openhab.io.hueemulation.internal.ConfigStore) ColorItem(org.openhab.core.library.items.ColorItem) HueStateColorBulb(org.openhab.io.hueemulation.internal.dto.HueStateColorBulb) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) DeviceType(org.openhab.io.hueemulation.internal.DeviceType) HueGroupEntry(org.openhab.io.hueemulation.internal.dto.HueGroupEntry) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) HueLightEntry(org.openhab.io.hueemulation.internal.dto.HueLightEntry) DummyItemRegistry(org.openhab.io.hueemulation.internal.rest.mocks.DummyItemRegistry) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) GroupItem(org.openhab.core.items.GroupItem) Event(org.openhab.core.events.Event) IOException(java.io.IOException) Entity(javax.ws.rs.client.Entity) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ItemRegistry(org.openhab.core.items.ItemRegistry) AfterEach(org.junit.jupiter.api.AfterEach) Response(javax.ws.rs.core.Response) HSBType(org.openhab.core.library.types.HSBType) HueLightEntry(org.openhab.io.hueemulation.internal.dto.HueLightEntry) Test(org.junit.jupiter.api.Test)

Example 4 with HueStateColorBulb

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));
}
Also used : Response(javax.ws.rs.core.Response) HueStateColorBulb(org.openhab.io.hueemulation.internal.dto.HueStateColorBulb) Test(org.junit.jupiter.api.Test)

Example 5 with HueStateColorBulb

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)));
}
Also used : Response(javax.ws.rs.core.Response) CoreMatchers(org.hamcrest.CoreMatchers) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) SwitchItem(org.openhab.core.library.items.SwitchItem) OnOffType(org.openhab.core.library.types.OnOffType) HueStatePlug(org.openhab.io.hueemulation.internal.dto.HueStatePlug) ConfigStore(org.openhab.io.hueemulation.internal.ConfigStore) ColorItem(org.openhab.core.library.items.ColorItem) HueStateColorBulb(org.openhab.io.hueemulation.internal.dto.HueStateColorBulb) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) DeviceType(org.openhab.io.hueemulation.internal.DeviceType) HueGroupEntry(org.openhab.io.hueemulation.internal.dto.HueGroupEntry) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) HueLightEntry(org.openhab.io.hueemulation.internal.dto.HueLightEntry) DummyItemRegistry(org.openhab.io.hueemulation.internal.rest.mocks.DummyItemRegistry) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) GroupItem(org.openhab.core.items.GroupItem) Event(org.openhab.core.events.Event) IOException(java.io.IOException) Entity(javax.ws.rs.client.Entity) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ItemRegistry(org.openhab.core.items.ItemRegistry) AfterEach(org.junit.jupiter.api.AfterEach) Response(javax.ws.rs.core.Response) HSBType(org.openhab.core.library.types.HSBType) HueLightEntry(org.openhab.io.hueemulation.internal.dto.HueLightEntry) Test(org.junit.jupiter.api.Test)

Aggregations

HueStateColorBulb (org.openhab.io.hueemulation.internal.dto.HueStateColorBulb)5 Response (javax.ws.rs.core.Response)3 Test (org.junit.jupiter.api.Test)3 HSBType (org.openhab.core.library.types.HSBType)3 OnOffType (org.openhab.core.library.types.OnOffType)3 HueStatePlug (org.openhab.io.hueemulation.internal.dto.HueStatePlug)3 IOException (java.io.IOException)2 Entity (javax.ws.rs.client.Entity)2 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)2 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)2 CoreMatchers (org.hamcrest.CoreMatchers)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 AfterEach (org.junit.jupiter.api.AfterEach)2 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ArgumentMatchers.argThat (org.mockito.ArgumentMatchers.argThat)2 Mockito.verify (org.mockito.Mockito.verify)2 Event (org.openhab.core.events.Event)2 GroupItem (org.openhab.core.items.GroupItem)2 ItemRegistry (org.openhab.core.items.ItemRegistry)2