2 * Copyright (c) 2010-2024 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:
119 case CHANNEL_QUEUED_ZONES:
120 updateState(channel, new DecimalType(localAPI.getQueuedZones()));
122 case CHANNEL_CLOUD_CONNECTED:
123 updateState(channel, OnOffType.from(localAPI.getCloudConnected() == 3));
125 case CHANNEL_PAUSE_PROGRAMS:
126 updateState(channel, new QuantityType<>(localAPI.getPausedState(), Units.SECOND));
129 logger.debug("Can not update the unknown channel {}", channel);
134 public void initialize() {
136 OpenSprinklerApi localAPI = getApi();
137 // Remove channels due to missing sensors or old firmware
138 if (localAPI != null) {
139 ArrayList<Channel> removeChannels = new ArrayList<>();
140 Channel channel = thing.getChannel(SENSOR_CURRENT_DRAW);
141 if (localAPI.currentDraw() == -1 && channel != null) {
142 logger.debug("No current sensor detected, removing channel.");
143 removeChannels.add(channel);
145 channel = thing.getChannel(SENSOR_SIGNAL_STRENGTH);
146 if (localAPI.signalStrength() == 1 && channel != null) {
147 removeChannels.add(channel);
149 channel = thing.getChannel(SENSOR_FLOW_COUNT);
150 if (localAPI.flowSensorCount() == -1 && channel != null) {
151 removeChannels.add(channel);
153 channel = thing.getChannel(SENSOR_2);
154 if (localAPI.getSensor2State() == -1 && channel != null) {
155 removeChannels.add(channel);
157 channel = thing.getChannel(CHANNEL_QUEUED_ZONES);
158 if (localAPI.getQueuedZones() == -1 && channel != null) {
159 removeChannels.add(channel);
161 channel = thing.getChannel(CHANNEL_CLOUD_CONNECTED);
162 if (localAPI.getCloudConnected() == -1 && channel != null) {
163 removeChannels.add(channel);
165 channel = thing.getChannel(CHANNEL_PAUSE_PROGRAMS);
166 if (localAPI.getPausedState() == -1 && channel != null) {
167 removeChannels.add(channel);
169 if (!removeChannels.isEmpty()) {
170 ThingBuilder thingBuilder = editThing();
171 thingBuilder.withoutChannels(removeChannels);
172 updateThing(thingBuilder.build());
174 updateProgramsChanOptions(localAPI);
175 updateStationsChanOptions(localAPI);
176 nextDurationTime = new BigDecimal(1800);
177 updateState(NEXT_DURATION, new QuantityType<>(nextDurationTime, Units.SECOND));
182 * Fetch the stored Program list and update the StateOptions on the channel so they match.
186 private void updateProgramsChanOptions(OpenSprinklerApi api) {
187 stateDescriptionProvider.setStateOptions(new ChannelUID(this.getThing().getUID(), CHANNEL_PROGRAMS),
191 private void updateStationsChanOptions(OpenSprinklerApi api) {
192 stateDescriptionProvider.setStateOptions(new ChannelUID(this.getThing().getUID(), CHANNEL_STATIONS),
196 protected void handleRainDelayCommand(ChannelUID channelUID, Command command, OpenSprinklerApi api)
197 throws UnauthorizedApiException, CommunicationApiException {
198 if (!(command instanceof QuantityType<?>)) {
199 logger.warn("Ignoring implausible non-QuantityType command for rainDelay.");
202 QuantityType<?> quantity = (QuantityType<?>) command;
203 quantity = quantity.toUnit(Units.HOUR);
204 if (quantity != null) {
205 api.setRainDelay(quantity.intValue());
210 public void handleCommand(ChannelUID channelUID, Command command) {
211 OpenSprinklerApi api = getApi();
213 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "OpenSprinkler bridge returned no API.");
216 OpenSprinklerHttpBridgeHandler localBridge = bridgeHandler;
217 if (localBridge == null) {
221 if (command instanceof RefreshType) {
222 switch (channelUID.getIdWithoutGroup()) {
223 case CHANNEL_PROGRAMS:
224 api.getProgramData();
225 updateProgramsChanOptions(api);
227 case CHANNEL_STATIONS:
228 api.getStationNames();
229 updateStationsChanOptions(api);
233 switch (channelUID.getIdWithoutGroup()) {
234 case CHANNEL_PROGRAMS:
235 api.runProgram(command);
237 case CHANNEL_ENABLE_PROGRAMS:
238 api.enablePrograms(command);
241 handleNextDurationCommand(channelUID, command);
243 case CHANNEL_RESET_STATIONS:
244 if (command == OnOffType.ON) {
248 case CHANNEL_STATIONS:
249 if (command instanceof StringType) {
250 BigDecimal temp = new BigDecimal(command.toString());
251 api.openStation(temp.intValue(), nextDurationValue());
254 case CHANNEL_RAIN_DELAY:
255 handleRainDelayCommand(channelUID, command, api);
257 case CHANNEL_PAUSE_PROGRAMS:
258 if (command == OnOffType.OFF) {
259 api.setPausePrograms(0);
260 } else if (command instanceof DecimalType) {
261 api.setPausePrograms(((BigDecimal) command).intValue());
262 } else if (command instanceof QuantityType<?>) {
263 QuantityType<?> quantity = (QuantityType<?>) command;
264 quantity = quantity.toUnit(Units.SECOND);
265 if (quantity != null) {
266 api.setPausePrograms(quantity.toBigDecimal().intValue());
270 "The CHANNEL_PAUSE_PROGRAMS only supports QuanityType in seconds, DecimalType and OFF");
275 localBridge.delayedRefresh();// update sensors and controls after command is sent
277 } catch (Exception e) {
278 localBridge.communicationError(e);