2 * Copyright (c) 2010-2020 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 org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.deconz.internal.Util;
21 import org.openhab.binding.deconz.internal.dto.DeconzBaseMessage;
22 import org.openhab.binding.deconz.internal.dto.GroupAction;
23 import org.openhab.binding.deconz.internal.dto.GroupMessage;
24 import org.openhab.binding.deconz.internal.dto.GroupState;
25 import org.openhab.binding.deconz.internal.types.ResourceType;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.HSBType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.RefreshType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.gson.Gson;
42 * This light thing doesn't establish any connections, that is done by the bridge Thing.
44 * It waits for the bridge to come online, grab the websocket connection and bridge configuration
45 * and registers to the websocket connection as a listener.
47 * A REST API call is made to get the initial light/rollershutter state.
49 * Every light and rollershutter is supported by this Thing, because a unified state is kept
50 * in {@link #groupStateCache}. Every field that got received by the REST API for this specific
51 * sensor is published to the framework.
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);
63 private GroupState groupStateCache = new GroupState();
65 public GroupThingHandler(Thing thing, Gson gson) {
66 super(thing, gson, ResourceType.GROUPS);
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 OnOffType) {
84 newGroupAction.alert = command == OnOffType.ON ? "alert" : "none";
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);
120 Integer bri = newGroupAction.bri;
121 if (bri != null && bri > 0) {
122 newGroupAction.on = true;
125 sendCommand(newGroupAction, command, channelUID, null);
129 protected void processStateResponse(DeconzBaseMessage stateResponse) {
130 messageReceived(config.id, stateResponse);
133 private void valueUpdated(String channelId, GroupState newState) {
136 updateState(channelId, OnOffType.from(newState.all_on));
139 updateState(channelId, OnOffType.from(newState.any_on));
146 public void messageReceived(String sensorID, DeconzBaseMessage message) {
147 if (message instanceof GroupMessage) {
148 GroupMessage groupMessage = (GroupMessage) message;
149 logger.trace("{} received {}", thing.getUID(), groupMessage);
150 GroupState groupState = groupMessage.state;
151 if (groupState != null) {
152 updateStatus(ThingStatus.ONLINE);
153 thing.getChannels().stream().map(c -> c.getUID().getId()).forEach(c -> valueUpdated(c, groupState));
154 groupStateCache = groupState;