Search in sources :

Example 1 with ChannelTypeProvider

use of org.openhab.core.thing.type.ChannelTypeProvider in project openhab-addons by openhab.

the class GenericWemoOSGiTest method setUpServices.

protected void setUpServices() throws IOException {
    WemoUtil.serviceAvailableFunction = (host, port) -> true;
    // StorageService is required from the ManagedThingProvider
    VolatileStorageService volatileStorageService = new VolatileStorageService();
    registerService(volatileStorageService);
    // Mock the UPnP Service, that is required from the UPnP IO Service
    mockUpnpService = new MockUpnpService(false, true);
    mockUpnpService.startup();
    registerService(mockUpnpService, UpnpService.class.getName());
    managedThingProvider = getService(ManagedThingProvider.class);
    assertThat(managedThingProvider, is(notNullValue()));
    thingRegistry = getService(ThingRegistry.class);
    assertThat(thingRegistry, is(notNullValue()));
    // UPnP IO Service is required from the WemoDiscoveryService and WemoHandlerFactory
    upnpIOService = getService(UpnpIOService.class);
    assertThat(upnpIOService, is(notNullValue()));
    mockCaller = Mockito.spy(new WemoHttpCall());
    WemoHttpCallFactory wemoHttpCallFactory = () -> mockCaller;
    registerService(wemoHttpCallFactory, WemoHttpCallFactory.class.getName());
    ChannelTypeProvider channelTypeProvider = mock(ChannelTypeProvider.class);
    when(channelTypeProvider.getChannelType(any(ChannelTypeUID.class), any(Locale.class))).thenReturn(ChannelTypeBuilder.state(DEFAULT_CHANNEL_TYPE_UID, "label", CoreItemFactory.SWITCH).build());
    registerService(channelTypeProvider);
}
Also used : MockUpnpService(org.jupnp.mock.MockUpnpService) UpnpService(org.jupnp.UpnpService) WemoHttpCallFactory(org.openhab.binding.wemo.internal.WemoHttpCallFactory) Locale(java.util.Locale) MockUpnpService(org.jupnp.mock.MockUpnpService) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) WemoHttpCall(org.openhab.binding.wemo.internal.http.WemoHttpCall) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ChannelTypeProvider(org.openhab.core.thing.type.ChannelTypeProvider) VolatileStorageService(org.openhab.core.test.storage.VolatileStorageService) UpnpIOService(org.openhab.core.io.transport.upnp.UpnpIOService) ThingRegistry(org.openhab.core.thing.ThingRegistry)

Example 2 with ChannelTypeProvider

use of org.openhab.core.thing.type.ChannelTypeProvider in project openhab-core by openhab.

the class GenericThingProviderTest3 method assertThatThingsAreUpdatedOnceTheXMLfilesHaveBeenProcessed.

@Test
public void assertThatThingsAreUpdatedOnceTheXMLfilesHaveBeenProcessed() {
    assertThat(thingRegistry.getAll().size(), is(1));
    Thing thing1 = thingRegistry.getAll().iterator().next();
    assertThat(thing1.getUID().toString(), is("dumb:DUMB:boo"));
    assertThat(thing1.getChannels().size(), is(1));
    assertThat(thing1.getChannel("manual"), is(notNullValue()));
    assertThat(thing1.getLabel(), is("Test Label"));
    assertThat(thing1.getLocation(), is("Test Location"));
    assertThat(thing1.getConfiguration().getProperties().get("testConf"), is("foo"));
    // now become smart again...
    dumbThingHandlerFactory.setDumb(false);
    ChannelType channelType1 = ChannelTypeBuilder.state(new ChannelTypeUID(DumbThingHandlerFactory.BINDING_ID, "channel1"), "Channel 1", CoreItemFactory.STRING).build();
    ChannelTypeProvider channelTypeProvider = mock(ChannelTypeProvider.class);
    when(channelTypeProvider.getChannelType(any(), nullable(Locale.class))).thenAnswer(invocation -> {
        if ("channel1".equals(channelType1.getUID().getId())) {
            return channelType1;
        }
        return null;
    });
    registerService(channelTypeProvider);
    // ensure thing type was considered and manual and predefined values are there.
    waitForAssert(() -> {
        Thing thing2 = thingRegistry.getAll().iterator().next();
        assertThat(thing2.getLabel(), is("Test Label"));
        assertThat(thing2.getLocation(), is("Test Location"));
        assertThat(thing2.getChannels().size(), is(2));
        assertThat(thing2.getChannel("manual"), is(notNullValue()));
        assertThat(thing2.getChannel("channel1"), is(notNullValue()));
        // there is a default, so make sure the manually configured one (from the DSL) wins
        assertThat(thing2.getConfiguration().getProperties().get("testConf"), is("foo"));
        // it's not manually configured, but the thing type defines a default, so ensure it's in
        assertThat(thing2.getConfiguration().getProperties().get("testAdditional"), is("hello world"));
    });
}
Also used : Locale(java.util.Locale) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ChannelTypeProvider(org.openhab.core.thing.type.ChannelTypeProvider) ChannelType(org.openhab.core.thing.type.ChannelType) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 3 with ChannelTypeProvider

