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.Temperature;
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.ImperialUnits;
31 import org.openhab.core.library.unit.SIUnits;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.types.Command;
37 import org.openhab.core.types.RefreshType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.digitaldan.jomnilinkII.Message;
42 import com.digitaldan.jomnilinkII.MessageTypes.CommandMessage;
43 import com.digitaldan.jomnilinkII.MessageTypes.ObjectStatus;
44 import com.digitaldan.jomnilinkII.MessageTypes.properties.AreaProperties;
45 import com.digitaldan.jomnilinkII.MessageTypes.properties.AuxSensorProperties;
46 import com.digitaldan.jomnilinkII.MessageTypes.statuses.ExtendedAuxSensorStatus;
47 import com.digitaldan.jomnilinkII.OmniInvalidResponseException;
48 import com.digitaldan.jomnilinkII.OmniUnknownMessageTypeException;
51 * The {@link TempSensorHandler} defines some methods that are used to interface
52 * with an OmniLink Temperature Sensor. This by extension also defines the
53 * Temperature Sensor thing that openHAB will be able to pick up and interface
56 * @author Craig Hamilton - Initial contribution
57 * @author Ethan Dye - openHAB3 rewrite
60 public class TempSensorHandler extends AbstractOmnilinkStatusHandler<ExtendedAuxSensorStatus> {
61 private final Logger logger = LoggerFactory.getLogger(TempSensorHandler.class);
62 private final int thingID = getThingNumber();
63 public @Nullable String number;
65 public TempSensorHandler(Thing thing) {
70 public void initialize() {
72 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
73 if (bridgeHandler != null) {
74 updateTempSensorProperties(bridgeHandler);
76 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
77 "Received null bridge while initializing Temperature Sensor!");
81 private void updateTempSensorProperties(OmnilinkBridgeHandler bridgeHandler) {
82 final List<AreaProperties> areas = getAreaProperties();
84 for (AreaProperties areaProperties : areas) {
85 int areaFilter = bitFilterForArea(areaProperties);
87 ObjectPropertyRequest<AuxSensorProperties> objectPropertyRequest = ObjectPropertyRequest
88 .builder(bridgeHandler, ObjectPropertyRequests.AUX_SENSORS, thingID, 0).selectNamed()
89 .areaFilter(areaFilter).build();
91 for (AuxSensorProperties auxSensorProperties : objectPropertyRequest) {
92 Map<String, String> properties = editProperties();
93 properties.put(THING_PROPERTIES_NAME, auxSensorProperties.getName());
94 properties.put(THING_PROPERTIES_AREA, Integer.toString(areaProperties.getNumber()));
95 updateProperties(properties);
102 @SuppressWarnings("unchecked")
103 public void handleCommand(ChannelUID channelUID, Command command) {
104 logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
105 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
106 Optional<TemperatureFormat> temperatureFormat = Optional.empty();
108 if (command instanceof RefreshType) {
109 retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
110 ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null status update!"));
114 if (!(command instanceof QuantityType)) {
115 logger.debug("Invalid command: {}, must be QuantityType", command);
118 if (bridgeHandler != null) {
119 temperatureFormat = bridgeHandler.getTemperatureFormat();
120 if (temperatureFormat.isEmpty()) {
121 logger.warn("Receieved null temperature format!");
125 logger.warn("Could not connect to Bridge, failed to get temperature format!");
129 switch (channelUID.getId()) {
130 case CHANNEL_AUX_LOW_SETPOINT:
131 sendOmnilinkCommand(CommandMessage.CMD_THERMO_SET_HEAT_POINT,
132 temperatureFormat.get().formatToOmni(((QuantityType<Temperature>) command).floatValue()),
135 case CHANNEL_AUX_HIGH_SETPOINT:
136 sendOmnilinkCommand(CommandMessage.CMD_THERMO_SET_COOL_POINT,
137 temperatureFormat.get().formatToOmni(((QuantityType<Temperature>) command).floatValue()),
141 logger.warn("Unknown channel for Temperature Sensor thing: {}", channelUID);
146 public void updateChannels(ExtendedAuxSensorStatus status) {
147 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
148 if (bridgeHandler != null) {
149 Optional<TemperatureFormat> temperatureFormat = bridgeHandler.getTemperatureFormat();
150 if (temperatureFormat.isPresent()) {
151 updateState(CHANNEL_AUX_TEMP, new QuantityType<>(
152 temperatureFormat.get().omniToFormat(status.getTemperature()),
153 temperatureFormat.get().getFormatNumber() == 1 ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS));
154 updateState(CHANNEL_AUX_LOW_SETPOINT, new QuantityType<>(
155 temperatureFormat.get().omniToFormat(status.getCoolSetpoint()),
156 temperatureFormat.get().getFormatNumber() == 1 ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS));
157 updateState(CHANNEL_AUX_HIGH_SETPOINT, new QuantityType<>(
158 temperatureFormat.get().omniToFormat(status.getHeatSetpoint()),
159 temperatureFormat.get().getFormatNumber() == 1 ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS));
161 logger.warn("Receieved null temperature format, could not update Temperature Sensor channels!");
164 logger.debug("Received null bridge while updating Temperature Sensor channels!");
169 protected Optional<ExtendedAuxSensorStatus> retrieveStatus() {
171 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
172 if (bridgeHandler != null) {
173 ObjectStatus objStatus = bridgeHandler.requestObjectStatus(Message.OBJ_TYPE_AUX_SENSOR, thingID,
175 return Optional.of((ExtendedAuxSensorStatus) objStatus.getStatuses()[0]);
177 logger.debug("Received null bridge while updating Temperature Sensor status!");
178 return Optional.empty();
180 } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
181 logger.debug("Received exception while refreshing Temperature Sensor status: {}", e.getMessage());
182 return Optional.empty();