]> git.basschouten.com Git - openhab-addons.git/blob
02f1299b2a994ea0b883dea07da97c2f909a59a6
[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.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;
33
34 /**
35  * TAPO Smart-Plug-Device.
36  *
37  * @author Christian Wild - Initial contribution
38  */
39 @NonNullByDefault
40 public class TapoSmartBulb extends TapoDevice {
41     private final Logger logger = LoggerFactory.getLogger(TapoSmartBulb.class);
42
43     /**
44      * Constructor
45      * 
46      * @param thing Thing object representing device
47      */
48     public TapoSmartBulb(Thing thing) {
49         super(thing);
50     }
51
52     /**
53      * handle command sent to device
54      * 
55      * @param channelUID channelUID command is sent to
56      * @param command command to be sent
57      */
58     @Override
59     public void handleCommand(ChannelUID channelUID, Command command) {
60         Boolean refreshInfo = false;
61
62         String channel = channelUID.getIdWithoutGroup();
63         if (command instanceof RefreshType) {
64             refreshInfo = true;
65         } else {
66             switch (channel) {
67                 case CHANNEL_OUTPUT:
68                     connector.sendDeviceCommand(DEVICE_PROPERTY_ON, command == OnOffType.ON);
69                     refreshInfo = true;
70                     break;
71                 case CHANNEL_BRIGHTNESS:
72                     if (command instanceof PercentType) {
73                         Float percent = ((PercentType) command).floatValue();
74                         setBrightness(percent.intValue()); // 0..100% = 0..100
75                         refreshInfo = true;
76                     } else if (command instanceof DecimalType) {
77                         setBrightness(((DecimalType) command).intValue());
78                         refreshInfo = true;
79                     }
80                     break;
81                 case CHANNEL_COLOR_TEMP:
82                     if (command instanceof DecimalType) {
83                         setColorTemp(((DecimalType) command).intValue());
84                         refreshInfo = true;
85                     }
86                     break;
87                 case CHANNEL_COLOR:
88                     if (command instanceof HSBType) {
89                         setColor((HSBType) command);
90                         refreshInfo = true;
91                     }
92                     break;
93                 default:
94                     logger.warn("({}) command type '{}' not supported for channel '{}'", uid, command.toString(),
95                             channelUID.getId());
96             }
97         }
98
99         /* refreshInfo */
100         if (refreshInfo) {
101             queryDeviceInfo(true);
102         }
103     }
104
105     /**
106      * SET BRIGHTNESS
107      * 
108      * @param newBrightness percentage 0-100 of new brightness
109      */
110     protected void setBrightness(Integer newBrightness) {
111         /* switch off if 0 */
112         if (newBrightness == 0) {
113             connector.sendDeviceCommand(DEVICE_PROPERTY_ON, false);
114         } else {
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);
119         }
120     }
121
122     /**
123      * SET COLOR
124      * 
125      * @param command
126      */
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);
134     }
135
136     /**
137      * SET COLORTEMP
138      * 
139      * @param colorTemp (Integer) in Kelvin
140      */
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);
147     }
148
149     /**
150      * UPDATE PROPERTIES
151      * 
152      * @param TapoDeviceInfo
153      */
154     @Override
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()));
168     }
169 }