]> git.basschouten.com Git - openhab-addons.git/blob
52930db7c0ebbc7afadb76f8c88fa804576fcba8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.deconz.internal.handler;
14
15 import static org.openhab.binding.deconz.internal.BindingConstants.*;
16
17 import java.util.Set;
18
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;
38
39 import com.google.gson.Gson;
40
41 /**
42  * This light thing doesn't establish any connections, that is done by the bridge Thing.
43  *
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.
46  *
47  * A REST API call is made to get the initial light/rollershutter state.
48  *
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.
52  *
53  * @author Jan N. Klug - Initial contribution
54  */
55 @NonNullByDefault
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
60     /**
61      * The group state.
62      */
63     private GroupState groupStateCache = new GroupState();
64
65     public GroupThingHandler(Thing thing, Gson gson) {
66         super(thing, gson, ResourceType.GROUPS);
67     }
68
69     @Override
70     public void handleCommand(ChannelUID channelUID, Command command) {
71         String channelId = channelUID.getId();
72
73         GroupAction newGroupAction = new GroupAction();
74         switch (channelId) {
75             case CHANNEL_ALL_ON:
76             case CHANNEL_ANY_ON:
77                 if (command instanceof RefreshType) {
78                     valueUpdated(channelUID.getId(), groupStateCache);
79                     return;
80                 }
81                 break;
82             case CHANNEL_ALERT:
83                 if (command instanceof OnOffType) {
84                     newGroupAction.alert = command == OnOffType.ON ? "alert" : "none";
85                 } else {
86                     return;
87                 }
88                 break;
89             case CHANNEL_COLOR:
90                 if (command instanceof HSBType) {
91                     HSBType hsbCommand = (HSBType) command;
92                     Integer bri = Util.fromPercentType(hsbCommand.getBrightness());
93                     newGroupAction.bri = bri;
94                     if (bri > 0) {
95                         newGroupAction.hue = (int) (hsbCommand.getHue().doubleValue() * HUE_FACTOR);
96                         newGroupAction.sat = Util.fromPercentType(hsbCommand.getSaturation());
97                     }
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);
104                 } else {
105                     return;
106                 }
107                 break;
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);
112                 } else {
113                     return;
114                 }
115                 break;
116             default:
117                 return;
118         }
119
120         Integer bri = newGroupAction.bri;
121         if (bri != null && bri > 0) {
122             newGroupAction.on = true;
123         }
124
125         sendCommand(newGroupAction, command, channelUID, null);
126     }
127
128     @Override
129     protected void processStateResponse(DeconzBaseMessage stateResponse) {
130         messageReceived(config.id, stateResponse);
131     }
132
133     private void valueUpdated(String channelId, GroupState newState) {
134         switch (channelId) {
135             case CHANNEL_ALL_ON:
136                 updateState(channelId, OnOffType.from(newState.all_on));
137                 break;
138             case CHANNEL_ANY_ON:
139                 updateState(channelId, OnOffType.from(newState.any_on));
140                 break;
141             default:
142         }
143     }
144
145     @Override
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;
155             }
156         }
157     }
158 }