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.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.TemperatureFormat;
26 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequest;
27 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequests;
28 import org.openhab.binding.omnilink.internal.exceptions.BridgeOfflineException;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.unit.Units;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingStatusDetail;
35 import org.openhab.core.types.Command;
36 import org.openhab.core.types.RefreshType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.digitaldan.jomnilinkII.Message;
41 import com.digitaldan.jomnilinkII.MessageTypes.CommandMessage;
42 import com.digitaldan.jomnilinkII.MessageTypes.ObjectStatus;
43 import com.digitaldan.jomnilinkII.MessageTypes.properties.AreaProperties;
44 import com.digitaldan.jomnilinkII.MessageTypes.properties.AuxSensorProperties;
45 import com.digitaldan.jomnilinkII.MessageTypes.statuses.ExtendedAuxSensorStatus;
46 import com.digitaldan.jomnilinkII.OmniInvalidResponseException;
47 import com.digitaldan.jomnilinkII.OmniUnknownMessageTypeException;
50 * The {@link HumiditySensorHandler} defines some methods that are used to
51 * interface with an OmniLink Humidity Sensor. This by extension also defines
52 * the Humidity Sensor thing that openHAB will be able to pick up and interface
55 * @author Craig Hamilton - Initial contribution
56 * @author Ethan Dye - openHAB3 rewrite
59 public class HumiditySensorHandler extends AbstractOmnilinkStatusHandler<ExtendedAuxSensorStatus> {
60 private final Logger logger = LoggerFactory.getLogger(HumiditySensorHandler.class);
61 private final int thingID = getThingNumber();
62 public @Nullable String number;
64 public HumiditySensorHandler(Thing thing) {
69 public void initialize() {
71 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
72 if (bridgeHandler != null) {
73 updateHumiditySensorProperties(bridgeHandler);
75 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
76 "Received null bridge while initializing Humidity Sensor!");
80 private void updateHumiditySensorProperties(OmnilinkBridgeHandler bridgeHandler) {
81 final List<AreaProperties> areas = getAreaProperties();
83 for (AreaProperties areaProperties : areas) {
84 int areaFilter = bitFilterForArea(areaProperties);
86 ObjectPropertyRequest<AuxSensorProperties> objectPropertyRequest = ObjectPropertyRequest
87 .builder(bridgeHandler, ObjectPropertyRequests.AUX_SENSORS, thingID, 0).selectNamed()
88 .areaFilter(areaFilter).build();
90 for (AuxSensorProperties auxSensorProperties : objectPropertyRequest) {
91 Map<String, String> properties = editProperties();
92 properties.put(THING_PROPERTIES_NAME, auxSensorProperties.getName());
93 properties.put(THING_PROPERTIES_AREA, Integer.toString(areaProperties.getNumber()));
94 updateProperties(properties);
101 @SuppressWarnings("unchecked")
102 public void handleCommand(ChannelUID channelUID, Command command) {
103 logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
105 if (command instanceof RefreshType) {
106 retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
107 ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null status update!"));
111 if (!(command instanceof QuantityType)) {
112 logger.debug("Invalid command: {}, must be QuantityType", command);
116 switch (channelUID.getId()) {
117 case CHANNEL_AUX_LOW_SETPOINT:
118 sendOmnilinkCommand(CommandMessage.CMD_THERMO_SET_HEAT_POINT,
119 TemperatureFormat.FAHRENHEIT.formatToOmni(((QuantityType<Dimensionless>) command).floatValue()),
122 case CHANNEL_AUX_HIGH_SETPOINT:
123 sendOmnilinkCommand(CommandMessage.CMD_THERMO_SET_COOL_POINT,
124 TemperatureFormat.FAHRENHEIT.formatToOmni(((QuantityType<Dimensionless>) command).floatValue()),
128 logger.warn("Unknown channel for Humdity Sensor thing: {}", channelUID);
133 public void updateChannels(ExtendedAuxSensorStatus status) {
134 logger.debug("updateChannels called for Humidity Sensor status: {}", status);
135 updateState(CHANNEL_AUX_HUMIDITY,
136 new QuantityType<>(TemperatureFormat.FAHRENHEIT.omniToFormat(status.getTemperature()), Units.PERCENT));
137 updateState(CHANNEL_AUX_LOW_SETPOINT,
138 new QuantityType<>(TemperatureFormat.FAHRENHEIT.omniToFormat(status.getHeatSetpoint()), Units.PERCENT));
139 updateState(CHANNEL_AUX_HIGH_SETPOINT,
140 new QuantityType<>(TemperatureFormat.FAHRENHEIT.omniToFormat(status.getCoolSetpoint()), Units.PERCENT));
144 protected Optional<ExtendedAuxSensorStatus> retrieveStatus() {
146 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
147 if (bridgeHandler != null) {
148 ObjectStatus objStatus = bridgeHandler.requestObjectStatus(Message.OBJ_TYPE_AUX_SENSOR, thingID,
150 return Optional.of((ExtendedAuxSensorStatus) objStatus.getStatuses()[0]);
152 logger.debug("Received null bridge while updating Humidity Sensor status!");
153 return Optional.empty();
155 } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
156 logger.debug("Received exception while refreshing Humidity Sensor status: {}", e.getMessage());
157 return Optional.empty();