use of org.openhab.core.thing.binding.BaseThingHandler in project openhab-core by openhab.
the class ThingManagerOSGiJavaTest method testChildHandlerInitializedModifiedUninitializedThing.
@Test
public void testChildHandlerInitializedModifiedUninitializedThing() {
Semaphore childHandlerInitializedSemaphore = new Semaphore(1);
Semaphore thingUpdatedSemapthore = new Semaphore(1);
registerThingHandlerFactory(BRIDGE_TYPE_UID, bridge -> new BaseBridgeHandler((Bridge) bridge) {
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
}
@Override
public void initialize() {
updateStatus(ThingStatus.ONLINE);
}
@Override
public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
assertDoesNotThrow(() -> childHandlerInitializedSemaphore.acquire());
}
});
registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
}
@Override
public void initialize() {
if (getBridge() == null) {
throw new IllegalStateException("Fail because of missing bridge");
}
updateStatus(ThingStatus.ONLINE);
}
@Override
public void thingUpdated(Thing thing) {
this.thing = thing;
assertDoesNotThrow(() -> thingUpdatedSemapthore.acquire());
}
});
Bridge bridge = BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_UID).build();
managedThingProvider.add(bridge);
waitForAssert(() -> {
assertEquals(ThingStatus.ONLINE, bridge.getStatus());
});
Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
managedThingProvider.add(thing);
waitForAssert(() -> {
assertEquals(ThingStatus.UNINITIALIZED, thing.getStatus());
assertEquals(ThingStatusDetail.HANDLER_INITIALIZING_ERROR, thing.getStatusInfo().getStatusDetail());
});
assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
thing.setBridgeUID(bridge.getUID());
managedThingProvider.update(thing);
waitForAssert(() -> {
assertEquals(ThingStatus.ONLINE, thing.getStatus());
});
// childHandlerInitialized(...) must be called
waitForAssert(() -> assertEquals(0, childHandlerInitializedSemaphore.availablePermits()));
// thingUpdated(...) is not called
assertEquals(1, thingUpdatedSemapthore.availablePermits());
}
use of org.openhab.core.thing.binding.BaseThingHandler 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.binding.BaseThingHandler in project openhab-core by openhab.
the class ThingManagerOSGiJavaTest method testChildHandlerInitializedReplacedInitializedThing.
@Test
public void testChildHandlerInitializedReplacedInitializedThing() {
Semaphore childHandlerInitializedSemaphore = new Semaphore(1);
Semaphore thingUpdatedSemapthore = new Semaphore(1);
registerThingHandlerFactory(BRIDGE_TYPE_UID, bridge -> new BaseBridgeHandler((Bridge) bridge) {
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
}
@Override
public void initialize() {
updateStatus(ThingStatus.ONLINE);
}
@Override
public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
assertDoesNotThrow(() -> childHandlerInitializedSemaphore.acquire());
}
});
registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
}
@Override
public void initialize() {
updateStatus(ThingStatus.ONLINE);
}
@Override
public void thingUpdated(Thing thing) {
this.thing = thing;
assertDoesNotThrow(() -> thingUpdatedSemapthore.acquire());
}
});
Bridge bridge = BridgeBuilder.create(BRIDGE_TYPE_UID, BRIDGE_UID).build();
managedThingProvider.add(bridge);
waitForAssert(() -> {
assertEquals(ThingStatus.ONLINE, bridge.getStatus());
});
Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).build();
managedThingProvider.add(thing);
waitForAssert(() -> {
assertEquals(ThingStatus.ONLINE, thing.getStatus());
});
assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
Thing thing2 = ThingBuilder.create(THING_TYPE_UID, THING_UID).withBridge(BRIDGE_UID).build();
managedThingProvider.update(thing2);
waitForAssert(() -> {
assertEquals(ThingStatus.ONLINE, thing2.getStatus());
});
// childHandlerInitialized(...) is not be called - framework calls ThingHandler.thingUpdated(...) instead.
assertEquals(1, childHandlerInitializedSemaphore.availablePermits());
// ThingHandler.thingUpdated(...) must be called
assertEquals(0, thingUpdatedSemapthore.availablePermits());
}
use of org.openhab.core.thing.binding.BaseThingHandler in project openhab-core by openhab.
the class GenericThingProviderTest4 method setUp.
@BeforeEach
public void setUp() {
registerVolatileStorageService();
readyService = getService(ReadyService.class);
assertThat(readyService, is(notNullValue()));
thingRegistry = getService(ThingRegistry.class);
assertThat(thingRegistry, is(notNullValue()));
modelRepository = getService(ModelRepository.class);
assertThat(modelRepository, is(notNullValue()));
modelRepository.removeModel(TESTMODEL_NAME);
ComponentContext componentContextMock = mock(ComponentContext.class);
when(componentContextMock.getBundleContext()).thenReturn(bundleContext);
hueThingHandlerFactory = new TestHueThingHandlerFactoryX(componentContextMock) {
@Override
@Nullable
protected ThingHandler createHandler(final Thing thing) {
if (thing instanceof Bridge) {
return new TestBridgeHandler((Bridge) thing);
} else {
return new BaseThingHandler(thing) {
@Override
public void handleCommand(ChannelUID arg0, Command arg1) {
}
@Override
public void initialize() {
updateStatus(ThingStatus.ONLINE);
}
};
}
}
};
bundle = FrameworkUtil.getBundle(TestHueThingHandlerFactoryX.class);
removeReadyMarker();
}
use of org.openhab.core.thing.binding.BaseThingHandler in project openhab-core by openhab.
the class ThingManagerOSGiJavaTest method testInitializeOnlyIfInitializable.
@Test
public void testInitializeOnlyIfInitializable() throws Exception {
registerThingTypeProvider();
registerChannelTypeProvider();
registerThingHandlerFactory(THING_TYPE_UID, thing -> new BaseThingHandler(thing) {
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
}
@Override
public void initialize() {
updateStatus(ThingStatus.ONLINE);
}
});
ConfigDescriptionProvider mockConfigDescriptionProvider = mock(ConfigDescriptionProvider.class);
ConfigDescriptionParameter parameter = ConfigDescriptionParameterBuilder.create(CONFIG_PARAM_NAME, Type.TEXT).withRequired(true).build();
registerService(mockConfigDescriptionProvider, ConfigDescriptionProvider.class.getName());
// verify a missing mandatory thing config prevents it from getting initialized
when(mockConfigDescriptionProvider.getConfigDescription(eq(configDescriptionThing), any())).thenReturn(ConfigDescriptionBuilder.create(configDescriptionThing).withParameter(parameter).build());
assertThingStatus(Collections.emptyMap(), Collections.emptyMap(), ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_CONFIGURATION_PENDING);
// verify a missing mandatory channel config prevents it from getting initialized
when(mockConfigDescriptionProvider.getConfigDescription(eq(configDescriptionChannel), any())).thenReturn(ConfigDescriptionBuilder.create(configDescriptionChannel).withParameter(parameter).build());
assertThingStatus(Map.of(CONFIG_PARAM_NAME, "value"), Collections.emptyMap(), ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_CONFIGURATION_PENDING);
// verify a satisfied config does not prevent it from getting initialized anymore
assertThingStatus(Map.of(CONFIG_PARAM_NAME, "value"), Map.of(CONFIG_PARAM_NAME, "value"), ThingStatus.ONLINE, ThingStatusDetail.NONE);
}
Aggregations