2 * Copyright (c) 2010-2024 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.tapocontrol.internal.device;
15 import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*;
16 import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.*;
18 import java.util.HashMap;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.tapocontrol.internal.structures.TapoDeviceInfo;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.HSBType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.PercentType;
26 import org.openhab.core.library.unit.Units;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.RefreshType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * TAPO Universal-Device
36 * universal device for testing pruposes
38 * @author Christian Wild - Initial contribution
41 public class TapoUniversalDevice extends TapoDevice {
42 private final Logger logger = LoggerFactory.getLogger(TapoUniversalDevice.class);
45 public static final String CHANNEL_GROUP_DEBUG = "debug";
46 public static final String CHANNEL_RESPONSE = "deviceResponse";
47 public static final String CHANNEL_COMMAND = "deviceCommand";
52 * @param thing Thing object representing device
54 public TapoUniversalDevice(Thing thing) {
59 public void handleCommand(ChannelUID channelUID, Command command) {
60 logger.debug("({}) handleCommand '{}' for channelUID {}", uid, command.toString(), channelUID.getId());
61 Boolean refreshInfo = false;
63 String channel = channelUID.getIdWithoutGroup();
64 if (command instanceof RefreshType) {
69 connector.sendDeviceCommand(JSON_KEY_ON, command == OnOffType.ON);
72 case CHANNEL_BRIGHTNESS:
73 if (command instanceof PercentType percentCommand) {
74 Float percent = percentCommand.floatValue();
75 setBrightness(percent.intValue()); // 0..100% = 0..100
77 } else if (command instanceof DecimalType decimalCommand) {
78 setBrightness(decimalCommand.intValue());
82 case CHANNEL_COLOR_TEMP:
83 if (command instanceof DecimalType decimalCommand) {
84 setColorTemp(decimalCommand.intValue());
89 if (command instanceof HSBType hsbCommand) {
95 String[] cmd = command.toString().split(":");
96 if (cmd.length == 1) {
97 connector.sendCustomQuery(cmd[0]);
98 } else if (cmd.length == 2) {
99 connector.sendDeviceCommand(cmd[0], cmd[1]);
101 logger.warn("({}) wrong command format '{}'", uid, command.toString());
105 logger.warn("({}) command type '{}' not supported for channel '{}'", uid, command.toString(),
119 * @param newBrightness percentage 0-100 of new brightness
121 protected void setBrightness(Integer newBrightness) {
122 /* switch off if 0 */
123 if (newBrightness == 0) {
124 connector.sendDeviceCommand(JSON_KEY_ON, false);
126 HashMap<String, Object> newState = new HashMap<>();
127 newState.put(JSON_KEY_ON, true);
128 newState.put(JSON_KEY_BRIGHTNESS, newBrightness);
129 connector.sendDeviceCommands(newState);
138 protected void setColor(HSBType command) {
139 HashMap<String, Object> newState = new HashMap<>();
140 newState.put(JSON_KEY_ON, true);
141 newState.put(JSON_KEY_HUE, command.getHue());
142 newState.put(JSON_KEY_SATURATION, command.getSaturation());
143 newState.put(JSON_KEY_BRIGHTNESS, command.getBrightness());
144 connector.sendDeviceCommands(newState);
150 * @param colorTemp (Integer) in Kelvin
152 protected void setColorTemp(Integer colorTemp) {
153 HashMap<String, Object> newState = new HashMap<>();
154 colorTemp = limitVal(colorTemp, BULB_MIN_COLORTEMP, BULB_MAX_COLORTEMP);
155 newState.put(JSON_KEY_ON, true);
156 newState.put(JSON_KEY_COLORTEMP, colorTemp);
157 connector.sendDeviceCommands(newState);
161 * SET DEVICE INFOs to device
166 public void setDeviceInfo(TapoDeviceInfo deviceInfo) {
167 devicePropertiesChanged(deviceInfo);
168 handleConnectionState();
172 * Handle full responsebody received from connector
174 * @param responseBody
177 public void responsePasstrough(String responseBody) {
178 logger.debug("({}) received response {}", uid, responseBody);
179 publishState(getChannelID(CHANNEL_GROUP_DEBUG, CHANNEL_RESPONSE), getStringType(responseBody));
185 * @param deviceInfo TapoDeviceInfo
188 protected void devicePropertiesChanged(TapoDeviceInfo deviceInfo) {
189 super.devicePropertiesChanged(deviceInfo);
190 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_OUTPUT), getOnOffType(deviceInfo.isOn()));
191 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_BRIGHTNESS),
192 getPercentType(deviceInfo.getBrightness()));
193 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_COLOR_TEMP),
194 getDecimalType(deviceInfo.getColorTemp()));
195 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_COLOR), deviceInfo.getHSB());
197 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_WIFI_STRENGTH),
198 getDecimalType(deviceInfo.getSignalLevel()));
199 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_ONTIME),
200 getTimeType(deviceInfo.getOnTime(), Units.SECOND));
201 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_OVERHEAT),
202 getDecimalType(deviceInfo.isOverheated() ? 1 : 0));
205 /***********************************
209 ************************************/
211 * Get ChannelID including group
213 * @param group String channel-group
214 * @param channel String channel-name
215 * @return String channelID
218 protected String getChannelID(String group, String channel) {
219 return group + "#" + channel;
223 * Get Channel from ChannelID
225 * @param channelID String channelID
226 * @return String channel-name
229 protected String getChannelFromID(ChannelUID channelID) {
230 String channel = channelID.getIdWithoutGroup();
231 channel = channel.replace(CHANNEL_GROUP_ACTUATOR + "#", "");
232 channel = channel.replace(CHANNEL_GROUP_DEVICE + "#", "");
233 channel = channel.replace(CHANNEL_GROUP_DEBUG + "#", "");