2 * Copyright (c) 2010-2023 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.*;
16 import static org.openhab.core.library.unit.Units.*;
18 import java.io.IOException;
19 import java.util.List;
20 import java.util.Locale;
22 import javax.measure.Unit;
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.handler.SmartHomeDeviceHandler;
28 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities.SmartHomeCapability;
29 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.StateDescription;
34 import org.openhab.core.types.UnDefType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.gson.JsonObject;
41 * The {@link HandlerHumiditySensor} is responsible for the Alexa.HumiditySensorInterface
43 * @author Daniel Campbell - Initial contribution
46 public class HandlerHumiditySensor extends HandlerBase {
48 private final Logger logger = LoggerFactory.getLogger(HandlerHumiditySensor.class);
50 public static final String INTERFACE = "Alexa.HumiditySensor";
51 // Channel definitions
52 private static final ChannelInfo HUMIDITY = new ChannelInfo("relativeHumidity" /* propertyName */ ,
53 "relativeHumidity" /* ChannelId */, CHANNEL_TYPE_HUMIDITY /* Channel Type */ ,
54 ITEM_TYPE_HUMIDITY /* Item Type */);
56 public HandlerHumiditySensor(SmartHomeDeviceHandler smartHomeDeviceHandler) {
57 super(smartHomeDeviceHandler);
61 public String[] getSupportedInterface() {
62 return new String[] { INTERFACE };
66 protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
67 if (HUMIDITY.propertyName.equals(property)) {
68 return new ChannelInfo[] { HUMIDITY };
74 public void updateChannels(String interfaceName, List<JsonObject> stateList, UpdateChannelResult result) {
75 for (JsonObject state : stateList) {
76 State humidityValue = null;
77 logger.debug("Updating {} with state: {}", interfaceName, state.toString());
78 if (HUMIDITY.propertyName.equals(state.get("name").getAsString())) {
79 // For groups take the first
80 humidityValue = getQuantityTypeState(state.get("value").getAsInt(), PERCENT);
81 updateState(HUMIDITY.channelId, humidityValue == null ? UnDefType.UNDEF : humidityValue);
86 protected State getQuantityTypeState(@Nullable Number value, Unit<?> unit) {
87 return (value == null) ? UnDefType.UNDEF : new QuantityType<>(value, unit);
91 public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
92 List<SmartHomeCapability> capabilities, String channelId, Command command) throws IOException {
97 public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
98 @Nullable Locale locale) {