]> git.basschouten.com Git - openhab-addons.git/blob
466818817ec0358bbf96b2d3ec086a5d719d69ac
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 HandlerThermostatController} is responsible for the Alexa.ThermostatControllerInterface
39  *
40  * @author Sven Killig - Initial contribution
41  */
42 @NonNullByDefault
43 public class HandlerThermostatController extends HandlerBase {
44     // Interface
45     public static final String INTERFACE = "Alexa.ThermostatController";
46     // Channel definitions
47     private static final ChannelInfo TARGET_SETPOINT = new ChannelInfo("targetSetpoint" /* propertyName */ ,
48             "targetSetpoint" /* ChannelId */, CHANNEL_TYPE_TEMPERATURE /* Channel Type */ ,
49             ITEM_TYPE_NUMBER_TEMPERATURE /* Item Type */);
50
51     @Override
52     public String[] getSupportedInterface() {
53         return new String[] { INTERFACE };
54     }
55
56     @Override
57     protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
58         if (TARGET_SETPOINT.propertyName.equals(property)) {
59             return new ChannelInfo[] { TARGET_SETPOINT };
60         }
61         return null;
62     }
63
64     @Override
65     public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
66         QuantityType<Temperature> temperatureValue = null;
67         for (JsonObject state : stateList) {
68             if (TARGET_SETPOINT.propertyName.equals(state.get("name").getAsString())) {
69                 JsonObject value = state.get("value").getAsJsonObject();
70                 // For groups take the first
71                 if (temperatureValue == null) {
72                     float temperature = value.get("value").getAsFloat();
73                     String scale = value.get("scale").getAsString().toUpperCase();
74                     if ("CELSIUS".equals(scale)) {
75                         temperatureValue = new QuantityType<Temperature>(temperature, SIUnits.CELSIUS);
76                     } else {
77                         temperatureValue = new QuantityType<Temperature>(temperature, ImperialUnits.FAHRENHEIT);
78                     }
79                 }
80             }
81         }
82         updateState(TARGET_SETPOINT.channelId, temperatureValue == null ? UnDefType.UNDEF : temperatureValue);
83     }
84
85     @Override
86     public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
87             SmartHomeCapability[] capabilties, String channelId, Command command)
88             throws IOException, InterruptedException {
89         if (channelId.equals(TARGET_SETPOINT.channelId)) {
90             if (containsCapabilityProperty(capabilties, TARGET_SETPOINT.propertyName)) {
91                 if (command instanceof QuantityType) {
92                     connection.smartHomeCommand(entityId, "setTargetTemperature", "targetTemperature", command);
93                     return true;
94                 }
95             }
96         }
97         return false;
98     }
99
100     @Override
101     public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
102             @Nullable Locale locale) {
103         return null;
104     }
105 }