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.io.hueemulation.internal.dto;
15 import java.lang.reflect.Type;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.items.GenericItem;
20 import org.openhab.core.library.items.StringItem;
21 import org.openhab.core.types.Command;
22 import org.openhab.io.hueemulation.internal.DeviceType;
23 import org.openhab.io.hueemulation.internal.StateUtils;
24 import org.openhab.io.hueemulation.internal.dto.changerequest.HueStateChange;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonSerializationContext;
28 import com.google.gson.JsonSerializer;
31 * Hue API device object
33 * @author Dan Cunningham - Initial contribution
34 * @author David Graeff - Color lights and plugs
35 * @author Florian Lentz - XY Support
38 public class HueLightEntry {
39 public AbstractHueState state = new AbstractHueState();
40 public final String type;
41 public final String modelid;
42 public final String uniqueid;
43 public final String manufacturername;
44 public final @Nullable String productname;
45 public final String swversion;
46 public final @Nullable String luminaireuniqueid = null;
47 public final @Nullable String swconfigid;
48 public final @Nullable String productid;
49 public @Nullable Boolean friendsOfHue = true;
50 public final @Nullable String colorGamut;
51 public @Nullable Boolean hascolor = null;
54 /** Associated item UID */
55 public @NonNullByDefault({}) transient GenericItem item;
56 public transient DeviceType deviceType;
57 public transient @Nullable Command lastCommand = null;
58 public transient @Nullable HueStateChange lastHueChange = null;
60 public static class Config {
61 public final String archetype = "classicbulb";
62 public final String function = "functional";
63 public final String direction = "omnidirectional";
66 public Config config = new Config();
68 public static class Streaming {
69 public boolean renderer = false;
70 public boolean proxy = false;
73 public static class Capabilities {
74 public boolean certified = false;
75 public final Streaming streaming = new Streaming();
76 public final Object control = new Object();
79 public Capabilities capabilities = new Capabilities();
81 private HueLightEntry() {
82 this(new StringItem(""), "", DeviceType.SwitchType);
86 * Create a hue device.
88 * @param item The associated item
89 * @param uniqueid The unique id
90 * @param deviceType The device type decides which capabilities this device has
92 public HueLightEntry(GenericItem item, String uniqueid, DeviceType deviceType) {
93 String label = item.getLabel();
95 this.deviceType = deviceType;
96 this.uniqueid = uniqueid;
99 this.name = label != null ? label : "";
100 this.type = "Extended Color light";
101 this.modelid = "LCT010";
102 this.colorGamut = "C";
103 this.manufacturername = "Philips";
104 this.swconfigid = "F921C859";
105 this.swversion = "1.15.2_r19181";
106 this.productid = "Philips-LCT010-1-A19ECLv4";
107 this.productname = null;
108 this.hascolor = true;
109 this.capabilities.certified = true;
112 /** Hue White A19 - 3nd gen - white, 2700K only */
113 this.name = label != null ? label : "";
114 this.type = "Dimmable light";
115 this.modelid = "LWB006";
116 this.colorGamut = null;
117 this.manufacturername = "Philips";
118 this.swconfigid = null;
119 this.swversion = "66012040";
120 this.productid = null;
121 this.hascolor = false;
122 this.productname = null;
123 this.capabilities.certified = true;
125 case WhiteTemperatureType:
126 this.name = label != null ? label : "";
127 this.type = "Color temperature light";
128 this.modelid = "LTW001";
129 this.colorGamut = "2200K-6500K";
130 this.manufacturername = "Philips";
131 this.swconfigid = null;
132 this.swversion = "66012040";
133 this.productid = null;
134 this.hascolor = false;
135 this.productname = null;
136 this.capabilities.certified = true;
141 * Pretend to be an OSRAM plug, there is no native Philips Hue plug on the market.
142 * Those are supported by most of the external apps and Alexa.
144 this.name = label != null ? label : "";
145 this.type = "On/off light";
146 this.modelid = "Plug 01";
147 this.colorGamut = null;
148 this.manufacturername = "OSRAM";
149 this.productname = "On/Off plug";
150 this.swconfigid = null;
151 this.swversion = "V1.04.12";
152 this.productid = null;
153 this.hascolor = false;
154 this.friendsOfHue = null;
158 state = StateUtils.colorStateFromItemState(item.getState(), deviceType);
162 * This custom serializer updates the light state and label, before serializing.
164 @NonNullByDefault({})
165 public static class Serializer implements JsonSerializer<HueLightEntry> {
167 static class HueDeviceHelper extends HueLightEntry {
171 public JsonElement serialize(HueLightEntry product, Type type, JsonSerializationContext context) {
172 product.state = StateUtils.adjustedColorStateFromItemState(product.item.getState(), product.deviceType,
173 product.lastCommand, product.lastHueChange);
174 String label = product.item.getLabel();
176 product.name = label;
179 return context.serialize(product, HueDeviceHelper.class);
184 * Replaces the associated openHAB item of this hue device with the given once
185 * and also synchronizes/updates the color information of this hue device with the item.
187 * @param element A replace item
189 public void updateItem(GenericItem element) {
191 state = StateUtils.colorStateFromItemState(item.getState(), deviceType);
194 lastHueChange = null;
196 String label = element.getLabel();