Skip to content

Commit

Permalink
[hue] Add console commands to list devices and groups set up on the h…
Browse files Browse the repository at this point in the history
…ue bridge

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo committed May 25, 2020
1 parent 51612b5 commit d7af847
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
16 changes: 16 additions & 0 deletions bundles/org.openhab.binding.hue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <bridgeUID> lights - list lights
Usage: smarthome:hue <bridgeUID> sensors - list sensors
Usage: smarthome:hue <bridgeUID> 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`.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> getUsages() {
return Arrays.asList(new String[] { buildCommandUsage("<bridgeUID> " + LIGHTS, "list lights"),
buildCommandUsage("<bridgeUID> " + SENSORS, "list sensors"),
buildCommandUsage("<bridgeUID> " + GROUPS, "list groups") });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -907,4 +908,35 @@ public Collection<ConfigStatusMessage> getConfigStatus() {

return configStatusMessages;
}

public List<String> reportLights() {
List<String> report = new ArrayList<>();
for (FullLight light : lastLightStates.values()) {
report.add(light.getId() + " : " + light.getName() + " (" + light.getType() + " "
+ light.getManufacturerName() + " " + light.getModelID() + ")");
}
return report;
}

public List<String> reportSensors() {
List<String> report = new ArrayList<>();
for (FullSensor sensor : lastSensorStates.values()) {
report.add(sensor.getId() + " : " + sensor.getName() + " (" + sensor.getType() + " "
+ sensor.getManufacturerName() + " " + sensor.getModelID() + ")");
}
return report;
}

public List<String> reportGroups() {
List<String> 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;
}
}

0 comments on commit d7af847

Please sign in to comment.