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