Search in sources :

Example 1 with BaseThingHandler

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());
}
Also used : BaseBridgeHandler(org.openhab.core.thing.binding.BaseBridgeHandler) Command(org.openhab.core.types.Command) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ChannelUID(org.openhab.core.thing.ChannelUID) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ThingHandler(org.openhab.core.thing.binding.ThingHandler) Semaphore(java.util.concurrent.Semaphore) Bridge(org.openhab.core.thing.Bridge) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 2 with BaseThingHandler

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())));
}
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 3 with BaseThingHandler

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());
}
Also used : BaseBridgeHandler(org.openhab.core.thing.binding.BaseBridgeHandler) Command(org.openhab.core.types.Command) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ChannelUID(org.openhab.core.thing.ChannelUID) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ThingHandler(org.openhab.core.thing.binding.ThingHandler) Semaphore(java.util.concurrent.Semaphore) Bridge(org.openhab.core.thing.Bridge) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 4 with BaseThingHandler

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();
}
Also used : TestHueThingHandlerFactoryX(org.openhab.core.model.thing.testsupport.hue.TestHueThingHandlerFactoryX) ComponentContext(org.osgi.service.component.ComponentContext) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ReadyService(org.openhab.core.service.ReadyService) ThingHandler(org.openhab.core.thing.binding.ThingHandler) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ThingRegistry(org.openhab.core.thing.ThingRegistry) ModelRepository(org.openhab.core.model.core.ModelRepository) Command(org.openhab.core.types.Command) ChannelUID(org.openhab.core.thing.ChannelUID) Nullable(org.eclipse.jdt.annotation.Nullable) Thing(org.openhab.core.thing.Thing) Bridge(org.openhab.core.thing.Bridge) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with BaseThingHandler

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);
}
Also used : BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) Command(org.openhab.core.types.Command) ChannelUID(org.openhab.core.thing.ChannelUID) ConfigDescriptionParameter(org.openhab.core.config.core.ConfigDescriptionParameter) ConfigDescriptionProvider(org.openhab.core.config.core.ConfigDescriptionProvider) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

ChannelUID (org.openhab.core.thing.ChannelUID)6 BaseThingHandler (org.openhab.core.thing.binding.BaseThingHandler)6 Command (org.openhab.core.types.Command)6 Test (org.junit.jupiter.api.Test)5 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)5 Thing (org.openhab.core.thing.Thing)5 ThingHandler (org.openhab.core.thing.binding.ThingHandler)5 Bridge (org.openhab.core.thing.Bridge)4 Semaphore (java.util.concurrent.Semaphore)3 BaseBridgeHandler (org.openhab.core.thing.binding.BaseBridgeHandler)3 ThingRegistry (org.openhab.core.thing.ThingRegistry)2 Map (java.util.Map)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ProviderChangeListener (org.openhab.core.common.registry.ProviderChangeListener)1 ConfigDescriptionParameter (org.openhab.core.config.core.ConfigDescriptionParameter)1 ConfigDescriptionProvider (org.openhab.core.config.core.ConfigDescriptionProvider)1 ModelRepository (org.openhab.core.model.core.ModelRepository)1 TestHueThingHandlerFactoryX (org.openhab.core.model.thing.testsupport.hue.TestHueThingHandlerFactoryX)1 ReadyService (org.openhab.core.service.ReadyService)1