2 * Copyright (c) 2010-2021 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.io.neeo.internal.models;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Objects;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.items.Item;
23 import org.openhab.core.library.types.HSBType;
24 import org.openhab.core.thing.Channel;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.type.ChannelKind;
27 import org.openhab.core.thing.type.ChannelType;
28 import org.openhab.io.neeo.internal.NeeoUtil;
31 * The model representing a Neeo Channel (serialize/deserialize json use only).
33 * @author Tim Roberts - Initial Contribution
36 public class NeeoDeviceChannel {
38 /** The channel kind */
39 private final NeeoDeviceChannelKind kind;
42 private final String itemName;
44 /** The channel number */
45 private final int channelNbr;
47 /** The item subtype id (the subtype allows multiple channels for the same itemName/channelNbr) */
48 private final ItemSubType subType;
50 /** The capability type */
51 private final NeeoCapabilityType type;
54 private final String label;
56 /** The action/text value */
58 private final String value; // could be either a format (text label) or a value to send (button)
60 /** The device channel range */
61 private final NeeoDeviceChannelRange range;
64 * Create a new channel based on the parms
66 * @param kind the non-null kind of channel
67 * @param itemName the non-empty item name
68 * @param channelNbr the channel number (must be >= 0)
69 * @param type the non-null type
70 * @param subType the non-null subtype
71 * @param label the non-empty label
72 * @param value the possibly null, possibly empty value
73 * @param range the possibly null range
75 public NeeoDeviceChannel(NeeoDeviceChannelKind kind, String itemName, int channelNbr, NeeoCapabilityType type,
76 ItemSubType subType, String label, @Nullable String value, @Nullable NeeoDeviceChannelRange range) {
77 Objects.requireNonNull(kind, "kind cannot be null");
78 NeeoUtil.requireNotEmpty(itemName, "itemName is required");
79 Objects.requireNonNull(type, "type is required");
80 Objects.requireNonNull(subType, "subType is required");
82 throw new IllegalArgumentException("channelNbr must be >= 0");
86 this.itemName = itemName;
87 this.channelNbr = channelNbr;
89 this.subType = subType;
92 this.range = range == null ? NeeoDeviceChannelRange.DEFAULT : range;
96 * Create a list of {@link NeeoDeviceChannel} from the given channel, capability type, sub type and labels
98 * @param channel a non-null channel
99 * @param capabilityType a non-null capability type
100 * @param subType a non-null sub type
101 * @param existingLabels a non-null, possibly empty set of existing labels
102 * @return a non-null, possibly empty list of device channels
104 public static List<NeeoDeviceChannel> from(Channel channel, NeeoCapabilityType capabilityType, ItemSubType subType,
105 Set<String> existingLabels) {
106 Objects.requireNonNull(channel);
107 Objects.requireNonNull(capabilityType);
108 Objects.requireNonNull(subType);
109 Objects.requireNonNull(existingLabels);
111 final ChannelUID uid = channel.getUID();
112 return Arrays.asList(new NeeoDeviceChannel(NeeoDeviceChannelKind.get(channel.getKind()), uid.getId(), 1,
113 capabilityType, subType, NeeoUtil.getUniqueLabel(existingLabels, uid.getIdWithoutGroup()), "", null));
117 * Create a list of {@link NeeoDeviceChannel} from the given item, channel, channel type, capability type and labels
119 * @param item a non-null item
120 * @param channel a possibly null channel
121 * @param channelType a possibly null channel type
122 * @param capabilityType a non-null capability type
123 * @param existingLabels a non-null, possibly empty set of existing labels
124 * @return a non-null, possibly empty list of device channels
126 public static List<NeeoDeviceChannel> from(Item item, @Nullable Channel channel, @Nullable ChannelType channelType,
127 NeeoCapabilityType capabilityType, Set<String> existingLabels) {
128 Objects.requireNonNull(item);
129 Objects.requireNonNull(capabilityType);
130 Objects.requireNonNull(existingLabels);
132 if (item.getAcceptedDataTypes().contains(HSBType.class)) {
133 return Arrays.asList(
134 new NeeoDeviceChannel(
135 NeeoDeviceChannelKind.get(channel == null ? ChannelKind.STATE : channel.getKind()),
136 item.getName(), 1, capabilityType, ItemSubType.NONE,
137 NeeoUtil.getUniqueLabel(existingLabels, NeeoUtil.getLabel(item, channelType)),
138 NeeoUtil.getPattern(item, channelType), NeeoDeviceChannelRange.from(item)),
139 new NeeoDeviceChannel(
140 NeeoDeviceChannelKind.get(channel == null ? ChannelKind.STATE : channel.getKind()),
141 item.getName(), 1, capabilityType, ItemSubType.HUE,
142 NeeoUtil.getUniqueLabel(existingLabels, NeeoUtil.getLabel(item, channelType) + " (Hue)"),
143 NeeoUtil.getPattern(item, channelType), NeeoDeviceChannelRange.from(item)),
144 new NeeoDeviceChannel(
145 NeeoDeviceChannelKind.get(channel == null ? ChannelKind.STATE : channel.getKind()),
146 item.getName(), 1, capabilityType, ItemSubType.SATURATION,
147 NeeoUtil.getUniqueLabel(existingLabels,
148 NeeoUtil.getLabel(item, channelType) + " (Saturation)"),
149 NeeoUtil.getPattern(item, channelType), NeeoDeviceChannelRange.from(item)),
150 new NeeoDeviceChannel(
151 NeeoDeviceChannelKind.get(channel == null ? ChannelKind.STATE : channel.getKind()),
152 item.getName(), 1, capabilityType, ItemSubType.BRIGHTNESS,
153 NeeoUtil.getUniqueLabel(existingLabels,
154 NeeoUtil.getLabel(item, channelType) + " (Brightness)"),
155 NeeoUtil.getPattern(item, channelType), NeeoDeviceChannelRange.from(item)));
157 return Arrays.asList(new NeeoDeviceChannel(
158 NeeoDeviceChannelKind.get(channel == null ? ChannelKind.STATE : channel.getKind()), item.getName(),
159 1, capabilityType, ItemSubType.NONE,
160 NeeoUtil.getUniqueLabel(existingLabels, NeeoUtil.getLabel(item, channelType)),
161 NeeoUtil.getPattern(item, channelType), NeeoDeviceChannelRange.from(item)));
166 * Returns the kind of channel
168 * @return the non-null kind of channel
170 public NeeoDeviceChannelKind getKind() {
175 * Gets the unique item name (which may include the channel number)
177 * @return the unique item name
179 public String getUniqueItemName() {
180 if (isPowerState()) {
183 return itemName + "-" + subType + (channelNbr > 1 ? ("-" + channelNbr) : "");
187 * Gets the channel number
189 * @return the channel number
191 public int getChannelNbr() {
196 * Gets the {@link NeeoCapabilityType}
198 * @return the {@link NeeoCapabilityType}
200 public NeeoCapabilityType getType() {
207 * @return the sub type
209 public ItemSubType getSubType() {
214 * Gets the item name.
216 * @return the item name
218 public String getItemName() {
227 public String getLabel() {
228 if (isPowerState()) {
240 public String getValue() {
245 * Gets the device channel range. If the range is null, the {@link NeeoDeviceChannelRange#DEFAULT} will be returned
247 * @return the possibly null {@link NeeoDeviceChannelRange}
249 public NeeoDeviceChannelRange getRange() {
254 * Helper method to determine if the channel is a powerstate channel or not
256 * @return true if powerstate, false otherwise
258 private boolean isPowerState() {
259 return NeeoCapabilityType.SENSOR_POWER.equals(type);
263 public String toString() {
264 return "NeeoDeviceChannel [kind=" + kind + ", itemName=" + itemName + ", channelNbr=" + channelNbr + ", type="
265 + type + ", subType=" + subType + ", label=" + label + ", value=" + value + ", range=" + range + "]";