2 * Copyright (c) 2010-2024 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;
18 import java.util.List;
21 import javax.measure.quantity.Dimensionless;
22 import javax.measure.quantity.Temperature;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.openhab.binding.verisure.internal.dto.VerisureBatteryStatusDTO;
26 import org.openhab.binding.verisure.internal.dto.VerisureClimatesDTO;
27 import org.openhab.binding.verisure.internal.dto.VerisureClimatesDTO.Climate;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.library.unit.SIUnits;
32 import org.openhab.core.library.unit.Units;
33 import org.openhab.core.thing.Channel;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingStatus;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.types.State;
38 import org.openhab.core.types.UnDefType;
41 * Handler for all Climate Device thing types that Verisure provides.
43 * @author Jan Gustafsson - Initial contribution
47 public class VerisureClimateDeviceThingHandler extends VerisureThingHandler<VerisureClimatesDTO> {
49 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<ThingTypeUID>();
51 SUPPORTED_THING_TYPES.add(THING_TYPE_SMOKEDETECTOR);
52 SUPPORTED_THING_TYPES.add(THING_TYPE_WATERDETECTOR);
53 SUPPORTED_THING_TYPES.add(THING_TYPE_SIREN);
54 SUPPORTED_THING_TYPES.add(THING_TYPE_NIGHT_CONTROL);
57 public VerisureClimateDeviceThingHandler(Thing thing) {
62 public Class<VerisureClimatesDTO> getVerisureThingClass() {
63 return VerisureClimatesDTO.class;
67 public synchronized void update(VerisureClimatesDTO thing) {
68 updateClimateDeviceState(thing);
69 updateStatus(ThingStatus.ONLINE);
72 private void updateClimateDeviceState(VerisureClimatesDTO climateJSON) {
73 getThing().getChannels().stream().map(Channel::getUID)
74 .filter(channelUID -> isLinked(channelUID) && !"timestamp".equals(channelUID.getId()))
75 .forEach(channelUID -> {
76 State state = getValue(channelUID.getId(), climateJSON);
77 updateState(channelUID, state);
79 List<Climate> climateList = climateJSON.getData().getInstallation().getClimates();
80 if (climateList != null && !climateList.isEmpty()) {
81 String timeStamp = climateList.get(0).getTemperatureTimestamp();
82 if (timeStamp != null) {
83 updateTimeStamp(timeStamp);
87 updateInstallationChannels(climateJSON);
90 public State getValue(String channelId, VerisureClimatesDTO climateJSON) {
91 List<Climate> climateList = climateJSON.getData().getInstallation().getClimates();
93 case CHANNEL_TEMPERATURE:
94 if (climateList != null && !climateList.isEmpty()) {
95 double temperature = climateList.get(0).getTemperatureValue();
96 return new QuantityType<Temperature>(temperature, SIUnits.CELSIUS);
98 case CHANNEL_HUMIDITY:
99 if (climateList != null && !climateList.isEmpty() && climateList.get(0).isHumidityEnabled()) {
100 double humidity = climateList.get(0).getHumidityValue();
101 return new QuantityType<Dimensionless>(humidity, Units.PERCENT);
103 case CHANNEL_HUMIDITY_ENABLED:
104 if (climateList != null && !climateList.isEmpty()) {
105 boolean humidityEnabled = climateList.get(0).isHumidityEnabled();
106 return OnOffType.from(humidityEnabled);
108 case CHANNEL_LOCATION:
109 String location = climateJSON.getLocation();
110 return location != null ? new StringType(location) : UnDefType.NULL;
111 case CHANNEL_BATTERY_STATUS:
112 VerisureBatteryStatusDTO batteryStatus = climateJSON.getBatteryStatus();
113 if (batteryStatus != null) {
114 String status = batteryStatus.getStatus();
115 if ("CRITICAL".equals(status)) {
116 return OnOffType.from(true);
119 return OnOffType.from(false);
121 return UnDefType.UNDEF;
125 public void updateTriggerChannel(String event) {
126 logger.debug("ClimateThingHandler trigger event {}", event);
127 triggerChannel(CHANNEL_SMOKE_DETECTION_TRIGGER_CHANNEL, event);