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.tradfri.internal.model;
15 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.tradfri.internal.TradfriColor;
20 import org.openhab.core.library.types.HSBType;
21 import org.openhab.core.library.types.PercentType;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonPrimitive;
29 * The {@link TradfriLightData} class is a Java wrapper for the raw JSON data about the light state.
31 * @author Kai Kreuzer - Initial contribution
32 * @author Holger Reichert - Support for color bulbs
33 * @author Christoph Weitkamp - Restructuring and refactoring of the binding
36 public class TradfriLightData extends TradfriDeviceData {
38 private final Logger logger = LoggerFactory.getLogger(TradfriLightData.class);
40 public TradfriLightData() {
44 public TradfriLightData(JsonElement json) {
48 public TradfriLightData setBrightness(PercentType brightness) {
49 attributes.add(DIMMER, new JsonPrimitive((int) Math.floor(brightness.doubleValue() * 2.54)));
53 public @Nullable PercentType getBrightness() {
54 PercentType result = null;
56 JsonElement dimmer = attributes.get(DIMMER);
58 result = TradfriColor.xyBrightnessToPercentType(dimmer.getAsInt());
64 public TradfriLightData setTransitionTime(int seconds) {
65 attributes.add(TRANSITION_TIME, new JsonPrimitive(seconds));
69 public int getTransitionTime() {
70 JsonElement transitionTime = attributes.get(TRANSITION_TIME);
71 if (transitionTime != null) {
72 return transitionTime.getAsInt();
78 public TradfriLightData setColorTemperature(PercentType c) {
79 TradfriColor color = new TradfriColor(c);
82 logger.debug("New color temperature: {},{} ({} %)", x, y, c.intValue());
83 attributes.add(COLOR_X, new JsonPrimitive(x));
84 attributes.add(COLOR_Y, new JsonPrimitive(y));
88 public @Nullable PercentType getColorTemperature() {
89 JsonElement colorX = attributes.get(COLOR_X);
90 JsonElement colorY = attributes.get(COLOR_Y);
91 if (colorX != null && colorY != null) {
92 TradfriColor color = new TradfriColor(colorX.getAsInt(), colorY.getAsInt(), null);
93 return color.getColorTemperature();
99 public TradfriLightData setColor(HSBType hsb) {
100 TradfriColor color = new TradfriColor(hsb);
101 attributes.add(COLOR_X, new JsonPrimitive(color.xyX));
102 attributes.add(COLOR_Y, new JsonPrimitive(color.xyY));
106 public @Nullable HSBType getColor() {
107 // XY color coordinates plus brightness is needed for color calculation
108 JsonElement colorX = attributes.get(COLOR_X);
109 JsonElement colorY = attributes.get(COLOR_Y);
110 JsonElement dimmer = attributes.get(DIMMER);
111 if (colorX != null && colorY != null && dimmer != null) {
112 int x = colorX.getAsInt();
113 int y = colorY.getAsInt();
114 int brightness = dimmer.getAsInt();
115 TradfriColor color = new TradfriColor(x, y, brightness);
116 return color.getHSB();
121 public TradfriLightData setOnOffState(boolean on) {
122 attributes.add(ONOFF, new JsonPrimitive(on ? 1 : 0));
126 public boolean getOnOffState() {
127 JsonElement onOff = attributes.get(ONOFF);
129 return onOff.getAsInt() == 1;
135 public String getJsonString() {
136 return root.toString();