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 Smart-Plug-Device.
37 * @author Christian Wild - Initial contribution
40 public class TapoSmartBulb extends TapoDevice {
41 private final Logger logger = LoggerFactory.getLogger(TapoSmartBulb.class);
46 * @param thing Thing object representing device
48 public TapoSmartBulb(Thing thing) {
53 * handle command sent to device
55 * @param channelUID channelUID command is sent to
56 * @param command command to be sent
59 public void handleCommand(ChannelUID channelUID, Command command) {
60 Boolean refreshInfo = false;
62 String channel = channelUID.getIdWithoutGroup();
63 if (command instanceof RefreshType) {
68 connector.sendDeviceCommand(DEVICE_PROPERTY_ON, command == OnOffType.ON);
71 case CHANNEL_BRIGHTNESS:
72 if (command instanceof PercentType) {
73 Float percent = ((PercentType) command).floatValue();
74 setBrightness(percent.intValue()); // 0..100% = 0..100
76 } else if (command instanceof DecimalType) {
77 setBrightness(((DecimalType) command).intValue());
81 case CHANNEL_COLOR_TEMP:
82 if (command instanceof DecimalType) {
83 setColorTemp(((DecimalType) command).intValue());
88 if (command instanceof HSBType) {
89 setColor((HSBType) command);
94 logger.warn("({}) command type '{}' not supported for channel '{}'", uid, command.toString(),
101 queryDeviceInfo(true);
108 * @param newBrightness percentage 0-100 of new brightness
110 protected void setBrightness(Integer newBrightness) {
111 /* switch off if 0 */
112 if (newBrightness == 0) {
113 connector.sendDeviceCommand(DEVICE_PROPERTY_ON, false);
115 HashMap<String, Object> newState = new HashMap<>();
116 newState.put(DEVICE_PROPERTY_ON, true);
117 newState.put(DEVICE_PROPERTY_BRIGHTNES, newBrightness);
118 connector.sendDeviceCommands(newState);
127 protected void setColor(HSBType command) {
128 HashMap<String, Object> newState = new HashMap<>();
129 newState.put(DEVICE_PROPERTY_ON, true);
130 newState.put(DEVICE_PROPERTY_HUE, command.getHue().intValue());
131 newState.put(DEVICE_PROPERTY_SATURATION, command.getSaturation().intValue());
132 newState.put(DEVICE_PROPERTY_BRIGHTNES, command.getBrightness().intValue());
133 connector.sendDeviceCommands(newState);
139 * @param colorTemp (Integer) in Kelvin
141 protected void setColorTemp(Integer colorTemp) {
142 HashMap<String, Object> newState = new HashMap<>();
143 colorTemp = limitVal(colorTemp, BULB_MIN_COLORTEMP, BULB_MAX_COLORTEMP);
144 newState.put(DEVICE_PROPERTY_ON, true);
145 newState.put(DEVICE_PROPERTY_COLORTEMP, colorTemp);
146 connector.sendDeviceCommands(newState);
152 * @param TapoDeviceInfo
155 protected void devicePropertiesChanged(TapoDeviceInfo deviceInfo) {
156 super.devicePropertiesChanged(deviceInfo);
157 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_OUTPUT), getOnOffType(deviceInfo.isOn()));
158 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_BRIGHTNESS),
159 getPercentType(deviceInfo.getBrightness()));
160 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_COLOR_TEMP),
161 getDecimalType(deviceInfo.getColorTemp()));
162 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_COLOR), deviceInfo.getHSB());
163 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_WIFI_STRENGTH),
164 getDecimalType(deviceInfo.getSignalLevel()));
165 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_ONTIME),
166 getTimeType(deviceInfo.getOnTime(), Units.SECOND));
167 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_OVERHEAT), getOnOffType(deviceInfo.isOverheated()));