2 * Copyright (c) 2010-2022 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.XmlRootElement;
20 import org.openhab.core.library.types.PercentType;
23 * See {@link DeviceListModel}.
25 * @author Joshua Bacher - Initial contribution
27 @XmlAccessorType(XmlAccessType.FIELD)
28 @XmlRootElement(name = "colorcontrol")
29 public class ColorControlModel {
31 private static final double SATURATION_FACTOR = 2.54;
33 @XmlAttribute(name = "supported_modes")
34 public int supportedModes;
35 @XmlAttribute(name = "current_mode")
36 public int currentMode;
38 public int saturation;
39 public int temperature;
42 * Converts a FRITZ!Box value to a percent value.
44 * @param fritzValue The FRITZ!Box value to be converted
45 * @return The percent value
47 public static PercentType toPercent(int saturation) {
48 int saturationInPercent = (int) Math.ceil(saturation / SATURATION_FACTOR);
49 return restrictToBounds(saturationInPercent);
53 * Converts a percent value to a FRITZ!Box value.
55 * @param saturationInPercent The percent value to be converted
56 * @return The FRITZ!Box value
58 public static int fromPercent(PercentType saturationInPercent) {
59 return (int) Math.floor(saturationInPercent.intValue() * SATURATION_FACTOR);
63 public String toString() {
64 return new StringBuilder("[supportedModes=").append(supportedModes).append(",currentMode=").append(currentMode)
65 .append(",hue=").append(hue).append(",saturation=").append(saturation).append(",temperature=")
66 .append(temperature).append("]").toString();
69 private static PercentType restrictToBounds(int percentValue) {
70 if (percentValue < 0) {
71 return PercentType.ZERO;
72 } else if (percentValue > 100) {
73 return PercentType.HUNDRED;
75 return new PercentType(percentValue);