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