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.bigassfan.internal.utils;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.library.types.PercentType;
19 * The {@link BigAssFanConverter} is responsible for converting between
20 * Dimmer values and values used for fan speed, light brightness, and
21 * light color temperature.
23 * @author Mark Hilbush - Initial contribution
26 public class BigAssFanConverter {
28 * Conversion factor for fan range (0-7) to dimmer range (0-100).
30 private static final double SPEED_CONVERSION_FACTOR = 14.2857;
33 * Conversion factor for light range (0-16) to dimmer range (0-100).
35 private static final double BRIGHTNESS_CONVERSION_FACTOR = 6.25;
38 * Conversion factor for hue range (2200-5000) to dimmer range (0-100).
40 private static final double HUE_CONVERSION_FACTOR = 28.0;
43 * Dimmer item will produce PercentType value, which is 0-100
44 * Convert that value to what the fan expects, which is 0-7
46 public static String percentToSpeed(PercentType command) {
47 return String.valueOf((int) Math.round(command.doubleValue() / SPEED_CONVERSION_FACTOR));
51 * Fan will supply fan speed value in range of 0-7
52 * Convert that value to a PercentType in range 0-100, which is what Dimmer item expects
54 public static PercentType speedToPercent(String speed) {
55 return new PercentType((int) Math.round(Integer.parseInt(speed) * SPEED_CONVERSION_FACTOR));
59 * Dimmer item will produce PercentType value, which is 0-100
60 * Convert that value to what the light expects, which is 0-16
62 public static String percentToLevel(PercentType command) {
63 return String.valueOf((int) Math.round(command.doubleValue() / BRIGHTNESS_CONVERSION_FACTOR));
67 * Light will supply brightness value in range of 0-16
68 * Convert that value to a PercentType in range 0-100, which is what Dimmer item expects
70 public static PercentType levelToPercent(String level) {
71 return new PercentType((int) Math.round(Integer.parseInt(level) * BRIGHTNESS_CONVERSION_FACTOR));
75 * Dimmer item will produce PercentType value, which is 0-100
76 * Convert that value to what the light expects, which is 2200-5000
78 public static String percentToHue(PercentType command) {
79 return String.valueOf(2200 + (int) Math.round(command.doubleValue() * HUE_CONVERSION_FACTOR));
83 * Light will supply hue value in range of 2200-5000
84 * Convert that value to a PercentType in range 0-100, which is what Dimmer item expects
86 public static PercentType hueToPercent(String hue) {
87 return new PercentType((int) Math.round((Integer.parseInt(hue) - 2200) / HUE_CONVERSION_FACTOR));