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.omnilink.internal.handler;
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.*;
17 import java.util.List;
19 import java.util.Optional;
21 import javax.measure.quantity.Dimensionless;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequest;
26 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequests;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.unit.Units;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.types.RefreshType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.digitaldan.jomnilinkII.Message;
39 import com.digitaldan.jomnilinkII.MessageTypes.ObjectStatus;
40 import com.digitaldan.jomnilinkII.MessageTypes.properties.AreaProperties;
41 import com.digitaldan.jomnilinkII.MessageTypes.properties.AuxSensorProperties;
42 import com.digitaldan.jomnilinkII.MessageTypes.statuses.ExtendedAuxSensorStatus;
43 import com.digitaldan.jomnilinkII.OmniInvalidResponseException;
44 import com.digitaldan.jomnilinkII.OmniUnknownMessageTypeException;
47 * The {@link HumiditySensorHandler} defines some methods that are used to
48 * interface with an OmniLink Humidity Sensor. This by extension also defines
49 * the Humidity Sensor thing that openHAB will be able to pick up and interface
52 * @author Craig Hamilton - Initial contribution
53 * @author Ethan Dye - openHAB3 rewrite
56 public class HumiditySensorHandler extends AbstractOmnilinkStatusHandler<ExtendedAuxSensorStatus> {
57 private final Logger logger = LoggerFactory.getLogger(HumiditySensorHandler.class);
58 private final int thingID = getThingNumber();
59 public @Nullable String number;
61 public HumiditySensorHandler(Thing thing) {
66 public void initialize() {
68 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
69 if (bridgeHandler != null) {
70 updateHumiditySensorProperties(bridgeHandler);
72 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
73 "Received null bridge while initializing Humidity Sensor!");
77 private void updateHumiditySensorProperties(OmnilinkBridgeHandler bridgeHandler) {
78 final List<AreaProperties> areas = getAreaProperties();
80 for (AreaProperties areaProperties : areas) {
81 int areaFilter = bitFilterForArea(areaProperties);
83 ObjectPropertyRequest<AuxSensorProperties> objectPropertyRequest = ObjectPropertyRequest
84 .builder(bridgeHandler, ObjectPropertyRequests.AUX_SENSORS, thingID, 0).selectNamed()
85 .areaFilter(areaFilter).build();
87 for (AuxSensorProperties auxSensorProperties : objectPropertyRequest) {
88 Map<String, String> properties = editProperties();
89 properties.put(THING_PROPERTIES_NAME, auxSensorProperties.getName());
90 properties.put(THING_PROPERTIES_AREA, Integer.toString(areaProperties.getNumber()));
91 updateProperties(properties);
98 @SuppressWarnings("unchecked")
99 public void handleCommand(ChannelUID channelUID, Command command) {
100 logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
102 if (command instanceof RefreshType) {
103 retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
104 ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null status update!"));
108 if (!(command instanceof QuantityType)) {
109 logger.debug("Invalid command: {}, must be QuantityType", command);
113 switch (channelUID.getId()) {
114 case CHANNEL_AUX_LOW_SETPOINT:
115 sendOmnilinkCommand(OmniLinkCmd.CMD_THERMO_SET_HEAT_LOW_POINT.getNumber(),
116 TemperatureFormat.FAHRENHEIT.formatToOmni(((QuantityType<Dimensionless>) command).intValue()),
119 case CHANNEL_AUX_HIGH_SETPOINT:
120 sendOmnilinkCommand(OmniLinkCmd.CMD_THERMO_SET_COOL_HIGH_POINT.getNumber(),
121 TemperatureFormat.FAHRENHEIT.formatToOmni(((QuantityType<Dimensionless>) command).intValue()),
125 logger.warn("Unknown channel for Humdity Sensor thing: {}", channelUID);
130 public void updateChannels(ExtendedAuxSensorStatus status) {
131 logger.debug("updateChannels called for Humidity Sensor status: {}", status);
132 updateState(CHANNEL_AUX_HUMIDITY,
133 new QuantityType<>(TemperatureFormat.FAHRENHEIT.omniToFormat(status.getTemperature()), Units.PERCENT));
134 updateState(CHANNEL_AUX_LOW_SETPOINT,
135 new QuantityType<>(TemperatureFormat.FAHRENHEIT.omniToFormat(status.getHeatSetpoint()), Units.PERCENT));
136 updateState(CHANNEL_AUX_HIGH_SETPOINT,
137 new QuantityType<>(TemperatureFormat.FAHRENHEIT.omniToFormat(status.getCoolSetpoint()), Units.PERCENT));
141 protected Optional<ExtendedAuxSensorStatus> retrieveStatus() {
143 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
144 if (bridgeHandler != null) {
145 ObjectStatus objStatus = bridgeHandler.requestObjectStatus(Message.OBJ_TYPE_AUX_SENSOR, thingID,
147 return Optional.of((ExtendedAuxSensorStatus) objStatus.getStatuses()[0]);
149 logger.debug("Received null bridge while updating Humidity Sensor status!");
150 return Optional.empty();
152 } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
153 logger.debug("Received exception while refreshing Humidity Sensor status: {}", e.getMessage());
154 return Optional.empty();