Java Code Examples for org.eclipse.smarthome.core.thing.Thing#setProperty()

The following examples show how to use org.eclipse.smarthome.core.thing.Thing#setProperty() . 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: ChangeThingTypeOSGiTest.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void assertChangingThingTypeWithinInitializeWorksEvenIfServiceDeregistrationIsSlow() {
    // println "[ChangeThingTypeOSGiTest] ======== assert changing thing type within initialize works even if
    // service deregistration is slow"
    selfChanging = true;
    unregisterHandlerDelay = 6000;
    // println "[ChangeThingTypeOSGiTest] Create thing"
    Thing thing = ThingFactory.createThing(thingTypeGeneric, new ThingUID("testBinding", "testThing"),
            new Configuration(), null, configDescriptionRegistry);
    thing.setProperty("universal", "survives");
    // println "[ChangeThingTypeOSGiTest] Add thing to managed thing provider"
    managedThingProvider.add(thing);

    // println "[ChangeThingTypeOSGiTest] Wait for thing changed"
    assertThingWasChanged(thing);
}
 
Example 2
Source File: ThingFactory.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
public static @Nullable Thing createThing(ThingUID thingUID, Configuration configuration,
        @Nullable Map<String, String> properties, @Nullable ThingUID bridgeUID, ThingTypeUID thingTypeUID,
        List<ThingHandlerFactory> thingHandlerFactories) {
    for (ThingHandlerFactory thingHandlerFactory : thingHandlerFactories) {
        if (thingHandlerFactory.supportsThingType(thingTypeUID)) {
            Thing thing = thingHandlerFactory.createThing(thingTypeUID, configuration, thingUID, bridgeUID);
            if (thing == null) {
                LOGGER.error(
                        "Thing factory ({}) returned null on create thing when it reports to support the thing type ({}).",
                        thingHandlerFactory.getClass(), thingTypeUID);
            } else {
                if (properties != null) {
                    for (String key : properties.keySet()) {
                        thing.setProperty(key, properties.get(key));
                    }
                }
            }
            return thing;
        }
    }
    return null;
}
 
Example 3
Source File: ModelRestrictedFirmwareUpdateServiceOSGiTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Creates a thing, adds it to the thing registry, and registers a {@link FirmwareUpdateHandler} for the thing.
 */
private Thing createAndRegisterThing(String thingUID, String modelId, String firmwareVersion) {
    Thing thing = ThingBuilder.create(thingType.getUID(), thingUID).build();
    thing.setProperty(PROPERTY_MODEL_ID, modelId);
    thing.setProperty(PROPERTY_FIRMWARE_VERSION, firmwareVersion);

    managedThingProvider.add(thing);

    registerService(createFirmwareUpdateHandler(thing));

    return thing;
}
 
Example 4
Source File: FirmwareTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testModelRestrictedFirmwareIsSuitableForThingWithSameThingTypeAndSameModel() {
    Firmware firmware = FirmwareBuilder.create(new ThingTypeUID("binding:thingTypeA"), "version")
            .withModelRestricted(true).withModel("someModel").build();
    Thing thing = ThingBuilder.create(new ThingTypeUID("binding:thingTypeA"), "thing").build();
    thing.setProperty(Thing.PROPERTY_MODEL_ID, "someModel");

    assertThat(firmware.isSuitableFor(thing), is(true));
}
 
Example 5
Source File: FirmwareTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testModelRestrictedFirmwareIsNotSuitableForThingWithSameThingTypeAndAnotherModel() {
    Firmware firmware = FirmwareBuilder.create(new ThingTypeUID("binding:thingTypeA"), "version")
            .withModelRestricted(true).withModel("someModel").build();
    Thing thing = ThingBuilder.create(new ThingTypeUID("binding:thingTypeA"), "thing").build();
    thing.setProperty(Thing.PROPERTY_MODEL_ID, "someOtherModel");

    assertThat(firmware.isSuitableFor(thing), is(false));
}
 
