Java Code Examples for org.eclipse.smarthome.core.library.types.OnOffType#ON

The following examples show how to use org.eclipse.smarthome.core.library.types.OnOffType#ON . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DS2405Test.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
private void digitalChannelTest(OnOffType state, int channelNo) {
    instantiateDevice();

    BitSet returnValue = new BitSet(8);
    if (state == OnOffType.ON) {
        returnValue.flip(0, 7);
    }

    try {
        Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
        Mockito.when(mockBridgeHandler.readBitSet(eq(testSensorId), any())).thenReturn(returnValue);

        testDevice.configureChannels();
        testDevice.refresh(mockBridgeHandler, true);

        inOrder.verify(mockBridgeHandler, times(2)).readBitSet(eq(testSensorId), any());
        inOrder.verify(mockThingHandler).postUpdate(eq(channelName(channelNo)), eq(state));
    } catch (OwException e) {
        Assert.fail("caught unexpected OwException");
    }
}
 
Example 2
Source File: OwserverConnection.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * check sensor presence
 *
 * @param path full owfs path to sensor
 * @return OnOffType, ON=present, OFF=not present
 * @throws OwException
 */
public State checkPresence(String path) throws OwException {
    State returnValue = OnOffType.OFF;
    try {
        OwserverPacket requestPacket;
        requestPacket = new OwserverPacket(OwserverMessageType.PRESENT, path, OwserverControlFlag.UNCACHED);

        OwserverPacket returnPacket = request(requestPacket);
        if (returnPacket.getReturnCode() == 0) {
            returnValue = OnOffType.ON;
        }

    } catch (OwException e) {
        returnValue = OnOffType.OFF;
    }
    logger.trace("presence {} : {}", path, returnValue);
    return returnValue;
}
 
Example 3
Source File: LifxLightHandler.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
private void handleOnOffCommand(OnOffType onOff) {
    HSBType localPowerOnColor = powerOnColor;
    if (localPowerOnColor != null && onOff == OnOffType.ON) {
        getLightStateForCommand().setColor(localPowerOnColor);
    }

    PercentType localPowerOnTemperature = powerOnTemperature;
    if (localPowerOnTemperature != null && onOff == OnOffType.ON) {
        getLightStateForCommand()
                .setTemperature(percentTypeToKelvin(localPowerOnTemperature, product.getTemperatureRange()));
    }

    if (powerOnBrightness != null) {
        PercentType newBrightness = onOff == OnOffType.ON ? powerOnBrightness : new PercentType(0);
        getLightStateForCommand().setBrightness(newBrightness);
    }
    getLightStateForCommand().setPowerState(onOff);
}
 
Example 4
Source File: DS2408Test.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
private void digitalChannelTest(OnOffType state, int channelNo) {
    instantiateDevice();

    BitSet returnValue = new BitSet(8);
    if (state == OnOffType.ON) {
        returnValue.flip(0, 8);
    }

    try {
        Mockito.when(mockBridgeHandler.checkPresence(testSensorId)).thenReturn(OnOffType.ON);
        Mockito.when(mockBridgeHandler.readBitSet(eq(testSensorId), any())).thenReturn(returnValue);

        testDevice.configureChannels();
        testDevice.refresh(mockBridgeHandler, true);

        inOrder.verify(mockBridgeHandler, times(2)).readBitSet(eq(testSensorId), any());
        inOrder.verify(mockThingHandler).postUpdate(eq(channelName(channelNo)), eq(state));
    } catch (OwException e) {
        Assert.fail("caught unexpected OwException");
    }
}
 
Example 5
Source File: ManagedItemProviderOSGiTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@SuppressWarnings("null")
@Test
public void assertGroupFunctionsAreStoredAndRetrievedAsWell() {
    assertThat(itemProvider.getAll().size(), is(0));

    GroupFunction function1 = new And(OnOffType.ON, OnOffType.OFF);
    GroupFunction function2 = new Sum();
    GroupItem item1 = new GroupItem("GroupItem1", new SwitchItem("Switch"), function1);
    GroupItem item2 = new GroupItem("GroupItem2", new NumberItem("Number"), function2);

    assertThat(item1.getName(), is("GroupItem1"));
    assertEquals(item1.getFunction().getClass(), And.class);
    assertThat(item1.getFunction().getParameters(), is(new State[] { OnOffType.ON, OnOffType.OFF }));

    assertThat(item2.name, is("GroupItem2"));
    assertEquals(item2.getFunction().getClass(), Sum.class);
    assertThat(item2.getFunction().getParameters(), is(new State[0]));

    itemProvider.add(item1);
    itemProvider.add(item2);

    Collection<Item> items = itemProvider.getAll();
    assertThat(items.size(), is(2));

    GroupItem result1 = (GroupItem) itemProvider.remove("GroupItem1");
    GroupItem result2 = (GroupItem) itemProvider.remove("GroupItem2");

    assertThat(result1.getName(), is("GroupItem1"));
    assertEquals(result1.getFunction().getClass(), And.class);
    assertThat(result1.function.getParameters(), is(new State[] { OnOffType.ON, OnOffType.OFF }));

    assertThat(result2.getName(), is("GroupItem2"));
    assertEquals(result2.getFunction().getClass(), Sum.class);
    assertThat(result2.function.getParameters(), is(new State[0]));

    assertThat(itemProvider.getAll().size(), is(0));
}
 
