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.webthing.internal.channel;
15 import static org.openhab.binding.webthing.internal.WebThingBindingConstants.BINDING_ID;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.webthing.internal.client.dto.Property;
21 import org.openhab.binding.webthing.internal.link.TypeMapping;
22 import org.openhab.core.thing.Channel;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.ThingUID;
25 import org.openhab.core.thing.binding.builder.ChannelBuilder;
26 import org.openhab.core.thing.type.ChannelTypeUID;
29 * The {@link Channels} class is an utility class to create Channel based on the property characteristics as
30 * well as ChannelUID identifier
32 * @author Gregor Roth - Initial contribution
35 public class Channels {
38 * create a ChannelUIFD identifier for a given property name
40 * @param thingUID the thing uid of the associated WebThing
41 * @param propertyName the property name
42 * @return the ChannelUID identifier
44 public static ChannelUID createChannelUID(ThingUID thingUID, String propertyName) {
45 return new ChannelUID(thingUID.toString() + ":" + propertyName);
49 * create a Channel base on a given WebThing property
51 * @param thingUID the thing uid of the associated WebThing
52 * @param propertyName the property name
53 * @param property the WebThing property
54 * @return the Channel according to the properties characteristics
56 public static Channel createChannel(ThingUID thingUID, String propertyName, Property property) {
57 var itemType = TypeMapping.toItemType(property);
58 var channelUID = createChannelUID(thingUID, propertyName);
59 var channelBuilder = ChannelBuilder.create(channelUID, itemType.getType());
61 // Currently, few predefined, generic channel types such as number, string or color are defined
62 // inside the thing-types.xml file. A better solution would be to create the channel types
63 // dynamically based on the WebThing description to make most of the meta data of a WebThing.
64 // The goal of the WebThing meta data is to enable semantic interoperability for connected things
65 channelBuilder.withType(new ChannelTypeUID(BINDING_ID, itemType.getType().toLowerCase()));
66 channelBuilder.withDescription(property.description);
67 channelBuilder.withLabel(property.title);
68 Set<String> defaultTags = itemType.getTags();
69 if (!defaultTags.isEmpty()) {
70 channelBuilder.withDefaultTags(defaultTags);
72 return channelBuilder.build();