Skip to content

Commit

Permalink
[opensprinkler] Add current draw channel (#7178)
Browse files Browse the repository at this point in the history
* [opensprinkler] Add current draw channel

The opensprinkler device, if it has a sensor for it, can display the
current draw of the device. This information is exposed as a read-only
channel with this commit.

Fixes #6386

Co-authored-by: cpmeister <mistercpp2000@gmail.com>
Signed-off-by: Florian <florian.schmidt.welzow@t-online.de>
  • Loading branch information
FlorianSW and cpmeister authored Apr 25, 2020
1 parent 9b11df9 commit 683853b
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 12 deletions.
9 changes: 6 additions & 3 deletions bundles/org.openhab.binding.opensprinkler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ When using the `nextDuration` channel, it is advised to setup persistence (e.g.

The following is supported by the `device` thing, but only when connected using the http interface.

| Channel Type ID | Item Type | | Description |
|-----------------|-----------|----|-----------------------------------------------------------------------|
| rainsensor | Switch | RO | This channel indicates whether rain is detected by the device or not. |
| Channel Type ID | Item Type | | Description |
|-----------------|------------------------|----|---------------------------------------------------------------------------|
| rainsensor | Switch | RO | This channel indicates whether rain is detected by the device or not. |
| currentDraw | Number:ElectricCurrent | RO | Shows the current draw of the device. If the device does not have sensors |
| | | | for this metric, the channel will not be available. |

## Example

Expand Down Expand Up @@ -95,6 +97,7 @@ Switch Station05 (stations) { channel="opensprinkler:station:http:05:stationStat
Switch Station06 (stations) { channel="opensprinkler:station:http:06:stationState" }
Switch RainSensor { channel="opensprinkler:station:http:device:rainsensor" }
Number:ElectricCurrent CurrentDraw {channel="opensprinkler:station:http:device:currentDraw"}
```

demo.sitemap:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class OpenSprinklerBindingConstants {

// List of all Channel ids
public static final String SENSOR_RAIN = "rainsensor";
public static final String SENSOR_CURRENT_DRAW = "currentDraw";
public static final String STATION_STATE = "stationState";
public static final String STATION_QUEUED = "queued";
public static final String REMAINING_WATER_TIME = "remainingWaterTime";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.openhab.binding.opensprinkler.internal.api.exception.CommunicationApiException;
import org.openhab.binding.opensprinkler.internal.api.exception.GeneralApiException;
import org.openhab.binding.opensprinkler.internal.model.NoCurrentDrawSensorException;
import org.openhab.binding.opensprinkler.internal.model.StationProgram;

/**
Expand All @@ -26,7 +27,7 @@
*/
public interface OpenSprinklerApi {
/**
* Whether the devie entered manual mode and accepts API requests to control the stations.
* Whether the device entered manual mode and accepts API requests to control the stations.
*
* @return True if this API interface is connected to the Open Sprinkler API. False otherwise.
*/
Expand Down Expand Up @@ -90,6 +91,15 @@ public abstract void openStation(int station, BigDecimal duration)
*/
public abstract boolean isRainDetected() throws CommunicationApiException;

/**
* Returns the current draw of all connected zones of the OpenSprinkler device in milliamperes.
*
* @return current draw in milliamperes
* @throws CommunicationApiException
* @throws
*/
public abstract int currentDraw() throws CommunicationApiException, NoCurrentDrawSensorException;

/**
* Returns the number of total stations that are controllable from the OpenSprinkler
* device.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.openhab.binding.opensprinkler.internal.api.exception.CommunicationApiException;
import org.openhab.binding.opensprinkler.internal.api.exception.GeneralApiException;
import org.openhab.binding.opensprinkler.internal.config.OpenSprinklerHttpInterfaceConfig;
import org.openhab.binding.opensprinkler.internal.model.NoCurrentDrawSensorException;
import org.openhab.binding.opensprinkler.internal.model.StationProgram;
import org.openhab.binding.opensprinkler.internal.util.Parse;

Expand Down Expand Up @@ -180,6 +181,15 @@ public boolean isRainDetected() throws CommunicationApiException {
}
}

@Override
public int currentDraw() throws CommunicationApiException, NoCurrentDrawSensorException {
JcResponse info = statusInfo();
if (info.curr == null) {
throw new NoCurrentDrawSensorException();
}
return info.curr;
}

@Override
public int getNumberOfStations() throws CommunicationApiException {
String returnContent;
Expand Down Expand Up @@ -259,6 +269,7 @@ private static class JcResponse {
public List<List<Integer>> ps;
@SerializedName(value = "sn1", alternate = "rs")
public int rs;
public Integer curr;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@
*/
package org.openhab.binding.opensprinkler.internal.handler;

import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.SENSOR_RAIN;
import static org.eclipse.smarthome.core.library.unit.MetricPrefix.MILLI;
import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.*;

import javax.measure.quantity.ElectricCurrent;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.QuantityType;
import org.eclipse.smarthome.core.library.unit.SmartHomeUnits;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.binding.builder.ChannelBuilder;
import org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder;
import org.eclipse.smarthome.core.thing.type.ChannelTypeUID;
import org.eclipse.smarthome.core.types.Command;
import org.openhab.binding.opensprinkler.internal.api.exception.CommunicationApiException;
import org.openhab.binding.opensprinkler.internal.model.NoCurrentDrawSensorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -36,20 +46,51 @@ public OpenSprinklerDeviceHandler(Thing thing) {

@Override
protected void updateChannel(ChannelUID channel) {
switch (channel.getIdWithoutGroup()) {
case SENSOR_RAIN:
try {
try {
switch (channel.getIdWithoutGroup()) {
case SENSOR_RAIN:
if (getApi().isRainDetected()) {
updateState(channel, OnOffType.ON);
} else {
updateState(channel, OnOffType.OFF);
}
} catch (CommunicationApiException e) {
logger.debug("Could not update rainsensor", e);
break;
case SENSOR_CURRENT_DRAW:
updateState(channel,
new QuantityType<ElectricCurrent>(getApi().currentDraw(), MILLI(SmartHomeUnits.AMPERE)));
break;
default:
logger.debug("Not updating unknown channel {}", channel);
}
} catch (CommunicationApiException | NoCurrentDrawSensorException e) {
logger.debug("Could not update {}", channel, e);
}
}

@Override
public void initialize() {
ChannelUID currentDraw = new ChannelUID(thing.getUID(), "currentDraw");
if (thing.getChannel(currentDraw) == null) {
ThingBuilder thingBuilder = editThing();
try {
getApi().currentDraw();

Channel currentDrawChannel = ChannelBuilder.create(currentDraw, "Number:ElectricCurrent")
.withType(new ChannelTypeUID(BINDING_ID, SENSOR_CURRENT_DRAW)).withLabel("Current Draw")
.withDescription("Provides the current draw.").build();
thingBuilder.withChannel(currentDrawChannel);

updateThing(thingBuilder.build());
} catch (NoCurrentDrawSensorException e) {
if (thing.getChannel(currentDraw) != null) {
thingBuilder.withoutChannel(currentDraw);
}
default:
logger.debug("Not updating unknown channel {}", channel);
updateThing(thingBuilder.build());
} catch (CommunicationApiException e) {
logger.debug("Could not query current draw. Not removing channel as it could be temporary.", e);
}
}
super.initialize();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.opensprinkler.internal.model;

/**
* Indicates, that a device is missing a sensor to measure the current draw of itself.
*
* @author Florian Schmidt - Initial contribution
*/
public class NoCurrentDrawSensorException extends Exception {
private static final long serialVersionUID = 2251925316743442346L;
}

0 comments on commit 683853b

Please sign in to comment.