2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.amazonechocontrol.internal.smarthome;
15 import static org.openhab.binding.amazonechocontrol.internal.smarthome.Constants.CHANNEL_TYPE_TEMPERATURE;
16 import static org.openhab.binding.amazonechocontrol.internal.smarthome.Constants.ITEM_TYPE_NUMBER_TEMPERATURE;
18 import java.io.IOException;
19 import java.util.List;
20 import java.util.Locale;
22 import javax.measure.quantity.Temperature;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.amazonechocontrol.internal.Connection;
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;
36 import com.google.gson.JsonObject;
39 * The {@link HandlerTemperatureSensor} is responsible for the Alexa.PowerControllerInterface
41 * @author Lukas Knoeller - Initial contribution
42 * @author Michael Geramb - Initial contribution
45 public class HandlerTemperatureSensor extends HandlerBase {
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 */);
54 public String[] getSupportedInterface() {
55 return new String[] { INTERFACE };
59 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
60 if (TEMPERATURE.propertyName.equals(property)) {
61 return new ChannelInfo[] { TEMPERATURE };
67 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
68 QuantityType<Temperature> temperatureValue = null;
69 for (JsonObject state : stateList) {
70 if (TEMPERATURE.propertyName.equals(state.get("name").getAsString())) {
71 JsonObject value = state.get("value").getAsJsonObject();
72 // For groups take the first
73 if (temperatureValue == null) {
74 int temperature = value.get("value").getAsInt();
75 String scale = value.get("scale").getAsString();
76 if ("CELSIUS".equals(scale)) {
77 temperatureValue = new QuantityType<Temperature>(temperature, SIUnits.CELSIUS);
79 temperatureValue = new QuantityType<Temperature>(temperature, ImperialUnits.FAHRENHEIT);
84 updateState(TEMPERATURE.channelId, temperatureValue == null ? UnDefType.UNDEF : temperatureValue);
88 public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
89 SmartHomeCapability[] capabilties, String channelId, Command command) throws IOException {
94 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
95 @Nullable Locale locale) {