]> git.basschouten.com Git - openhab-addons.git/blob
237176ec850e036ff959e830fe3be0504d0827b1
[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.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;
36
37 /**
38  * Handler for all Climate Device thing types that Verisure provides.
39  *
40  * @author Jan Gustafsson - Initial contribution
41  *
42  */
43 @NonNullByDefault
44 public class VerisureClimateDeviceThingHandler extends VerisureThingHandler<VerisureClimatesDTO> {
45
46     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<ThingTypeUID>();
47     static {
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);
52     }
53
54     public VerisureClimateDeviceThingHandler(Thing thing) {
55         super(thing);
56     }
57
58     @Override
59     public Class<VerisureClimatesDTO> getVerisureThingClass() {
60         return VerisureClimatesDTO.class;
61     }
62
63     @Override
64     public synchronized void update(VerisureClimatesDTO thing) {
65         updateClimateDeviceState(thing);
66         updateStatus(ThingStatus.ONLINE);
67     }
68
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);
75                 });
76         String timeStamp = climateJSON.getData().getInstallation().getClimates().get(0).getTemperatureTimestamp();
77         if (timeStamp != null) {
78             updateTimeStamp(timeStamp);
79         }
80         updateInstallationChannels(climateJSON);
81     }
82
83     public State getValue(String channelId, VerisureClimatesDTO climateJSON) {
84         switch (channelId) {
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);
92                 }
93             case CHANNEL_HUMIDITY_ENABLED:
94                 boolean humidityEnabled = climateJSON.getData().getInstallation().getClimates().get(0)
95                         .isHumidityEnabled();
96                 return OnOffType.from(humidityEnabled);
97             case CHANNEL_LOCATION:
98                 String location = climateJSON.getLocation();
99                 return location != null ? new StringType(location) : UnDefType.NULL;
100         }
101         return UnDefType.UNDEF;
102     }
103
104     @Override
105     public void updateTriggerChannel(String event) {
106         logger.debug("ClimateThingHandler trigger event {}", event);
107         triggerChannel(CHANNEL_SMOKE_DETECTION_TRIGGER_CHANNEL, event);
108     }
109 }