]> git.basschouten.com Git - openhab-addons.git/blob
a84dfdd858c318e1248a09cce7b853cee2414b6c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.hueemulation.internal.dto;
14
15 import java.lang.reflect.Type;
16
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;
25
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonSerializationContext;
28 import com.google.gson.JsonSerializer;
29
30 /**
31  * Hue API device object
32  *
33  * @author Dan Cunningham - Initial contribution
34  * @author David Graeff - Color lights and plugs
35  * @author Florian Lentz - XY Support
36  */
37 @NonNullByDefault
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;
52
53     public String name;
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;
59
60     public static class Config {
61         public final String archetype = "classicbulb";
62         public final String function = "functional";
63         public final String direction = "omnidirectional";
64     }
65
66     public Config config = new Config();
67
68     public static class Streaming {
69         public boolean renderer = false;
70         public boolean proxy = false;
71     }
72
73     public static class Capabilities {
74         public boolean certified = false;
75         public final Streaming streaming = new Streaming();
76         public final Object control = new Object();
77     }
78
79     public Capabilities capabilities = new Capabilities();
80
81     private HueLightEntry() {
82         this(new StringItem(""), "", DeviceType.SwitchType);
83     }
84
85     /**
86      * Create a hue device.
87      *
88      * @param item The associated item
89      * @param uniqueid The unique id
90      * @param deviceType The device type decides which capabilities this device has
91      */
92     public HueLightEntry(GenericItem item, String uniqueid, DeviceType deviceType) {
93         String label = item.getLabel();
94         this.item = item;
95         this.deviceType = deviceType;
96         this.uniqueid = uniqueid;
97         switch (deviceType) {
98             case ColorType:
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;
110                 break;
111             case WhiteType:
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;
124                 break;
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;
137                 break;
138             case SwitchType:
139             default:
140                 /**
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.
143                  */
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;
155                 break;
156         }
157
158         state = StateUtils.colorStateFromItemState(item.getState(), deviceType);
159     }
160
161     /**
162      * This custom serializer updates the light state and label, before serializing.
163      */
164     @NonNullByDefault({})
165     public static class Serializer implements JsonSerializer<HueLightEntry> {
166
167         static class HueDeviceHelper extends HueLightEntry {
168         }
169
170         @Override
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();
175             if (label != null) {
176                 product.name = label;
177             }
178
179             return context.serialize(product, HueDeviceHelper.class);
180         }
181     }
182
183     /**
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.
186      *
187      * @param element A replace item
188      */
189     public void updateItem(GenericItem element) {
190         item = element;
191         state = StateUtils.colorStateFromItemState(item.getState(), deviceType);
192
193         lastCommand = null;
194         lastHueChange = null;
195
196         String label = element.getLabel();
197         if (label != null) {
198             name = label;
199         }
200     }
201 }