Search in sources :

Example 6 with ThingRegistry

use of org.openhab.core.thing.ThingRegistry in project openhab-core by openhab.

the class GenericThingProviderTest method assertThatTheThingsInAnUpdatedThingsFileIsRegisteredInTheThingRegistry.

@Test
@SuppressWarnings("null")
public void assertThatTheThingsInAnUpdatedThingsFileIsRegisteredInTheThingRegistry() {
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    assertThat(thingRegistry, is(notNullValue()));
    Collection<Thing> things = thingRegistry.getAll();
    assertThat(things.size(), is(0));
    ModelRepository modelRepository = getService(ModelRepository.class);
    assertThat(modelRepository, is(notNullValue()));
    String model = // 
    "Bridge hue:bridge:myBridge [ ip = \"1.2.3.4\", username = \"123\" ] {" + // 
    "    LCT001 bulb1 [ lightId = \"1\" ] { Switch : notification }" + // 
    "    Bridge bridge myBridge2 [ ] {" + // 
    "        LCT001 bulb2 [ ]" + // 
    "    }" + // 
    "}" + // 
    "hue:LCT001:bulb3 [ lightId = \"4\" ] {" + // 
    "    Switch : notification [ duration = \"5\" ]" + "}";
    modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
    String newModel = // 
    "Bridge hue:bridge:myBridge [ ip = \"5.6.7.8\", secret = \"123\" ] {" + // 
    "    LCT001 bulb1 [ ]" + // 
    "}" + // 
    "hue:LCT001:bulb2 [ lightId = \"2\" ] {" + // 
    "    Color : color" + "}";
    modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(newModel.getBytes()));
    Collection<Thing> actualThings = thingRegistry.getAll();
    assertThat(actualThings.size(), is(3));
    Thing bridge1 = actualThings.stream().filter(t -> "hue:bridge:myBridge".equals(t.getUID().toString())).findFirst().get();
    assertThat(bridge1, instanceOf(Bridge.class));
    assertThat(bridge1.getChannels().size(), is(0));
    assertThat(bridge1.getBridgeUID(), is(nullValue()));
    assertThat(bridge1.getConfiguration().values().size(), is(2));
    assertThat(bridge1.getConfiguration().get("ip"), is("5.6.7.8"));
    assertThat(bridge1.getConfiguration().get("secret"), is("123"));
    assertThat(bridge1.getThingTypeUID().toString(), is("hue:bridge"));
    Thing bulb1 = actualThings.stream().filter(t -> "hue:LCT001:myBridge:bulb1".equals(t.getUID().toString())).findFirst().get();
    assertThat(bulb1, isA(Thing.class));
    // there should be color and color_temperature from thingType definition
    assertThat(bulb1.getChannels().size(), is(2));
    assertThat(bulb1.getBridgeUID(), is(bridge1.getUID()));
    assertThat(bulb1.getConfiguration().values().size(), is(0));
    assertThat(bulb1.getThingTypeUID().toString(), is("hue:LCT001"));
    Thing bulb2 = actualThings.stream().filter(t -> "hue:LCT001:bulb2".equals(t.getUID().toString())).findFirst().get();
    assertThat(bulb2, isA(Thing.class));
    // channels should be Color as defined in dsl and color_temperature from thingType
    assertThat(bulb2.getChannels().size(), is(2));
    Channel firstChannel = bulb2.getChannels().get(0);
    assertThat(firstChannel.getUID().toString(), is("hue:LCT001:bulb2:color"));
    assertThat(firstChannel.getAcceptedItemType(), is(CoreItemFactory.COLOR));
    assertThat(bulb2.getBridgeUID(), is(nullValue()));
    assertThat(bulb2.getConfiguration().values().size(), is(1));
    assertThat(bulb2.getConfiguration().get("lightId"), is("2"));
    assertThat(bulb2.getThingTypeUID().toString(), is("hue:LCT001"));
}
Also used : ModelRepository(org.openhab.core.model.core.ModelRepository) ByteArrayInputStream(java.io.ByteArrayInputStream) Channel(org.openhab.core.thing.Channel) Thing(org.openhab.core.thing.Thing) Bridge(org.openhab.core.thing.Bridge) ThingRegistry(org.openhab.core.thing.ThingRegistry) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 7 with ThingRegistry

use of org.openhab.core.thing.ThingRegistry in project openhab-core by openhab.

