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.binding.tapocontrol.internal.structures.TapoLightEffect;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.HSBType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.PercentType;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.RefreshType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * TAPO Smart-Plug-Device.
38 * @author Christian Wild - Initial contribution
41 public class TapoSmartBulb extends TapoDevice {
42 private final Logger logger = LoggerFactory.getLogger(TapoSmartBulb.class);
47 * @param thing Thing object representing device
49 public TapoSmartBulb(Thing thing) {
54 * handle command sent to device
56 * @param channelUID channelUID command is sent to
57 * @param command command to be sent
60 public void handleCommand(ChannelUID channelUID, Command command) {
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) {
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 setLightEffect(command.toString());
98 logger.warn("({}) command type '{}' not supported for channel '{}'", uid, command.toString(),
105 queryDeviceInfo(true);
112 * @param newBrightness percentage 0-100 of new brightness
114 protected void setBrightness(Integer newBrightness) {
115 /* switch off if 0 */
116 if (newBrightness == 0) {
117 connector.sendDeviceCommand(JSON_KEY_ON, false);
119 HashMap<String, Object> newState = new HashMap<>();
120 newState.put(JSON_KEY_ON, true);
121 newState.put(JSON_KEY_BRIGHTNES, newBrightness);
122 connector.sendDeviceCommands(newState);
131 protected void setColor(HSBType command) {
132 HashMap<String, Object> newState = new HashMap<>();
133 newState.put(JSON_KEY_ON, true);
134 newState.put(JSON_KEY_HUE, command.getHue().intValue());
135 newState.put(JSON_KEY_SATURATION, command.getSaturation().intValue());
136 newState.put(JSON_KEY_BRIGHTNES, command.getBrightness().intValue());
137 newState.put(JSON_KEY_LIGHTNING_DYNAMIC_ENABLE, false);
138 connector.sendDeviceCommands(newState);
144 * @param colorTemp (Integer) in Kelvin
146 protected void setColorTemp(Integer colorTemp) {
147 HashMap<String, Object> newState = new HashMap<>();
148 colorTemp = limitVal(colorTemp, BULB_MIN_COLORTEMP, BULB_MAX_COLORTEMP);
149 newState.put(JSON_KEY_ON, true);
150 newState.put(JSON_KEY_COLORTEMP, colorTemp);
151 connector.sendDeviceCommands(newState);
157 * @param fxName (String) id of LightEffect
159 protected void setLightEffect(String fxName) {
160 HashMap<String, Object> newState = new HashMap<>();
161 if (fxName.length() > 0) {
162 newState.put(JSON_KEY_ON, true);
163 newState.put(JSON_KEY_LIGHTNING_DYNAMIC_ENABLE, true);
164 newState.put(JSON_KEY_LIGHTNING_DYNAMIC_ID, fxName);
166 newState.put(JSON_KEY_LIGHTNING_DYNAMIC_ENABLE, false);
167 newState.put(JSON_KEY_LIGHTNING_DYNAMIC_ID, "");
169 connector.sendDeviceCommands(newState);
175 * @param TapoDeviceInfo
178 protected void devicePropertiesChanged(TapoDeviceInfo deviceInfo) {
179 super.devicePropertiesChanged(deviceInfo);
180 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_OUTPUT), getOnOffType(deviceInfo.isOn()));
181 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_BRIGHTNESS),
182 getPercentType(deviceInfo.getBrightness()));
183 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_COLOR_TEMP),
184 getDecimalType(deviceInfo.getColorTemp()));
185 publishState(getChannelID(CHANNEL_GROUP_ACTUATOR, CHANNEL_COLOR), deviceInfo.getHSB());
186 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_WIFI_STRENGTH),
187 getDecimalType(deviceInfo.getSignalLevel()));
188 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_ONTIME),
189 getTimeType(deviceInfo.getOnTime(), Units.SECOND));
190 publishState(getChannelID(CHANNEL_GROUP_DEVICE, CHANNEL_OVERHEAT), getOnOffType(deviceInfo.isOverheated()));
192 updateLightEffectChannels(deviceInfo.getLightEffect());
196 * Set light effect channels
200 protected void updateLightEffectChannels(TapoLightEffect lightEffect) {
202 if (lightEffect.getEnable().equals(true)) {
203 fxId = lightEffect.getId();
205 publishState(getChannelID(CHANNEL_GROUP_EFFECTS, CHANNEL_FX_NAME), getStringType(fxId));