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.avmfritz.internal.dto;
15 import javax.xml.bind.annotation.XmlAccessType;
16 import javax.xml.bind.annotation.XmlAccessorType;
17 import javax.xml.bind.annotation.XmlAttribute;
18 import javax.xml.bind.annotation.XmlElement;
19 import javax.xml.bind.annotation.XmlRootElement;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.library.types.PercentType;
25 * See {@link DeviceListModel}.
27 * @author Joshua Bacher - Initial contribution
29 @XmlAccessorType(XmlAccessType.FIELD)
30 @XmlRootElement(name = "colorcontrol")
31 public class ColorControlModel {
33 private static final double SATURATION_FACTOR = 2.54;
35 @XmlAttribute(name = "supported_modes")
36 public int supportedModes;
37 @XmlAttribute(name = "current_mode")
38 public int currentMode;
40 public int saturation;
41 @XmlElement(name = "unmapped_hue")
42 public @Nullable Integer unmappedHue;
43 @XmlElement(name = "unmapped_saturation")
44 public @Nullable Integer unmappedSaturation;
45 public int temperature;
48 * Converts a FRITZ!Box value to a percent value.
50 * @param fritzValue The FRITZ!Box value to be converted
51 * @return The percent value
53 public static PercentType toPercent(int saturation) {
54 int saturationInPercent = (int) Math.ceil(saturation / SATURATION_FACTOR);
55 return restrictToBounds(saturationInPercent);
59 * Converts a percent value to a FRITZ!Box value.
61 * @param saturationInPercent The percent value to be converted
62 * @return The FRITZ!Box value
64 public static int fromPercent(PercentType saturationInPercent) {
65 return (int) Math.floor(saturationInPercent.intValue() * SATURATION_FACTOR);
69 public String toString() {
70 return new StringBuilder("[supportedModes=").append(supportedModes).append(",currentMode=").append(currentMode)
71 .append(",hue=").append(hue).append(",saturation=").append(saturation).append(",unmapped_hue=")
72 .append(unmappedHue).append(",unmapped_saturation=").append(unmappedSaturation).append(",temperature=")
73 .append(temperature).append("]").toString();
76 private static PercentType restrictToBounds(int percentValue) {
77 if (percentValue < 0) {
78 return PercentType.ZERO;
79 } else if (percentValue > 100) {
80 return PercentType.HUNDRED;
82 return new PercentType(percentValue);