2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.netatmo.internal.providers;
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.BINDING_ID;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.HashSet;
20 import java.util.Locale;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.MeasureClass;
25 import org.openhab.core.i18n.LocaleProvider;
26 import org.openhab.core.thing.i18n.ChannelTypeI18nLocalizationService;
27 import org.openhab.core.thing.type.ChannelType;
28 import org.openhab.core.thing.type.ChannelTypeBuilder;
29 import org.openhab.core.thing.type.ChannelTypeProvider;
30 import org.openhab.core.thing.type.ChannelTypeUID;
31 import org.openhab.core.thing.type.StateChannelTypeBuilder;
32 import org.osgi.framework.Bundle;
33 import org.osgi.service.component.ComponentContext;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
39 * Extends the ChannelTypeProvider generating Channel Types based on {@link MeasureClass} enum.
41 * @author Gaƫl L'hopital - Initial contribution
42 * @author Laurent Garnier - Localizing the extensible channel types
46 @Component(service = { NetatmoChannelTypeProvider.class, ChannelTypeProvider.class })
47 public class NetatmoChannelTypeProvider implements ChannelTypeProvider {
48 private final Collection<ChannelType> channelTypes = new HashSet<>();
51 public NetatmoChannelTypeProvider(final @Reference ChannelTypeI18nLocalizationService localizationService,
52 final @Reference LocaleProvider localeProvider, ComponentContext componentContext) {
53 final Bundle bundle = componentContext.getBundleContext().getBundle();
54 MeasureClass.AS_SET.forEach(mc -> mc.channels.forEach((measureChannel, channelDetails) -> {
55 StateChannelTypeBuilder channelTypeBuilder = ChannelTypeBuilder
56 .state(new ChannelTypeUID(BINDING_ID, measureChannel),
57 String.format("@text/extensible-channel-type.%s.label", measureChannel),
58 channelDetails.itemType)
59 .withStateDescriptionFragment(channelDetails.stateDescriptionFragment)
60 .withConfigDescriptionURI(channelDetails.configURI);
61 ChannelType channelType = localizationService.createLocalizedChannelType(bundle, channelTypeBuilder.build(),
62 localeProvider.getLocale());
63 channelTypes.add(channelType);
68 public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
69 return Collections.unmodifiableCollection(channelTypes);
73 public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
74 return channelTypes.stream().filter(ct -> ct.getUID().equals(channelTypeUID)).findFirst().orElse(null);