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.*;
16 import static org.openhab.core.library.unit.MetricPrefix.*;
17 import static org.openhab.core.library.unit.SIUnits.*;
18 import static org.openhab.core.library.unit.SmartHomeUnits.*;
20 import java.util.Arrays;
21 import java.util.List;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.deconz.internal.dto.*;
27 import org.openhab.core.library.types.HSBType;
28 import org.openhab.core.library.types.OpenClosedType;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.type.ChannelKind;
35 import org.openhab.core.types.Command;
36 import org.openhab.core.types.RefreshType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.google.gson.Gson;
43 * This sensor Thing doesn't establish any connections, that is done by the bridge Thing.
45 * It waits for the bridge to come online, grab the websocket connection and bridge configuration
46 * and registers to the websocket connection as a listener.
48 * A REST API call is made to get the initial sensor state.
50 * Every sensor and switch is supported by this Thing, because a unified state is kept
51 * in {@link #sensorState}. Every field that got received by the REST API for this specific
52 * sensor is published to the framework.
54 * @author David Graeff - Initial contribution
57 public class SensorThingHandler extends SensorBaseThingHandler {
58 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_PRESENCE_SENSOR,
59 THING_TYPE_DAYLIGHT_SENSOR, THING_TYPE_POWER_SENSOR, THING_TYPE_CONSUMPTION_SENSOR, THING_TYPE_LIGHT_SENSOR,
60 THING_TYPE_TEMPERATURE_SENSOR, THING_TYPE_HUMIDITY_SENSOR, THING_TYPE_PRESSURE_SENSOR, THING_TYPE_SWITCH,
61 THING_TYPE_OPENCLOSE_SENSOR, THING_TYPE_WATERLEAKAGE_SENSOR, THING_TYPE_FIRE_SENSOR,
62 THING_TYPE_ALARM_SENSOR, THING_TYPE_VIBRATION_SENSOR, THING_TYPE_BATTERY_SENSOR,
63 THING_TYPE_CARBONMONOXIDE_SENSOR, THING_TYPE_COLOR_CONTROL);
65 private static final List<String> CONFIG_CHANNELS = Arrays.asList(CHANNEL_BATTERY_LEVEL, CHANNEL_BATTERY_LOW,
68 private final Logger logger = LoggerFactory.getLogger(SensorThingHandler.class);
70 public SensorThingHandler(Thing thing, Gson gson) {
75 public void handleCommand(ChannelUID channelUID, Command command) {
76 if (!(command instanceof RefreshType)) {
80 sensorState.buttonevent = null;
81 valueUpdated(channelUID.getId(), sensorState, false);
85 protected void valueUpdated(ChannelUID channelUID, SensorConfig newConfig) {
86 super.valueUpdated(channelUID, newConfig);
87 Float temperature = newConfig.temperature;
89 switch (channelUID.getId()) {
90 case CHANNEL_TEMPERATURE:
91 if (temperature != null) {
92 updateState(channelUID, new QuantityType<>(temperature / 100, CELSIUS));
99 protected void valueUpdated(String channelID, SensorState newState, boolean initializing) {
100 super.valueUpdated(channelID, newState, initializing);
102 case CHANNEL_BATTERY_LEVEL:
103 updateDecimalTypeChannel(channelID, newState.battery);
106 Boolean dark = newState.dark;
108 Boolean daylight = newState.daylight;
109 if (dark) { // if it's dark, it's dark ;)
110 updateState(channelID, new StringType("Dark"));
111 } else if (daylight != null) { // if its not dark, it might be between darkness and daylight
113 updateState(channelID, new StringType("Daylight"));
115 updateState(channelID, new StringType("Sunset"));
117 } else { // if no daylight value is known, we assume !dark means daylight
118 updateState(channelID, new StringType("Daylight"));
123 updateQuantityTypeChannel(channelID, newState.power, WATT);
125 case CHANNEL_CONSUMPTION:
126 updateQuantityTypeChannel(channelID, newState.consumption, WATT_HOUR);
128 case CHANNEL_VOLTAGE:
129 updateQuantityTypeChannel(channelID, newState.voltage, VOLT);
131 case CHANNEL_CURRENT:
132 updateQuantityTypeChannel(channelID, newState.current, MILLI(AMPERE));
134 case CHANNEL_LIGHT_LUX:
135 updateQuantityTypeChannel(channelID, newState.lux, LUX);
138 final double @Nullable [] xy = newState.xy;
139 if (xy != null && xy.length == 2) {
140 updateState(channelID, HSBType.fromXY((float) xy[0], (float) xy[1]));
143 case CHANNEL_LIGHT_LEVEL:
144 updateDecimalTypeChannel(channelID, newState.lightlevel);
147 updateSwitchChannel(channelID, newState.dark);
149 case CHANNEL_DAYLIGHT:
150 updateSwitchChannel(channelID, newState.daylight);
152 case CHANNEL_TEMPERATURE:
153 updateQuantityTypeChannel(channelID, newState.temperature, CELSIUS, 1.0 / 100);
155 case CHANNEL_HUMIDITY:
156 updateQuantityTypeChannel(channelID, newState.humidity, PERCENT, 1.0 / 100);
158 case CHANNEL_PRESSURE:
159 updateQuantityTypeChannel(channelID, newState.pressure, HECTO(PASCAL));
161 case CHANNEL_PRESENCE:
162 updateSwitchChannel(channelID, newState.presence);
165 updateDecimalTypeChannel(channelID, newState.status);
167 case CHANNEL_OPENCLOSE:
168 Boolean open = newState.open;
170 updateState(channelID, open ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
173 case CHANNEL_WATERLEAKAGE:
174 updateSwitchChannel(channelID, newState.water);
177 updateSwitchChannel(channelID, newState.fire);
180 updateSwitchChannel(channelID, newState.alarm);
182 case CHANNEL_TAMPERED:
183 updateSwitchChannel(channelID, newState.tampered);
185 case CHANNEL_VIBRATION:
186 updateSwitchChannel(channelID, newState.vibration);
188 case CHANNEL_CARBONMONOXIDE:
189 updateSwitchChannel(channelID, newState.carbonmonoxide);
192 updateDecimalTypeChannel(channelID, newState.buttonevent);
194 case CHANNEL_BUTTONEVENT:
195 Integer buttonevent = newState.buttonevent;
196 if (buttonevent != null && !initializing) {
197 triggerChannel(channelID, String.valueOf(buttonevent));
200 case CHANNEL_GESTURE:
201 updateDecimalTypeChannel(channelID, newState.gesture);
203 case CHANNEL_GESTUREEVENT:
204 Integer gesture = newState.gesture;
205 if (gesture != null && !initializing) {
206 triggerChannel(channelID, String.valueOf(gesture));
213 protected void createTypeSpecificChannels(SensorConfig sensorConfig, SensorState sensorState) {
214 // some Xiaomi sensors
215 if (sensorConfig.temperature != null) {
216 createChannel(CHANNEL_TEMPERATURE, ChannelKind.STATE);
219 // ZHAPresence - e.g. IKEA TRÅDFRI motion sensor
220 if (sensorState.dark != null) {
221 createChannel(CHANNEL_DARK, ChannelKind.STATE);
224 // ZHAConsumption - e.g Bitron 902010/25 or Heiman SmartPlug
225 if (sensorState.power != null) {
226 createChannel(CHANNEL_POWER, ChannelKind.STATE);
229 // ZHAPower - e.g. Heiman SmartPlug
230 if (sensorState.voltage != null) {
231 createChannel(CHANNEL_VOLTAGE, ChannelKind.STATE);
233 if (sensorState.current != null) {
234 createChannel(CHANNEL_CURRENT, ChannelKind.STATE);
237 // IAS Zone sensor - e.g. Heiman HS1MS motion sensor
238 if (sensorState.tampered != null) {
239 createChannel(CHANNEL_TAMPERED, ChannelKind.STATE);
243 if (sensorState.gesture != null) {
244 createChannel(CHANNEL_GESTURE, ChannelKind.STATE);
245 createChannel(CHANNEL_GESTUREEVENT, ChannelKind.TRIGGER);
250 protected List<String> getConfigChannels() {
251 return CONFIG_CHANNELS;