2 * Copyright (c) 2010-2022 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.miio.internal.basic;
15 import static org.openhab.binding.miio.internal.MiIoBindingConstants.BINDING_ID;
17 import java.math.BigDecimal;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.Locale;
24 import java.util.concurrent.ConcurrentHashMap;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.core.thing.type.ChannelType;
29 import org.openhab.core.thing.type.ChannelTypeBuilder;
30 import org.openhab.core.thing.type.ChannelTypeProvider;
31 import org.openhab.core.thing.type.ChannelTypeUID;
32 import org.openhab.core.thing.type.StateChannelTypeBuilder;
33 import org.openhab.core.types.StateDescriptionFragmentBuilder;
34 import org.openhab.core.types.StateOption;
35 import org.osgi.service.component.annotations.Component;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Provide channelTypes for Mi IO Basic devices
42 * @author Marcel Verpaalen - Initial contribution
44 @Component(service = { ChannelTypeProvider.class, BasicChannelTypeProvider.class })
46 public class BasicChannelTypeProvider implements ChannelTypeProvider {
47 private final Map<String, ChannelType> channelTypes = new ConcurrentHashMap<>();
48 private final Logger logger = LoggerFactory.getLogger(BasicChannelTypeProvider.class);
51 public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
52 return channelTypes.values();
56 public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
57 if (channelTypes.containsKey(channelTypeUID.getAsString())) {
58 return channelTypes.get(channelTypeUID.getAsString());
63 public void addChannelType(MiIoBasicChannel miChannel, String model) {
64 ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID,
65 model.toUpperCase().replace(".", "_") + "_" + miChannel.getChannel());
66 logger.debug("Adding channel definitions for {} -> {}", channelTypeUID, miChannel.getFriendlyName());
68 final StateDescriptionDTO stateDescription = miChannel.getStateDescription();
69 StateChannelTypeBuilder channelTypeBuilder = ChannelTypeBuilder.state(channelTypeUID,
70 miChannel.getFriendlyName(), miChannel.getType()); //
71 if (stateDescription != null) {
72 StateDescriptionFragmentBuilder sdf = StateDescriptionFragmentBuilder.create();
73 final BigDecimal maximum = stateDescription.getMaximum();
74 if (maximum != null) {
75 sdf.withMaximum(maximum);
77 final BigDecimal minimum = stateDescription.getMinimum();
78 if (minimum != null) {
79 sdf.withMinimum(minimum);
81 final BigDecimal step = stateDescription.getStep();
85 final String pattern = stateDescription.getPattern();
86 if (pattern != null) {
87 sdf.withPattern(pattern);
89 final Boolean readOnly = stateDescription.getReadOnly();
90 if (readOnly != null) {
91 sdf.withReadOnly(readOnly);
93 List<OptionsValueListDTO> optionList = stateDescription.getOptions();
94 if (optionList != null) {
95 List<StateOption> options = new ArrayList<>();
96 for (OptionsValueListDTO option : optionList) {
97 String value = option.getValue();
99 options.add(new StateOption(value, option.getLabel()));
102 sdf.withOptions(options);
104 channelTypeBuilder.withStateDescriptionFragment(sdf.build());
105 logger.debug("added stateDescription: {}", sdf);
107 final String category = miChannel.getCategory();
108 if (category != null) {
109 channelTypeBuilder.withCategory(category);
111 final Set<String> tags = miChannel.getTags();
112 if (tags != null && !tags.isEmpty()) {
113 channelTypeBuilder.withTags(tags);
115 channelTypes.put(channelTypeUID.getAsString(), channelTypeBuilder.build());
116 } catch (Exception e) {
117 logger.warn("Failed creating channelType {}: {} ", channelTypeUID, e.getMessage());