Skip to content

Commit

Permalink
[opensprinkler] Add water level channel (openhab#8232)
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Frank <pail@gmx.net>
  • Loading branch information
pail23 authored and andrewfg committed Aug 31, 2020
1 parent dd6d95c commit f87c307
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 28 deletions.
12 changes: 7 additions & 5 deletions bundles/org.openhab.binding.opensprinkler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ 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. |
| 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. |
| 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. |
| waterlevel | Number:Dimensionless | RO | This channel shows the current water level in percent (0-250%). The water level is |
| | | | calculated based on the weather and influences the duration of the water programs. |

## Example

Expand Down
4 changes: 3 additions & 1 deletion bundles/org.openhab.binding.opensprinkler/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

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_WATERLEVEL = "waterlevel";
public static final String SENSOR_CURRENT_DRAW = "currentDraw";
public static final String STATION_STATE = "stationState";
public static final String STATION_QUEUED = "queued";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ public abstract void openStation(int station, BigDecimal duration)
*/
public abstract int currentDraw() throws CommunicationApiException, NoCurrentDrawSensorException;

/**
* Returns the water level in %.
*
* @return waterLevel in %
* @throws CommunicationApiException
* @throws
*/
public abstract int waterLevel() throws CommunicationApiException;

/**
* 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 @@ -39,8 +39,8 @@
import com.google.gson.annotations.SerializedName;

/**
* The {@link OpenSprinklerHttpApiV100} class is used for communicating with
* the OpenSprinkler API for firmware versions less than 2.1.0
* The {@link OpenSprinklerHttpApiV100} class is used for communicating with the
* OpenSprinkler API for firmware versions less than 2.1.0
*
* @author Chris Graham - Initial contribution
* @author Florian Schmidt - Allow https URLs and basic auth
Expand All @@ -61,10 +61,11 @@ class OpenSprinklerHttpApiV100 implements OpenSprinklerApi {
protected HttpRequestSender http;

/**
* Constructor for the OpenSprinkler API class to create a connection to the OpenSprinkler
* device for control and obtaining status info.
* Constructor for the OpenSprinkler API class to create a connection to the
* OpenSprinkler device for control and obtaining status info.
*
* @param hostname Hostname or IP address as a String of the OpenSprinkler device.
* @param hostname Hostname or IP address as a String of the OpenSprinkler
* device.
* @param port The port number the OpenSprinkler API is listening on.
* @param password Admin password for the OpenSprinkler device.
* @param basicUsername only needed if basic auth is required
Expand Down Expand Up @@ -190,6 +191,12 @@ public int currentDraw() throws CommunicationApiException, NoCurrentDrawSensorEx
return info.curr;
}

@Override
public int waterLevel() throws CommunicationApiException {
JoResponse info = getOptions();
return info.wl;
}

@Override
public int getNumberOfStations() throws CommunicationApiException {
String returnContent;
Expand All @@ -208,17 +215,10 @@ public int getNumberOfStations() throws CommunicationApiException {

@Override
public int getFirmwareVersion() throws CommunicationApiException {
String returnContent;

try {
returnContent = http.sendHttpGet(getBaseUrl() + CMD_OPTIONS_INFO, null);
} catch (Exception exp) {
throw new CommunicationApiException(
"There was a problem in the HTTP communication with the OpenSprinkler API: " + exp.getMessage());
}

try {
this.firmwareVersion = Parse.jsonInt(returnContent, JSON_OPTION_FIRMWARE_VERSION);
JoResponse info = getOptions();
this.firmwareVersion = info.fwv;
} catch (Exception exp) {
this.firmwareVersion = -1;
}
Expand Down Expand Up @@ -272,9 +272,28 @@ private static class JcResponse {
public Integer curr;
}

private JoResponse getOptions() throws CommunicationApiException {
String returnContent;

try {
returnContent = http.sendHttpGet(getBaseUrl() + CMD_OPTIONS_INFO, getRequestRequiredOptions());
} catch (CommunicationApiException exp) {
throw new CommunicationApiException(
"There was a problem in the HTTP communication with the OpenSprinkler API: " + exp.getMessage());
}

JoResponse resp = gson.fromJson(returnContent, JoResponse.class);
return resp;
}

private static class JoResponse {
public int wl;
public int fwv;
}

/**
* This class contains helper methods for communicating HTTP GET
* and HTTP POST requests.
* This class contains helper methods for communicating HTTP GET and HTTP POST
* requests.
*
* @author Chris Graham - Initial contribution
* @author Florian Schmidt - Reduce visibility of Http communication to Api
Expand All @@ -290,11 +309,12 @@ public HttpRequestSender(HttpClient httpClient) {
}

/**
* Given a URL and a set parameters, send a HTTP GET request to the URL location created by the URL and
* parameters.
* Given a URL and a set parameters, send a HTTP GET request to the URL location
* created by the URL and parameters.
*
* @param url The URL to send a GET request to.
* @param urlParameters List of parameters to use in the URL for the GET request. Null if no parameters.
* @param urlParameters List of parameters to use in the URL for the GET
* request. Null if no parameters.
* @return String contents of the response for the GET request.
* @throws Exception
*/
Expand Down Expand Up @@ -333,11 +353,12 @@ private Request withGeneralProperties(Request request) {
}

/**
* Given a URL and a set parameters, send a HTTP POST request to the URL location created by the URL and
* parameters.
* Given a URL and a set parameters, send a HTTP POST request to the URL
* location created by the URL and parameters.
*
* @param url The URL to send a POST request to.
* @param urlParameters List of parameters to use in the URL for the POST request. Null if no parameters.
* @param urlParameters List of parameters to use in the URL for the POST
* request. Null if no parameters.
* @return String contents of the response for the POST request.
* @throws Exception
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.binding.opensprinkler.internal.handler;

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

import javax.measure.quantity.ElectricCurrent;
Expand Down Expand Up @@ -55,6 +56,9 @@ protected void updateChannel(ChannelUID channel) {
updateState(channel, OnOffType.OFF);
}
break;
case SENSOR_WATERLEVEL:
updateState(channel, QuantityType.valueOf(getApi().waterLevel(), PERCENT));
break;
case SENSOR_CURRENT_DRAW:
updateState(channel,
new QuantityType<ElectricCurrent>(getApi().currentDraw(), MILLI(SmartHomeUnits.AMPERE)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@

<channels>
<channel id="rainsensor" typeId="rainsensor"></channel>
<channel id="waterlevel" typeId="waterlevel"></channel>
</channels>
</thing-type>

Expand All @@ -85,6 +86,14 @@
<state readOnly="true"/>
</channel-type>


<channel-type id="waterlevel">
<item-type>Number:Dimensionless</item-type>
<label>Water Level</label>
<description>The current water level in percent</description>
<state readOnly="true"/>
</channel-type>

<channel-type id="stationState">
<item-type>Switch</item-type>
<label>Station</label>
Expand Down

0 comments on commit f87c307

Please sign in to comment.