]> git.basschouten.com Git - openhab-addons.git/blob
184a394ce2f3328d799f15013f9a63504963c0ee
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.opensprinkler.internal.handler;
14
15 import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.*;
16 import static org.openhab.core.library.unit.MetricPrefix.MILLI;
17 import static org.openhab.core.library.unit.SmartHomeUnits.PERCENT;
18
19 import javax.measure.quantity.ElectricCurrent;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.opensprinkler.internal.api.exception.CommunicationApiException;
23 import org.openhab.binding.opensprinkler.internal.model.NoCurrentDrawSensorException;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.unit.SmartHomeUnits;
27 import org.openhab.core.thing.Channel;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.binding.builder.ChannelBuilder;
31 import org.openhab.core.thing.binding.builder.ThingBuilder;
32 import org.openhab.core.thing.type.ChannelTypeUID;
33 import org.openhab.core.types.Command;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * @author Florian Schmidt - Refactoring
39  */
40 @NonNullByDefault
41 public class OpenSprinklerDeviceHandler extends OpenSprinklerBaseHandler {
42     private final Logger logger = LoggerFactory.getLogger(OpenSprinklerDeviceHandler.class);
43
44     public OpenSprinklerDeviceHandler(Thing thing) {
45         super(thing);
46     }
47
48     @Override
49     protected void updateChannel(ChannelUID channel) {
50         try {
51             switch (channel.getIdWithoutGroup()) {
52                 case SENSOR_RAIN:
53                     if (getApi().isRainDetected()) {
54                         updateState(channel, OnOffType.ON);
55                     } else {
56                         updateState(channel, OnOffType.OFF);
57                     }
58                     break;
59                 case SENSOR_WATERLEVEL:
60                     updateState(channel, QuantityType.valueOf(getApi().waterLevel(), PERCENT));
61                     break;
62                 case SENSOR_CURRENT_DRAW:
63                     updateState(channel,
64                             new QuantityType<ElectricCurrent>(getApi().currentDraw(), MILLI(SmartHomeUnits.AMPERE)));
65                     break;
66                 default:
67                     logger.debug("Not updating unknown channel {}", channel);
68             }
69         } catch (CommunicationApiException | NoCurrentDrawSensorException e) {
70             logger.debug("Could not update {}", channel, e);
71         }
72     }
73
74     @Override
75     public void initialize() {
76         ChannelUID currentDraw = new ChannelUID(thing.getUID(), "currentDraw");
77         if (thing.getChannel(currentDraw) == null) {
78             ThingBuilder thingBuilder = editThing();
79             try {
80                 getApi().currentDraw();
81
82                 Channel currentDrawChannel = ChannelBuilder.create(currentDraw, "Number:ElectricCurrent")
83                         .withType(new ChannelTypeUID(BINDING_ID, SENSOR_CURRENT_DRAW)).withLabel("Current Draw")
84                         .withDescription("Provides the current draw.").build();
85                 thingBuilder.withChannel(currentDrawChannel);
86
87                 updateThing(thingBuilder.build());
88             } catch (NoCurrentDrawSensorException e) {
89                 if (thing.getChannel(currentDraw) != null) {
90                     thingBuilder.withoutChannel(currentDraw);
91                 }
92                 updateThing(thingBuilder.build());
93             } catch (CommunicationApiException e) {
94                 logger.debug("Could not query current draw. Not removing channel as it could be temporary.", e);
95             }
96         }
97         super.initialize();
98     }
99
100     @Override
101     public void handleCommand(ChannelUID channelUID, Command command) {
102         // nothing to do here
103     }
104 }