2 * Copyright (c) 2010-2021 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;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
20 import org.openhab.binding.hue.internal.handler.HueGroupHandler;
21 import org.openhab.core.io.console.Console;
22 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
23 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingRegistry;
26 import org.openhab.core.thing.ThingUID;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.Reference;
33 * The {@link HueCommandExtension} is responsible for handling console commands
35 * @author Laurent Garnier - Initial contribution
39 @Component(service = ConsoleCommandExtension.class)
40 public class HueCommandExtension extends AbstractConsoleCommandExtension {
42 private static final String USER_NAME = "username";
43 private static final String SCENES = "scenes";
45 private final ThingRegistry thingRegistry;
48 public HueCommandExtension(final @Reference ThingRegistry thingRegistry) {
49 super("hue", "Interact with the hue binding.");
50 this.thingRegistry = thingRegistry;
54 public void execute(String[] args, Console console) {
55 if (args.length == 2) {
58 ThingUID thingUID = new ThingUID(args[0]);
59 thing = thingRegistry.get(thingUID);
60 } catch (IllegalArgumentException e) {
63 ThingHandler thingHandler = null;
64 HueBridgeHandler bridgeHandler = null;
65 HueGroupHandler groupHandler = null;
67 thingHandler = thing.getHandler();
68 if (thingHandler instanceof HueBridgeHandler) {
69 bridgeHandler = (HueBridgeHandler) thingHandler;
70 } else if (thingHandler instanceof HueGroupHandler) {
71 groupHandler = (HueGroupHandler) thingHandler;
75 console.println("Bad thing id '" + args[0] + "'");
77 } else if (thingHandler == null) {
78 console.println("No handler initialized for the thingUID '" + args[0] + "'");
80 } else if (bridgeHandler == null && groupHandler == null) {
81 console.println("'" + args[0] + "' is neither a Hue bridgeUID nor a Hue groupThingUID");
86 if (bridgeHandler != null) {
87 String userName = bridgeHandler.getUserName();
88 console.println("Your user name is " + (userName != null ? userName : "undefined"));
90 console.println("'" + args[0] + "' is not a Hue bridgeUID");
95 if (bridgeHandler != null) {
96 bridgeHandler.listScenesForConsole().forEach(console::println);
97 } else if (groupHandler != null) {
98 groupHandler.listScenesForConsole().forEach(console::println);
112 public List<String> getUsages() {
113 return Arrays.asList(new String[] { buildCommandUsage("<bridgeUID> " + USER_NAME, "show the user name"),
114 buildCommandUsage("<bridgeUID> " + SCENES, "list all the scenes with their id"),
115 buildCommandUsage("<groupThingUID> " + SCENES, "list all the scenes from this group with their id") });