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.CommandMessage;
40 import com.digitaldan.jomnilinkII.MessageTypes.ObjectStatus;
41 import com.digitaldan.jomnilinkII.MessageTypes.properties.AreaProperties;
42 import com.digitaldan.jomnilinkII.MessageTypes.properties.AuxSensorProperties;
43 import com.digitaldan.jomnilinkII.MessageTypes.statuses.ExtendedAuxSensorStatus;
44 import com.digitaldan.jomnilinkII.OmniInvalidResponseException;
45 import com.digitaldan.jomnilinkII.OmniUnknownMessageTypeException;
48 * The {@link HumiditySensorHandler} defines some methods that are used to
49 * interface with an OmniLink Humidity Sensor. This by extension also defines
50 * the Humidity Sensor thing that openHAB will be able to pick up and interface
53 * @author Craig Hamilton - Initial contribution
54 * @author Ethan Dye - openHAB3 rewrite
57 public class HumiditySensorHandler extends AbstractOmnilinkStatusHandler<ExtendedAuxSensorStatus> {
58 private final Logger logger = LoggerFactory.getLogger(HumiditySensorHandler.class);
59 private final int thingID = getThingNumber();
60 public @Nullable String number;
62 public HumiditySensorHandler(Thing thing) {
67 public void initialize() {
69 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
70 if (bridgeHandler != null) {
71 updateHumiditySensorProperties(bridgeHandler);
73 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
74 "Received null bridge while initializing Humidity Sensor!");
78 private void updateHumiditySensorProperties(OmnilinkBridgeHandler bridgeHandler) {
79 final List<AreaProperties> areas = getAreaProperties();
81 for (AreaProperties areaProperties : areas) {
82 int areaFilter = bitFilterForArea(areaProperties);
84 ObjectPropertyRequest<AuxSensorProperties> objectPropertyRequest = ObjectPropertyRequest
85 .builder(bridgeHandler, ObjectPropertyRequests.AUX_SENSORS, thingID, 0).selectNamed()
86 .areaFilter(areaFilter).build();
88 for (AuxSensorProperties auxSensorProperties : objectPropertyRequest) {
89 Map<String, String> properties = editProperties();
90 properties.put(THING_PROPERTIES_NAME, auxSensorProperties.getName());
91 properties.put(THING_PROPERTIES_AREA, Integer.toString(areaProperties.getNumber()));
92 updateProperties(properties);
99 @SuppressWarnings("unchecked")
100 public void handleCommand(ChannelUID channelUID, Command command) {
101 logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
103 if (command instanceof RefreshType) {
104 retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
105 ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null status update!"));
109 if (!(command instanceof QuantityType)) {
110 logger.debug("Invalid command: {}, must be QuantityType", command);
114 switch (channelUID.getId()) {
115 case CHANNEL_AUX_LOW_SETPOINT:
116 sendOmnilinkCommand(CommandMessage.CMD_THERMO_SET_HEAT_POINT,
117 TemperatureFormat.FAHRENHEIT.formatToOmni(((QuantityType<Dimensionless>) command).floatValue()),
120 case CHANNEL_AUX_HIGH_SETPOINT:
121 sendOmnilinkCommand(CommandMessage.CMD_THERMO_SET_COOL_POINT,
122 TemperatureFormat.FAHRENHEIT.formatToOmni(((QuantityType<Dimensionless>) command).floatValue()),
126 logger.warn("Unknown channel for Humdity Sensor thing: {}", channelUID);
131 public void updateChannels(ExtendedAuxSensorStatus status) {
132 logger.debug("updateChannels called for Humidity Sensor status: {}", status);
133 updateState(CHANNEL_AUX_HUMIDITY,
134 new QuantityType<>(TemperatureFormat.FAHRENHEIT.omniToFormat(status.getTemperature()), Units.PERCENT));
135 updateState(CHANNEL_AUX_LOW_SETPOINT,
136 new QuantityType<>(TemperatureFormat.FAHRENHEIT.omniToFormat(status.getHeatSetpoint()), Units.PERCENT));
137 updateState(CHANNEL_AUX_HIGH_SETPOINT,
138 new QuantityType<>(TemperatureFormat.FAHRENHEIT.omniToFormat(status.getCoolSetpoint()), Units.PERCENT));
142 protected Optional<ExtendedAuxSensorStatus> retrieveStatus() {
144 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
145 if (bridgeHandler != null) {
146 ObjectStatus objStatus = bridgeHandler.requestObjectStatus(Message.OBJ_TYPE_AUX_SENSOR, thingID,
148 return Optional.of((ExtendedAuxSensorStatus) objStatus.getStatuses()[0]);
150 logger.debug("Received null bridge while updating Humidity Sensor status!");
151 return Optional.empty();
153 } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
154 logger.debug("Received exception while refreshing Humidity Sensor status: {}", e.getMessage());
155 return Optional.empty();