Skip to content

Commit

Permalink
Dynamic state options for scene channel of the bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
leluna committed May 5, 2020
1 parent c06b199 commit e73762c
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledExecutorService;
import java.util.stream.Collectors;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -851,6 +852,27 @@ public void deleteSchedule(Schedule schedule) throws IOException, ApiException {
handleErrors(result);
}

/**
* Returns the list of scenes that are not recyclable.
*
* @return all scenes that can be activated
*/
public List<Scene> getScenes() throws IOException, ApiException {
requireAuthentication();

Result result = http.get(getRelativeURL("scenes"));
handleErrors(result);

Map<String, Scene> sceneMap = safeFromJson(result.getBody(), Scene.GSON_TYPE);
return sceneMap.entrySet().parallelStream()//
.map(e -> {
e.getValue().setId(e.getKey());
return e.getValue();
})//
.filter(scene -> !scene.isRecycle())//
.collect(Collectors.toList());
}

/**
* Activate scene to all lights that belong to the scene.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
import org.openhab.binding.hue.internal.handler.HueGroupHandler;
import org.openhab.binding.hue.internal.handler.HueLightHandler;
import org.openhab.binding.hue.internal.handler.HueStateDescriptionOptionProvider;
import org.openhab.binding.hue.internal.handler.sensors.ClipHandler;
import org.openhab.binding.hue.internal.handler.sensors.DimmerSwitchHandler;
import org.openhab.binding.hue.internal.handler.sensors.LightLevelHandler;
Expand All @@ -45,6 +46,7 @@
import org.openhab.binding.hue.internal.handler.sensors.TemperatureHandler;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
* {@link HueThingHandlerFactory} is a factory for {@link HueBridgeHandler}s.
Expand All @@ -69,6 +71,9 @@ public class HueThingHandlerFactory extends BaseThingHandlerFactory {

private final Map<ThingUID, @Nullable ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();

@NonNullByDefault({})
private HueStateDescriptionOptionProvider stateOptionProvider;

@Override
public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
@Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
Expand Down Expand Up @@ -136,7 +141,7 @@ private ThingUID getThingUID(ThingTypeUID thingTypeUID, String id, @Nullable Thi
@Override
protected @Nullable ThingHandler createHandler(Thing thing) {
if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
HueBridgeHandler handler = new HueBridgeHandler((Bridge) thing);
HueBridgeHandler handler = new HueBridgeHandler((Bridge) thing, stateOptionProvider);
registerLightDiscoveryService(handler);
return handler;
} else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
Expand Down Expand Up @@ -182,4 +187,13 @@ protected synchronized void removeHandler(ThingHandler thingHandler) {
}
}
}

@Reference
protected void setSceneChannelTypeProvider(HueStateDescriptionOptionProvider stateOptionProvider) {
this.stateOptionProvider = stateOptionProvider;
}

protected void unsetSceneChannelTypeProvider(HueStateDescriptionOptionProvider stateOptionProvider) {
this.stateOptionProvider = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* 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;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.smarthome.core.types.StateOption;

import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;

/**
* Basic scene information.
*/
public class Scene {
public static final Type GSON_TYPE = new TypeToken<Map<String, Scene>>() {
}.getType();

private String id;
private String name;
@SerializedName("lights")
private List<String> lightIds;
@SerializedName("group")
private String groupId;
private boolean recycle;

@NonNull
public String getId() {
return id;
}

void setId(String id) {
this.id = id;
}

/**
* Returns the human readable name of the scene. If the name is omitted upon creation, this
* defaults to the ID.
*
* @return human readable name of the scene
*/
@NonNull
public String getName() {
return name;
}

/**
* Returns the list of lights that the scene applies to. For group scenes, this list should be identical to the list
* of all lights that are in the group.
*
* @return list of lights that the scene applies to
*/
@NonNull
public List<String> getLightIds() {
return lightIds;
}

/**
* Returns the group that the scene belongs to. This field is optional for scenes that applies to a specific list of
* lights instead of a group.
*
* @return the group that the scene belongs to
*/
public String getGroupId() {
return groupId;
}

/**
* Indicates if the scene can be recycled by the bridge. A recyclable scene is not able to be activated.
*
* @return whether the scene can be recycled
*/
public boolean isRecycle() {
return recycle;
}

public StateOption toStateOption(Map<String, String> groupNames) {
if (id.contentEquals(name)) {
return new StateOption(id, id);
} else {
StringBuilder stateOptionLabel = new StringBuilder(name);
if (groupId != null && groupNames.containsKey(groupId)) {
stateOptionLabel.append(" ").append(groupNames.get(groupId));
}
stateOptionLabel.append(" ( ").append(id).append(")");
return new StateOption(id, stateOptionLabel.toString());
}
}
}
Loading

0 comments on commit e73762c

Please sign in to comment.