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.solarwatt.internal.channel;
15 import java.util.Collection;
16 import java.util.Locale;
18 import java.util.concurrent.ConcurrentHashMap;
20 import javax.measure.Unit;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.solarwatt.internal.SolarwattBindingConstants;
25 import org.openhab.binding.solarwatt.internal.domain.SolarwattChannel;
26 import org.openhab.core.library.CoreItemFactory;
27 import org.openhab.core.library.unit.Units;
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.util.UnitUtils;
35 import org.osgi.service.component.annotations.Component;
38 * A {@link ChannelTypeProvider} that creates {@link ChannelType}s according to
39 * the requested tags. It creates one {@link ChannelType} per tag value.
41 * @author Matthias Steigenberger - Initial contribution
42 * @author Sven Carstens - Adapted to solarwatt binding
46 @Component(service = { ChannelTypeProvider.class, SolarwattChannelTypeProvider.class })
47 public class SolarwattChannelTypeProvider implements ChannelTypeProvider {
49 private final Map<String, ChannelType> channelMap = new ConcurrentHashMap<>();
52 public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
53 return this.channelMap.values();
57 public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
58 return this.channelMap.values().stream().filter(channelType -> channelType.getUID().equals(channelTypeUID))
59 .findFirst().orElse(null);
63 * Assert that the {@link ChannelType} matching our requirements exists.
65 * Only create once for each tagname supplied via {@link SolarwattChannel}.
67 * @param solarwattChannel channeltype requirements
68 * @return UID of existing channeltype
70 public ChannelTypeUID assertChannelType(SolarwattChannel solarwattChannel) {
71 ChannelType existingChannel = this.channelMap.get(solarwattChannel.getChannelName());
72 if (existingChannel == null) {
73 ChannelType createdChannel = this.getChannelType(solarwattChannel);
74 this.channelMap.put(solarwattChannel.getChannelName(), createdChannel);
75 return createdChannel.getUID();
77 return existingChannel.getUID();
81 private ChannelType getChannelType(SolarwattChannel solarwattChannel) {
82 StateChannelTypeBuilder stateDescriptionBuilder;
83 Unit<?> unit = solarwattChannel.getUnit();
85 if ("switch".equals(solarwattChannel.getCategory())) {
86 stateDescriptionBuilder = ChannelTypeBuilder
87 .state(new ChannelTypeUID(SolarwattBindingConstants.BINDING_ID,
88 solarwattChannel.getChannelName()), solarwattChannel.getChannelName(),
89 CoreItemFactory.SWITCH)
90 .withCategory(solarwattChannel.getCategory()).isAdvanced(solarwattChannel.getAdvanced())
91 .withStateDescriptionFragment(
92 StateDescriptionFragmentBuilder.create().withReadOnly(true).build());
94 String dimension = ":" + UnitUtils.getDimensionName(unit);
95 String unitString = unit.toString();
97 if (Units.PERCENT.equals(unit)) {
98 // strangely it is Angle
99 dimension = ":Dimensionless";
103 stateDescriptionBuilder = ChannelTypeBuilder
104 .state(new ChannelTypeUID(SolarwattBindingConstants.BINDING_ID,
105 solarwattChannel.getChannelName()), solarwattChannel.getChannelName(),
106 CoreItemFactory.NUMBER + dimension)
107 .withCategory(solarwattChannel.getCategory()).isAdvanced(solarwattChannel.getAdvanced())
108 .withStateDescriptionFragment(StateDescriptionFragmentBuilder.create().withReadOnly(true)
109 .withPattern("%.2f " + unitString).build());
112 stateDescriptionBuilder = ChannelTypeBuilder
113 .state(new ChannelTypeUID(SolarwattBindingConstants.BINDING_ID, solarwattChannel.getChannelName()),
114 solarwattChannel.getChannelName(), CoreItemFactory.STRING)
115 .withCategory(solarwattChannel.getCategory()).isAdvanced(solarwattChannel.getAdvanced())
116 .withStateDescriptionFragment(StateDescriptionFragmentBuilder.create().withReadOnly(true).build());
118 return stateDescriptionBuilder.build();