]> git.basschouten.com Git - openhab-addons.git/blob
1ac6f65852875841b2914f86f5a039257e7bf26a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.Set;
19
20 import javax.measure.quantity.Dimensionless;
21 import javax.measure.quantity.Temperature;
22
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;
37
38 /**
39  * Handler for all Climate Device thing types that Verisure provides.
40  *
41  * @author Jan Gustafsson - Initial contribution
42  *
43  */
44 @NonNullByDefault
45 public class VerisureClimateDeviceThingHandler extends VerisureThingHandler<VerisureClimatesDTO> {
46
47     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<ThingTypeUID>();
48     static {
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);
53     }
54
55     public VerisureClimateDeviceThingHandler(Thing thing) {
56         super(thing);
57     }
58
59     @Override
60     public Class<VerisureClimatesDTO> getVerisureThingClass() {
61         return VerisureClimatesDTO.class;
62     }
63
64     @Override
65     public synchronized void update(VerisureClimatesDTO thing) {
66         updateClimateDeviceState(thing);
67         updateStatus(ThingStatus.ONLINE);
68     }
69
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);
76                 });
77         String timeStamp = climateJSON.getData().getInstallation().getClimates().get(0).getTemperatureTimestamp();
78         if (timeStamp != null) {
79             updateTimeStamp(timeStamp);
80         }
81         updateInstallationChannels(climateJSON);
82     }
83
84     public State getValue(String channelId, VerisureClimatesDTO climateJSON) {
85         switch (channelId) {
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);
93                 }
94             case CHANNEL_HUMIDITY_ENABLED:
95                 boolean humidityEnabled = climateJSON.getData().getInstallation().getClimates().get(0)
96                         .isHumidityEnabled();
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);
107                     }
108                 }
109                 return OnOffType.from(false);
110         }
111         return UnDefType.UNDEF;
112     }
113
114     @Override
115     public void updateTriggerChannel(String event) {
116         logger.debug("ClimateThingHandler trigger event {}", event);
117         triggerChannel(CHANNEL_SMOKE_DETECTION_TRIGGER_CHANNEL, event);
118     }
119 }