]> git.basschouten.com Git - openhab-addons.git/blob
ee9475a817c8338b683df328ebbfe924433dda26
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.tapocontrol.internal.device;
14
15 import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*;
16 import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.*;
17
18 import java.util.HashMap;
19
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;
34
35 /**
36  * TAPO Smart-Plug-Device.
37  *
38  * @author Christian Wild - Initial contribution
39  */
40 @NonNullByDefault
41 public class TapoSmartBulb extends TapoDevice {
42     private final Logger logger = LoggerFactory.getLogger(TapoSmartBulb.class);
43
44     /**
45      * Constructor
46      * 
47      * @param thing Thing object representing device
48      */
49     public TapoSmartBulb(Thing thing) {
50         super(thing);
51     }
52
53     /**
54      * handle command sent to device
55      * 
56      * @param channelUID channelUID command is sent to
57      * @param command command to be sent
58      */
59     @Override
60     public void handleCommand(ChannelUID channelUID, Command command) {
61         Boolean refreshInfo = false;
62
63         String channel = channelUID.getIdWithoutGroup();
64         if (command instanceof RefreshType) {
65             refreshInfo = true;
66         } else {
67             switch (channel) {
68                 case CHANNEL_OUTPUT:
69                     connector.sendDeviceCommand(JSON_KEY_ON, command == OnOffType.ON);
70                     refreshInfo = true;
71                     break;
72                 case CHANNEL_BRIGHTNESS:
73                     if (command instanceof PercentType) {
74                         Float percent = ((PercentType) command).floatValue();
75                         setBrightness(percent.intValue()); // 0..100% = 0..100
76                         refreshInfo = true;
77                     } else if (command instanceof DecimalType) {
78                         setBrightness(((DecimalType) command).intValue());
79                         refreshInfo = true;
80                     }
81                     break;
82                 case CHANNEL_COLOR_TEMP:
83                     if (command instanceof DecimalType) {
84                         setColorTemp(((DecimalType) command).intValue());
85                         refreshInfo = true;
86                     }
87                     break;
88                 case CHANNEL_COLOR:
89                     if (command instanceof HSBType) {
90                         setColor((HSBType) command);
91                         refreshInfo = true;
92                     }
93                     break;
94                 case CHANNEL_FX_NAME:
95                     setLightEffect(command.toString());
96                     break;
97                 default:
98                     logger.warn("({}) command type '{}' not supported for channel '{}'", uid, command.toString(),
99                             channelUID.getId());
100             }
101         }
102
103         /* refreshInfo */
104         if (refreshInfo) {
105             queryDeviceInfo(true);
106         }
107     }
108
109     /**
110      * SET BRIGHTNESS
111      * 
112      * @param newBrightness percentage 0-100 of new brightness
113      */
114     protected void setBrightness(Integer newBrightness) {
115         /* switch off if 0 */
116         if (newBrightness == 0) {
117             connector.sendDeviceCommand(JSON_KEY_ON, false);
118         } else {
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);
123         }
124     }
125
126     /**
127      * SET COLOR
128      * 
129      * @param command
130      */
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);
139     }
140
141     /**
142      * SET COLORTEMP
143      * 
144      * @param colorTemp (Integer) in Kelvin
145      */
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);
152     }
153
154     /**
155      * Set light effect
156      * 
157      * @param fxName (String) id of LightEffect
158      */
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);
165         } else {
166             newState.put(JSON_KEY_LIGHTNING_DYNAMIC_ENABLE, false);
167             newState.put(JSON_KEY_LIGHTNING_DYNAMIC_ID, "");
168         }
169         connector.sendDeviceCommands(newState);
170     }
171
172     /**
173      * UPDATE PROPERTIES
174      * 
175      * @param TapoDeviceInfo
176      */
177     @Override
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()));
191
192         updateLightEffectChannels(deviceInfo.getLightEffect());
193     }
194
195     /**
196      * Set light effect channels
197      * 
198      * @param lightEffect
199      */
200     protected void updateLightEffectChannels(TapoLightEffect lightEffect) {
201         String fxId = "";
202         if (lightEffect.getEnable().equals(true)) {
203             fxId = lightEffect.getId();
204         }
205         publishState(getChannelID(CHANNEL_GROUP_EFFECTS, CHANNEL_FX_NAME), getStringType(fxId));
206     }
207 }