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.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 */
57 private final @Nullable String value; // could be either a format (text label) or a value to send (button)
59 /** The device channel range */
60 private final NeeoDeviceChannelRange range;
63 * Create a new channel based on the parms
65 * @param kind the non-null kind of channel
66 * @param itemName the non-empty item name
67 * @param channelNbr the channel number (must be >= 0)
68 * @param type the non-null type
69 * @param subType the non-null subtype
70 * @param label the non-empty label
71 * @param value the possibly null, possibly empty value
72 * @param range the possibly null range
74 public NeeoDeviceChannel(NeeoDeviceChannelKind kind, String itemName, int channelNbr, NeeoCapabilityType type,
75 ItemSubType subType, String label, @Nullable String value, @Nullable NeeoDeviceChannelRange range) {
76 Objects.requireNonNull(kind, "kind cannot be null");
77 NeeoUtil.requireNotEmpty(itemName, "itemName is required");
78 Objects.requireNonNull(type, "type is required");
79 Objects.requireNonNull(subType, "subType is required");
81 throw new IllegalArgumentException("channelNbr must be >= 0");
85 this.itemName = itemName;
86 this.channelNbr = channelNbr;
88 this.subType = subType;
91 this.range = range == null ? NeeoDeviceChannelRange.DEFAULT : range;
95 * Create a list of {@link NeeoDeviceChannel} from the given channel, capability type, sub type and labels
97 * @param channel a non-null channel
98 * @param capabilityType a non-null capability type
99 * @param subType a non-null sub type
100 * @param existingLabels a non-null, possibly empty set of existing labels
101 * @return a non-null, possibly empty list of device channels
103 public static List<NeeoDeviceChannel> from(Channel channel, NeeoCapabilityType capabilityType, ItemSubType subType,
104 Set<String> existingLabels) {
105 Objects.requireNonNull(channel);
106 Objects.requireNonNull(capabilityType);
107 Objects.requireNonNull(subType);
108 Objects.requireNonNull(existingLabels);
110 final ChannelUID uid = channel.getUID();
111 return Arrays.asList(new NeeoDeviceChannel(NeeoDeviceChannelKind.get(channel.getKind()), uid.getId(), 1,
112 capabilityType, subType, NeeoUtil.getUniqueLabel(existingLabels, uid.getIdWithoutGroup()), "", null));
116 * Create a list of {@link NeeoDeviceChannel} from the given item, channel, channel type, capability type and labels
118 * @param item a non-null item
119 * @param channel a possibly null channel
120 * @param channelType a possibly null channel type
121 * @param capabilityType a non-null capability type
122 * @param existingLabels a non-null, possibly empty set of existing labels
123 * @return a non-null, possibly empty list of device channels
125 public static List<NeeoDeviceChannel> from(Item item, @Nullable Channel channel, @Nullable ChannelType channelType,
126 NeeoCapabilityType capabilityType, Set<String> existingLabels) {
127 Objects.requireNonNull(item);
128 Objects.requireNonNull(capabilityType);
129 Objects.requireNonNull(existingLabels);
131 if (item.getAcceptedDataTypes().contains(HSBType.class)) {
132 return Arrays.asList(
133 new NeeoDeviceChannel(
134 NeeoDeviceChannelKind.get(channel == null ? ChannelKind.STATE : channel.getKind()),
135 item.getName(), 1, capabilityType, ItemSubType.NONE,
136 NeeoUtil.getUniqueLabel(existingLabels, NeeoUtil.getLabel(item, channelType)),
137 NeeoUtil.getPattern(item, channelType), NeeoDeviceChannelRange.from(item)),
138 new NeeoDeviceChannel(
139 NeeoDeviceChannelKind.get(channel == null ? ChannelKind.STATE : channel.getKind()),
140 item.getName(), 1, capabilityType, ItemSubType.HUE,
141 NeeoUtil.getUniqueLabel(existingLabels, NeeoUtil.getLabel(item, channelType) + " (Hue)"),
142 NeeoUtil.getPattern(item, channelType), NeeoDeviceChannelRange.from(item)),
143 new NeeoDeviceChannel(
144 NeeoDeviceChannelKind.get(channel == null ? ChannelKind.STATE : channel.getKind()),
145 item.getName(), 1, capabilityType, ItemSubType.SATURATION,
146 NeeoUtil.getUniqueLabel(existingLabels,
147 NeeoUtil.getLabel(item, channelType) + " (Saturation)"),
148 NeeoUtil.getPattern(item, channelType), NeeoDeviceChannelRange.from(item)),
149 new NeeoDeviceChannel(
150 NeeoDeviceChannelKind.get(channel == null ? ChannelKind.STATE : channel.getKind()),
151 item.getName(), 1, capabilityType, ItemSubType.BRIGHTNESS,
152 NeeoUtil.getUniqueLabel(existingLabels,
153 NeeoUtil.getLabel(item, channelType) + " (Brightness)"),
154 NeeoUtil.getPattern(item, channelType), NeeoDeviceChannelRange.from(item)));
156 return Arrays.asList(new NeeoDeviceChannel(
157 NeeoDeviceChannelKind.get(channel == null ? ChannelKind.STATE : channel.getKind()), item.getName(),
158 1, capabilityType, ItemSubType.NONE,
159 NeeoUtil.getUniqueLabel(existingLabels, NeeoUtil.getLabel(item, channelType)),
160 NeeoUtil.getPattern(item, channelType), NeeoDeviceChannelRange.from(item)));
165 * Returns the kind of channel
167 * @return the non-null kind of channel
169 public NeeoDeviceChannelKind getKind() {
174 * Gets the unique item name (which may include the channel number)
176 * @return the unique item name
178 public String getUniqueItemName() {
179 if (isPowerState()) {
182 return itemName + "-" + subType + (channelNbr > 1 ? ("-" + channelNbr) : "");
186 * Gets the channel number
188 * @return the channel number
190 public int getChannelNbr() {
195 * Gets the {@link NeeoCapabilityType}
197 * @return the {@link NeeoCapabilityType}
199 public NeeoCapabilityType getType() {
206 * @return the sub type
208 public ItemSubType getSubType() {
213 * Gets the item name.
215 * @return the item name
217 public String getItemName() {
226 public String getLabel() {
227 if (isPowerState()) {
238 public @Nullable String getValue() {
243 * Gets the device channel range. If the range is null, the {@link NeeoDeviceChannelRange#DEFAULT} will be returned
245 * @return the possibly null {@link NeeoDeviceChannelRange}
247 public NeeoDeviceChannelRange getRange() {
252 * Helper method to determine if the channel is a powerstate channel or not
254 * @return true if powerstate, false otherwise
256 private boolean isPowerState() {
257 return NeeoCapabilityType.SENSOR_POWER.equals(type);
261 public String toString() {
262 return "NeeoDeviceChannel [kind=" + kind + ", itemName=" + itemName + ", channelNbr=" + channelNbr + ", type="
263 + type + ", subType=" + subType + ", label=" + label + ", value=" + value + ", range=" + range + "]";