]> git.basschouten.com Git - openhab-addons.git/blob
9fa309016d90905f3ba11e89b7dd9d32d7d5fb46
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.netatmo.internal.providers;
14
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.BINDING_ID;
16
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.HashSet;
20 import java.util.Locale;
21
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;
37
38 /**
39  * Extends the ChannelTypeProvider generating Channel Types based on {@link MeasureClass} enum.
40  *
41  * @author GaĆ«l L'hopital - Initial contribution
42  * @author Laurent Garnier - Localizing the extensible channel types
43  *
44  */
45 @NonNullByDefault
46 @Component(service = { NetatmoChannelTypeProvider.class, ChannelTypeProvider.class })
47 public class NetatmoChannelTypeProvider implements ChannelTypeProvider {
48     private final Collection<ChannelType> channelTypes = new HashSet<>();
49
50     @Activate
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);
64         }));
65     }
66
67     @Override
68     public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
69         return Collections.unmodifiableCollection(channelTypes);
70     }
71
72     @Override
73     public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
74         return channelTypes.stream().filter(ct -> ct.getUID().equals(channelTypeUID)).findFirst().orElse(null);
75     }
76 }