2 * Copyright (c) 2010-2022 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.fineoffsetweatherstation.internal.handler;
15 import java.math.BigDecimal;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.fineoffsetweatherstation.internal.FineOffsetWeatherStationBindingConstants;
20 import org.openhab.binding.fineoffsetweatherstation.internal.domain.response.SensorDevice;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.Channel;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingStatusDetail;
28 import org.openhab.core.thing.binding.BaseThingHandler;
29 import org.openhab.core.types.Command;
32 * The {@link FineOffsetSensorHandler} keeps track of the signal and battery of the sensor attached to the gateway.
34 * @author Andreas Berger - Initial contribution
37 public class FineOffsetSensorHandler extends BaseThingHandler {
38 private boolean disposed;
40 public FineOffsetSensorHandler(Thing thing) {
45 public void handleCommand(ChannelUID channelUID, Command command) {
49 public void initialize() {
50 updateStatus(ThingStatus.ONLINE);
55 public void dispose() {
59 public void updateSensorState(@Nullable SensorDevice sensorDevice) {
63 if (sensorDevice == null) {
64 updateStatus(ThingStatus.OFFLINE);
67 if (sensorDevice.getSignal() == 0) {
68 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
70 updateStatus(ThingStatus.ONLINE);
72 updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_SIGNAL,
73 new DecimalType(sensorDevice.getSignal()));
74 updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_LOW_BATTERY,
75 sensorDevice.getBatteryStatus().isLow() ? OnOffType.ON : OnOffType.OFF);
76 Integer percentage = sensorDevice.getBatteryStatus().getPercentage();
77 if (percentage != null) {
78 updateState(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_BATTERY_LEVEL,
79 new DecimalType(new BigDecimal(percentage)));
82 Channel channel = thing.getChannel(FineOffsetWeatherStationBindingConstants.SENSOR_CHANNEL_BATTERY_LEVEL);
83 if (channel != null) {
84 updateThing(editThing().withoutChannels(channel).build());