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