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.Units.*;
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;
38 import com.google.gson.Gson;
41 * This sensor Thing doesn't establish any connections, that is done by the bridge Thing.
43 * It waits for the bridge to come online, grab the websocket connection and bridge configuration
44 * and registers to the websocket connection as a listener.
46 * A REST API call is made to get the initial sensor state.
48 * Every sensor and switch is supported by this Thing, because a unified state is kept
49 * in {@link #sensorState}. Every field that got received by the REST API for this specific
50 * sensor is published to the framework.
52 * @author David Graeff - Initial contribution
55 public class SensorThingHandler extends SensorBaseThingHandler {
56 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_PRESENCE_SENSOR,
57 THING_TYPE_DAYLIGHT_SENSOR, THING_TYPE_POWER_SENSOR, THING_TYPE_CONSUMPTION_SENSOR, THING_TYPE_LIGHT_SENSOR,
58 THING_TYPE_TEMPERATURE_SENSOR, THING_TYPE_HUMIDITY_SENSOR, THING_TYPE_PRESSURE_SENSOR, THING_TYPE_SWITCH,
59 THING_TYPE_OPENCLOSE_SENSOR, THING_TYPE_WATERLEAKAGE_SENSOR, THING_TYPE_FIRE_SENSOR,
60 THING_TYPE_ALARM_SENSOR, THING_TYPE_VIBRATION_SENSOR, THING_TYPE_BATTERY_SENSOR,
61 THING_TYPE_CARBONMONOXIDE_SENSOR, THING_TYPE_COLOR_CONTROL);
63 private static final List<String> CONFIG_CHANNELS = Arrays.asList(CHANNEL_BATTERY_LEVEL, CHANNEL_BATTERY_LOW,
66 public SensorThingHandler(Thing thing, Gson gson) {
71 public void handleCommand(ChannelUID channelUID, Command command) {
72 if (!(command instanceof RefreshType)) {
76 sensorState.buttonevent = null;
77 valueUpdated(channelUID.getId(), sensorState, false);
81 protected void valueUpdated(ChannelUID channelUID, SensorConfig newConfig) {
82 super.valueUpdated(channelUID, newConfig);
83 Float temperature = newConfig.temperature;
85 switch (channelUID.getId()) {
86 case CHANNEL_TEMPERATURE:
87 if (temperature != null) {
88 updateState(channelUID, new QuantityType<>(temperature / 100, CELSIUS));
95 protected void valueUpdated(String channelID, SensorState newState, boolean initializing) {
96 super.valueUpdated(channelID, newState, initializing);
98 case CHANNEL_BATTERY_LEVEL:
99 updateDecimalTypeChannel(channelID, newState.battery);
102 Boolean dark = newState.dark;
104 Boolean daylight = newState.daylight;
105 if (dark) { // if it's dark, it's dark ;)
106 updateState(channelID, new StringType("Dark"));
107 } else if (daylight != null) { // if its not dark, it might be between darkness and daylight
109 updateState(channelID, new StringType("Daylight"));
111 updateState(channelID, new StringType("Sunset"));
113 } else { // if no daylight value is known, we assume !dark means daylight
114 updateState(channelID, new StringType("Daylight"));
119 updateQuantityTypeChannel(channelID, newState.power, WATT);
121 case CHANNEL_CONSUMPTION:
122 updateQuantityTypeChannel(channelID, newState.consumption, WATT_HOUR);
124 case CHANNEL_VOLTAGE:
125 updateQuantityTypeChannel(channelID, newState.voltage, VOLT);
127 case CHANNEL_CURRENT:
128 updateQuantityTypeChannel(channelID, newState.current, MILLI(AMPERE));
130 case CHANNEL_LIGHT_LUX:
131 updateQuantityTypeChannel(channelID, newState.lux, LUX);
134 final double @Nullable [] xy = newState.xy;
135 if (xy != null && xy.length == 2) {
136 updateState(channelID, HSBType.fromXY((float) xy[0], (float) xy[1]));
139 case CHANNEL_LIGHT_LEVEL:
140 updateDecimalTypeChannel(channelID, newState.lightlevel);
143 updateSwitchChannel(channelID, newState.dark);
145 case CHANNEL_DAYLIGHT:
146 updateSwitchChannel(channelID, newState.daylight);
148 case CHANNEL_TEMPERATURE:
149 updateQuantityTypeChannel(channelID, newState.temperature, CELSIUS, 1.0 / 100);
151 case CHANNEL_HUMIDITY:
152 updateQuantityTypeChannel(channelID, newState.humidity, PERCENT, 1.0 / 100);
154 case CHANNEL_PRESSURE:
155 updateQuantityTypeChannel(channelID, newState.pressure, HECTO(PASCAL));
157 case CHANNEL_PRESENCE:
158 updateSwitchChannel(channelID, newState.presence);
161 updateDecimalTypeChannel(channelID, newState.status);
163 case CHANNEL_OPENCLOSE:
164 Boolean open = newState.open;
166 updateState(channelID, open ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
169 case CHANNEL_WATERLEAKAGE:
170 updateSwitchChannel(channelID, newState.water);
173 updateSwitchChannel(channelID, newState.fire);
176 updateSwitchChannel(channelID, newState.alarm);
178 case CHANNEL_TAMPERED:
179 updateSwitchChannel(channelID, newState.tampered);
181 case CHANNEL_VIBRATION:
182 updateSwitchChannel(channelID, newState.vibration);
184 case CHANNEL_CARBONMONOXIDE:
185 updateSwitchChannel(channelID, newState.carbonmonoxide);
188 updateDecimalTypeChannel(channelID, newState.buttonevent);
190 case CHANNEL_BUTTONEVENT:
191 Integer buttonevent = newState.buttonevent;
192 if (buttonevent != null && !initializing) {
193 triggerChannel(channelID, String.valueOf(buttonevent));
196 case CHANNEL_GESTURE:
197 updateDecimalTypeChannel(channelID, newState.gesture);
199 case CHANNEL_GESTUREEVENT:
200 Integer gesture = newState.gesture;
201 if (gesture != null && !initializing) {
202 triggerChannel(channelID, String.valueOf(gesture));
209 protected void createTypeSpecificChannels(SensorConfig sensorConfig, SensorState sensorState) {
210 // some Xiaomi sensors
211 if (sensorConfig.temperature != null) {
212 createChannel(CHANNEL_TEMPERATURE, ChannelKind.STATE);
215 // ZHAPresence - e.g. IKEA TRÅDFRI motion sensor
216 if (sensorState.dark != null) {
217 createChannel(CHANNEL_DARK, ChannelKind.STATE);
220 // ZHAConsumption - e.g Bitron 902010/25 or Heiman SmartPlug
221 if (sensorState.power != null) {
222 createChannel(CHANNEL_POWER, ChannelKind.STATE);
225 // ZHAPower - e.g. Heiman SmartPlug
226 if (sensorState.voltage != null) {
227 createChannel(CHANNEL_VOLTAGE, ChannelKind.STATE);
229 if (sensorState.current != null) {
230 createChannel(CHANNEL_CURRENT, ChannelKind.STATE);
233 // IAS Zone sensor - e.g. Heiman HS1MS motion sensor
234 if (sensorState.tampered != null) {
235 createChannel(CHANNEL_TAMPERED, ChannelKind.STATE);
239 if (sensorState.gesture != null) {
240 createChannel(CHANNEL_GESTURE, ChannelKind.STATE);
241 createChannel(CHANNEL_GESTUREEVENT, ChannelKind.TRIGGER);
246 protected List<String> getConfigChannels() {
247 return CONFIG_CHANNELS;