]> git.basschouten.com Git - openhab-addons.git/blob
a36e6663fa7e56b93de2c77f0fb7739c2e7fb9c9
[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 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.*;
19
20 import java.util.Arrays;
21 import java.util.List;
22 import java.util.Set;
23
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
38 import com.google.gson.Gson;
39
40 /**
41  * This sensor Thing doesn't establish any connections, that is done by the bridge Thing.
42  *
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.
45  *
46  * A REST API call is made to get the initial sensor state.
47  *
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.
51  *
52  * @author David Graeff - Initial contribution
53  */
54 @NonNullByDefault
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);
62
63     private static final List<String> CONFIG_CHANNELS = Arrays.asList(CHANNEL_BATTERY_LEVEL, CHANNEL_BATTERY_LOW,
64             CHANNEL_TEMPERATURE);
65
66     public SensorThingHandler(Thing thing, Gson gson) {
67         super(thing, gson);
68     }
69
70     @Override
71     public void handleCommand(ChannelUID channelUID, Command command) {
72         if (!(command instanceof RefreshType)) {
73             return;
74         }
75
76         sensorState.buttonevent = null;
77         valueUpdated(channelUID.getId(), sensorState, false);
78     }
79
80     @Override
81     protected void valueUpdated(ChannelUID channelUID, SensorConfig newConfig) {
82         super.valueUpdated(channelUID, newConfig);
83         Float temperature = newConfig.temperature;
84
85         switch (channelUID.getId()) {
86             case CHANNEL_TEMPERATURE:
87                 if (temperature != null) {
88                     updateState(channelUID, new QuantityType<>(temperature / 100, CELSIUS));
89                 }
90                 break;
91         }
92     }
93
94     @Override
95     protected void valueUpdated(String channelID, SensorState newState, boolean initializing) {
96         super.valueUpdated(channelID, newState, initializing);
97         switch (channelID) {
98             case CHANNEL_BATTERY_LEVEL:
99                 updateDecimalTypeChannel(channelID, newState.battery);
100                 break;
101             case CHANNEL_LIGHT:
102                 Boolean dark = newState.dark;
103                 if (dark != null) {
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
108                         if (daylight) {
109                             updateState(channelID, new StringType("Daylight"));
110                         } else {
111                             updateState(channelID, new StringType("Sunset"));
112                         }
113                     } else { // if no daylight value is known, we assume !dark means daylight
114                         updateState(channelID, new StringType("Daylight"));
115                     }
116                 }
117                 break;
118             case CHANNEL_POWER:
119                 updateQuantityTypeChannel(channelID, newState.power, WATT);
120                 break;
121             case CHANNEL_CONSUMPTION:
122                 updateQuantityTypeChannel(channelID, newState.consumption, WATT_HOUR);
123                 break;
124             case CHANNEL_VOLTAGE:
125                 updateQuantityTypeChannel(channelID, newState.voltage, VOLT);
126                 break;
127             case CHANNEL_CURRENT:
128                 updateQuantityTypeChannel(channelID, newState.current, MILLI(AMPERE));
129                 break;
130             case CHANNEL_LIGHT_LUX:
131                 updateQuantityTypeChannel(channelID, newState.lux, LUX);
132                 break;
133             case CHANNEL_COLOR:
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]));
137                 }
138                 break;
139             case CHANNEL_LIGHT_LEVEL:
140                 updateDecimalTypeChannel(channelID, newState.lightlevel);
141                 break;
142             case CHANNEL_DARK:
143                 updateSwitchChannel(channelID, newState.dark);
144                 break;
145             case CHANNEL_DAYLIGHT:
146                 updateSwitchChannel(channelID, newState.daylight);
147                 break;
148             case CHANNEL_TEMPERATURE:
149                 updateQuantityTypeChannel(channelID, newState.temperature, CELSIUS, 1.0 / 100);
150                 break;
151             case CHANNEL_HUMIDITY:
152                 updateQuantityTypeChannel(channelID, newState.humidity, PERCENT, 1.0 / 100);
153                 break;
154             case CHANNEL_PRESSURE:
155                 updateQuantityTypeChannel(channelID, newState.pressure, HECTO(PASCAL));
156                 break;
157             case CHANNEL_PRESENCE:
158                 updateSwitchChannel(channelID, newState.presence);
159                 break;
160             case CHANNEL_VALUE:
161                 updateDecimalTypeChannel(channelID, newState.status);
162                 break;
163             case CHANNEL_OPENCLOSE:
164                 Boolean open = newState.open;
165                 if (open != null) {
166                     updateState(channelID, open ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
167                 }
168                 break;
169             case CHANNEL_WATERLEAKAGE:
170                 updateSwitchChannel(channelID, newState.water);
171                 break;
172             case CHANNEL_FIRE:
173                 updateSwitchChannel(channelID, newState.fire);
174                 break;
175             case CHANNEL_ALARM:
176                 updateSwitchChannel(channelID, newState.alarm);
177                 break;
178             case CHANNEL_TAMPERED:
179                 updateSwitchChannel(channelID, newState.tampered);
180                 break;
181             case CHANNEL_VIBRATION:
182                 updateSwitchChannel(channelID, newState.vibration);
183                 break;
184             case CHANNEL_CARBONMONOXIDE:
185                 updateSwitchChannel(channelID, newState.carbonmonoxide);
186                 break;
187             case CHANNEL_BUTTON:
188                 updateDecimalTypeChannel(channelID, newState.buttonevent);
189                 break;
190             case CHANNEL_BUTTONEVENT:
191                 Integer buttonevent = newState.buttonevent;
192                 if (buttonevent != null && !initializing) {
193                     triggerChannel(channelID, String.valueOf(buttonevent));
194                 }
195                 break;
196             case CHANNEL_GESTURE:
197                 updateDecimalTypeChannel(channelID, newState.gesture);
198                 break;
199             case CHANNEL_GESTUREEVENT:
200                 Integer gesture = newState.gesture;
201                 if (gesture != null && !initializing) {
202                     triggerChannel(channelID, String.valueOf(gesture));
203                 }
204                 break;
205         }
206     }
207
208     @Override
209     protected void createTypeSpecificChannels(SensorConfig sensorConfig, SensorState sensorState) {
210         // some Xiaomi sensors
211         if (sensorConfig.temperature != null) {
212             createChannel(CHANNEL_TEMPERATURE, ChannelKind.STATE);
213         }
214
215         // ZHAPresence - e.g. IKEA TRÅDFRI motion sensor
216         if (sensorState.dark != null) {
217             createChannel(CHANNEL_DARK, ChannelKind.STATE);
218         }
219
220         // ZHAConsumption - e.g Bitron 902010/25 or Heiman SmartPlug
221         if (sensorState.power != null) {
222             createChannel(CHANNEL_POWER, ChannelKind.STATE);
223         }
224
225         // ZHAPower - e.g. Heiman SmartPlug
226         if (sensorState.voltage != null) {
227             createChannel(CHANNEL_VOLTAGE, ChannelKind.STATE);
228         }
229         if (sensorState.current != null) {
230             createChannel(CHANNEL_CURRENT, ChannelKind.STATE);
231         }
232
233         // IAS Zone sensor - e.g. Heiman HS1MS motion sensor
234         if (sensorState.tampered != null) {
235             createChannel(CHANNEL_TAMPERED, ChannelKind.STATE);
236         }
237
238         // e.g. Aqara Cube
239         if (sensorState.gesture != null) {
240             createChannel(CHANNEL_GESTURE, ChannelKind.STATE);
241             createChannel(CHANNEL_GESTUREEVENT, ChannelKind.TRIGGER);
242         }
243     }
244
245     @Override
246     protected List<String> getConfigChannels() {
247         return CONFIG_CHANNELS;
248     }
249 }