2 * Copyright (c) 2010-2024 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.hue.internal.api.dto.clip1;
15 import java.lang.reflect.Type;
16 import java.util.ArrayList;
17 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.types.StateOption;
24 import com.google.gson.annotations.SerializedName;
25 import com.google.gson.reflect.TypeToken;
28 * Basic scene information.
30 * @author Hengrui Jiang - Initial contribution
34 public static final Type GSON_TYPE = new TypeToken<Map<String, Scene>>() {
37 private @NonNullByDefault({}) String id;
38 private @NonNullByDefault({}) String name;
39 @SerializedName("lights")
40 private @Nullable List<String> lightIds;
41 @SerializedName("group")
42 private @Nullable String groupId;
43 private boolean recycle;
46 * Default constructor for GSon.
55 public Scene(String id, String name, @Nullable String groupId, List<String> lightIds, boolean recycle) {
58 this.groupId = groupId;
59 this.lightIds = lightIds;
60 this.recycle = recycle;
63 public String getId() {
67 public void setId(String id) {
72 * Returns the human readable name of the scene. If the name is omitted upon creation, this
75 * @return human readable name of the scene
77 public String getName() {
82 * Returns the list of lights that the scene applies to. For group scenes, this list should be identical to the list
83 * of all lights that are in the group.
85 * @return list of lights that the scene applies to
87 public List<String> getLightIds() {
88 List<String> lightIds = this.lightIds;
89 return lightIds != null ? lightIds : new ArrayList<String>();
93 * Returns the group that the scene belongs to. This field is optional for scenes that applies to a specific list of
94 * lights instead of a group.
96 * @return the group that the scene belongs to
98 public @Nullable String getGroupId() {
103 * Indicates if the scene can be recycled by the bridge. A recyclable scene is not able to be activated.
105 * @return whether the scene can be recycled
107 public boolean isRecycle() {
112 * Creates a {@link StateOption} to display this scene, including the group that it belongs to.
114 * The display name is built with the following pattern:
116 * <li>Human readable name of the scene if set. Otherwise, the ID is displayed</li>
117 * <li>Group for which the scene is defined</li>
120 public StateOption toStateOption(Map<String, String> groupNames) {
121 StringBuilder stateOptionLabel = new StringBuilder(name);
122 if (groupId != null && groupNames.containsKey(groupId)) {
123 stateOptionLabel.append(" (").append(groupNames.get(groupId)).append(")");
126 return new StateOption(id, stateOptionLabel.toString());
130 * Creates a {@link StateOption} to display this scene.
132 public StateOption toStateOption() {
133 return new StateOption(id, name);
137 * Returns whether the scene is applicable to the given group.
139 * According to the hue API, a scene is applicable to a group if either
141 * <li>The scene is defined for the group</li>
142 * <li>All lights of the scene also belong to the group</li>
145 public boolean isApplicableTo(FullGroup group) {
146 String groupId = this.groupId;
147 if (groupId == null) {
148 return getLightIds().stream().allMatch(id -> group.getLightIds().contains(id));
150 return group.getId().contentEquals(groupId);
154 public String extractKeyForComparator() {
155 return (groupId != null ? groupId : "") + "#" + name;
159 public String toString() {
160 return String.format("{Scene name: %s; id: %s; lightIds: %s; groupId: %s; recycle: %s}", name, id, lightIds,