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.hue.internal.console;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.stream.Collectors;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.hue.internal.HueBindingConstants;
22 import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
23 import org.openhab.binding.hue.internal.handler.HueGroupHandler;
24 import org.openhab.core.io.console.Console;
25 import org.openhab.core.io.console.ConsoleCommandCompleter;
26 import org.openhab.core.io.console.StringsCompleter;
27 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
28 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingRegistry;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
38 * The {@link HueCommandExtension} is responsible for handling console commands
40 * @author Laurent Garnier - Initial contribution
44 @Component(service = ConsoleCommandExtension.class)
45 public class HueCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
47 private static final String USER_NAME = "username";
48 private static final String SCENES = "scenes";
49 private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(USER_NAME, SCENES), false);
50 private static final StringsCompleter SCENES_COMPLETER = new StringsCompleter(List.of(SCENES), false);
52 private final ThingRegistry thingRegistry;
55 public HueCommandExtension(final @Reference ThingRegistry thingRegistry) {
56 super("hue", "Interact with the Hue binding.");
57 this.thingRegistry = thingRegistry;
61 public void execute(String[] args, Console console) {
62 if (args.length == 2) {
63 Thing thing = getThing(args[0]);
64 ThingHandler thingHandler = null;
65 HueBridgeHandler bridgeHandler = null;
66 HueGroupHandler groupHandler = null;
68 thingHandler = thing.getHandler();
69 if (thingHandler instanceof HueBridgeHandler) {
70 bridgeHandler = (HueBridgeHandler) thingHandler;
71 } else if (thingHandler instanceof HueGroupHandler) {
72 groupHandler = (HueGroupHandler) thingHandler;
76 console.println("Bad thing id '" + args[0] + "'");
78 } else if (thingHandler == null) {
79 console.println("No handler initialized for the thingUID '" + args[0] + "'");
81 } else if (bridgeHandler == null && groupHandler == null) {
82 console.println("'" + args[0] + "' is neither a Hue BridgeUID nor a Hue groupThingUID");
87 if (bridgeHandler != null) {
88 String userName = bridgeHandler.getUserName();
89 console.println("Your user name is " + (userName != null ? userName : "undefined"));
91 console.println("'" + args[0] + "' is not a Hue BridgeUID");
96 if (bridgeHandler != null) {
97 bridgeHandler.listScenesForConsole().forEach(console::println);
98 } else if (groupHandler != null) {
99 groupHandler.listScenesForConsole().forEach(console::println);
113 public List<String> getUsages() {
114 return Arrays.asList(new String[] { buildCommandUsage("<bridgeUID> " + USER_NAME, "show the user name"),
115 buildCommandUsage("<bridgeUID> " + SCENES, "list all the scenes with their id"),
116 buildCommandUsage("<groupThingUID> " + SCENES, "list all the scenes from this group with their id") });
120 public @Nullable ConsoleCommandCompleter getCompleter() {
125 public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
126 if (cursorArgumentIndex <= 0) {
127 return new StringsCompleter(thingRegistry.getAll().stream()
128 .filter(t -> HueBindingConstants.THING_TYPE_BRIDGE.equals(t.getThingTypeUID())
129 || HueBindingConstants.THING_TYPE_GROUP.equals(t.getThingTypeUID()))
130 .map(t -> t.getUID().getAsString()).collect(Collectors.toList()), true).complete(args,
131 cursorArgumentIndex, cursorPosition, candidates);
132 } else if (cursorArgumentIndex == 1) {
133 Thing thing = getThing(args[0]);
134 if (thing != null && HueBindingConstants.THING_TYPE_BRIDGE.equals(thing.getThingTypeUID())) {
135 return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
136 } else if (thing != null && HueBindingConstants.THING_TYPE_GROUP.equals(thing.getThingTypeUID())) {
137 return SCENES_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
143 private @Nullable Thing getThing(String uid) {
146 ThingUID thingUID = new ThingUID(uid);
147 thing = thingRegistry.get(thingUID);
148 } catch (IllegalArgumentException e) {