]> git.basschouten.com Git - openhab-addons.git/blob
42fe411ce496ca81c5d77f1e66add509a2d7d03e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.thing.type.ChannelType;
26 import org.openhab.core.thing.type.ChannelTypeBuilder;
27 import org.openhab.core.thing.type.ChannelTypeProvider;
28 import org.openhab.core.thing.type.ChannelTypeUID;
29 import org.openhab.core.thing.type.StateChannelTypeBuilder;
30 import org.osgi.service.component.annotations.Component;
31
32 /**
33  * Extends the ChannelTypeProvider generating Channel Types based on {@link MeasureClass} enum.
34  *
35  * @author GaĆ«l L'hopital - Initial contribution
36  *
37  */
38 @NonNullByDefault
39 @Component(service = { NetatmoChannelTypeProvider.class, ChannelTypeProvider.class })
40 public class NetatmoChannelTypeProvider implements ChannelTypeProvider {
41     private final Collection<ChannelType> channelTypes = new HashSet<>();
42
43     public NetatmoChannelTypeProvider() {
44         MeasureClass.AS_SET.forEach(mc -> mc.channels.forEach((measureChannel, channelDetails) -> {
45             StateChannelTypeBuilder channelTypeBuilder = ChannelTypeBuilder
46                     .state(new ChannelTypeUID(BINDING_ID, measureChannel), measureChannel.replace("-", " "),
47                             channelDetails.itemType)
48                     .withStateDescriptionFragment(channelDetails.stateDescriptionFragment)
49                     .withConfigDescriptionURI(channelDetails.configURI);
50
51             channelTypes.add(channelTypeBuilder.build());
52         }));
53     }
54
55     @Override
56     public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
57         return Collections.unmodifiableCollection(channelTypes);
58     }
59
60     @Override
61     public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
62         return channelTypes.stream().filter(ct -> ct.getUID().equals(channelTypeUID)).findFirst().orElse(null);
63     }
64 }