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"));
}
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;
}
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);
}
});
}
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())));
}
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()));
}
}
Aggregations