From d7af8472f030b406864a74ef188494bc1b425419 Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Mon, 25 May 2020 20:40:50 +0200 Subject: [PATCH] [hue] Add console commands to list devices and groups set up on the hue bridge Signed-off-by: Laurent Garnier --- bundles/org.openhab.binding.hue/README.md | 16 ++++ .../internal/console/HueCommandExtension.java | 96 +++++++++++++++++++ .../internal/handler/HueBridgeHandler.java | 32 +++++++ 3 files changed, 144 insertions(+) create mode 100644 bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/console/HueCommandExtension.java diff --git a/bundles/org.openhab.binding.hue/README.md b/bundles/org.openhab.binding.hue/README.md index 93aeb290ff9a8..8cace9aebfd8e 100644 --- a/bundles/org.openhab.binding.hue/README.md +++ b/bundles/org.openhab.binding.hue/README.md @@ -247,6 +247,22 @@ hueActions.fadingLightCommand("color", new PercentType(100), new DecimalType(100 | command | All commands supported by the channel can be used | | fadeTime | Fade time in Milliseconds to a new light value (min="0", step="100") | +## Console Commands + +The binding provides few specific commands you can use in the console. +Enter the command `hue` to get the usage. + +``` +openhab> hue +Usage: smarthome:hue lights - list lights +Usage: smarthome:hue sensors - list sensors +Usage: smarthome:hue groups - list groups +``` + +The command `lights` reports in the console the list of all lights registered in the Hue bridge. +The command `sensors` reports in the console the list of all snesors registered in the Hue bridge. +The command `groups` reports in the console the list of all groups set up on the Hue bridge. + ## Full Example In this example **bulb1** is a standard Philips Hue bulb (LCT001) which supports `color` and `color_temperature`. diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/console/HueCommandExtension.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/console/HueCommandExtension.java new file mode 100644 index 0000000000000..87b2fcd36338d --- /dev/null +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/console/HueCommandExtension.java @@ -0,0 +1,96 @@ +/** + * Copyright (c) 2010-2020 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.hue.internal.console; + +import java.util.Arrays; +import java.util.List; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.smarthome.core.thing.Thing; +import org.eclipse.smarthome.core.thing.ThingRegistry; +import org.eclipse.smarthome.core.thing.ThingUID; +import org.eclipse.smarthome.io.console.Console; +import org.eclipse.smarthome.io.console.extensions.AbstractConsoleCommandExtension; +import org.eclipse.smarthome.io.console.extensions.ConsoleCommandExtension; +import org.openhab.binding.hue.internal.handler.HueBridgeHandler; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; + +/** + * The {@link HueCommandExtension} is responsible for handling console commands + * + * @author Laurent Garnier - Initial contribution + */ + +@NonNullByDefault +@Component(service = ConsoleCommandExtension.class) +public class HueCommandExtension extends AbstractConsoleCommandExtension { + + private static final String LIGHTS = "lights"; + private static final String SENSORS = "sensors"; + private static final String GROUPS = "groups"; + + private final ThingRegistry thingRegistry; + + @Activate + public HueCommandExtension(final @Reference ThingRegistry thingRegistry) { + super("hue", "Interact with the hue binding."); + this.thingRegistry = thingRegistry; + } + + @Override + public void execute(String[] args, Console console) { + if (args.length == 2) { + HueBridgeHandler bridgeHandler = null; + try { + ThingUID thingUID = new ThingUID(args[0]); + Thing thing = thingRegistry.get(thingUID); + if ((thing != null) && (thing.getHandler() != null) + && (thing.getHandler() instanceof HueBridgeHandler)) { + bridgeHandler = (HueBridgeHandler) thing.getHandler(); + } + } catch (IllegalArgumentException e) { + bridgeHandler = null; + } + if (bridgeHandler == null) { + console.println("Bad bridge id '" + args[0] + "'"); + printUsage(console); + } else { + switch (args[1]) { + case LIGHTS: + bridgeHandler.reportLights().forEach(console::println); + break; + case SENSORS: + bridgeHandler.reportSensors().forEach(console::println); + break; + case GROUPS: + bridgeHandler.reportGroups().forEach(console::println); + break; + default: + printUsage(console); + break; + } + } + } else { + printUsage(console); + } + } + + @Override + public List getUsages() { + return Arrays.asList(new String[] { buildCommandUsage(" " + LIGHTS, "list lights"), + buildCommandUsage(" " + SENSORS, "list sensors"), + buildCommandUsage(" " + GROUPS, "list groups") }); + } +} diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java index 156eb98a8996f..46cf8bf9844e4 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java @@ -16,6 +16,7 @@ import static org.openhab.binding.hue.internal.HueBindingConstants.*; import java.io.IOException; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -907,4 +908,35 @@ public Collection getConfigStatus() { return configStatusMessages; } + + public List reportLights() { + List report = new ArrayList<>(); + for (FullLight light : lastLightStates.values()) { + report.add(light.getId() + " : " + light.getName() + " (" + light.getType() + " " + + light.getManufacturerName() + " " + light.getModelID() + ")"); + } + return report; + } + + public List reportSensors() { + List report = new ArrayList<>(); + for (FullSensor sensor : lastSensorStates.values()) { + report.add(sensor.getId() + " : " + sensor.getName() + " (" + sensor.getType() + " " + + sensor.getManufacturerName() + " " + sensor.getModelID() + ")"); + } + return report; + } + + public List reportGroups() { + List report = new ArrayList<>(); + for (FullGroup group : lastGroupStates.values()) { + String value = group.getId() + " : " + group.getName() + " (" + group.getType() + " including lights"; + for (String lightId : group.getLights()) { + value += " " + lightId; + } + value += ")"; + report.add(value); + } + return report; + } }