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