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