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.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 TempSensorHandler} defines some methods that are used to interface
49 * with an OmniLink Temperature Sensor. This by extension also defines the
50 * Temperature 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 TempSensorHandler extends AbstractOmnilinkStatusHandler<ExtendedAuxSensorStatus> {
58 private final Logger logger = LoggerFactory.getLogger(TempSensorHandler.class);
59 private final int thingID = getThingNumber();
60 public @Nullable String number;
62 public TempSensorHandler(Thing thing) {
67 public void initialize() {
69 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
70 if (bridgeHandler != null) {
71 updateTempSensorProperties(bridgeHandler);
73 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
74 "Received null bridge while initializing Temperature Sensor!");
78 private void updateTempSensorProperties(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);
102 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
103 Optional<TemperatureFormat> temperatureFormat = Optional.empty();
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);
115 if (bridgeHandler != null) {
116 temperatureFormat = bridgeHandler.getTemperatureFormat();
117 if (!temperatureFormat.isPresent()) {
118 logger.warn("Receieved null temperature format!");
122 logger.warn("Could not connect to Bridge, failed to get temperature format!");
126 switch (channelUID.getId()) {
127 case CHANNEL_AUX_LOW_SETPOINT:
128 sendOmnilinkCommand(OmniLinkCmd.CMD_THERMO_SET_HEAT_LOW_POINT.getNumber(),
129 temperatureFormat.get().formatToOmni(((QuantityType<Temperature>) command).floatValue()),
132 case CHANNEL_AUX_HIGH_SETPOINT:
133 sendOmnilinkCommand(OmniLinkCmd.CMD_THERMO_SET_COOL_HIGH_POINT.getNumber(),
134 temperatureFormat.get().formatToOmni(((QuantityType<Temperature>) command).floatValue()),
138 logger.warn("Unknown channel for Temperature Sensor thing: {}", channelUID);
143 public void updateChannels(ExtendedAuxSensorStatus status) {
144 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
145 if (bridgeHandler != null) {
146 Optional<TemperatureFormat> temperatureFormat = bridgeHandler.getTemperatureFormat();
147 if (temperatureFormat.isPresent()) {
148 updateState(CHANNEL_AUX_TEMP, new QuantityType<>(
149 temperatureFormat.get().omniToFormat(status.getTemperature()),
150 temperatureFormat.get().getFormatNumber() == 1 ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS));
151 updateState(CHANNEL_AUX_LOW_SETPOINT, new QuantityType<>(
152 temperatureFormat.get().omniToFormat(status.getCoolSetpoint()),
153 temperatureFormat.get().getFormatNumber() == 1 ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS));
154 updateState(CHANNEL_AUX_HIGH_SETPOINT, new QuantityType<>(
155 temperatureFormat.get().omniToFormat(status.getHeatSetpoint()),
156 temperatureFormat.get().getFormatNumber() == 1 ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS));
158 logger.warn("Receieved null temperature format, could not update Temperature Sensor channels!");
161 logger.debug("Received null bridge while updating Temperature Sensor channels!");
166 protected Optional<ExtendedAuxSensorStatus> retrieveStatus() {
168 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
169 if (bridgeHandler != null) {
170 ObjectStatus objStatus = bridgeHandler.requestObjectStatus(Message.OBJ_TYPE_AUX_SENSOR, thingID,
172 return Optional.of((ExtendedAuxSensorStatus) objStatus.getStatuses()[0]);
174 logger.debug("Received null bridge while updating Temperature Sensor status!");
175 return Optional.empty();
177 } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
178 logger.debug("Received exception while refreshing Temperature Sensor status: {}", e.getMessage());
179 return Optional.empty();