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.SmartHomeUnits.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.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;
38 * @author Florian Schmidt - Refactoring
41 public class OpenSprinklerDeviceHandler extends OpenSprinklerBaseHandler {
42 private final Logger logger = LoggerFactory.getLogger(OpenSprinklerDeviceHandler.class);
44 public OpenSprinklerDeviceHandler(Thing thing) {
49 protected void updateChannel(ChannelUID channel) {
51 switch (channel.getIdWithoutGroup()) {
53 if (getApi().isRainDetected()) {
54 updateState(channel, OnOffType.ON);
56 updateState(channel, OnOffType.OFF);
59 case SENSOR_WATERLEVEL:
60 updateState(channel, QuantityType.valueOf(getApi().waterLevel(), PERCENT));
62 case SENSOR_CURRENT_DRAW:
64 new QuantityType<ElectricCurrent>(getApi().currentDraw(), MILLI(SmartHomeUnits.AMPERE)));
67 logger.debug("Not updating unknown channel {}", channel);
69 } catch (CommunicationApiException | NoCurrentDrawSensorException e) {
70 logger.debug("Could not update {}", channel, e);
75 public void initialize() {
76 ChannelUID currentDraw = new ChannelUID(thing.getUID(), "currentDraw");
77 if (thing.getChannel(currentDraw) == null) {
78 ThingBuilder thingBuilder = editThing();
80 getApi().currentDraw();
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);
87 updateThing(thingBuilder.build());
88 } catch (NoCurrentDrawSensorException e) {
89 if (thing.getChannel(currentDraw) != null) {
90 thingBuilder.withoutChannel(currentDraw);
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);
101 public void handleCommand(ChannelUID channelUID, Command command) {
102 // nothing to do here