2 * Copyright (c) 2010-2021 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.*;
17 import java.io.IOException;
18 import java.util.List;
19 import java.util.Locale;
21 import javax.measure.quantity.Temperature;
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;
35 import com.google.gson.JsonObject;
38 * The {@link HandlerTemperatureSensor} is responsible for the Alexa.PowerControllerInterface
40 * @author Lukas Knoeller - Initial contribution
41 * @author Michael Geramb - Initial contribution
44 public class HandlerTemperatureSensor extends HandlerBase {
46 public static final String INTERFACE = "Alexa.TemperatureSensor";
47 // Channel definitions
48 private static final ChannelInfo TEMPERATURE = new ChannelInfo("temperature" /* propertyName */ ,
49 "temperature" /* ChannelId */, CHANNEL_TYPE_TEMPERATURE /* Channel Type */ ,
50 ITEM_TYPE_NUMBER_TEMPERATURE /* Item Type */);
53 public String[] getSupportedInterface() {
54 return new String[] { INTERFACE };
58 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
59 if (TEMPERATURE.propertyName.equals(property)) {
60 return new ChannelInfo[] { TEMPERATURE };
66 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
67 QuantityType<Temperature> temperatureValue = null;
68 for (JsonObject state : stateList) {
69 if (TEMPERATURE.propertyName.equals(state.get("name").getAsString())) {
70 JsonObject value = state.get("value").getAsJsonObject();
71 // For groups take the first
72 if (temperatureValue == null) {
73 float temperature = value.get("value").getAsFloat();
74 String scale = value.get("scale").getAsString();
75 if ("CELSIUS".equals(scale)) {
76 temperatureValue = new QuantityType<Temperature>(temperature, SIUnits.CELSIUS);
78 temperatureValue = new QuantityType<Temperature>(temperature, ImperialUnits.FAHRENHEIT);
83 updateState(TEMPERATURE.channelId, temperatureValue == null ? UnDefType.UNDEF : temperatureValue);
87 public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
88 SmartHomeCapability[] capabilties, String channelId, Command command) throws IOException {
93 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
94 @Nullable Locale locale) {