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
13 package org.openhab.binding.hue.internal.dto.clip2;
15 import java.util.Objects;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.hue.internal.exceptions.DTOPresentButEmptyException;
20 import org.openhab.core.library.types.QuantityType;
21 import org.openhab.core.library.unit.Units;
23 import com.google.gson.annotations.SerializedName;
26 * DTO for colour temperature of a light in CLIP 2.
28 * @author Andrew Fiddian-Green - Initial contribution
31 public class ColorTemperature {
32 private @Nullable Long mirek;
33 private @Nullable @SerializedName("mirek_schema") MirekSchema mirekSchema;
36 * Get the color temperature as a QuantityType value.
38 * @return a QuantityType value
39 * @throws DTOPresentButEmptyException to indicate that the DTO is present but empty.
41 public @Nullable QuantityType<?> getAbsolute() throws DTOPresentButEmptyException {
42 Long mirek = this.mirek;
43 if (Objects.nonNull(mirek)) {
44 return QuantityType.valueOf(mirek, Units.MIRED).toInvertibleUnit(Units.KELVIN);
46 throw new DTOPresentButEmptyException("'color_temperature' DTO is present but empty");
49 public @Nullable Long getMirek() {
53 public @Nullable MirekSchema getMirekSchema() {
58 * Get the color temperature as a percentage based on the MirekSchema. Note: this method is only to be used on
59 * cached state DTOs which already have a defined mirek schema.
61 * @return the percentage of the mirekSchema range.
62 * @throws DTOPresentButEmptyException to indicate that the DTO is present but empty.
64 public @Nullable Double getPercent() throws DTOPresentButEmptyException {
65 Long mirek = this.mirek;
66 if (Objects.nonNull(mirek)) {
67 MirekSchema mirekSchema = this.mirekSchema;
68 mirekSchema = Objects.nonNull(mirekSchema) ? mirekSchema : MirekSchema.DEFAULT_SCHEMA;
69 double min = mirekSchema.getMirekMinimum();
70 double max = mirekSchema.getMirekMaximum();
71 double percent = 100f * (mirek.doubleValue() - min) / (max - min);
72 return Math.max(0, Math.min(100, percent));
74 throw new DTOPresentButEmptyException("'mirek_schema' DTO is present but empty");
77 public ColorTemperature setMirek(double mirek) {
78 this.mirek = Math.round(mirek);
82 public ColorTemperature setMirekSchema(@Nullable MirekSchema mirekSchema) {
83 this.mirekSchema = mirekSchema;