use of org.openhab.core.thing.binding.ThingHandlerFactory in project openhab-core by openhab.
the class DynamicThingUpdateOSGiTest method setUp.
@BeforeEach
public void setUp() {
registerVolatileStorageService();
ThingTypeProvider thingTypeProvider = mock(ThingTypeProvider.class);
when(thingTypeProvider.getThingType(eq(THING_TYPE_UID), any())).thenReturn(THING_TYPE);
registerService(thingTypeProvider);
inbox = getService(Inbox.class);
managedThingProvider = getService(ManagedThingProvider.class);
assertEquals(0, inbox.getAll().size());
ThingHandlerFactory thingHandlerFactory = createThingHandlerFactory();
registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
}
use of org.openhab.core.thing.binding.ThingHandlerFactory 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()));
}
}
use of org.openhab.core.thing.binding.ThingHandlerFactory in project openhab-core by openhab.
the class ChannelCommandDescriptionProviderOSGiTest method beforeEach.
@BeforeEach
public void beforeEach() throws Exception {
Mockito.when(componentContextMock.getBundleContext()).thenReturn(bundleContext);
registerVolatileStorageService();
itemRegistry = getService(ItemRegistry.class);
assertNotNull(itemRegistry);
final TestThingHandlerFactory thingHandlerFactory = new TestThingHandlerFactory();
thingHandlerFactory.activate(componentContextMock);
registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
final StateDescriptionFragment stateDescriptionFragment = StateDescriptionFragmentBuilder.create().withMinimum(BigDecimal.ZERO).withMaximum(BigDecimal.valueOf(100)).withStep(BigDecimal.TEN).withPattern("%d Peek").withReadOnly(true).withOption(new StateOption("SOUND", "My great sound.")).build();
final CommandDescription command = CommandDescriptionBuilder.create().withCommandOption(new CommandOption("COMMAND", "My command.")).build();
final ChannelType channelType1 = ChannelTypeBuilder.state(new ChannelTypeUID("hue:state-as-command"), " ", CoreItemFactory.NUMBER).withStateDescriptionFragment(stateDescriptionFragment).build();
final ChannelType channelType2 = ChannelTypeBuilder.state(new ChannelTypeUID("hue:static"), " ", CoreItemFactory.STRING).withTag("Light").withCommandDescription(command).build();
final ChannelType channelType3 = ChannelTypeBuilder.state(CHANNEL_TYPE_UID, " ", CoreItemFactory.STRING).withTag("Light").build();
List<ChannelType> channelTypes = new ArrayList<>();
channelTypes.add(channelType1);
channelTypes.add(channelType2);
channelTypes.add(channelType3);
registerService(new ChannelTypeProvider() {
@Override
public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
return channelTypes;
}
@Override
@Nullable
public ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
for (final ChannelType channelType : channelTypes) {
if (channelType.getUID().equals(channelTypeUID)) {
return channelType;
}
}
return null;
}
});
testBundle = SyntheticBundleInstaller.install(bundleContext, TEST_BUNDLE_NAME);
assertThat(testBundle, is(notNullValue()));
thingStatusInfoI18nLocalizationService = getService(ThingStatusInfoI18nLocalizationService.class);
assertThat(thingStatusInfoI18nLocalizationService, is(notNullValue()));
thingStatusInfoI18nLocalizationService.setBundleResolver(new BundleResolverImpl());
List<ChannelDefinition> channelDefinitions = new ArrayList<>();
channelDefinitions.add(new ChannelDefinitionBuilder("1", channelType1.getUID()).build());
channelDefinitions.add(new ChannelDefinitionBuilder("7_1", channelType2.getUID()).build());
channelDefinitions.add(new ChannelDefinitionBuilder("7_2", channelType3.getUID()).build());
registerService(new SimpleThingTypeProvider(Set.of(ThingTypeBuilder.instance(new ThingTypeUID("hue:lamp"), "label").withChannelDefinitions(channelDefinitions).build())));
List<Item> items = new ArrayList<>();
items.add(new NumberItem("TestItem1"));
items.add(new NumberItem("TestItem7_1"));
items.add(new NumberItem("TestItem7_2"));
registerService(new TestItemProvider(items));
linkRegistry = getService(ItemChannelLinkRegistry.class);
}
use of org.openhab.core.thing.binding.ThingHandlerFactory in project openhab-core by openhab.
the class ThingRegistryImpl method createThingOfType.
@Override
@Nullable
public Thing createThingOfType(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID, @Nullable String label, Configuration configuration) {
logger.debug("Creating thing for type '{}'.", thingTypeUID);
for (ThingHandlerFactory thingHandlerFactory : thingHandlerFactories) {
if (thingHandlerFactory.supportsThingType(thingTypeUID)) {
Thing thing = thingHandlerFactory.createThing(thingTypeUID, configuration, thingUID, bridgeUID);
if (thing == null) {
logger.warn("Cannot create thing of type '{}'. Binding '{}' says it supports it, but it could not be created.", thingTypeUID, thingHandlerFactory.getClass().getName());
} else {
thing.setLabel(label);
return thing;
}
}
}
logger.warn("Cannot create thing. No binding found that supports creating a thing of type '{}'.", thingTypeUID);
return null;
}
use of org.openhab.core.thing.binding.ThingHandlerFactory in project openhab-core by openhab.
the class ThingManagerImpl method thingUpdated.
@Override
@SuppressWarnings("PMD.CompareObjectsWithEquals")
public void thingUpdated(final Thing thing, ThingTrackerEvent thingTrackerEvent) {
ThingUID thingUID = thing.getUID();
if (thingUpdatedLock.contains(thingUID)) {
// called from the thing handler itself, therefore
// it exists, is initializing/initialized and
// must not be informed (in order to prevent infinite loops)
replaceThing(getThing(thingUID), thing);
} else {
Lock lock1 = getLockForThing(thing.getUID());
try {
lock1.lock();
Thing oldThing = getThing(thingUID);
ThingHandler thingHandler = replaceThing(oldThing, thing);
if (thingHandler != null) {
if (ThingHandlerHelper.isHandlerInitialized(thing) || isInitializing(thing)) {
if (oldThing != null) {
oldThing.setHandler(null);
}
thing.setHandler(thingHandler);
if (isInitializable(thing, getThingType(thing))) {
safeCaller.create(thingHandler, ThingHandler.class).build().thingUpdated(thing);
} else {
final ThingHandlerFactory thingHandlerFactory = findThingHandlerFactory(thing.getThingTypeUID());
if (thingHandlerFactory != null) {
if (isBridge(thing)) {
unregisterAndDisposeChildHandlers((Bridge) thing, thingHandlerFactory);
}
disposeHandler(thing, thingHandler);
setThingStatus(thing, buildStatusInfo(ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_CONFIGURATION_PENDING, "@text/missing-or-invalid-configuration"));
}
}
} else {
logger.debug("Cannot notify handler about updated thing '{}', because handler is not initialized (thing must be in status UNKNOWN, ONLINE or OFFLINE).", thing.getThingTypeUID());
if (thingHandler.getThing() == thing) {
logger.debug("Initializing handler of thing '{}'", thing.getThingTypeUID());
if (oldThing != null) {
oldThing.setHandler(null);
}
thing.setHandler(thingHandler);
initializeHandler(thing);
} else {
logger.debug("Replacing uninitialized handler for updated thing '{}'", thing.getThingTypeUID());
ThingHandlerFactory thingHandlerFactory = getThingHandlerFactory(thing);
if (thingHandlerFactory != null) {
unregisterHandler(thingHandler.getThing(), thingHandlerFactory);
} else {
logger.debug("No ThingHandlerFactory available that can handle {}", thing.getThingTypeUID());
}
registerAndInitializeHandler(thing, thingHandlerFactory);
}
}
} else {
registerAndInitializeHandler(thing, getThingHandlerFactory(thing));
}
} finally {
lock1.unlock();
}
}
}
Aggregations