2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.opensprinkler.internal.handler;
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;
19 import javax.measure.quantity.ElectricCurrent;
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;
39 * @author Florian Schmidt - Refactoring
42 public class OpenSprinklerDeviceHandler extends OpenSprinklerBaseHandler {
43 private final Logger logger = LoggerFactory.getLogger(OpenSprinklerDeviceHandler.class);
45 public OpenSprinklerDeviceHandler(Thing thing) {
50 protected void updateChannel(ChannelUID channel) {
52 switch (channel.getIdWithoutGroup()) {
54 if (getApi().isRainDetected()) {
55 updateState(channel, OnOffType.ON);
57 updateState(channel, OnOffType.OFF);
60 case SENSOR_WATERLEVEL:
61 updateState(channel, QuantityType.valueOf(getApi().waterLevel(), PERCENT));
63 case SENSOR_CURRENT_DRAW:
65 new QuantityType<ElectricCurrent>(getApi().currentDraw(), MILLI(Units.AMPERE)));
68 logger.debug("Not updating unknown channel {}", channel);
70 } catch (CommunicationApiException | NoCurrentDrawSensorException e) {
71 logger.debug("Could not update {}", channel, e);
76 public void initialize() {
77 ChannelUID currentDraw = new ChannelUID(thing.getUID(), "currentDraw");
78 if (thing.getChannel(currentDraw) == null) {
79 ThingBuilder thingBuilder = editThing();
81 getApi().currentDraw();
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);
88 updateThing(thingBuilder.build());
89 } catch (NoCurrentDrawSensorException e) {
90 if (thing.getChannel(currentDraw) != null) {
91 thingBuilder.withoutChannel(currentDraw);
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);
98 updateStatus(ThingStatus.ONLINE);
102 public void handleCommand(ChannelUID channelUID, Command command) {
103 // nothing to do here