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.networkupstools.internal;
15 import static org.openhab.binding.networkupstools.internal.NUTBindingConstants.BINDING_ID;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.library.CoreItemFactory;
20 import org.openhab.core.library.types.QuantityType;
21 import org.openhab.core.thing.Channel;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.binding.builder.ChannelBuilder;
24 import org.openhab.core.thing.type.ChannelType;
25 import org.openhab.core.thing.type.ChannelTypeBuilder;
26 import org.openhab.core.thing.type.ChannelTypeUID;
27 import org.openhab.core.types.StateDescriptionFragmentBuilder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Factory class to enrich dynamic created channels with additional configurations.
34 * @author Hilbrand Bouwkamp - Initial contribution
37 class NUTDynamicChannelFactory {
39 private static final String QUANTITY_ITEM_TYPE_PREFIX = CoreItemFactory.NUMBER + ':';
41 private final Logger logger = LoggerFactory.getLogger(NUTDynamicChannelFactory.class);
43 private final NUTChannelTypeProvider channelTypeProvider;
45 NUTDynamicChannelFactory(final NUTChannelTypeProvider channelTypeProvider) {
46 this.channelTypeProvider = channelTypeProvider;
50 * Enriches the channel from the given channel and returns the newly created channel.
52 * @param channel Channel to enrich
53 * @param channelConfig channel configuration
54 * @return new created channel or null if it was not possible to create a new channel due to missing information or
57 public @Nullable Channel createChannel(final Channel channel, final NUTDynamicChannelConfiguration channelConfig) {
58 final String acceptedItemType = channel.getAcceptedItemType();
60 if (acceptedItemType == null) {
63 final ChannelTypeUID channelTypeUID;
65 if (acceptedItemType.startsWith(QUANTITY_ITEM_TYPE_PREFIX)) {
66 channelTypeUID = createQuantityTypeChannel(channel, acceptedItemType, channelConfig);
68 channelTypeUID = getChannelTypeUID(acceptedItemType, channel.getUID());
70 return channelTypeUID == null ? null : ChannelBuilder.create(channel).withType(channelTypeUID).build();
74 * Returns the {@link ChannelTypeUID} for channels types that are supported and have a channel-type definition in
75 * the binding thing xml.
77 * @param itemType item type to get the channel type for
78 * @param channelUID ChannelUID for which the channel type is determined
79 * @return channel type or null if not supported
81 private @Nullable ChannelTypeUID getChannelTypeUID(final String itemType, final ChannelUID channelUID) {
83 case CoreItemFactory.NUMBER:
84 return NUTBindingConstants.CHANNEL_TYPE_DYNAMIC_NUMBER;
85 case CoreItemFactory.STRING:
86 return NUTBindingConstants.CHANNEL_TYPE_DYNAMIC_STRING;
87 case CoreItemFactory.SWITCH:
88 return NUTBindingConstants.CHANNEL_TYPE_DYNAMIC_SWITCH;
90 logger.info("Dynamic channel '{}' is ignored because the type '{}' is not supported.", channelUID,
97 * Creates a new {@link ChannelTypeUID} for dynamically created {@link QuantityType} channels.
98 * It registers the new channel type with the channel type provider.
100 * @param channel Channel to enrich
101 * @param itemType item type to get the channel type for
102 * @param channelConfig channel configuration
103 * @return channel type or null if not supported
105 private @Nullable ChannelTypeUID createQuantityTypeChannel(final Channel channel, final String itemType,
106 final NUTDynamicChannelConfiguration channelConfig) {
107 if (channelConfig.unit == null || channelConfig.unit.isEmpty()) {
108 logger.info("Dynamic Channel '{}' is ignored because it's a QuantityType without a 'unit' property.",
112 final StateDescriptionFragmentBuilder sdb = StateDescriptionFragmentBuilder.create();
113 final ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, channel.getUID().getId() + "Type");
114 final String label = channel.getLabel();
115 final ChannelType channelType = ChannelTypeBuilder.state(channelTypeUID, label == null ? "" : label, itemType)
116 .withStateDescriptionFragment(sdb.withReadOnly(Boolean.TRUE).build())
117 .withConfigDescriptionURI(NUTBindingConstants.DYNAMIC_CHANNEL_CONFIG_QUANTITY_TYPE).build();
118 channelTypeProvider.addChannelType(channelType);
119 return channelTypeUID;