]> git.basschouten.com Git - openhab-addons.git/blob
eb863d655bebd781f764925ca29f20319f559d50
[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.amazonechocontrol.internal.smarthome;
14
15 import static org.openhab.binding.amazonechocontrol.internal.smarthome.Constants.*;
16
17 import java.io.IOException;
18 import java.util.List;
19 import java.util.Locale;
20
21 import javax.measure.quantity.Temperature;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.amazonechocontrol.internal.Connection;
26 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities.SmartHomeCapability;
27 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.unit.ImperialUnits;
30 import org.openhab.core.library.unit.SIUnits;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.StateDescription;
33 import org.openhab.core.types.UnDefType;
34
35 import com.google.gson.JsonObject;
36
37 /**
38  * The {@link HandlerTemperatureSensor} is responsible for the Alexa.PowerControllerInterface
39  *
40  * @author Lukas Knoeller - Initial contribution
41  * @author Michael Geramb - Initial contribution
42  */
43 @NonNullByDefault
44 public class HandlerTemperatureSensor extends HandlerBase {
45     // Interface
46     public static final String INTERFACE = "Alexa.TemperatureSensor";
47     // Channel definitions
48     private static final ChannelInfo TEMPERATURE = new ChannelInfo("temperature" /* propertyName */ ,
49             "temperature" /* ChannelId */, CHANNEL_TYPE_TEMPERATURE /* Channel Type */ ,
50             ITEM_TYPE_NUMBER_TEMPERATURE /* Item Type */);
51
52     @Override
53     public String[] getSupportedInterface() {
54         return new String[] { INTERFACE };
55     }
56
57     @Override
58     protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
59         if (TEMPERATURE.propertyName.equals(property)) {
60             return new ChannelInfo[] { TEMPERATURE };
61         }
62         return null;
63     }
64
65     @Override
66     public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
67         QuantityType<Temperature> temperatureValue = null;
68         for (JsonObject state : stateList) {
69             if (TEMPERATURE.propertyName.equals(state.get("name").getAsString())) {
70                 JsonObject value = state.get("value").getAsJsonObject();
71                 // For groups take the first
72                 if (temperatureValue == null) {
73                     float temperature = value.get("value").getAsFloat();
74                     String scale = value.get("scale").getAsString();
75                     if ("CELSIUS".equals(scale)) {
76                         temperatureValue = new QuantityType<Temperature>(temperature, SIUnits.CELSIUS);
77                     } else {
78                         temperatureValue = new QuantityType<Temperature>(temperature, ImperialUnits.FAHRENHEIT);
79                     }
80                 }
81             }
82         }
83         updateState(TEMPERATURE.channelId, temperatureValue == null ? UnDefType.UNDEF : temperatureValue);
84     }
85
86     @Override
87     public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
88             SmartHomeCapability[] capabilties, String channelId, Command command) throws IOException {
89         return false;
90     }
91
92     @Override
93     public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
94             @Nullable Locale locale) {
95         return null;
96     }
97 }