use of org.openhab.core.thing.type.ChannelTypeProvider in project openhab-core by openhab.

the class ChangeThingTypeOSGiTest method setup.

@BeforeEach
public void setup() throws URISyntaxException {
    registerVolatileStorageService();
    managedThingProvider = getService(ManagedThingProvider.class);
    assertThat(managedThingProvider, is(notNullValue()));
    configDescriptionRegistry = getService(ConfigDescriptionRegistry.class);
    assertThat(configDescriptionRegistry, is(notNullValue()));
    managedItemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
    assertThat(managedItemChannelLinkProvider, is(notNullValue()));
    managedItemProvider = getService(ManagedItemProvider.class);
    assertThat(managedItemProvider, is(notNullValue()));
    ComponentContext componentContext = mock(ComponentContext.class);
    when(componentContext.getBundleContext()).thenReturn(bundleContext);
    thingHandlerFactory = new SampleThingHandlerFactory();
    thingHandlerFactory.activate(componentContext);
    registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
    Map<String, String> thingTypeGenericProperties = Map.ofEntries(entry(PROPERTY_ON_GENERIC_THING_TYPE, GENERIC_VALUE), entry(PROPERTY_ON_GENERIC_AND_SPECIFIC_THING_TYPE, GENERIC_VALUE));
    Map<String, String> thingTypeSpecificProperties = Map.ofEntries(entry(PROPERTY_ON_SPECIFIC_THING_TYPE, SPECIFIC_VALUE), entry(PROPERTY_ON_GENERIC_AND_SPECIFIC_THING_TYPE, SPECIFIC_VALUE));
    thingTypeGeneric = registerThingTypeAndConfigDescription(THING_TYPE_GENERIC_UID, thingTypeGenericProperties);
    thingTypeSpecific = registerThingTypeAndConfigDescription(THING_TYPE_SPECIFIC_UID, thingTypeSpecificProperties);
    ThingTypeProvider thingTypeProvider = mock(ThingTypeProvider.class);
    when(thingTypeProvider.getThingType(any(), any())).thenAnswer(invocation -> thingTypes.get(invocation.getArgument(0)));
    registerService(thingTypeProvider);
    ThingTypeRegistry thingTypeRegistry = mock(ThingTypeRegistry.class);
    when(thingTypeRegistry.getThingType(any(), any())).thenAnswer(invocation -> thingTypes.get(invocation.getArgument(0)));
    registerService(thingTypeRegistry);
    ConfigDescriptionProvider configDescriptionProvider = mock(ConfigDescriptionProvider.class);
    when(configDescriptionProvider.getConfigDescription(any(), any())).thenAnswer(invocation -> configDescriptions.get(invocation.getArgument(0)));
    registerService(configDescriptionProvider);
    ChannelTypeProvider channelTypeProvider = mock(ChannelTypeProvider.class);
    when(channelTypeProvider.getChannelTypes(any())).thenReturn(channelTypes.values());
    when(channelTypeProvider.getChannelType(any(), any())).thenAnswer(invocation -> channelTypes.get(invocation.getArgument(0)));
    registerService(channelTypeProvider);
    ChannelGroupTypeProvider channelGroupTypeProvider = mock(ChannelGroupTypeProvider.class);
    when(channelGroupTypeProvider.getChannelGroupTypes(any())).thenReturn(channelGroupTypes.values());
    when(channelGroupTypeProvider.getChannelGroupType(any(), any())).thenAnswer(invocation -> channelGroupTypes.get(invocation.getArgument(0)));
    registerService(channelGroupTypeProvider);
    managedItemProvider.add(new StringItem(ITEM_GENERIC));
    managedItemProvider.add(new StringItem(ITEM_SPECIFIC));
    managedItemChannelLinkProvider.add(ITEM_CHANNEL_LINK_GENERIC);
    managedItemChannelLinkProvider.add(ITEM_CHANNEL_LINK_SPECIFIC);
}
Also used : ManagedItemProvider(org.openhab.core.items.ManagedItemProvider) ManagedItemChannelLinkProvider(org.openhab.core.thing.link.ManagedItemChannelLinkProvider) ComponentContext(org.osgi.service.component.ComponentContext) StringItem(org.openhab.core.library.items.StringItem) ChannelGroupTypeProvider(org.openhab.core.thing.type.ChannelGroupTypeProvider) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ThingTypeRegistry(org.openhab.core.thing.type.ThingTypeRegistry) ChannelTypeProvider(org.openhab.core.thing.type.ChannelTypeProvider) ConfigDescriptionRegistry(org.openhab.core.config.core.ConfigDescriptionRegistry) ConfigDescriptionProvider(org.openhab.core.config.core.ConfigDescriptionProvider) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with ChannelTypeProvider

