41
41
import org .openhab .core .thing .binding .BaseThingHandler ;
42
42
import org .openhab .core .types .Command ;
43
43
import org .openhab .core .types .RefreshType ;
44
+ import org .openhab .core .types .UnDefType ;
44
45
import org .openhab .core .util .ColorUtil ;
45
46
import org .slf4j .Logger ;
46
47
import org .slf4j .LoggerFactory ;
@@ -89,8 +90,9 @@ public class GoveeHandler extends BaseThingHandler {
89
90
90
91
private CommunicationManager communicationManager ;
91
92
93
+ private OnOffType lastOn = OnOffType .OFF ;
92
94
private HSBType lastColor = new HSBType ();
93
- private int lastKelvin = COLOR_TEMPERATURE_MIN_VALUE . intValue () ;
95
+ private int lastKelvin ;
94
96
95
97
/**
96
98
* This thing related job <i>thingRefreshSender</i> triggers an update to the Govee device.
@@ -159,23 +161,31 @@ public void handleCommand(ChannelUID channelUID, Command command) {
159
161
} else {
160
162
switch (channelUID .getId ()) {
161
163
case CHANNEL_COLOR :
164
+ boolean triggerRefresh = false ;
162
165
Command doCommand = command ;
163
166
if (doCommand instanceof HSBType hsb ) {
164
167
sendColor (hsb );
168
+ triggerRefresh = true ;
165
169
doCommand = hsb .getBrightness (); // fall through
166
170
}
167
171
if (doCommand instanceof PercentType percent ) {
168
172
sendBrightness (percent );
173
+ triggerRefresh = true ;
169
174
doCommand = OnOffType .from (percent .intValue () > 0 ); // fall through
170
175
}
171
176
if (doCommand instanceof OnOffType onOff ) {
172
177
sendOnOff (onOff );
178
+ triggerRefresh = true ;
179
+ }
180
+ if (triggerRefresh ) {
181
+ triggerDeviceStatusRefresh ();
173
182
}
174
183
break ;
175
184
176
185
case CHANNEL_COLOR_TEMPERATURE :
177
186
if (command instanceof PercentType percent ) {
178
187
sendKelvin (percentToKelvin (percent ));
188
+ triggerDeviceStatusRefresh ();
179
189
}
180
190
break ;
181
191
@@ -187,6 +197,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
187
197
break ;
188
198
}
189
199
sendKelvin (kelvin .intValue ());
200
+ triggerDeviceStatusRefresh ();
190
201
}
191
202
break ;
192
203
}
@@ -218,7 +229,6 @@ public void sendColor(HSBType color) throws IOException {
218
229
GenericGoveeData data = new ColorData (new Color (normalRGB [0 ], normalRGB [1 ], normalRGB [2 ]), 0 );
219
230
GenericGoveeRequest request = new GenericGoveeRequest (new GenericGoveeMsg ("colorwc" , data ));
220
231
communicationManager .sendRequest (this , request );
221
- lastColor = color ;
222
232
}
223
233
224
234
/**
@@ -229,7 +239,6 @@ public void sendBrightness(PercentType brightness) throws IOException {
229
239
GenericGoveeData data = new ValueIntData (brightness .intValue ());
230
240
GenericGoveeRequest request = new GenericGoveeRequest (new GenericGoveeMsg ("brightness" , data ));
231
241
communicationManager .sendRequest (this , request );
232
- lastColor = new HSBType (lastColor .getHue (), lastColor .getSaturation (), brightness );
233
242
}
234
243
235
244
/**
@@ -250,23 +259,23 @@ private void sendKelvin(int kelvin) throws IOException {
250
259
GenericGoveeData data = new ColorData (new Color (0 , 0 , 0 ), kelvin );
251
260
GenericGoveeRequest request = new GenericGoveeRequest (new GenericGoveeMsg ("colorwc" , data ));
252
261
communicationManager .sendRequest (this , request );
253
- lastKelvin = kelvin ;
254
262
}
255
263
256
264
/**
257
- * Build an {@link HSBType} from the given normalized {@link Color} record, brightness, and on state.
265
+ * Build an {@link HSBType} from the given normalized {@link Color} RGB parameters, brightness, and on-off state
266
+ * parameters. If the on parameter is true then use the brightness parameter, otherwise use a brightness of zero.
258
267
*
259
- * @param normalColor record containing the lamp's normalized RGB parameters (0..255)
260
- * @param brightness the lamp brightness in range 0..100
261
- * @param on the lamp state
268
+ * @param normalRgbParams record containing the lamp's normalized RGB parameters (0..255)
269
+ * @param brightnessParam the lamp brightness in range 0..100
270
+ * @param onParam the lamp on-off state
262
271
*
263
- * @return the HSB presentation state
272
+ * @return the respective HSBType
264
273
*/
265
- private static HSBType buildHSB (Color normalColor , int brightness , boolean on ) {
266
- int [] normalRGB = { normalColor . r (), normalColor . g (), normalColor . b () };
267
- HSBType normalHSB = ColorUtil .rgbToHsb (normalRGB );
268
- PercentType brightnessPercent = on ? new PercentType (brightness ) : PercentType .ZERO ;
269
- return new HSBType (normalHSB .getHue (), normalHSB .getSaturation (), brightnessPercent );
274
+ private static HSBType buildHSB (Color normalRgbParams , int brightnessParam , boolean onParam ) {
275
+ HSBType normalColor = ColorUtil
276
+ .rgbToHsb (new int [] { normalRgbParams . r (), normalRgbParams . g (), normalRgbParams . b () } );
277
+ PercentType brightness = onParam ? new PercentType (brightnessParam ) : PercentType .ZERO ;
278
+ return new HSBType (normalColor .getHue (), normalColor .getSaturation (), brightness );
270
279
}
271
280
272
281
void handleIncomingStatus (String response ) {
@@ -294,7 +303,7 @@ public void updateDeviceState(@Nullable StatusResponse message) {
294
303
295
304
logger .trace ("Receiving Device State" );
296
305
297
- boolean on = message .msg ().data ().onOff () == 1 ;
306
+ OnOffType on = OnOffType . from ( message .msg ().data ().onOff () == 1 ) ;
298
307
logger .trace ("on:{}" , on );
299
308
300
309
int brightness = message .msg ().data ().brightness ();
@@ -306,23 +315,28 @@ public void updateDeviceState(@Nullable StatusResponse message) {
306
315
int kelvin = message .msg ().data ().colorTemInKelvin ();
307
316
logger .trace ("kelvin:{}" , kelvin );
308
317
309
- HSBType color = buildHSB (normalRGB , brightness , on );
310
-
311
- kelvin = Math .min (COLOR_TEMPERATURE_MAX_VALUE .intValue (),
312
- Math .max (COLOR_TEMPERATURE_MIN_VALUE .intValue (), kelvin ));
318
+ HSBType color = buildHSB (normalRGB , brightness , true );
313
319
314
- logger .trace ("Comparing color old:{} to new:{}" , lastColor , color );
315
- if (!color .equals (lastColor )) {
316
- logger .trace ("Updating color old:{} to new:{}" , lastColor , color );
317
- updateState (CHANNEL_COLOR , color );
320
+ logger .trace ("Comparing color old:{} to new:{}, on state old:{} to new:{}" , lastColor , color , lastOn , on );
321
+ if ((on != lastOn ) || !color .equals (lastColor )) {
322
+ logger .trace ("Updating color old:{} to new:{}, on state old:{} to new:{}" , lastColor , color , lastOn , on );
323
+ updateState (CHANNEL_COLOR , buildHSB (normalRGB , brightness , on == OnOffType .ON ));
324
+ lastOn = on ;
318
325
lastColor = color ;
319
326
}
320
327
321
328
logger .trace ("Comparing color temperature old:{} to new:{}" , lastKelvin , kelvin );
322
329
if (kelvin != lastKelvin ) {
323
330
logger .trace ("Updating color temperature old:{} to new:{}" , lastKelvin , kelvin );
324
- updateState (CHANNEL_COLOR_TEMPERATURE_ABS , QuantityType .valueOf (kelvin , Units .KELVIN ));
325
- updateState (CHANNEL_COLOR_TEMPERATURE , kelvinToPercent (kelvin ));
331
+ if (kelvin != 0 ) {
332
+ kelvin = Math .min (COLOR_TEMPERATURE_MAX_VALUE .intValue (),
333
+ Math .max (COLOR_TEMPERATURE_MIN_VALUE .intValue (), kelvin ));
334
+ updateState (CHANNEL_COLOR_TEMPERATURE , kelvinToPercent (kelvin ));
335
+ updateState (CHANNEL_COLOR_TEMPERATURE_ABS , QuantityType .valueOf (kelvin , Units .KELVIN ));
336
+ } else {
337
+ updateState (CHANNEL_COLOR_TEMPERATURE , UnDefType .UNDEF );
338
+ updateState (CHANNEL_COLOR_TEMPERATURE_ABS , UnDefType .UNDEF );
339
+ }
326
340
lastKelvin = kelvin ;
327
341
}
328
342
}
0 commit comments