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.Temperature;
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.ImperialUnits;
29 import org.openhab.core.library.unit.SIUnits;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingStatusDetail;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.RefreshType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.digitaldan.jomnilinkII.Message;
40 import com.digitaldan.jomnilinkII.MessageTypes.CommandMessage;
41 import com.digitaldan.jomnilinkII.MessageTypes.ObjectStatus;
42 import com.digitaldan.jomnilinkII.MessageTypes.properties.AreaProperties;
43 import com.digitaldan.jomnilinkII.MessageTypes.properties.AuxSensorProperties;
44 import com.digitaldan.jomnilinkII.MessageTypes.statuses.ExtendedAuxSensorStatus;
45 import com.digitaldan.jomnilinkII.OmniInvalidResponseException;
46 import com.digitaldan.jomnilinkII.OmniUnknownMessageTypeException;
49 * The {@link TempSensorHandler} defines some methods that are used to interface
50 * with an OmniLink Temperature Sensor. This by extension also defines the
51 * Temperature Sensor thing that openHAB will be able to pick up and interface
54 * @author Craig Hamilton - Initial contribution
55 * @author Ethan Dye - openHAB3 rewrite
58 public class TempSensorHandler extends AbstractOmnilinkStatusHandler<ExtendedAuxSensorStatus> {
59 private final Logger logger = LoggerFactory.getLogger(TempSensorHandler.class);
60 private final int thingID = getThingNumber();
61 public @Nullable String number;
63 public TempSensorHandler(Thing thing) {
68 public void initialize() {
70 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
71 if (bridgeHandler != null) {
72 updateTempSensorProperties(bridgeHandler);
74 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
75 "Received null bridge while initializing Temperature Sensor!");
79 private void updateTempSensorProperties(OmnilinkBridgeHandler bridgeHandler) {
80 final List<AreaProperties> areas = getAreaProperties();
82 for (AreaProperties areaProperties : areas) {
83 int areaFilter = bitFilterForArea(areaProperties);
85 ObjectPropertyRequest<AuxSensorProperties> objectPropertyRequest = ObjectPropertyRequest
86 .builder(bridgeHandler, ObjectPropertyRequests.AUX_SENSORS, thingID, 0).selectNamed()
87 .areaFilter(areaFilter).build();
89 for (AuxSensorProperties auxSensorProperties : objectPropertyRequest) {
90 Map<String, String> properties = editProperties();
91 properties.put(THING_PROPERTIES_NAME, auxSensorProperties.getName());
92 properties.put(THING_PROPERTIES_AREA, Integer.toString(areaProperties.getNumber()));
93 updateProperties(properties);
100 @SuppressWarnings("unchecked")
101 public void handleCommand(ChannelUID channelUID, Command command) {
102 logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
103 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
104 Optional<TemperatureFormat> temperatureFormat = Optional.empty();
106 if (command instanceof RefreshType) {
107 retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
108 ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null status update!"));
112 if (!(command instanceof QuantityType)) {
113 logger.debug("Invalid command: {}, must be QuantityType", command);
116 if (bridgeHandler != null) {
117 temperatureFormat = bridgeHandler.getTemperatureFormat();
118 if (!temperatureFormat.isPresent()) {
119 logger.warn("Receieved null temperature format!");
123 logger.warn("Could not connect to Bridge, failed to get temperature format!");
127 switch (channelUID.getId()) {
128 case CHANNEL_AUX_LOW_SETPOINT:
129 sendOmnilinkCommand(CommandMessage.CMD_THERMO_SET_HEAT_POINT,
130 temperatureFormat.get().formatToOmni(((QuantityType<Temperature>) command).floatValue()),
133 case CHANNEL_AUX_HIGH_SETPOINT:
134 sendOmnilinkCommand(CommandMessage.CMD_THERMO_SET_COOL_POINT,
135 temperatureFormat.get().formatToOmni(((QuantityType<Temperature>) command).floatValue()),
139 logger.warn("Unknown channel for Temperature Sensor thing: {}", channelUID);
144 public void updateChannels(ExtendedAuxSensorStatus status) {
145 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
146 if (bridgeHandler != null) {
147 Optional<TemperatureFormat> temperatureFormat = bridgeHandler.getTemperatureFormat();
148 if (temperatureFormat.isPresent()) {
149 updateState(CHANNEL_AUX_TEMP, new QuantityType<>(
150 temperatureFormat.get().omniToFormat(status.getTemperature()),
151 temperatureFormat.get().getFormatNumber() == 1 ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS));
152 updateState(CHANNEL_AUX_LOW_SETPOINT, new QuantityType<>(
153 temperatureFormat.get().omniToFormat(status.getCoolSetpoint()),
154 temperatureFormat.get().getFormatNumber() == 1 ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS));
155 updateState(CHANNEL_AUX_HIGH_SETPOINT, new QuantityType<>(
156 temperatureFormat.get().omniToFormat(status.getHeatSetpoint()),
157 temperatureFormat.get().getFormatNumber() == 1 ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS));
159 logger.warn("Receieved null temperature format, could not update Temperature Sensor channels!");
162 logger.debug("Received null bridge while updating Temperature Sensor channels!");
167 protected Optional<ExtendedAuxSensorStatus> retrieveStatus() {
169 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
170 if (bridgeHandler != null) {
171 ObjectStatus objStatus = bridgeHandler.requestObjectStatus(Message.OBJ_TYPE_AUX_SENSOR, thingID,
173 return Optional.of((ExtendedAuxSensorStatus) objStatus.getStatuses()[0]);
175 logger.debug("Received null bridge while updating Temperature Sensor status!");
176 return Optional.empty();
178 } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
179 logger.debug("Received exception while refreshing Temperature Sensor status: {}", e.getMessage());
180 return Optional.empty();