]> git.basschouten.com Git - openhab-addons.git/blob
51d61c738f7a44f71e6323ddb6b584521f4f662f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.verisure.internal.handler;
14
15 import static org.openhab.binding.verisure.internal.VerisureBindingConstants.*;
16
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Set;
20
21 import javax.measure.quantity.Dimensionless;
22 import javax.measure.quantity.Temperature;
23
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;
39
40 /**
41  * Handler for all Climate Device thing types that Verisure provides.
42  *
43  * @author Jan Gustafsson - Initial contribution
44  *
45  */
46 @NonNullByDefault
47 public class VerisureClimateDeviceThingHandler extends VerisureThingHandler<VerisureClimatesDTO> {
48
49     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<ThingTypeUID>();
50     static {
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);
55     }
56
57     public VerisureClimateDeviceThingHandler(Thing thing) {
58         super(thing);
59     }
60
61     @Override
62     public Class<VerisureClimatesDTO> getVerisureThingClass() {
63         return VerisureClimatesDTO.class;
64     }
65
66     @Override
67     public synchronized void update(VerisureClimatesDTO thing) {
68         updateClimateDeviceState(thing);
69         updateStatus(ThingStatus.ONLINE);
70     }
71
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);
78                 });
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);
84             }
85         }
86
87         updateInstallationChannels(climateJSON);
88     }
89
90     public State getValue(String channelId, VerisureClimatesDTO climateJSON) {
91         List<Climate> climateList = climateJSON.getData().getInstallation().getClimates();
92         switch (channelId) {
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);
97                 }
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);
102                 }
103             case CHANNEL_HUMIDITY_ENABLED:
104                 if (climateList != null && !climateList.isEmpty()) {
105                     boolean humidityEnabled = climateList.get(0).isHumidityEnabled();
106                     return OnOffType.from(humidityEnabled);
107                 }
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);
117                     }
118                 }
119                 return OnOffType.from(false);
120         }
121         return UnDefType.UNDEF;
122     }
123
124     @Override
125     public void updateTriggerChannel(String event) {
126         logger.debug("ClimateThingHandler trigger event {}", event);
127         triggerChannel(CHANNEL_SMOKE_DETECTION_TRIGGER_CHANNEL, event);
128     }
129 }