Example 6
Source File: WemoLightHandlerOSGiTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void handleOnCommandForSTATEChannel() throws MalformedURLException, URISyntaxException, ValidationException {
    Command command = OnOffType.ON;
    String channelID = WemoBindingConstants.CHANNEL_STATE;

    // Command ON for this channel sends the following data to the device
    String action = SET_ACTION;
    String value = "1";
    String capitability = "10006";

    assertRequestForCommand(channelID, command, action, value, capitability);
}
 
Example 7
Source File: WemoLightHandlerOSGiTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void handleONcommandForBRIGHTNESSchannel()
        throws MalformedURLException, URISyntaxException, ValidationException {
    Command command = OnOffType.ON;
    String channelID = WemoBindingConstants.CHANNEL_BRIGHTNESS;

    // Command ON for this channel sends the following data to the device
    String action = SET_ACTION;
    // ON is equal to brightness value of 255
    String value = "255:0";
    String capitability = "10008";

    assertRequestForCommand(channelID, command, action, value, capitability);
}
 
Example 8
Source File: RawButtonToggleSwitchProfile.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void onTriggerFromHandler(String event) {
    if (CommonTriggerEvents.PRESSED.equals(event)) {
        OnOffType newState = OnOffType.ON.equals(previousState) ? OnOffType.OFF : OnOffType.ON;
        callback.sendCommand(newState);
        previousState = newState;
    }
}
 
Example 9
Source File: WemoHandlerTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void assertThatChannelSTATEisUpdatedOnReceivedValue() {
    insightParams.state = STATE_PARAM;
    State expectedStateType = OnOffType.ON;
    String expectedChannel = CHANNEL_STATE;

    testOnValueReceived(expectedChannel, expectedStateType, insightParams.toString());
}
 
Example 10
Source File: DialogProcessor.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private void toggleProcessing(boolean value) {
    if (this.processing == value) {
        return;
    }
    this.processing = value;
    if (listeningItem != null && ItemUtil.isValidItemName(listeningItem)) {
        OnOffType command = (value) ? OnOffType.ON : OnOffType.OFF;
        eventPublisher.post(ItemEventFactory.createCommandEvent(listeningItem, command));
    }
}
 
Example 11
Source File: TradfriWirelessDeviceData.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
public OnOffType getBatteryLow() {
    if (generalInfo.get(DEVICE_BATTERY_LEVEL) != null) {
        return generalInfo.get(DEVICE_BATTERY_LEVEL).getAsInt() <= 10 ? OnOffType.ON : OnOffType.OFF;
    } else {
        return null;
    }
}
 
Example 12
Source File: GenericItemProvider2Test.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testGroupItemIsSame() {
    GenericItemProvider gip = new GenericItemProvider();

    GroupItem g1 = new GroupItem("testGroup", new SwitchItem("test"),
            new ArithmeticGroupFunction.Or(OnOffType.ON, OnOffType.OFF));
    GroupItem g2 = new GroupItem("testGroup", new SwitchItem("test"),
            new ArithmeticGroupFunction.Or(OnOffType.ON, OnOffType.OFF));

    assertFalse(gip.hasItemChanged(g1, g2));
}
 
Example 13
Source File: GroupItemTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void assertThatGroupItemChangesRespectGroupFunctionOR() {
    events.clear();
    GroupItem groupItem = new GroupItem("root", new SwitchItem("mySwitch"),
            new ArithmeticGroupFunction.Or(OnOffType.ON, OnOffType.OFF));
    groupItem.setItemStateConverter(itemStateConverter);

    SwitchItem sw1 = new SwitchItem("switch1");
    SwitchItem sw2 = new SwitchItem("switch2");
    groupItem.addMember(sw1);
    groupItem.addMember(sw2);

    groupItem.setEventPublisher(publisher);

    // State changes -> one change event is fired
    sw1.setState(OnOffType.ON);

    waitForAssert(() -> assertThat(events.size(), is(1)));

    List<Event> changes = events.stream().filter(it -> it instanceof GroupItemStateChangedEvent)
            .collect(Collectors.toList());
    assertThat(changes.size(), is(1));

    GroupItemStateChangedEvent change = (GroupItemStateChangedEvent) changes.get(0);
    assertTrue(change.getItemName().equals(groupItem.getName()));

    assertTrue(change.getOldItemState().equals(UnDefType.NULL));
    assertTrue(change.getItemState().equals(OnOffType.ON));

    assertTrue(groupItem.getState().equals(OnOffType.ON));
}
 
