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.verisure.internal.handler;
15 import static org.openhab.binding.verisure.internal.VerisureBindingConstants.*;
17 import java.util.HashSet;
20 import javax.measure.quantity.Dimensionless;
21 import javax.measure.quantity.Temperature;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.verisure.internal.dto.VerisureBatteryStatusDTO;
25 import org.openhab.binding.verisure.internal.dto.VerisureClimatesDTO;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.types.StringType;
29 import org.openhab.core.library.unit.SIUnits;
30 import org.openhab.core.library.unit.Units;
31 import org.openhab.core.thing.Channel;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.types.State;
36 import org.openhab.core.types.UnDefType;
39 * Handler for all Climate Device thing types that Verisure provides.
41 * @author Jan Gustafsson - Initial contribution
45 public class VerisureClimateDeviceThingHandler extends VerisureThingHandler<VerisureClimatesDTO> {
47 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<ThingTypeUID>();
49 SUPPORTED_THING_TYPES.add(THING_TYPE_SMOKEDETECTOR);
50 SUPPORTED_THING_TYPES.add(THING_TYPE_WATERDETECTOR);
51 SUPPORTED_THING_TYPES.add(THING_TYPE_SIREN);
52 SUPPORTED_THING_TYPES.add(THING_TYPE_NIGHT_CONTROL);
55 public VerisureClimateDeviceThingHandler(Thing thing) {
60 public Class<VerisureClimatesDTO> getVerisureThingClass() {
61 return VerisureClimatesDTO.class;
65 public synchronized void update(VerisureClimatesDTO thing) {
66 updateClimateDeviceState(thing);
67 updateStatus(ThingStatus.ONLINE);
70 private void updateClimateDeviceState(VerisureClimatesDTO climateJSON) {
71 getThing().getChannels().stream().map(Channel::getUID)
72 .filter(channelUID -> isLinked(channelUID) && !channelUID.getId().equals("timestamp"))
73 .forEach(channelUID -> {
74 State state = getValue(channelUID.getId(), climateJSON);
75 updateState(channelUID, state);
77 String timeStamp = climateJSON.getData().getInstallation().getClimates().get(0).getTemperatureTimestamp();
78 if (timeStamp != null) {
79 updateTimeStamp(timeStamp);
81 updateInstallationChannels(climateJSON);
84 public State getValue(String channelId, VerisureClimatesDTO climateJSON) {
86 case CHANNEL_TEMPERATURE:
87 double temperature = climateJSON.getData().getInstallation().getClimates().get(0).getTemperatureValue();
88 return new QuantityType<Temperature>(temperature, SIUnits.CELSIUS);
89 case CHANNEL_HUMIDITY:
90 if (climateJSON.getData().getInstallation().getClimates().get(0).isHumidityEnabled()) {
91 double humidity = climateJSON.getData().getInstallation().getClimates().get(0).getHumidityValue();
92 return new QuantityType<Dimensionless>(humidity, Units.PERCENT);
94 case CHANNEL_HUMIDITY_ENABLED:
95 boolean humidityEnabled = climateJSON.getData().getInstallation().getClimates().get(0)
97 return OnOffType.from(humidityEnabled);
98 case CHANNEL_LOCATION:
99 String location = climateJSON.getLocation();
100 return location != null ? new StringType(location) : UnDefType.NULL;
101 case CHANNEL_BATTERY_STATUS:
102 VerisureBatteryStatusDTO batteryStatus = climateJSON.getBatteryStatus();
103 if (batteryStatus != null) {
104 String status = batteryStatus.getStatus();
105 if (status != null && status.equals("CRITICAL")) {
106 return OnOffType.from(true);
109 return OnOffType.from(false);
111 return UnDefType.UNDEF;
115 public void updateTriggerChannel(String event) {
116 logger.debug("ClimateThingHandler trigger event {}", event);
117 triggerChannel(CHANNEL_SMOKE_DETECTION_TRIGGER_CHANNEL, event);