]> git.basschouten.com Git - openhab-addons.git/blob
8844d1c5b6a88caa3a5e9324883839eaacb75dff
[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.hue.internal.api.dto.clip1;
14
15 import org.openhab.binding.hue.internal.api.dto.clip1.State.AlertMode;
16 import org.openhab.binding.hue.internal.api.dto.clip1.State.Effect;
17
18 /**
19  * Collection of updates to the state of a light.
20  *
21  * @author Q42 - Initial contribution
22  * @author Thomas Höfer - added unique id and changed range check for brightness and saturation
23  * @author Denis Dudnik - moved Jue library source code inside the smarthome Hue binding, minor code cleanup
24  * @author Samuel Leisering - refactor configuration updates
25  */
26 public class StateUpdate extends ConfigUpdate {
27
28     private Integer colorTemperature;
29     private Integer brightness;
30
31     /**
32      * Turn light on.
33      *
34      * @return this object for chaining calls
35      */
36     public StateUpdate turnOn() {
37         return setOn(true);
38     }
39
40     /**
41      * Turn light off.
42      *
43      * @return this object for chaining calls
44      */
45     public StateUpdate turnOff() {
46         return setOn(false);
47     }
48
49     /**
50      * Turn light on or off.
51      *
52      * @param on on if true, off otherwise
53      * @return this object for chaining calls
54      */
55     public StateUpdate setOn(boolean on) {
56         commands.add(new Command("on", on));
57         return this;
58     }
59
60     /**
61      * Set brightness of light.
62      * Brightness 0 is not the same as off.
63      *
64      * @param brightness brightness [1..254]
65      * @return this object for chaining calls
66      */
67     public StateUpdate setBrightness(int brightness) {
68         if (brightness < 1 || brightness > 254) {
69             throw new IllegalArgumentException("Brightness out of range");
70         }
71
72         commands.add(new Command("bri", brightness));
73         this.brightness = brightness;
74         return this;
75     }
76
77     public Integer getBrightness() {
78         return this.brightness;
79     }
80
81     /**
82      * Switch to HS color mode and set hue.
83      *
84      * @param hue hue [0..65535]
85      * @return this object for chaining calls
86      */
87     public StateUpdate setHue(int hue) {
88         if (hue < 0 || hue > 65535) {
89             throw new IllegalArgumentException("Hue out of range");
90         }
91
92         commands.add(new Command("hue", hue));
93         return this;
94     }
95
96     /**
97      * Switch to HS color mode and set saturation.
98      *
99      * @param saturation saturation [0..254]
100      * @return this object for chaining calls
101      */
102     public StateUpdate setSat(int saturation) {
103         if (saturation < 0 || saturation > 254) {
104             throw new IllegalArgumentException("Saturation out of range");
105         }
106
107         commands.add(new Command("sat", saturation));
108         return this;
109     }
110
111     /**
112      * Switch to XY color mode and set CIE color space coordinates.
113      *
114      * @param x x coordinate [0..1]
115      * @param y y coordinate [0..1]
116      * @return this object for chaining calls
117      */
118     public StateUpdate setXY(float x, float y) {
119         return setXY(new float[] { x, y });
120     }
121
122     /**
123      * Switch to XY color mode and set CIE color space coordinates.
124      *
125      * @param xy x and y coordinates [0..1, 0..1]
126      * @return this object for chaining calls
127      */
128     public StateUpdate setXY(float[] xy) {
129         if (xy.length != 2) {
130             throw new IllegalArgumentException("Invalid coordinate array given");
131         } else if (xy[0] < 0.0f || xy[0] > 1.0f || xy[1] < 0.0f || xy[1] > 1.0f) {
132             throw new IllegalArgumentException("X and/or Y coordinate(s) out of bounds");
133         }
134
135         commands.add(new Command("xy", xy));
136         return this;
137     }
138
139     /**
140      * Switch to CT color mode and set color temperature in mired.
141      *
142      * @param colorTemperature color temperature
143      * @return this object for chaining calls
144      */
145     public StateUpdate setColorTemperature(int colorTemperature, ColorTemperature capabilities) {
146         if (colorTemperature < capabilities.min || colorTemperature > capabilities.max) {
147             throw new IllegalArgumentException(String.format("Color temperature %d is out of range [%d..%d]",
148                     colorTemperature, capabilities.min, capabilities.max));
149         }
150
151         commands.add(new Command("ct", colorTemperature));
152         this.colorTemperature = colorTemperature;
153         return this;
154     }
155
156     public Integer getColorTemperature() {
157         return this.colorTemperature;
158     }
159
160     /**
161      * Set the alert mode.
162      *
163      * @see AlertMode
164      * @param mode alert mode
165      * @return this object for chaining calls
166      */
167     public StateUpdate setAlert(AlertMode mode) {
168         commands.add(new Command("alert", mode.toString().toLowerCase()));
169         return this;
170     }
171
172     /**
173      * Set the current effect.
174      *
175      * @see Effect
176      * @param effect effect
177      * @return this object for chaining calls
178      */
179     public StateUpdate setEffect(Effect effect) {
180         commands.add(new Command("effect", effect.toString().toLowerCase()));
181         return this;
182     }
183
184     /**
185      * Set the transition time from the current state to the new state.
186      * Time is accurate to 100 milliseconds.
187      *
188      * @param timeMillis time in milliseconds [0..6553600]
189      * @return this object for chaining calls
190      */
191     public StateUpdate setTransitionTime(long timeMillis) {
192         if (timeMillis < 0 || timeMillis > 6553600) {
193             throw new IllegalArgumentException("Transition time out of range");
194         }
195
196         commands.add(new Command("transitiontime", timeMillis / 100));
197         return this;
198     }
199
200     /**
201      * Turn sensor flag on or off.
202      *
203      * @param flag on if true, off otherwise
204      * @return this object for chaining calls
205      */
206
207     public StateUpdate setFlag(boolean flag) {
208         commands.add(new Command("flag", flag));
209         return this;
210     }
211
212     /**
213      * Set status of sensor.
214      *
215      * @param status status
216      * @return this object for chaining calls
217      */
218     public StateUpdate setStatus(int status) {
219         commands.add(new Command("status", status));
220         return this;
221     }
222
223     /**
224      * Recall the given scene.
225      *
226      * @param sceneId Identifier of the scene
227      * @return this object for chaining calls
228      */
229     public StateUpdate setScene(String sceneId) {
230         commands.add(new Command("scene", sceneId));
231         return this;
232     }
233 }