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.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(DEVICE_PROPERTY_ON, command == OnOffType.ON);
72 case CHANNEL_BRIGHTNESS:
73 if (command instanceof PercentType) {
74 Float percent = ((PercentType) command).floatValue();
75 setBrightness(percent.intValue()); // 0..100% = 0..100
77 } else if (command instanceof DecimalType) {
78 setBrightness(((DecimalType) command).intValue());
82 case CHANNEL_COLOR_TEMP:
83 if (command instanceof DecimalType) {
84 setColorTemp(((DecimalType) command).intValue());
89 if (command instanceof HSBType) {
90 setColor((HSBType) command);
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(DEVICE_PROPERTY_ON, false);
126 HashMap<String, Object> newState = new HashMap<>();
127 newState.put(DEVICE_PROPERTY_ON, true);
128 newState.put(DEVICE_PROPERTY_BRIGHTNES, newBrightness);
129 connector.sendDeviceCommands(newState);
138 protected void setColor(HSBType command) {
139 HashMap<String, Object> newState = new HashMap<>();
140 newState.put(DEVICE_PROPERTY_ON, true);
141 newState.put(DEVICE_PROPERTY_HUE, command.getHue());
142 newState.put(DEVICE_PROPERTY_SATURATION, command.getSaturation());
143 newState.put(DEVICE_PROPERTY_BRIGHTNES, 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(DEVICE_PROPERTY_ON, true);
156 newState.put(DEVICE_PROPERTY_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
176 public void responsePasstrough(String responseBody) {
177 logger.info("({}) received response {}", uid, responseBody);
178 publishState(getChannelID(CHANNEL_GROUP_DEBUG, CHANNEL_RESPONSE), getStringType(responseBody));
184 * @param TapoDeviceInfo
187 protected void devicePropertiesChanged(TapoDeviceInfo deviceInfo) {
188 super.devicePropertiesChanged(deviceInfo);
189 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_OUTPUT), getOnOffType(deviceInfo.isOn()));
190 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_BRIGHTNESS),
191 getPercentType(deviceInfo.getBrightness()));
192 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_COLOR_TEMP),
193 getDecimalType(deviceInfo.getColorTemp()));
194 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_COLOR), deviceInfo.getHSB());
196 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_WIFI_STRENGTH),
197 getDecimalType(deviceInfo.getSignalLevel()));
198 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_ONTIME),
199 getTimeType(deviceInfo.getOnTime(), Units.SECOND));
200 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_OVERHEAT),
201 getDecimalType(deviceInfo.isOverheated() ? 1 : 0));
204 /***********************************
208 ************************************/
210 * Get ChannelID including group
212 * @param group String channel-group
213 * @param channel String channel-name
214 * @return String channelID
217 protected String getChannelID(String group, String channel) {
218 return group + "#" + channel;
222 * Get Channel from ChannelID
224 * @param channelID String channelID
225 * @return String channel-name
227 protected String getChannelFromID(ChannelUID channelID) {
228 String channel = channelID.getIdWithoutGroup();
229 channel = channel.replace(CHANNEL_GROUP_ACTUATOR + "#", "");
230 channel = channel.replace(CHANNEL_GROUP_DEVICE + "#", "");
231 channel = channel.replace(CHANNEL_GROUP_DEBUG + "#", "");