the class ScriptThingActionsImpl method get.

@Override
@Nullable
public ThingActions get(@Nullable String scope, @Nullable String thingUid) {
    ThingRegistry thingRegistry = this.thingRegistry;
    if (thingUid != null && scope != null && thingRegistry != null) {
        ThingUID uid = new ThingUID(thingUid);
        Thing thing = thingRegistry.get(uid);
        if (thing != null) {
            ThingHandler handler = thing.getHandler();
            if (handler != null) {
                return THING_ACTIONS_MAP.get(getKey(scope, thingUid));
            }
        }
    }
    return null;
}
Also used : ThingUID(org.openhab.core.thing.ThingUID) ThingHandler(org.openhab.core.thing.binding.ThingHandler) Thing(org.openhab.core.thing.Thing) ThingRegistry(org.openhab.core.thing.ThingRegistry) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 8 with ThingRegistry

use of org.openhab.core.thing.ThingRegistry in project openhab-core by openhab.

the class ThingManagerOSGiJavaTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    configDescriptionChannel = new URI("test:channel");
    configDescriptionThing = new URI("test:test");
    thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).withChannels(// 
    List.of(ChannelBuilder.create(CHANNEL_UID, CoreItemFactory.SWITCH).withLabel("Test Label").withDescription("Test Description").withType(CHANNEL_TYPE_UID).withDefaultTags(Set.of("Test Tag")).build())).build();
    registerVolatileStorageService();
    managedThingProvider = getService(ManagedThingProvider.class);
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    thingRegistry = getService(ThingRegistry.class);
    assertNotNull(thingRegistry);
    StorageService storageService;
    storageService = getService(StorageService.class);
    assertNotNull(storageService);
    storage = storageService.getStorage("thing_status_storage");
    itemChannelLinkRegistry = getService(ItemChannelLinkRegistry.class);
    assertNotNull(itemChannelLinkRegistry);
    readyService = getService(ReadyService.class);
    assertNotNull(readyService);
    thingManager = getService(ThingManager.class);
    assertNotNull(thingManager);
    waitForAssert(() -> {
        try {
            assertThat(bundleContext.getServiceReferences(ReadyMarker.class, "(openhab.xmlThingTypes=" + bundleContext.getBundle().getSymbolicName() + ")"), is(notNullValue()));
        } catch (InvalidSyntaxException e) {
            throw new IllegalArgumentException(e);
        }
    });
}
Also used : ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ReadyService(org.openhab.core.service.ReadyService) ItemChannelLinkRegistry(org.openhab.core.thing.link.ItemChannelLinkRegistry) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ThingManager(org.openhab.core.thing.ThingManager) ItemRegistry(org.openhab.core.items.ItemRegistry) ReadyMarker(org.openhab.core.service.ReadyMarker) URI(java.net.URI) ThingRegistry(org.openhab.core.thing.ThingRegistry) StorageService(org.openhab.core.storage.StorageService) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 9 with ThingRegistry

use of org.openhab.core.thing.ThingRegistry in project openhab-core by openhab.

the class ThingRegistryOSGiTest method assertThatThingRegistryDelegatesConfigUpdateToThingHandler.

@Test
public void assertThatThingRegistryDelegatesConfigUpdateToThingHandler() {
    ThingUID thingUID = new ThingUID("binding:type:thing");
    Thing thing = ThingBuilder.create(THING_TYPE_UID, thingUID).build();
    ThingHandler thingHandler = new BaseThingHandler(thing) {

        @Override
        public void handleCommand(ChannelUID channelUID, Command command) {
        }

        @Override
        public void handleConfigurationUpdate(Map<String, Object> configurationParameters) {
            changedParameters = configurationParameters;
        }

        @Override
        public void initialize() {
            updateStatus(ThingStatus.ONLINE);
        }
    };
    thing.setHandler(thingHandler);
    ThingProvider thingProvider = new ThingProvider() {

        @Override
        public void addProviderChangeListener(ProviderChangeListener<Thing> listener) {
        }

        @Override
        public Collection<Thing> getAll() {
            return Set.of(thing);
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<Thing> listener) {
        }
    };
    registerService(thingProvider);
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    Map<String, Object> parameters = Map.ofEntries(entry("param1", "value1"), entry("param2", 1));
    thingRegistry.updateConfiguration(thingUID, parameters);
    assertThat(changedParameters.entrySet(), is(equalTo(parameters.entrySet())));
}
Also used : BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ThingHandler(org.openhab.core.thing.binding.ThingHandler) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ThingRegistry(org.openhab.core.thing.ThingRegistry) ThingProvider(org.openhab.core.thing.ThingProvider) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) Command(org.openhab.core.types.Command) ChannelUID(org.openhab.core.thing.ChannelUID) ThingUID(org.openhab.core.thing.ThingUID) ProviderChangeListener(org.openhab.core.common.registry.ProviderChangeListener) Map(java.util.Map) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 10 with ThingRegistry