Example 14
Source File: GroupItemTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testGetStateAs_shouldEqualStateUpdate() {
    // Main group uses AND function
    GroupItem rootGroupItem = new GroupItem("root", new SwitchItem("baseItem"),
            new ArithmeticGroupFunction.And(OnOffType.ON, OnOffType.OFF));
    rootGroupItem.setItemStateConverter(itemStateConverter);

    TestItem member1 = new TestItem("member1");
    rootGroupItem.addMember(member1);
    TestItem member2 = new TestItem("member2");
    rootGroupItem.addMember(member2);

    // Sub-group uses NAND function
    GroupItem subGroup = new GroupItem("subGroup1", new SwitchItem("baseItem"),
            new ArithmeticGroupFunction.NAnd(OnOffType.ON, OnOffType.OFF));
    TestItem subMember = new TestItem("subGroup member 1");
    subGroup.addMember(subMember);
    rootGroupItem.addMember(subGroup);

    member1.setState(OnOffType.ON);
    member2.setState(OnOffType.ON);
    subMember.setState(OnOffType.OFF);

    // subGroup and subMember state differ
    assertThat(subGroup.getStateAs(OnOffType.class), is(OnOffType.ON));
    assertThat(subMember.getStateAs(OnOffType.class), is(OnOffType.OFF));

    // We expect ON here
    State getStateAsState = rootGroupItem.getStateAs(OnOffType.class);

    rootGroupItem.stateUpdated(member1, UnDefType.NULL); // recalculate the state
    State stateUpdatedState = rootGroupItem.getState();

    assertThat(getStateAsState, is(OnOffType.ON));
    assertThat(stateUpdatedState, is(OnOffType.ON));
}
 
Example 15
Source File: GenericItemProvider2Test.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testGroupItemChangesBaseItemAndFunction() {
    GenericItemProvider gip = new GenericItemProvider();

    GroupItem g1 = new GroupItem("testGroup", new SwitchItem("test"),
            new ArithmeticGroupFunction.Or(OnOffType.ON, OnOffType.OFF));
    GroupItem g2 = new GroupItem("testGroup", new NumberItem("number"), new ArithmeticGroupFunction.Sum());

    assertTrue(gip.hasItemChanged(g1, g2));
}
 
Example 16
Source File: GenericItemProvider2Test.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testGroupItemChangesBaseItem() {
    GenericItemProvider gip = new GenericItemProvider();

    GroupItem g1 = new GroupItem("testGroup", new SwitchItem("test"),
            new ArithmeticGroupFunction.Or(OnOffType.ON, OnOffType.OFF));
    GroupItem g2 = new GroupItem("testGroup", new NumberItem("test"),
            new ArithmeticGroupFunction.Or(OnOffType.ON, OnOffType.OFF));

    assertTrue(gip.hasItemChanged(g1, g2));
}
 
Example 17
Source File: OnOffValue.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public String getMQTTpublishValue() {
    return (state == OnOffType.ON) ? onString : offString;
}
 
Example 18
Source File: WemoLightHandler.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * The {@link getDeviceState} is used for polling the actual state of a WeMo Light and updating the according
 * channel states.
 */
