]> git.basschouten.com Git - openhab-addons.git/blob
2a256ae3034a16598dc683245b70159058045677
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 import static org.openhab.core.library.unit.Units.*;
17
18 import java.io.IOException;
19 import java.util.List;
20 import java.util.Locale;
21
22 import javax.measure.Unit;
23
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;
37
38 import com.google.gson.JsonObject;
39
40 /**
41  * The {@link HandlerHumiditySensor} is responsible for the Alexa.HumiditySensorInterface
42  *
43  * @author Daniel Campbell - Initial contribution
44  */
45 @NonNullByDefault
46 public class HandlerHumiditySensor extends HandlerBase {
47     // Logger
48     private final Logger logger = LoggerFactory.getLogger(HandlerHumiditySensor.class);
49     // Interface
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 */);
55
56     public HandlerHumiditySensor(SmartHomeDeviceHandler smartHomeDeviceHandler) {
57         super(smartHomeDeviceHandler);
58     }
59
60     @Override
61     public String[] getSupportedInterface() {
62         return new String[] { INTERFACE };
63     }
64
65     @Override
66     protected ChannelInfo @Nullable [] findChannelInfos(SmartHomeCapability capability, String property) {
67         if (HUMIDITY.propertyName.equals(property)) {
68             return new ChannelInfo[] { HUMIDITY };
69         }
70         return null;
71     }
72
73     @Override
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);
82             }
83         }
84     }
85
86     protected State getQuantityTypeState(@Nullable Number value, Unit<?> unit) {
87         return (value == null) ? UnDefType.UNDEF : new QuantityType<>(value, unit);
88     }
89
90     @Override
91     public boolean handleCommand(Connection connection, SmartHomeDevice shd, String entityId,
92             List<SmartHomeCapability> capabilities, String channelId, Command command) throws IOException {
93         return false;
94     }
95
96     @Override
97     public @Nullable StateDescription findStateDescription(String channelId, StateDescription originalStateDescription,
98             @Nullable Locale locale) {
99         return null;
100     }
101 }