]> git.basschouten.com Git - openhab-addons.git/blob
d4ca5490aab630088fe1324da64478a59192f71b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.tplinksmarthome.internal.device;
14
15 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;
16
17 import java.io.IOException;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.tplinksmarthome.internal.Commands;
22 import org.openhab.binding.tplinksmarthome.internal.model.HasErrorResponse;
23 import org.openhab.binding.tplinksmarthome.internal.model.LightState;
24 import org.openhab.binding.tplinksmarthome.internal.model.TransitionLightStateResponse;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.HSBType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.PercentType;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.UnDefType;
34
35 /**
36  * TP-Link Smart Home Light Bulb.
37  *
38  * @author Hilbrand Bouwkamp - Initial contribution
39  */
40 @NonNullByDefault
41 public class BulbDevice extends SmartHomeDevice {
42
43     protected Commands commands = new Commands();
44
45     private final int colorTempMin;
46     private final int colorTempMax;
47     private final int colorTempRangeFactor;
48
49     public BulbDevice(ThingTypeUID thingTypeUID) {
50         this(thingTypeUID, 0, 0);
51     }
52
53     public BulbDevice(ThingTypeUID thingTypeUID, int colorTempMin, int colorTempMax) {
54         this.colorTempMin = colorTempMin;
55         this.colorTempMax = colorTempMax;
56         colorTempRangeFactor = (colorTempMax - colorTempMin) / 100;
57     }
58
59     @Override
60     public String getUpdateCommand() {
61         return Commands.getRealtimeBulbAndSysinfo();
62     }
63
64     @Override
65     public boolean handleCommand(ChannelUID channelUid, Command command) throws IOException {
66         final String channelId = channelUid.getId();
67         final int transitionPeriod = configuration.transitionPeriod;
68         final HasErrorResponse response;
69
70         if (command instanceof OnOffType) {
71             response = handleOnOffType(channelId, (OnOffType) command, transitionPeriod);
72         } else if (command instanceof HSBType) {
73             response = handleHSBType(channelId, (HSBType) command, transitionPeriod);
74         } else if (command instanceof DecimalType) {
75             response = handleDecimalType(channelId, (DecimalType) command, transitionPeriod);
76         } else {
77             return false;
78         }
79         checkErrors(response);
80         return response != null;
81     }
82
83     private @Nullable HasErrorResponse handleOnOffType(String channelID, OnOffType onOff, int transitionPeriod)
84             throws IOException {
85         if (CHANNELS_BULB_SWITCH.contains(channelID)) {
86             return commands.setTransitionLightStateResponse(
87                     connection.sendCommand(commands.setLightState(onOff, transitionPeriod)));
88         }
89         return null;
90     }
91
92     private @Nullable HasErrorResponse handleDecimalType(String channelID, DecimalType command, int transitionPeriod)
93             throws IOException {
94         if (CHANNEL_COLOR.equals(channelID) || CHANNEL_BRIGHTNESS.equals(channelID)) {
95             return commands.setTransitionLightStateResponse(
96                     connection.sendCommand(commands.setBrightness(command.intValue(), transitionPeriod)));
97         } else if (CHANNEL_COLOR_TEMPERATURE.equals(channelID)) {
98             return handleColorTemperature(convertPercentageToKelvin(command.intValue()), transitionPeriod);
99         } else if (CHANNEL_COLOR_TEMPERATURE_ABS.equals(channelID)) {
100             return handleColorTemperature(guardColorTemperature(command.intValue()), transitionPeriod);
101         }
102         return null;
103     }
104
105     private @Nullable TransitionLightStateResponse handleColorTemperature(int colorTemperature, int transitionPeriod)
106             throws IOException {
107         return commands.setTransitionLightStateResponse(
108                 connection.sendCommand(commands.setColorTemperature(colorTemperature, transitionPeriod)));
109     }
110
111     @Nullable
112     private HasErrorResponse handleHSBType(String channelID, HSBType command, int transitionPeriod) throws IOException {
113         if (CHANNEL_COLOR.equals(channelID)) {
114             return commands.setTransitionLightStateResponse(
115                     connection.sendCommand(commands.setColor(command, transitionPeriod)));
116         }
117         return null;
118     }
119
120     @Override
121     public State updateChannel(ChannelUID channelUid, DeviceState deviceState) {
122         final LightState lightState = deviceState.getSysinfo().getLightState();
123         final State state;
124
125         switch (channelUid.getId()) {
126             case CHANNEL_BRIGHTNESS:
127                 state = lightState.getBrightness();
128                 break;
129             case CHANNEL_COLOR:
130                 state = new HSBType(lightState.getHue(), lightState.getSaturation(), lightState.getBrightness());
131                 break;
132             case CHANNEL_COLOR_TEMPERATURE:
133                 state = new PercentType(convertKelvinToPercentage(lightState.getColorTemp()));
134                 break;
135             case CHANNEL_COLOR_TEMPERATURE_ABS:
136                 state = new DecimalType(guardColorTemperature(lightState.getColorTemp()));
137                 break;
138             case CHANNEL_SWITCH:
139                 state = lightState.getOnOff();
140                 break;
141             case CHANNEL_ENERGY_POWER:
142                 state = new DecimalType(deviceState.getRealtime().getPower());
143                 break;
144             default:
145                 state = UnDefType.UNDEF;
146                 break;
147         }
148         return state;
149     }
150
151     private int convertPercentageToKelvin(int percentage) {
152         return guardColorTemperature(colorTempMin + colorTempRangeFactor * percentage);
153     }
154
155     private int convertKelvinToPercentage(int colorTemperature) {
156         return (guardColorTemperature(colorTemperature) - colorTempMin) / colorTempRangeFactor;
157     }
158
159     private int guardColorTemperature(int colorTemperature) {
160         return Math.max(colorTempMin, Math.min(colorTempMax, colorTemperature));
161     }
162 }