2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
14 package org.openhab.binding.mqtt.espmilighthub.internal.handler;
16 import static org.openhab.binding.mqtt.MqttBindingConstants.BINDING_ID;
17 import static org.openhab.binding.mqtt.espmilighthub.internal.EspMilightHubBindingConstants.*;
19 import java.math.BigDecimal;
20 import java.math.MathContext;
21 import java.nio.charset.StandardCharsets;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.mqtt.espmilighthub.internal.ConfigOptions;
29 import org.openhab.binding.mqtt.espmilighthub.internal.Helper;
30 import org.openhab.binding.mqtt.handler.AbstractBrokerHandler;
31 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
32 import org.openhab.core.io.transport.mqtt.MqttMessageSubscriber;
33 import org.openhab.core.library.types.DecimalType;
34 import org.openhab.core.library.types.HSBType;
35 import org.openhab.core.library.types.IncreaseDecreaseType;
36 import org.openhab.core.library.types.OnOffType;
37 import org.openhab.core.library.types.PercentType;
38 import org.openhab.core.library.types.StringType;
39 import org.openhab.core.thing.Bridge;
40 import org.openhab.core.thing.ChannelUID;
41 import org.openhab.core.thing.Thing;
42 import org.openhab.core.thing.ThingRegistry;
43 import org.openhab.core.thing.ThingStatus;
44 import org.openhab.core.thing.ThingStatusDetail;
45 import org.openhab.core.thing.ThingStatusInfo;
46 import org.openhab.core.thing.binding.BaseThingHandler;
47 import org.openhab.core.thing.binding.ThingHandler;
48 import org.openhab.core.types.Command;
49 import org.openhab.core.types.RefreshType;
50 import org.openhab.core.types.State;
51 import org.openhab.core.util.ColorUtil;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * The {@link EspMilightHubHandler} is responsible for handling commands of the globes, which are then
57 * sent to one of the bridges to be sent out by MQTT.
59 * @author Matthew Skinner - Initial contribution
62 public class EspMilightHubHandler extends BaseThingHandler implements MqttMessageSubscriber {
63 // these are all constants used in color conversion calcuations.
64 // strings are necessary to prevent floating point loss of precision
65 private static final BigDecimal BIG_DECIMAL_THOUSAND = new BigDecimal(1000);
66 private static final BigDecimal BIG_DECIMAL_MILLION = new BigDecimal(1000000);
68 private static final BigDecimal[][] KANG_X_COEFFICIENTS = {
69 { new BigDecimal("-3.0258469"), new BigDecimal("2.1070379"), new BigDecimal("0.2226347"),
70 new BigDecimal("0.24039") },
71 { new BigDecimal("-0.2661239"), new BigDecimal("-0.234589"), new BigDecimal("0.8776956"),
72 new BigDecimal("0.179910") } };
74 private static final BigDecimal[][] KANG_Y_COEFFICIENTS = {
75 { new BigDecimal("3.0817580"), new BigDecimal("-5.8733867"), new BigDecimal("3.75112997"),
76 new BigDecimal("-0.37001483") },
77 { new BigDecimal("-0.9549476"), new BigDecimal("-1.37418593"), new BigDecimal("2.09137015"),
78 new BigDecimal("-0.16748867") },
79 { new BigDecimal("-1.1063814"), new BigDecimal("-1.34811020"), new BigDecimal("2.18555832"),
80 new BigDecimal("-0.20219683") } };
82 private static final BigDecimal BIG_DECIMAL_03320 = new BigDecimal("0.3320");
83 private static final BigDecimal BIG_DECIMAL_01858 = new BigDecimal("0.1858");
84 private static final BigDecimal[] MCCAMY_COEFFICIENTS = { new BigDecimal(437), new BigDecimal(3601),
85 new BigDecimal(6862), new BigDecimal(5517) };
87 private static final BigDecimal BIG_DECIMAL_2 = new BigDecimal(2);
88 private static final BigDecimal BIG_DECIMAL_3 = new BigDecimal(3);
89 private static final BigDecimal BIG_DECIMAL_4 = new BigDecimal(4);
90 private static final BigDecimal BIG_DECIMAL_6 = new BigDecimal(6);
91 private static final BigDecimal BIG_DECIMAL_12 = new BigDecimal(12);
93 private static final BigDecimal BIG_DECIMAL_0292 = new BigDecimal("0.292");
94 private static final BigDecimal BIG_DECIMAL_024 = new BigDecimal("0.24");
96 private static final BigDecimal[] CORM_COEFFICIENTS = { new BigDecimal("-0.00616793"), new BigDecimal("0.0893944"),
97 new BigDecimal("-0.5179722"), new BigDecimal("1.5317403"), new BigDecimal("-2.4243787"),
98 new BigDecimal("1.925865"), new BigDecimal("-0.471106") };
100 private static final BigDecimal BIG_DECIMAL_153 = new BigDecimal(153);
101 private static final BigDecimal BIG_DECIMAL_217 = new BigDecimal(217);
103 private final Logger logger = LoggerFactory.getLogger(this.getClass());
104 private @Nullable MqttBrokerConnection connection;
105 private ThingRegistry thingRegistry;
106 private String globeType = "";
107 private String bulbMode = "";
108 private String remotesGroupID = "";
109 private String channelPrefix = "";
110 private String fullCommandTopic = "";
111 private String fullStatesTopic = "";
112 private BigDecimal maxColourTemp = BigDecimal.ZERO;
113 private BigDecimal minColourTemp = BigDecimal.ZERO;
114 private BigDecimal savedLevel = BIG_DECIMAL_100;
115 private ConfigOptions config = new ConfigOptions();
117 public EspMilightHubHandler(Thing thing, ThingRegistry thingRegistry) {
119 this.thingRegistry = thingRegistry;
122 void changeChannel(String channel, State state) {
123 updateState(new ChannelUID(channelPrefix + channel), state);
124 // Remote code of 0 means that all channels need to follow this change.
125 if ("0".equals(remotesGroupID)) {
127 // These two are 8 channel remotes
130 updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "5:" + channel),
132 updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "6:" + channel),
134 updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "7:" + channel),
136 updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "8:" + channel),
139 updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "1:" + channel),
141 updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "2:" + channel),
143 updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "3:" + channel),
145 updateState(new ChannelUID(channelPrefix.substring(0, channelPrefix.length() - 2) + "4:" + channel),
151 private void processIncomingState(String messageJSON) {
152 // Need to handle State and Level at the same time to process level=0 as off//
153 PercentType tempBulbLevel = PercentType.ZERO;
154 String bulbState = Helper.resolveJSON(messageJSON, "\"state\":\"", 3);
155 String bulbLevel = Helper.resolveJSON(messageJSON, "\"level\":", 3);
156 if (!bulbLevel.isEmpty()) {
157 if ("0".equals(bulbLevel) || "OFF".equals(bulbState)) {
158 changeChannel(CHANNEL_LEVEL, OnOffType.OFF);
160 tempBulbLevel = new PercentType(Integer.valueOf(bulbLevel));
161 changeChannel(CHANNEL_LEVEL, tempBulbLevel);
163 } else if ("ON".equals(bulbState) || "OFF".equals(bulbState)) { // NOTE: Level is missing when this runs
164 changeChannel(CHANNEL_LEVEL, OnOffType.valueOf(bulbState));
166 bulbMode = Helper.resolveJSON(messageJSON, "\"bulb_mode\":\"", 5);
170 changeChannel(CHANNEL_BULB_MODE, new StringType("white"));
171 changeChannel(CHANNEL_DISCO_MODE, new StringType("None"));
173 String bulbCTempS = Helper.resolveJSON(messageJSON, "\"color_temp\":", 3);
174 if (!bulbCTempS.isEmpty()) {
175 var bulbCTemp = Integer.valueOf(bulbCTempS);
176 changeChannel(CHANNEL_COLOURTEMP, scaleMireds(bulbCTemp));
178 changeChannel(CHANNEL_COLOUR, calculateHSBFromColorTemp(bulbCTemp, tempBulbLevel));
183 changeChannel(CHANNEL_BULB_MODE, new StringType("color"));
184 changeChannel(CHANNEL_DISCO_MODE, new StringType("None"));
185 String bulbHue = Helper.resolveJSON(messageJSON, "\"hue\":", 3);
186 String bulbSaturation = Helper.resolveJSON(messageJSON, "\"saturation\":", 3);
187 if (bulbHue.isEmpty()) {
188 logger.warn("Milight MQTT message came in as being a colour mode, but was missing a HUE value.");
190 if (bulbSaturation.isEmpty()) {
191 bulbSaturation = "100";
193 // 360 isn't allowed by OpenHAB
194 if ("360".equals(bulbHue)) {
197 var hsb = new HSBType(new DecimalType(Integer.valueOf(bulbHue)),
198 new PercentType(Integer.valueOf(bulbSaturation)), tempBulbLevel);
199 changeChannel(CHANNEL_COLOUR, hsb);
201 changeChannel(CHANNEL_COLOURTEMP, scaleMireds(calculateColorTempFromHSB(hsb)));
207 changeChannel(CHANNEL_BULB_MODE, new StringType("scene"));
209 String bulbDiscoMode = Helper.resolveJSON(messageJSON, "\"mode\":", 1);
210 if (!bulbDiscoMode.isEmpty()) {
211 changeChannel(CHANNEL_DISCO_MODE, new StringType(bulbDiscoMode));
216 changeChannel(CHANNEL_BULB_MODE, new StringType("night"));
217 if (config.oneTriggersNightMode) {
218 changeChannel(CHANNEL_LEVEL, new PercentType("1"));
225 private boolean hasCCT() {
237 private boolean hasRGB() {
250 * Scales mireds to 0-100%
252 private static PercentType scaleMireds(int mireds) {
253 // range in mireds is 153-370
254 // 100 - (mireds - 153) / (370 - 153) * 100
256 return PercentType.HUNDRED;
257 } else if (mireds <= 153) {
258 return PercentType.ZERO;
260 return new PercentType(BIG_DECIMAL_100.subtract(new BigDecimal(mireds).subtract(BIG_DECIMAL_153)
261 .divide(BIG_DECIMAL_217, MathContext.DECIMAL128).multiply(BIG_DECIMAL_100)));
264 private static BigDecimal polynomialFit(BigDecimal x, BigDecimal[] coefficients) {
265 var result = BigDecimal.ZERO;
266 var xAccumulator = BigDecimal.ONE;
267 // forms K[4]*x^0 + K[3]*x^1 + K[2]*x^2 + K[1]*x^3 + K[0]*x^4
268 // (or reverse the order of terms for the usual way of writing it in academic papers)
269 for (int i = coefficients.length - 1; i >= 0; i--) {
270 result = result.add(coefficients[i].multiply(xAccumulator));
271 xAccumulator = xAccumulator.multiply(x);
276 // https://www.jkps.or.kr/journal/download_pdf.php?spage=865&volume=41&number=6 (8) and (9)
277 private static HSBType calculateHSBFromColorTemp(int mireds, PercentType brightness) {
278 var cct = BIG_DECIMAL_MILLION.divide(new BigDecimal(mireds), MathContext.DECIMAL128);
279 var cctInt = cct.intValue();
281 BigDecimal[] coefficients;
282 // 1667K to 4000K and 4000K to 25000K; no range checks since our mired range fits within this
283 if (cctInt <= 4000) {
284 coefficients = KANG_X_COEFFICIENTS[1];
286 coefficients = KANG_X_COEFFICIENTS[0];
288 BigDecimal x = polynomialFit(BIG_DECIMAL_THOUSAND.divide(cct, MathContext.DECIMAL128), coefficients);
290 if (cctInt <= 2222) {
291 coefficients = KANG_Y_COEFFICIENTS[2];
292 } else if (cctInt <= 4000) {
293 coefficients = KANG_Y_COEFFICIENTS[1];
295 coefficients = KANG_Y_COEFFICIENTS[0];
297 BigDecimal y = polynomialFit(x, coefficients);
298 var rawHsb = ColorUtil.xyToHsb(new double[] { x.doubleValue(), y.doubleValue() });
299 return new HSBType(rawHsb.getHue(), rawHsb.getSaturation(), brightness);
302 // https://www.waveformlighting.com/tech/calculate-color-temperature-cct-from-cie-1931-xy-coordinates/
303 private static int calculateColorTempFromHSB(HSBType hsb) {
304 PercentType[] xy = hsb.toXY();
305 var x = xy[0].toBigDecimal().divide(BIG_DECIMAL_100);
306 var y = xy[1].toBigDecimal().divide(BIG_DECIMAL_100);
307 var n = x.subtract(BIG_DECIMAL_03320).divide(BIG_DECIMAL_01858.subtract(y), MathContext.DECIMAL128);
308 BigDecimal cctK = polynomialFit(n, MCCAMY_COEFFICIENTS);
309 return BIG_DECIMAL_MILLION.divide(cctK, MathContext.DECIMAL128).round(new MathContext(0)).intValue();
312 // https://cormusa.org/wp-content/uploads/2018/04/CORM_2011_Calculation_of_CCT_and_Duv_and_Practical_Conversion_Formulae.pdf
314 private static BigDecimal calculateDuvFromHSB(HSBType hsb) {
315 PercentType[] xy = hsb.toXY();
316 var x = xy[0].toBigDecimal().divide(BIG_DECIMAL_100);
317 var y = xy[1].toBigDecimal().divide(BIG_DECIMAL_100);
318 var u = BIG_DECIMAL_4.multiply(x).divide(
319 BIG_DECIMAL_2.multiply(x).negate().add(BIG_DECIMAL_12.multiply(y).add(BIG_DECIMAL_3)),
320 MathContext.DECIMAL128);
321 var v = BIG_DECIMAL_6.multiply(y).divide(
322 BIG_DECIMAL_2.multiply(x).negate().add(BIG_DECIMAL_12.multiply(y).add(BIG_DECIMAL_3)),
323 MathContext.DECIMAL128);
324 var Lfp = u.subtract(BIG_DECIMAL_0292).pow(2).add(v.subtract(BIG_DECIMAL_024).pow(2))
325 .sqrt(MathContext.DECIMAL128);
326 var a = new BigDecimal(
327 Math.acos(u.subtract(BIG_DECIMAL_0292).divide(Lfp, MathContext.DECIMAL128).doubleValue()));
328 BigDecimal Lbb = polynomialFit(a, CORM_COEFFICIENTS);
329 return Lfp.subtract(Lbb);
333 * Used to calculate the colour temp for a globe if you want the light to get warmer as it is dimmed like a
334 * traditional halogen globe
336 private int autoColourTemp(int brightness) {
337 return minColourTemp.subtract(
338 minColourTemp.subtract(maxColourTemp).divide(BIG_DECIMAL_100).multiply(new BigDecimal(brightness)))
343 if (config.powerFailsToMinimum) {
344 sendMQTT("{\"state\":\"OFF\",\"level\":0}");
346 sendMQTT("{\"state\":\"OFF\"}");
350 void handleLevelColour(Command command) {
353 if (command instanceof OnOffType) {
354 if (OnOffType.ON.equals(command)) {
355 sendMQTT("{\"state\":\"ON\",\"level\":" + savedLevel + "}");
360 } else if (command instanceof IncreaseDecreaseType) {
361 if (IncreaseDecreaseType.INCREASE.equals(command)) {
362 if (savedLevel.intValue() <= 90) {
363 savedLevel = savedLevel.add(BigDecimal.TEN);
366 if (savedLevel.intValue() >= 10) {
367 savedLevel = savedLevel.subtract(BigDecimal.TEN);
370 sendMQTT("{\"state\":\"ON\",\"level\":" + savedLevel.intValue() + "}");
372 } else if (command instanceof HSBType hsb) {
373 // This feature allows google home or Echo to trigger white mode when asked to turn color to white.
374 if (hsb.getHue().intValue() == config.whiteHue && hsb.getSaturation().intValue() == config.whiteSat) {
376 sendMQTT("{\"state\":\"ON\",\"color_temp\":" + config.favouriteWhite + "}");
377 } else {// globe must only have 1 type of white
378 sendMQTT("{\"command\":\"set_white\"}");
381 } else if (PercentType.ZERO.equals(hsb.getBrightness())) {
384 } else if (config.duvThreshold.compareTo(BigDecimal.ONE) < 0
385 && calculateDuvFromHSB(hsb).abs().compareTo(config.duvThreshold) <= 0
386 && (mireds = calculateColorTempFromHSB(hsb)) >= 153 && mireds <= 370) {
387 sendMQTT("{\"state\":\"ON\",\"level\":" + hsb.getBrightness() + ",\"color_temp\":" + mireds + "}");
388 } else if (config.whiteThreshold != -1 && hsb.getSaturation().intValue() <= config.whiteThreshold) {
389 sendMQTT("{\"command\":\"set_white\"}");// Can't send the command and level in the same message.
390 sendMQTT("{\"level\":" + hsb.getBrightness().intValue() + "}");
392 sendMQTT("{\"state\":\"ON\",\"level\":" + hsb.getBrightness().intValue() + ",\"hue\":"
393 + hsb.getHue().intValue() + ",\"saturation\":" + hsb.getSaturation().intValue() + "}");
395 savedLevel = hsb.getBrightness().toBigDecimal();
397 } else if (command instanceof PercentType percentType) {
398 if (percentType.intValue() == 0) {
401 } else if (percentType.intValue() == 1 && config.oneTriggersNightMode) {
402 sendMQTT("{\"command\":\"night_mode\"}");
405 sendMQTT("{\"state\":\"ON\",\"level\":" + command + "}");
406 savedLevel = percentType.toBigDecimal();
408 if (config.dimmedCT > 0 && "white".equals(bulbMode)) {
409 sendMQTT("{\"state\":\"ON\",\"color_temp\":" + autoColourTemp(savedLevel.intValue()) + "}");
417 public void handleCommand(ChannelUID channelUID, Command command) {
418 if (command instanceof RefreshType) {
421 switch (channelUID.getId()) {
424 handleLevelColour(command);
426 case CHANNEL_BULB_MODE:
427 bulbMode = command.toString();
429 case CHANNEL_COLOURTEMP:
430 int scaledCommand = (int) Math.round((370 - (2.17 * Float.valueOf(command.toString()))));
431 sendMQTT("{\"state\":\"ON\",\"level\":" + savedLevel + ",\"color_temp\":" + scaledCommand + "}");
433 case CHANNEL_COMMAND:
434 sendMQTT("{\"command\":\"" + command + "\"}");
436 case CHANNEL_DISCO_MODE:
437 sendMQTT("{\"mode\":\"" + command + "\"}");
443 public void initialize() {
444 config = getConfigAs(ConfigOptions.class);
445 if (config.dimmedCT > 0) {
446 maxColourTemp = new BigDecimal(config.favouriteWhite);
447 minColourTemp = new BigDecimal(config.dimmedCT);
448 if (minColourTemp.intValue() <= maxColourTemp.intValue()) {
449 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
450 "The dimmedCT config value must be greater than the favourite White value.");
455 globeType = thing.getThingTypeUID().getId();// eg rgb_cct
456 String globeLocation = this.getThing().getUID().getId();// eg 0x014
457 remotesGroupID = globeLocation.substring(globeLocation.length() - 1, globeLocation.length());// eg 4
458 String remotesIDCode = globeLocation.substring(0, globeLocation.length() - 1);// eg 0x01
459 fullCommandTopic = COMMANDS_BASE_TOPIC + remotesIDCode + "/" + globeType + "/" + remotesGroupID;
460 fullStatesTopic = STATES_BASE_TOPIC + remotesIDCode + "/" + globeType + "/" + remotesGroupID;
461 // Need to remove the lowercase x from 0x12AB in case it contains all numbers
462 String caseCheck = globeLocation.substring(2, globeLocation.length() - 1);
463 if (!caseCheck.equals(caseCheck.toUpperCase())) {
464 logger.warn("The milight globe {}{} is using lowercase for the remote code when the hub needs UPPERCASE",
465 remotesIDCode, remotesGroupID);
467 channelPrefix = BINDING_ID + ":" + globeType + ":" + thing.getBridgeUID().getId() + ":" + remotesIDCode
468 + remotesGroupID + ":";
469 bridgeStatusChanged(getBridgeStatus());
472 private void sendMQTT(String payload) {
473 MqttBrokerConnection localConnection = connection;
474 if (localConnection != null) {
475 localConnection.publish(fullCommandTopic, payload.getBytes(), 1, false);
480 public void processMessage(String topic, byte[] payload) {
481 String state = new String(payload, StandardCharsets.UTF_8);
482 logger.trace("Received the following new Milight state:{}:{}", topic, state);
484 if (topic.equals(STATUS_TOPIC)) {
485 if (state.equals(CONNECTED)) {
486 updateStatus(ThingStatus.ONLINE);
488 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
489 "Waiting for 'milight/status: connected' MQTT message to be sent from your ESP Milight hub.");
493 processIncomingState(state);
494 } catch (Exception e) {
495 logger.warn("Failed processing Milight state {} for {}", state, topic, e);
500 public ThingStatusInfo getBridgeStatus() {
501 Bridge b = getBridge();
503 return b.getStatusInfo();
505 return new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, null);
510 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
511 if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
512 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
516 if (bridgeStatusInfo.getStatus() != ThingStatus.ONLINE) {
517 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
521 Bridge localBridge = this.getBridge();
522 if (localBridge == null) {
523 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED,
524 "Bridge is missing or offline, you need to setup a working MQTT broker first.");
527 ThingHandler handler = localBridge.getHandler();
528 if (handler instanceof AbstractBrokerHandler abh) {
529 final MqttBrokerConnection connection;
531 connection = abh.getConnectionAsync().get(500, TimeUnit.MILLISECONDS);
532 } catch (InterruptedException | ExecutionException | TimeoutException ignored) {
533 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED,
534 "Bridge handler has no valid broker connection!");
537 this.connection = connection;
538 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.CONFIGURATION_PENDING,
539 "Waiting for 'milight/status: connected' MQTT message to be received. Check hub has 'MQTT Client Status Topic' configured.");
540 connection.subscribe(fullStatesTopic, this);
541 connection.subscribe(STATUS_TOPIC, this);
547 public void dispose() {
548 MqttBrokerConnection localConnection = connection;
549 if (localConnection != null) {
550 localConnection.unsubscribe(fullStatesTopic, this);
551 localConnection.unsubscribe(STATUS_TOPIC, this);