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.deconz.internal.handler;
15 import static org.openhab.binding.deconz.internal.BindingConstants.*;
19 import java.util.stream.Collectors;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.deconz.internal.CommandDescriptionProvider;
23 import org.openhab.binding.deconz.internal.Util;
24 import org.openhab.binding.deconz.internal.dto.DeconzBaseMessage;
25 import org.openhab.binding.deconz.internal.dto.GroupAction;
26 import org.openhab.binding.deconz.internal.dto.GroupMessage;
27 import org.openhab.binding.deconz.internal.dto.GroupState;
28 import org.openhab.binding.deconz.internal.types.ResourceType;
29 import org.openhab.core.library.types.DecimalType;
30 import org.openhab.core.library.types.HSBType;
31 import org.openhab.core.library.types.OnOffType;
32 import org.openhab.core.library.types.PercentType;
33 import org.openhab.core.library.types.StringType;
34 import org.openhab.core.thing.ChannelUID;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingStatus;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.types.Command;
39 import org.openhab.core.types.CommandDescriptionBuilder;
40 import org.openhab.core.types.CommandOption;
41 import org.openhab.core.types.RefreshType;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.google.gson.Gson;
48 * This group thing doesn't establish any connections, that is done by the bridge Thing.
50 * It waits for the bridge to come online, grab the websocket connection and bridge configuration
51 * and registers to the websocket connection as a listener.
53 * @author Jan N. Klug - Initial contribution
56 public class GroupThingHandler extends DeconzBaseThingHandler {
57 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPE_UIDS = Set.of(THING_TYPE_LIGHTGROUP);
58 private final Logger logger = LoggerFactory.getLogger(GroupThingHandler.class);
59 private final CommandDescriptionProvider commandDescriptionProvider;
61 private Map<String, String> scenes = Map.of();
62 private GroupState groupStateCache = new GroupState();
64 public GroupThingHandler(Thing thing, Gson gson, CommandDescriptionProvider commandDescriptionProvider) {
65 super(thing, gson, ResourceType.GROUPS);
66 this.commandDescriptionProvider = commandDescriptionProvider;
70 public void handleCommand(ChannelUID channelUID, Command command) {
71 String channelId = channelUID.getId();
73 GroupAction newGroupAction = new GroupAction();
77 if (command instanceof RefreshType) {
78 valueUpdated(channelUID.getId(), groupStateCache);
83 if (command instanceof StringType) {
84 newGroupAction.alert = command.toString();
90 if (command instanceof HSBType) {
91 HSBType hsbCommand = (HSBType) command;
92 Integer bri = Util.fromPercentType(hsbCommand.getBrightness());
93 newGroupAction.bri = bri;
95 newGroupAction.hue = (int) (hsbCommand.getHue().doubleValue() * HUE_FACTOR);
96 newGroupAction.sat = Util.fromPercentType(hsbCommand.getSaturation());
98 } else if (command instanceof PercentType) {
99 newGroupAction.bri = Util.fromPercentType((PercentType) command);
100 } else if (command instanceof DecimalType) {
101 newGroupAction.bri = ((DecimalType) command).intValue();
102 } else if (command instanceof OnOffType) {
103 newGroupAction.on = OnOffType.ON.equals(command);
108 case CHANNEL_COLOR_TEMPERATURE:
109 if (command instanceof DecimalType) {
110 int miredValue = Util.kelvinToMired(((DecimalType) command).intValue());
111 newGroupAction.ct = Util.constrainToRange(miredValue, ZCL_CT_MIN, ZCL_CT_MAX);
117 if (command instanceof StringType) {
118 String sceneId = scenes.get(command.toString());
119 if (sceneId != null) {
120 sendCommand(null, command, channelUID, "scenes/" + sceneId + "/recall", null);
122 logger.debug("Ignoring command {} for {}, scene is not found in available scenes: {}", command,
131 Integer bri = newGroupAction.bri;
133 newGroupAction.on = (bri > 0);
136 sendCommand(newGroupAction, command, channelUID, null);
140 protected void processStateResponse(DeconzBaseMessage stateResponse) {
141 if (stateResponse instanceof GroupMessage) {
142 GroupMessage groupMessage = (GroupMessage) stateResponse;
143 scenes = groupMessage.scenes.stream().collect(Collectors.toMap(scene -> scene.name, scene -> scene.id));
144 ChannelUID channelUID = new ChannelUID(thing.getUID(), CHANNEL_SCENE);
145 commandDescriptionProvider.setDescription(channelUID,
146 CommandDescriptionBuilder.create().withCommandOptions(groupMessage.scenes.stream()
147 .map(scene -> new CommandOption(scene.name, scene.name)).collect(Collectors.toList()))
151 messageReceived(config.id, stateResponse);
154 private void valueUpdated(String channelId, GroupState newState) {
157 updateState(channelId, OnOffType.from(newState.all_on));
160 updateState(channelId, OnOffType.from(newState.any_on));
167 public void messageReceived(String sensorID, DeconzBaseMessage message) {
168 if (message instanceof GroupMessage) {
169 GroupMessage groupMessage = (GroupMessage) message;
170 logger.trace("{} received {}", thing.getUID(), groupMessage);
171 GroupState groupState = groupMessage.state;
172 if (groupState != null) {
173 updateStatus(ThingStatus.ONLINE);
174 thing.getChannels().stream().map(c -> c.getUID().getId()).forEach(c -> valueUpdated(c, groupState));
175 groupStateCache = groupState;