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.deconz.internal.action;
16 import java.util.Objects;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.jetty.http.HttpMethod;
21 import org.openhab.binding.deconz.internal.Util;
22 import org.openhab.binding.deconz.internal.handler.DeconzBridgeHandler;
23 import org.openhab.core.automation.annotation.ActionInput;
24 import org.openhab.core.automation.annotation.RuleAction;
25 import org.openhab.core.thing.binding.ThingActions;
26 import org.openhab.core.thing.binding.ThingActionsScope;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * The {@link BridgeActions} provides actions for managing scenes in groups
34 * @author Jan N. Klug - Initial contribution
36 @ThingActionsScope(name = "deconz")
38 public class BridgeActions implements ThingActions {
39 private final Logger logger = LoggerFactory.getLogger(BridgeActions.class);
41 private @Nullable DeconzBridgeHandler handler;
43 @RuleAction(label = "@text/action.permit-join-network.label", description = "@text/action.permit-join-network.description")
44 public void permitJoin(
45 @ActionInput(name = "duration", label = "@text/action.permit-join-network.duration.label", description = "@text/action.permit-join-network.duration.description") @Nullable Integer duration) {
46 DeconzBridgeHandler handler = this.handler;
48 if (handler == null) {
49 logger.warn("Deconz BridgeActions service ThingHandler is null!");
53 int searchDuration = Util.constrainToRange(Objects.requireNonNullElse(duration, 120), 1, 240);
55 Object object = Map.of("permitjoin", searchDuration);
56 handler.sendObject("config", object, HttpMethod.PUT).thenAccept(v -> {
57 if (v.getResponseCode() != java.net.HttpURLConnection.HTTP_OK) {
58 logger.warn("Sending {} via PUT to config failed: {} - {}", object, v.getResponseCode(), v.getBody());
60 logger.trace("Result code={}, body={}", v.getResponseCode(), v.getBody());
61 logger.info("Enabled device searching for {} seconds on bridge {}.", searchDuration,
62 handler.getThing().getUID());
64 }).exceptionally(e -> {
65 logger.warn("Sending {} via PUT to config failed: {} - {}", object, e.getClass(), e.getMessage());
70 public static void permitJoin(ThingActions actions, @Nullable Integer duration) {
71 if (actions instanceof BridgeActions bridgeActions) {
72 bridgeActions.permitJoin(duration);
77 public void setThingHandler(@Nullable ThingHandler handler) {
78 if (handler instanceof DeconzBridgeHandler bridgeHandler) {
79 this.handler = bridgeHandler;
84 public @Nullable ThingHandler getThingHandler() {