Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hue] Add console commands to list devices and groups set up on the h… #7664

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}