2 * Copyright (c) 2010-2023 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 java.math.BigDecimal;
20 import java.util.ArrayList;
22 import javax.measure.quantity.Dimensionless;
23 import javax.measure.quantity.ElectricCurrent;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.openhab.binding.opensprinkler.internal.OpenSprinklerStateDescriptionProvider;
27 import org.openhab.binding.opensprinkler.internal.api.OpenSprinklerApi;
28 import org.openhab.binding.opensprinkler.internal.api.exception.CommunicationApiException;
29 import org.openhab.binding.opensprinkler.internal.api.exception.UnauthorizedApiException;
30 import org.openhab.core.library.types.DecimalType;
31 import org.openhab.core.library.types.OnOffType;
32 import org.openhab.core.library.types.QuantityType;
33 import org.openhab.core.library.types.StringType;
34 import org.openhab.core.library.unit.Units;
35 import org.openhab.core.thing.Channel;
36 import org.openhab.core.thing.ChannelUID;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingStatus;
39 import org.openhab.core.thing.ThingStatusDetail;
40 import org.openhab.core.thing.binding.builder.ThingBuilder;
41 import org.openhab.core.types.Command;
42 import org.openhab.core.types.RefreshType;
45 * @author Chris Graham - Initial contribution
46 * @author Florian Schmidt - Refactoring
49 public class OpenSprinklerDeviceHandler extends OpenSprinklerBaseHandler {
50 public final OpenSprinklerStateDescriptionProvider stateDescriptionProvider;
52 public OpenSprinklerDeviceHandler(Thing thing, OpenSprinklerStateDescriptionProvider stateDescriptionProvider) {
54 this.stateDescriptionProvider = stateDescriptionProvider;
58 protected void updateChannel(ChannelUID channel) {
59 OpenSprinklerApi localAPI = getApi();
60 if (localAPI == null) {
63 switch (channel.getIdWithoutGroup()) {
65 if (localAPI.isRainDetected()) {
66 updateState(channel, OnOffType.ON);
68 updateState(channel, OnOffType.OFF);
71 case CHANNEL_RAIN_DELAY:
72 updateState(channel, localAPI.getRainDelay());
75 if (localAPI.getSensor2State() == 1) {
76 updateState(channel, OnOffType.ON);
78 updateState(channel, OnOffType.OFF);
81 case SENSOR_WATERLEVEL:
82 updateState(channel, QuantityType.valueOf(localAPI.waterLevel(), PERCENT));
84 case SENSOR_CURRENT_DRAW:
85 updateState(channel, new QuantityType<ElectricCurrent>(localAPI.currentDraw(), MILLI(Units.AMPERE)));
87 case SENSOR_SIGNAL_STRENGTH:
88 int rssiValue = localAPI.signalStrength();
89 if (rssiValue < -80) {
90 updateState(channel, DecimalType.ZERO);
91 } else if (rssiValue < -70) {
92 updateState(channel, new DecimalType(1));
93 } else if (rssiValue < -60) {
94 updateState(channel, new DecimalType(2));
95 } else if (rssiValue < -40) {
96 updateState(channel, new DecimalType(3));
97 } else if (rssiValue >= -40) {
98 updateState(channel, new DecimalType(4));
101 case SENSOR_FLOW_COUNT:
102 updateState(channel, new QuantityType<Dimensionless>(localAPI.flowSensorCount(), Units.ONE));
104 case CHANNEL_PROGRAMS:
106 case CHANNEL_ENABLE_PROGRAMS:
107 if (localAPI.getIsEnabled()) {
108 updateState(channel, OnOffType.ON);
110 updateState(channel, OnOffType.OFF);
113 case CHANNEL_STATIONS:
117 case CHANNEL_RESET_STATIONS:
120 logger.debug("Can not update the unknown channel {}", channel);
125 public void initialize() {
127 OpenSprinklerApi localAPI = getApi();
128 // Remove channels due to missing sensors or old firmware
129 if (localAPI != null) {
130 ArrayList<Channel> removeChannels = new ArrayList<>();
131 Channel channel = thing.getChannel(SENSOR_CURRENT_DRAW);
132 if (localAPI.currentDraw() == -1 && channel != null) {
133 logger.debug("No current sensor detected, removing channel.");
134 removeChannels.add(channel);
136 channel = thing.getChannel(SENSOR_SIGNAL_STRENGTH);
137 if (localAPI.signalStrength() == 1 && channel != null) {
138 removeChannels.add(channel);
140 channel = thing.getChannel(SENSOR_FLOW_COUNT);
141 if (localAPI.flowSensorCount() == -1 && channel != null) {
142 removeChannels.add(channel);
144 channel = thing.getChannel(SENSOR_2);
145 if (localAPI.getSensor2State() == -1 && channel != null) {
146 removeChannels.add(channel);
148 if (!removeChannels.isEmpty()) {
149 ThingBuilder thingBuilder = editThing();
150 thingBuilder.withoutChannels(removeChannels);
151 updateThing(thingBuilder.build());
153 updateProgramsChanOptions(localAPI);
154 updateStationsChanOptions(localAPI);
155 nextDurationTime = new BigDecimal(1800);
156 updateState(NEXT_DURATION, new QuantityType<>(nextDurationTime, Units.SECOND));
161 * Fetch the stored Program list and update the StateOptions on the channel so they match.
165 private void updateProgramsChanOptions(OpenSprinklerApi api) {
166 stateDescriptionProvider.setStateOptions(new ChannelUID(this.getThing().getUID(), CHANNEL_PROGRAMS),
170 private void updateStationsChanOptions(OpenSprinklerApi api) {
171 stateDescriptionProvider.setStateOptions(new ChannelUID(this.getThing().getUID(), CHANNEL_STATIONS),
175 protected void handleRainDelayCommand(ChannelUID channelUID, Command command, OpenSprinklerApi api)
176 throws UnauthorizedApiException, CommunicationApiException {
177 if (!(command instanceof QuantityType<?>)) {
178 logger.warn("Ignoring implausible non-QuantityType command for rainDelay.");
181 QuantityType<?> quantity = (QuantityType<?>) command;
182 quantity = quantity.toUnit(Units.HOUR);
183 if (quantity != null) {
184 api.setRainDelay(quantity.intValue());
189 public void handleCommand(ChannelUID channelUID, Command command) {
190 OpenSprinklerApi api = getApi();
192 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "OpenSprinkler bridge returned no API.");
195 OpenSprinklerHttpBridgeHandler localBridge = bridgeHandler;
196 if (localBridge == null) {
200 if (command instanceof RefreshType) {
201 switch (channelUID.getIdWithoutGroup()) {
202 case CHANNEL_PROGRAMS:
203 api.getProgramData();
204 updateProgramsChanOptions(api);
206 case CHANNEL_STATIONS:
207 api.getStationNames();
208 updateStationsChanOptions(api);
212 switch (channelUID.getIdWithoutGroup()) {
213 case CHANNEL_PROGRAMS:
214 api.runProgram(command);
216 case CHANNEL_ENABLE_PROGRAMS:
217 api.enablePrograms(command);
220 handleNextDurationCommand(channelUID, command);
222 case CHANNEL_RESET_STATIONS:
223 if (command == OnOffType.ON) {
227 case CHANNEL_STATIONS:
228 if (command instanceof StringType) {
229 BigDecimal temp = new BigDecimal(command.toString());
230 api.openStation(temp.intValue(), nextDurationValue());
233 case CHANNEL_RAIN_DELAY:
234 handleRainDelayCommand(channelUID, command, api);
237 localBridge.delayedRefresh();// update sensors and controls after command is sent
239 } catch (Exception e) {
240 localBridge.communicationError(e);