Example 6
Source File: ChangeThingTypeOSGiTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void assertChangingTheThingTypeWorks() {
    // println "[ChangeThingTypeOSGiTest] ======== assert changing the ThingType works"
    Thing thing = ThingFactory.createThing(thingTypeGeneric, new ThingUID("testBinding", "testThing"),
            new Configuration(), null, configDescriptionRegistry);
    thing.setProperty("universal", "survives");
    managedThingProvider.add(thing);

    // Pre-flight checks - see below
    assertThat(thing.getHandler(), instanceOf(GenericThingHandler.class));
    assertThat(thing.getConfiguration().get("parametergeneric"), is("defaultgeneric"));
    assertThat(thing.getConfiguration().get("providedspecific"), is(nullValue()));
    assertThat(thing.getChannels().size(), is(1));
    assertThat(thing.getChannels().get(0).getUID(), is(CHANNEL_GENERIC_UID));
    assertThat(thing.getProperties().get("universal"), is("survives"));

    ThingHandlerFactory handlerFactory = getService(ThingHandlerFactory.class, SampleThingHandlerFactory.class);
    assertThat(handlerFactory, not(nullValue()));

    thing.getHandler().handleCommand(mock(ChannelUID.class), mock(Command.class));
    waitForAssert(() -> {
        assertThat(thing.getStatus(), is(ThingStatus.ONLINE));
    }, 4000, 100);

    // Now do the actual migration
    Map<String, Object> properties = new HashMap<>(1);
    properties.put("providedspecific", "there");
    ((BaseThingHandler) thing.getHandler()).changeThingType(THING_TYPE_SPECIFIC_UID, new Configuration(properties));

    assertThingWasChanged(thing);
}
 
Example 7
Source File: ChangeThingTypeOSGiTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void assertChangingThingTypeWithinInitializeWorks() {
    // println "[ChangeThingTypeOSGiTest] ======== assert changing thing type within initialize works"
    selfChanging = true;
    // println "[ChangeThingTypeOSGiTest] Create thing"
    Thing thing = ThingFactory.createThing(thingTypeGeneric, new ThingUID("testBinding", "testThing"),
            new Configuration(), null, configDescriptionRegistry);
    thing.setProperty("universal", "survives");
    // println "[ChangeThingTypeOSGiTest] Add thing to managed thing provider"
    managedThingProvider.add(thing);

    // println "[ChangeThingTypeOSGiTest] Wait for thing changed"
    assertThingWasChanged(thing);
}
 
Example 8
Source File: ChangeThingTypeOSGiTest.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void assertLoadingSpecializedThingTypeWorksDirectly() {
    // println "[ChangeThingTypeOSGiTest] ======== assert loading specialized thing type works directly"

    StorageService storage = getService(StorageService.class);
    Map<String, Object> properties = new HashMap<>(1);
    properties.put("providedspecific", "there");
    Thing persistedThing = ThingFactory.createThing(thingTypeSpecific,
            new ThingUID("testBinding", "persistedThing"), new Configuration(properties), null, null);
    persistedThing.setProperty("universal", "survives");
    storage.getStorage(Thing.class.getName()).put("testBinding::persistedThing", persistedThing);
    selfChanging = true;

    unregisterService(storage);
    managedThingProvider = getService(ManagedThingProvider.class);
    assertThat(managedThingProvider, is(nullValue()));

    registerService(storage);
    managedThingProvider = getService(ManagedThingProvider.class);
    assertThat(managedThingProvider, is(notNullValue()));

    Collection<Thing> res = managedThingProvider.getAll();
    assertThat(res.size(), is(1));

    Thing thing = res.iterator().next();
    assertThat(thing.getUID().toString(), is("testBinding::persistedThing"));

    // Ensure that the ThingHandler has been registered as an OSGi service correctly
    waitForAssert(() -> {
        assertThat(thing.getHandler(), instanceOf(SpecificThingHandler.class));
    }, 4000, 100);
    ThingHandlerFactory handlerFactory = getService(ThingHandlerFactory.class, SampleThingHandlerFactory.class);
    assertThat(handlerFactory, not(nullValue()));

    // Ensure it's initialized
    waitForAssert(() -> {
        assertThat(specificInits, is(1));
        assertThat(genericInits, is(0));
    });

    // Ensure the Thing is ONLINE again
    assertThat(thing.getStatus(), is(ThingStatus.ONLINE));
}