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.VerisureClimatesDTO;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.library.unit.SIUnits;
29 import org.openhab.core.library.unit.Units;
30 import org.openhab.core.thing.Channel;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.types.State;
35 import org.openhab.core.types.UnDefType;
38 * Handler for all Climate Device thing types that Verisure provides.
40 * @author Jan Gustafsson - Initial contribution
44 public class VerisureClimateDeviceThingHandler extends VerisureThingHandler<VerisureClimatesDTO> {
46 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<ThingTypeUID>();
48 SUPPORTED_THING_TYPES.add(THING_TYPE_SMOKEDETECTOR);
49 SUPPORTED_THING_TYPES.add(THING_TYPE_WATERDETECTOR);
50 SUPPORTED_THING_TYPES.add(THING_TYPE_SIREN);
51 SUPPORTED_THING_TYPES.add(THING_TYPE_NIGHT_CONTROL);
54 public VerisureClimateDeviceThingHandler(Thing thing) {
59 public Class<VerisureClimatesDTO> getVerisureThingClass() {
60 return VerisureClimatesDTO.class;
64 public synchronized void update(VerisureClimatesDTO thing) {
65 updateClimateDeviceState(thing);
66 updateStatus(ThingStatus.ONLINE);
69 private void updateClimateDeviceState(VerisureClimatesDTO climateJSON) {
70 getThing().getChannels().stream().map(Channel::getUID)
71 .filter(channelUID -> isLinked(channelUID) && !channelUID.getId().equals("timestamp"))
72 .forEach(channelUID -> {
73 State state = getValue(channelUID.getId(), climateJSON);
74 updateState(channelUID, state);
76 String timeStamp = climateJSON.getData().getInstallation().getClimates().get(0).getTemperatureTimestamp();
77 if (timeStamp != null) {
78 updateTimeStamp(timeStamp);
80 updateInstallationChannels(climateJSON);
83 public State getValue(String channelId, VerisureClimatesDTO climateJSON) {
85 case CHANNEL_TEMPERATURE:
86 double temperature = climateJSON.getData().getInstallation().getClimates().get(0).getTemperatureValue();
87 return new QuantityType<Temperature>(temperature, SIUnits.CELSIUS);
88 case CHANNEL_HUMIDITY:
89 if (climateJSON.getData().getInstallation().getClimates().get(0).isHumidityEnabled()) {
90 double humidity = climateJSON.getData().getInstallation().getClimates().get(0).getHumidityValue();
91 return new QuantityType<Dimensionless>(humidity, Units.PERCENT);
93 case CHANNEL_HUMIDITY_ENABLED:
94 boolean humidityEnabled = climateJSON.getData().getInstallation().getClimates().get(0)
96 return OnOffType.from(humidityEnabled);
97 case CHANNEL_LOCATION:
98 String location = climateJSON.getLocation();
99 return location != null ? new StringType(location) : UnDefType.NULL;
101 return UnDefType.UNDEF;
105 public void updateTriggerChannel(String event) {
106 logger.debug("ClimateThingHandler trigger event {}", event);
107 triggerChannel(CHANNEL_SMOKE_DETECTION_TRIGGER_CHANNEL, event);