Skip to content

Commit 7d49ffd

Browse files
committed
improve update speed, add property
Signed-off-by: AndrewFG <[email protected]>
1 parent d5daa92 commit 7d49ffd

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

bundles/org.openhab.binding.govee/src/main/java/org/openhab/binding/govee/internal/GoveeHandler.java

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.openhab.core.thing.binding.BaseThingHandler;
4242
import org.openhab.core.types.Command;
4343
import org.openhab.core.types.RefreshType;
44+
import org.openhab.core.types.UnDefType;
4445
import org.openhab.core.util.ColorUtil;
4546
import org.slf4j.Logger;
4647
import org.slf4j.LoggerFactory;
@@ -89,8 +90,9 @@ public class GoveeHandler extends BaseThingHandler {
8990

9091
private CommunicationManager communicationManager;
9192

93+
private OnOffType lastOn = OnOffType.OFF;
9294
private HSBType lastColor = new HSBType();
93-
private int lastKelvin = COLOR_TEMPERATURE_MIN_VALUE.intValue();
95+
private int lastKelvin;
9496

9597
/**
9698
* 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) {
159161
} else {
160162
switch (channelUID.getId()) {
161163
case CHANNEL_COLOR:
164+
boolean triggerRefresh = false;
162165
Command doCommand = command;
163166
if (doCommand instanceof HSBType hsb) {
164167
sendColor(hsb);
168+
triggerRefresh = true;
165169
doCommand = hsb.getBrightness(); // fall through
166170
}
167171
if (doCommand instanceof PercentType percent) {
168172
sendBrightness(percent);
173+
triggerRefresh = true;
169174
doCommand = OnOffType.from(percent.intValue() > 0); // fall through
170175
}
171176
if (doCommand instanceof OnOffType onOff) {
172177
sendOnOff(onOff);
178+
triggerRefresh = true;
179+
}
180+
if (triggerRefresh) {
181+
triggerDeviceStatusRefresh();
173182
}
174183
break;
175184

176185
case CHANNEL_COLOR_TEMPERATURE:
177186
if (command instanceof PercentType percent) {
178187
sendKelvin(percentToKelvin(percent));
188+
triggerDeviceStatusRefresh();
179189
}
180190
break;
181191

@@ -187,6 +197,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
187197
break;
188198
}
189199
sendKelvin(kelvin.intValue());
200+
triggerDeviceStatusRefresh();
190201
}
191202
break;
192203
}
@@ -218,7 +229,6 @@ public void sendColor(HSBType color) throws IOException {
218229
GenericGoveeData data = new ColorData(new Color(normalRGB[0], normalRGB[1], normalRGB[2]), 0);
219230
GenericGoveeRequest request = new GenericGoveeRequest(new GenericGoveeMsg("colorwc", data));
220231
communicationManager.sendRequest(this, request);
221-
lastColor = color;
222232
}
223233

224234
/**
@@ -229,7 +239,6 @@ public void sendBrightness(PercentType brightness) throws IOException {
229239
GenericGoveeData data = new ValueIntData(brightness.intValue());
230240
GenericGoveeRequest request = new GenericGoveeRequest(new GenericGoveeMsg("brightness", data));
231241
communicationManager.sendRequest(this, request);
232-
lastColor = new HSBType(lastColor.getHue(), lastColor.getSaturation(), brightness);
233242
}
234243

235244
/**
@@ -250,23 +259,23 @@ private void sendKelvin(int kelvin) throws IOException {
250259
GenericGoveeData data = new ColorData(new Color(0, 0, 0), kelvin);
251260
GenericGoveeRequest request = new GenericGoveeRequest(new GenericGoveeMsg("colorwc", data));
252261
communicationManager.sendRequest(this, request);
253-
lastKelvin = kelvin;
254262
}
255263

256264
/**
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.
258267
*
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
262271
*
263-
* @return the HSB presentation state
272+
* @return the respective HSBType
264273
*/
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);
270279
}
271280

272281
void handleIncomingStatus(String response) {
@@ -294,7 +303,7 @@ public void updateDeviceState(@Nullable StatusResponse message) {
294303

295304
logger.trace("Receiving Device State");
296305

297-
boolean on = message.msg().data().onOff() == 1;
306+
OnOffType on = OnOffType.from(message.msg().data().onOff() == 1);
298307
logger.trace("on:{}", on);
299308

300309
int brightness = message.msg().data().brightness();
@@ -306,23 +315,28 @@ public void updateDeviceState(@Nullable StatusResponse message) {
306315
int kelvin = message.msg().data().colorTemInKelvin();
307316
logger.trace("kelvin:{}", kelvin);
308317

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);
313319

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;
318325
lastColor = color;
319326
}
320327

321328
logger.trace("Comparing color temperature old:{} to new:{}", lastKelvin, kelvin);
322329
if (kelvin != lastKelvin) {
323330
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+
}
326340
lastKelvin = kelvin;
327341
}
328342
}

bundles/org.openhab.binding.govee/src/main/resources/OH-INF/i18n/govee.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ discovery.govee-light.H6059 = H6059 RGBWW Night Light for Kids
7474
discovery.govee-light.H618F = H618F RGBIC LED Strip Lights
7575
discovery.govee-light.H618E = H618E LED Strip Lights 22m
7676
discovery.govee-light.H6168 = H6168 TV LED Backlight
77+
discovery.govee-light.H60A1 = H60A1 Smart Ceiling Light
7778

7879
# thing status descriptions
7980

0 commit comments

Comments
 (0)