use of org.openhab.core.thing.ThingRegistry in project openhab-core by openhab.

the class ThingRegistryOSGiTest method assertThatCreateThingDelegatesToRegisteredThingHandlerFactory.

@Test
public void assertThatCreateThingDelegatesToRegisteredThingHandlerFactory() {
    ThingTypeUID expectedThingTypeUID = THING_TYPE_UID;
    ThingUID expectedBridgeUID = new ThingUID(THING_TYPE_UID, THING2_ID);
    ThingUID expectedThingUID = new ThingUID(THING_TYPE_UID, expectedBridgeUID, THING1_ID);
    String expectedLabel = "Test Thing";
    Configuration expectedConfiguration = new Configuration();
    AtomicReference<Thing> thingResultWrapper = new AtomicReference<>();
    ThingRegistry thingRegistry = getService(ThingRegistry.class);
    ThingHandlerFactory thingHandlerFactory = new BaseThingHandlerFactory() {

        @Override
        public boolean supportsThingType(ThingTypeUID thingTypeUID) {
            return true;
        }

        @Override
        @Nullable
        protected ThingHandler createHandler(Thing thing) {
            return null;
        }

        @Override
        @Nullable
        public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
            assertThat(thingTypeUID, is(expectedThingTypeUID));
            assertThat(configuration, is(expectedConfiguration));
            assertThat(thingUID, is(expectedThingUID));
            assertThat(bridgeUID, is(expectedBridgeUID));
            Thing thing = ThingBuilder.create(thingTypeUID, thingUID.getId()).withBridge(bridgeUID).build();
            thingResultWrapper.set(thing);
            return thing;
        }
    };
    registerThingHandlerFactory(thingHandlerFactory);
    Thing thing = thingRegistry.createThingOfType(expectedThingTypeUID, expectedThingUID, expectedBridgeUID, expectedLabel, expectedConfiguration);
    waitForAssert(() -> {
        assertTrue(thingResultWrapper.get() != null);
    });
    assertThat(thing, is(notNullValue()));
    if (thing != null) {
        assertThat(thing, is(thingResultWrapper.get()));
    }
}
Also used : Configuration(org.openhab.core.config.core.Configuration) BaseThingHandlerFactory(org.openhab.core.thing.binding.BaseThingHandlerFactory) ThingUID(org.openhab.core.thing.ThingUID) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) AtomicReference(java.util.concurrent.atomic.AtomicReference) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) BaseThingHandlerFactory(org.openhab.core.thing.binding.BaseThingHandlerFactory) Thing(org.openhab.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) ThingRegistry(org.openhab.core.thing.ThingRegistry) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

ThingRegistry (org.openhab.core.thing.ThingRegistry)29 Thing (org.openhab.core.thing.Thing)18 Test (org.junit.jupiter.api.Test)17 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)14 ThingUID (org.openhab.core.thing.ThingUID)13 ManagedThingProvider (org.openhab.core.thing.ManagedThingProvider)11 Configuration (org.openhab.core.config.core.Configuration)9 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)9 BeforeEach (org.junit.jupiter.api.BeforeEach)8 ThingHandler (org.openhab.core.thing.binding.ThingHandler)7 Nullable (org.eclipse.jdt.annotation.Nullable)6 ThingHandlerFactory (org.openhab.core.thing.binding.ThingHandlerFactory)6 Bridge (org.openhab.core.thing.Bridge)5 ThingProvider (org.openhab.core.thing.ThingProvider)5 ItemChannelLink (org.openhab.core.thing.link.ItemChannelLink)5 Item (org.openhab.core.items.Item)4 NumberItem (org.openhab.core.library.items.NumberItem)4 VolatileStorageService (org.openhab.core.test.storage.VolatileStorageService)4 Locale (java.util.Locale)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3