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.deconz.internal.action;
15 import java.lang.reflect.Type;
16 import java.util.List;
18 import java.util.concurrent.CompletableFuture;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.eclipse.jetty.http.HttpMethod;
26 import org.openhab.binding.deconz.internal.dto.NewSceneResponse;
27 import org.openhab.binding.deconz.internal.handler.GroupThingHandler;
28 import org.openhab.core.automation.annotation.ActionInput;
29 import org.openhab.core.automation.annotation.ActionOutput;
30 import org.openhab.core.automation.annotation.RuleAction;
31 import org.openhab.core.thing.binding.ThingActions;
32 import org.openhab.core.thing.binding.ThingActionsScope;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.gson.Gson;
38 import com.google.gson.JsonParseException;
39 import com.google.gson.reflect.TypeToken;
42 * The {@link GroupActions} provides actions for managing scenes in groups
44 * @author Jan N. Klug - Initial contribution
46 @ThingActionsScope(name = "deconz")
48 public class GroupActions implements ThingActions {
49 private static final String NEW_SCENE_ID_OUTPUT = "newSceneId";
50 private static final Type NEW_SCENE_RESPONSE_TYPE = new TypeToken<List<NewSceneResponse>>() {
53 private final Logger logger = LoggerFactory.getLogger(GroupActions.class);
54 private final Gson gson = new Gson();
56 private @Nullable GroupThingHandler handler;
58 @RuleAction(label = "@text/action.create-scene.label", description = "@text/action.create-scene.description")
59 public @ActionOutput(name = NEW_SCENE_ID_OUTPUT, type = "java.lang.Integer") Map<String, Object> createScene(
60 @ActionInput(name = "name", label = "@text/action.create-scene.name.label", description = "@text/action.create-scene.name.description") @Nullable String name) {
61 GroupThingHandler handler = this.handler;
63 if (handler == null) {
64 logger.warn("Deconz GroupActions service ThingHandler is null!");
69 logger.debug("Skipping scene creation due to missing scene name");
73 CompletableFuture<String> newSceneId = new CompletableFuture<>();
74 handler.doNetwork(Map.of("name", name), "scenes", HttpMethod.POST, newSceneId::complete);
77 String returnedJson = newSceneId.get(2000, TimeUnit.MILLISECONDS);
78 List<NewSceneResponse> newSceneResponses = gson.fromJson(returnedJson, NEW_SCENE_RESPONSE_TYPE);
79 if (newSceneResponses != null && !newSceneResponses.isEmpty()) {
80 return Map.of(NEW_SCENE_ID_OUTPUT, newSceneResponses.get(0).success.id);
82 throw new IllegalStateException("response is empty");
83 } catch (InterruptedException | ExecutionException | TimeoutException | JsonParseException
84 | IllegalStateException e) {
85 logger.warn("Couldn't get newSceneId", e);
90 public static Map<String, Object> createScene(ThingActions actions, @Nullable String name) {
91 if (actions instanceof GroupActions groupActions) {
92 return groupActions.createScene(name);
97 @RuleAction(label = "@text/action.delete-scene.label", description = "@text/action.delete-scene.description")
98 public void deleteScene(
99 @ActionInput(name = "sceneId", label = "@text/action.delete-scene.sceneId.label", description = "@text/action.delete-scene.sceneId.description") @Nullable Integer sceneId) {
100 GroupThingHandler handler = this.handler;
102 if (handler == null) {
103 logger.warn("Deconz GroupActions service ThingHandler is null!");
107 if (sceneId == null) {
108 logger.warn("Skipping scene deletion due to missing scene id");
112 handler.doNetwork(null, "scenes/" + sceneId, HttpMethod.DELETE, null);
115 public static void deleteScene(ThingActions actions, @Nullable Integer sceneId) {
116 if (actions instanceof GroupActions groupActions) {
117 groupActions.deleteScene(sceneId);
121 @RuleAction(label = "@text/action.store-as-scene.label", description = "@text/action.store-as-scene.description")
122 public void storeScene(
123 @ActionInput(name = "sceneId", label = "@text/action.store-as-scene.sceneId.label", description = "@text/action.store-as-scene.sceneId.description") @Nullable Integer sceneId) {
124 GroupThingHandler handler = this.handler;
126 if (handler == null) {
127 logger.warn("Deconz GroupActions service ThingHandler is null!");
131 if (sceneId == null) {
132 logger.warn("Skipping scene storage due to missing scene id");
136 handler.doNetwork(null, "scenes/" + sceneId + "/store", HttpMethod.PUT, null);
139 public static void storeScene(ThingActions actions, @Nullable Integer sceneId) {
140 if (actions instanceof GroupActions groupActions) {
141 groupActions.storeScene(sceneId);
146 public void setThingHandler(@Nullable ThingHandler handler) {
147 if (handler instanceof GroupThingHandler) {
148 this.handler = (GroupThingHandler) handler;
153 public @Nullable ThingHandler getThingHandler() {