public void getDeviceState() {
    logger.debug("Request actual state for LightID '{}'", wemoLightID);
    try {
        String soapHeader = "\"urn:Belkin:service:bridge:1#GetDeviceStatus\"";
        String content = "<?xml version=\"1.0\"?>"
                + "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
                + "<s:Body>" + "<u:GetDeviceStatus xmlns:u=\"urn:Belkin:service:bridge:1\">" + "<DeviceIDs>"
                + wemoLightID + "</DeviceIDs>" + "</u:GetDeviceStatus>" + "</s:Body>" + "</s:Envelope>";

        String wemoURL = getWemoURL();

        if (wemoURL != null) {
            String wemoCallResponse = wemoHttpCaller.executeCall(wemoURL, soapHeader, content);
            if (wemoCallResponse != null) {
                wemoCallResponse = StringEscapeUtils.unescapeXml(wemoCallResponse);
                String response = StringUtils.substringBetween(wemoCallResponse, "<CapabilityValue>",
                        "</CapabilityValue>");
                logger.trace("wemoNewLightState = {}", response);
                String[] splitResponse = response.split(",");
                if (splitResponse[0] != null) {
                    OnOffType binaryState = null;
                    binaryState = splitResponse[0].equals("0") ? OnOffType.OFF : OnOffType.ON;
                    if (binaryState != null) {
                        updateState(CHANNEL_STATE, binaryState);
                    }
                }
                if (splitResponse[1] != null) {
                    String splitBrightness[] = splitResponse[1].split(":");
                    if (splitBrightness[0] != null) {
                        int newBrightnessValue = Integer.valueOf(splitBrightness[0]);
                        int newBrightness = Math.round(newBrightnessValue * 100 / 255);
                        logger.trace("newBrightness = {}", newBrightness);
                        State newBrightnessState = new PercentType(newBrightness);
                        updateState(CHANNEL_BRIGHTNESS, newBrightnessState);
                        currentBrightness = newBrightness;
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new IllegalStateException("Could not retrieve new Wemo light state", e);
    }
}
 
Example 19
Source File: GroupItemTest.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void assertThatGroupItemChangesRespectGroupFunctionAND() {
    events.clear();
    GroupItem groupItem = new GroupItem("root", new SwitchItem("mySwitch"),
            new ArithmeticGroupFunction.And(OnOffType.ON, OnOffType.OFF));
    groupItem.setItemStateConverter(itemStateConverter);

    SwitchItem sw1 = new SwitchItem("switch1");
    SwitchItem sw2 = new SwitchItem("switch2");
    groupItem.addMember(sw1);
    groupItem.addMember(sw2);

    groupItem.setEventPublisher(publisher);

    // State changes -> one change event is fired
    sw1.setState(OnOffType.ON);

    waitForAssert(() -> assertThat(events.size(), is(1)));

    List<Event> changes = events.stream().filter(it -> it instanceof GroupItemStateChangedEvent)
            .collect(Collectors.toList());
    assertThat(changes.size(), is(1));

    GroupItemStateChangedEvent change = (GroupItemStateChangedEvent) changes.get(0);
    assertTrue(change.getItemName().equals(groupItem.getName()));

    // we expect that the group should now have status "OFF"
    assertTrue(change.getOldItemState().equals(UnDefType.NULL));
    assertTrue(change.getItemState().equals(OnOffType.OFF));

    events.clear();

    // State changes -> one change event is fired
    sw2.setState(OnOffType.ON);

    waitForAssert(() -> assertThat(events.size(), is(1)));

    changes = events.stream().filter(it -> it instanceof GroupItemStateChangedEvent).collect(Collectors.toList());
    assertThat(changes.size(), is(1));

    change = (GroupItemStateChangedEvent) changes.get(0);
    assertTrue(change.getItemName().equals(groupItem.getName()));

    // we expect that the group should now have status "ON"
    assertTrue(change.getOldItemState().equals(OnOffType.OFF));
    assertTrue(change.getItemState().equals(OnOffType.ON));

    assertTrue(groupItem.getState().equals(OnOffType.ON));
}
 
Example 20
Source File: GroupItemTest.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void assertThatGroupItemChangesRespectGroupFunctionORwithUNDEF() throws InterruptedException {
    events.clear();
    GroupItem groupItem = new GroupItem("root", new SwitchItem("mySwitch"),
            new ArithmeticGroupFunction.Or(OnOffType.ON, OnOffType.OFF));
    groupItem.setItemStateConverter(itemStateConverter);

    SwitchItem sw1 = new SwitchItem("switch1");
    SwitchItem sw2 = new SwitchItem("switch2");
    groupItem.addMember(sw1);
    groupItem.addMember(sw2);

    groupItem.setEventPublisher(publisher);

    // State changes -> one change event is fired
    sw1.setState(OnOffType.ON);

    waitForAssert(() -> assertThat(events.size(), is(1)));

    List<Event> changes = events.stream().filter(it -> it instanceof GroupItemStateChangedEvent)
            .collect(Collectors.toList());
    assertThat(changes.size(), is(1));

    GroupItemStateChangedEvent change = (GroupItemStateChangedEvent) changes.get(0);
    assertTrue(change.getItemName().equals(groupItem.getName()));

    assertTrue(change.getOldItemState().equals(UnDefType.NULL));
    assertTrue(change.getItemState().equals(OnOffType.ON));

    events.clear();

    sw2.setState(OnOffType.ON);

    sw2.setState(UnDefType.UNDEF);

    // wait to see that the event doesn't fire
    Thread.sleep(WAIT_EVENT_TO_BE_HANDLED);

    assertThat(events.size(), is(0));

    assertTrue(groupItem.getState().equals(OnOffType.ON));
}