use of org.openhab.core.thing.type.ChannelTypeProvider 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);
}
Also used : Locale(java.util.Locale) ChannelDefinitionBuilder(org.openhab.core.thing.type.ChannelDefinitionBuilder) CommandOption(org.openhab.core.types.CommandOption) ArrayList(java.util.ArrayList) ItemRegistry(org.openhab.core.items.ItemRegistry) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ItemChannelLinkRegistry(org.openhab.core.thing.link.ItemChannelLinkRegistry) StateDescriptionFragment(org.openhab.core.types.StateDescriptionFragment) ThingStatusInfoI18nLocalizationService(org.openhab.core.thing.i18n.ThingStatusInfoI18nLocalizationService) CommandDescription(org.openhab.core.types.CommandDescription) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) BaseThingHandlerFactory(org.openhab.core.thing.binding.BaseThingHandlerFactory) StateOption(org.openhab.core.types.StateOption) NumberItem(org.openhab.core.library.items.NumberItem) ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) ChannelTypeProvider(org.openhab.core.thing.type.ChannelTypeProvider) Collection(java.util.Collection) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) ChannelType(org.openhab.core.thing.type.ChannelType) Nullable(org.eclipse.jdt.annotation.Nullable) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with ChannelTypeProvider

use of org.openhab.core.thing.type.ChannelTypeProvider in project openhab-core by openhab.

the class ThingManagerOSGiTest method setUp.

@BeforeEach
@SuppressWarnings("null")
public void setUp() {
    thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).withChannels(ChannelBuilder.create(CHANNEL_UID, CoreItemFactory.SWITCH).withKind(ChannelKind.STATE).withType(CHANNEL_TYPE_UID).build()).build();
    registerVolatileStorageService();
    configurationAdmin = getService(ConfigurationAdmin.class);
    assertNotNull(configurationAdmin);
    channelTypeProvider = mock(ChannelTypeProvider.class);
    when(channelTypeProvider.getChannelType(any(ChannelTypeUID.class), nullable(Locale.class))).thenReturn(ChannelTypeBuilder.state(CHANNEL_TYPE_UID, "label", CoreItemFactory.SWITCH).build());
    registerService(channelTypeProvider);
    managedItemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
    managedThingProvider = getService(ManagedThingProvider.class);
    eventPublisher = getService(EventPublisher.class);
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    itemChannelLinkRegistry = getService(ItemChannelLinkRegistry.class);
    assertNotNull(itemChannelLinkRegistry);
    readyService = getService(ReadyService.class);
    assertNotNull(readyService);
    waitForAssert(() -> {
        try {
            assertThat(bundleContext.getServiceReferences(ReadyMarker.class, "(" + ThingManagerImpl.XML_THING_TYPE + "=" + bundleContext.getBundle().getSymbolicName() + ")"), is(notNullValue()));
        } catch (InvalidSyntaxException e) {
            fail("Failed to get service reference: " + e.getMessage());
        }
    });
    Bundle bundle = mock(Bundle.class);
    when(bundle.getSymbolicName()).thenReturn("org.openhab.core.thing");
    BundleResolver bundleResolver = mock(BundleResolver.class);
    when(bundleResolver.resolveBundle(any())).thenReturn(bundle);
    ThingManagerImpl thingManager = (ThingManagerImpl) getService(ThingTypeMigrationService.class);
    thingManager.setBundleResolver(bundleResolver);
}
Also used : Locale(java.util.Locale) ManagedItemChannelLinkProvider(org.openhab.core.thing.link.ManagedItemChannelLinkProvider) EventPublisher(org.openhab.core.events.EventPublisher) Bundle(org.osgi.framework.Bundle) ReadyService(org.openhab.core.service.ReadyService) ItemRegistry(org.openhab.core.items.ItemRegistry) BundleResolver(org.openhab.core.util.BundleResolver) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ThingTypeMigrationService(org.openhab.core.thing.ThingTypeMigrationService) ChannelTypeProvider(org.openhab.core.thing.type.ChannelTypeProvider) ManagedThingProvider(org.openhab.core.thing.ManagedThingProvider) ItemChannelLinkRegistry(org.openhab.core.thing.link.ItemChannelLinkRegistry) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ReadyMarker(org.openhab.core.service.ReadyMarker) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ChannelTypeProvider (org.openhab.core.thing.type.ChannelTypeProvider)11 Locale (java.util.Locale)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ChannelTypeUID (org.openhab.core.thing.type.ChannelTypeUID)7 ChannelType (org.openhab.core.thing.type.ChannelType)5 ItemRegistry (org.openhab.core.items.ItemRegistry)4 Collection (java.util.Collection)3 Nullable (org.eclipse.jdt.annotation.Nullable)3 ManagedThingProvider (org.openhab.core.thing.ManagedThingProvider)3 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)3 ChannelDefinition (org.openhab.core.thing.type.ChannelDefinition)3 ChannelDefinitionBuilder (org.openhab.core.thing.type.ChannelDefinitionBuilder)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 ConfigDescriptionRegistry (org.openhab.core.config.core.ConfigDescriptionRegistry)2 Item (org.openhab.core.items.Item)2 DefaultSystemChannelTypeProvider (org.openhab.core.thing.DefaultSystemChannelTypeProvider)2 ItemChannelLinkRegistry (org.openhab.core.thing.link.ItemChannelLinkRegistry)2 ManagedItemChannelLinkProvider (org.openhab.core.thing.link.ManagedItemChannelLinkProvider)2