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.onewire.internal.device;
15 import static org.openhab.binding.onewire.internal.OwBindingConstants.*;
17 import javax.measure.quantity.Temperature;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.onewire.internal.OwException;
21 import org.openhab.binding.onewire.internal.SensorId;
22 import org.openhab.binding.onewire.internal.handler.OwBaseThingHandler;
23 import org.openhab.binding.onewire.internal.handler.OwserverBridgeHandler;
24 import org.openhab.binding.onewire.internal.owserver.OwserverDeviceParameter;
25 import org.openhab.core.config.core.Configuration;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.unit.SIUnits;
29 import org.openhab.core.thing.Channel;
30 import org.openhab.core.thing.Thing;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The {@link DS18x20} class defines a DS18x20 or DS1822 device
37 * @author Jan N. Klug - Initial contribution
40 public class DS18x20 extends AbstractOwDevice {
41 private final Logger logger = LoggerFactory.getLogger(DS18x20.class);
43 private OwserverDeviceParameter temperatureParameter = new OwserverDeviceParameter("/temperature");
45 private boolean ignorePOR = false;
47 public DS18x20(SensorId sensorId, OwBaseThingHandler callback) {
48 super(sensorId, callback);
52 public void configureChannels() throws OwException {
53 Thing thing = callback.getThing();
54 Channel temperatureChannel = thing.getChannel(CHANNEL_TEMPERATURE);
56 if (temperatureChannel != null) {
57 Configuration channelConfiguration = temperatureChannel.getConfiguration();
58 if (channelConfiguration.containsKey(CONFIG_RESOLUTION)) {
59 temperatureParameter = new OwserverDeviceParameter(
60 "/temperature" + (String) channelConfiguration.get(CONFIG_RESOLUTION));
62 temperatureParameter = new OwserverDeviceParameter("/temperature");
64 if (channelConfiguration.containsKey(CONFIG_IGNORE_POR)) {
65 ignorePOR = (Boolean) channelConfiguration.get(CONFIG_IGNORE_POR);
70 throw new OwException(CHANNEL_TEMPERATURE + " not found");
77 public void refresh(OwserverBridgeHandler bridgeHandler, Boolean forcedRefresh) throws OwException {
78 if (isConfigured && enabledChannels.contains(CHANNEL_TEMPERATURE)) {
79 logger.trace("refresh of sensor {} started", sensorId);
80 QuantityType<Temperature> temperature = new QuantityType<>(
81 (DecimalType) bridgeHandler.readDecimalType(sensorId, temperatureParameter), SIUnits.CELSIUS);
82 logger.trace("read temperature {} from {}", temperature, sensorId);
83 if (ignorePOR && (Double.compare(temperature.doubleValue(), 85.0) == 0)) {
84 logger.trace("ignored POR value from sensor {}", sensorId);
86 callback.postUpdate(CHANNEL_TEMPERATURE, temperature);