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;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.stream.Collectors;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.items.GroupItem;
23 import org.openhab.io.hueemulation.internal.ConfigStore;
24 import org.openhab.io.hueemulation.internal.DeviceType;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonSerializationContext;
28 import com.google.gson.JsonSerializer;
29 import com.google.gson.annotations.SerializedName;
32 * Hue API group object
34 * @author David Graeff - Initial contribution
37 public class HueGroupEntry {
38 public static enum TypeEnum {
43 Entertainment, // 1.22
47 public AbstractHueState action = new HueStatePlug();
50 public String type = TypeEnum.LightGroup.name();
52 // A unique, editable name given to the group.
55 @SerializedName("class")
56 public String roomclass = "Other";
58 // The IDs of the lights that are in the group.
59 public List<String> lights = Collections.emptyList();
60 public List<String> sensors = Collections.emptyList();
62 public transient @NonNullByDefault({}) GroupItem groupItem;
63 public transient @Nullable DeviceType deviceType;
65 // For deserialisation
70 public HueGroupEntry(String name, @Nullable GroupItem groupItem, @Nullable DeviceType deviceType) {
72 this.groupItem = groupItem;
73 this.deviceType = deviceType;
76 public void updateItem(GroupItem element) {
81 * This custom serializer computes the {@link HueGroupEntry#lights} list, before serializing.
82 * It does so, by looking up all item members of the references groupItem.
85 public static class Serializer implements JsonSerializer<HueGroupEntry> {
87 private ConfigStore cs;
89 public Serializer(ConfigStore cs) {
93 static class HueGroupHelper extends HueGroupEntry {
98 public JsonElement serialize(HueGroupEntry product, Type type, JsonSerializationContext context) {
99 GroupItem item = product.groupItem;
101 product.lights = item.getMembers().stream().map(gitem -> cs.mapItemUIDtoHueID(gitem))
102 .collect(Collectors.toList());
105 JsonElement jsonSubscription = context.serialize(product, HueGroupHelper.class);
106 return jsonSubscription;