Skip to content

Commit

Permalink
[tellstick] Fix auto-detect handling for Rain and Wind devices (openh…
Browse files Browse the repository at this point in the history
…ab#7172)

* Fixes openhab#7171.
Updated humidity and rain channels to use measurement units, changed state to use %unit%.
Updated README with correct measurement units.

Signed-off-by: Jan Gustafsson <jannegpriv@gmail.com>
  • Loading branch information
jannegpriv authored and LoungeFlyZ committed Jun 8, 2020
1 parent 9a82854 commit e2b430b
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 101 deletions.
40 changes: 20 additions & 20 deletions bundles/org.openhab.binding.tellstick/README.md
Expand Up @@ -123,39 +123,39 @@ Actuators (dimmer/switch) support the following channels:

Sensors (sensor) support the following channels:

| Channel Type ID | Item Type | Description |
|-----------------|-----------|-------------------------------------------------------------|
| humidity | Number | This channel reports the current humidity in percentage. |
| temperature | Number | This channel reports the current temperature in celsius. |
| timestamp | DateTime | This channel reports the last time this sensor was updates. |
| Channel Type ID | Item Type | Description |
|-----------------|---------------------|-------------------------------------------------------------|
| humidity | Number:Dimensionless| This channel reports the current humidity in percentage. |
| temperature | Number:Temperature | This channel reports the current temperature. |
| timestamp | DateTime | This channel reports the last time this sensor was updates. |

PowerSensors ([powersensor]) support the following channels:

| Channel Type ID | Item Type | Description |
|-----------------|-----------|-------------------------------------------------------------|
| watt | Number | This channel reports the current watt. |
| ampere | Number | This channel reports the current ampere. |
| timestamp | DateTime | This channel reports the last time this sensor was updates. |
| Channel Type ID | Item Type | Description |
|-----------------|------------------------|-------------------------------------------------------------|
| watt | Number:Power | This channel reports the current watt. |
| ampere | Number:ElectricCurrent | This channel reports the current ampere. |
| timestamp | DateTime | This channel reports the last time this sensor was updates. |

WindSensors ([windsensor]) support the following channels:

| Channel Type ID | Item Type | Description |
|-----------------|-----------|------------------------------|
| windgust | Number | This current peak wind gust. |
| winddirection | Number | The current wind direction. |
| windaverage | DateTime | The current wind avarage. |
| Channel Type ID | Item Type | Description |
|-----------------|--------------|------------------------------|
| windgust | Number:Speed | The current peak wind gust. |
| winddirection | Number:Angle | The current wind direction. |
| windaverage | Number:Speed | The current wind average. |

RainSensors ([rainsensor]) support the following channels:

| Channel Type ID | Item Type | Description |
|-----------------|-----------|----------------------------|
| rainrate | Number | This current rate of rain. |
| raintotal | Number | The total rain. |
| Channel Type ID | Item Type | Description |
|-----------------|---------------|----------------------------|
| rainrate | Number:Length | This current rate of rain. |
| raintotal | Number:Length | The total rain. |

### Switchbased sensor workaround

All switchbased sensors are binary and the goal is to represent them as a `contact` item in openHAB. Eg. a door is open or closed and can't be altered by sending a radio signal.
To achive that we will create a proxy item which is updated by a rule.
To achieve that we will create a proxy item which is updated by a rule.

First create another proxy item for every sensor:

Expand Down
Expand Up @@ -12,12 +12,27 @@
*/
package org.openhab.binding.tellstick.internal;

import static org.eclipse.smarthome.core.library.unit.MetricPrefix.*;

import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.measure.Unit;
import javax.measure.quantity.Angle;
import javax.measure.quantity.Dimensionless;
import javax.measure.quantity.ElectricCurrent;
import javax.measure.quantity.Illuminance;
import javax.measure.quantity.Length;
import javax.measure.quantity.Power;
import javax.measure.quantity.Pressure;
import javax.measure.quantity.Speed;
import javax.measure.quantity.Temperature;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.library.unit.SIUnits;
import org.eclipse.smarthome.core.library.unit.SmartHomeUnits;
import org.eclipse.smarthome.core.thing.ThingTypeUID;

/**
Expand All @@ -31,6 +46,16 @@ public class TellstickBindingConstants {

public static final String BINDING_ID = "tellstick";

public static final Unit<Dimensionless> HUMIDITY_UNIT = SmartHomeUnits.PERCENT;
public static final Unit<Temperature> TEMPERATURE_UNIT = SIUnits.CELSIUS;
public static final Unit<Pressure> PRESSURE_UNIT = HECTO(SIUnits.PASCAL);
public static final Unit<Speed> WIND_SPEED_UNIT_MS = SmartHomeUnits.METRE_PER_SECOND;
public static final Unit<Angle> WIND_DIRECTION_UNIT = SmartHomeUnits.DEGREE_ANGLE;
public static final Unit<Length> RAIN_UNIT = MILLI(SIUnits.METRE);
public static final Unit<Illuminance> LUX_UNIT = SmartHomeUnits.LUX;
public static final Unit<ElectricCurrent> ELECTRIC_UNIT = SmartHomeUnits.AMPERE;
public static final Unit<Power> POWER_UNIT = KILO(SmartHomeUnits.WATT);

public static final String CONFIGPATH_ID = "location";
public static final String DEVICE_ID = "deviceId";
public static final String DEVICE_PROTOCOL = "protocol";
Expand Down
Expand Up @@ -80,9 +80,10 @@ public Set<ThingTypeUID> getSupportedThingTypes() {

@Override
public void onDeviceAdded(Bridge bridge, Device device) {
logger.debug("Adding new TellstickDevice! {} with id '{}' to smarthome inbox", device.getDeviceType(),
device.getId());
logger.debug("Adding new TellstickDevice! '{}' with id '{}' and type '{}' to smarthome inbox", device,
device.getId(), device.getDeviceType());
ThingUID thingUID = getThingUID(bridge, device);
logger.debug("Detected thingUID: {}", thingUID);
if (thingUID != null) {
DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withTTL(DEFAULT_TTL)
.withProperty(TellstickBindingConstants.DEVICE_ID, device.getUUId())
Expand Down Expand Up @@ -149,21 +150,25 @@ private ThingUID getThingUID(Bridge bridge, Device device) {
}

private ThingTypeUID findSensorType(Device device) {
logger.debug("Device: {}", device);
ThingTypeUID sensorThingId;
if (device instanceof TellstickSensor) {
TellstickSensor sensor = (TellstickSensor) device;
if (sensor.getData(DataType.WINDAVERAGE) != null) {
logger.debug("Sensor: {}", device);
if (sensor.getData(DataType.WINDAVERAGE) != null || sensor.getData(DataType.WINDGUST) != null
|| sensor.getData(DataType.WINDDIRECTION) != null) {
sensorThingId = TellstickBindingConstants.WINDSENSOR_THING_TYPE;
} else if (sensor.getData(DataType.RAINTOTAL) != null) {
} else if (sensor.getData(DataType.RAINTOTAL) != null || sensor.getData(DataType.RAINRATE) != null) {
sensorThingId = TellstickBindingConstants.RAINSENSOR_THING_TYPE;
} else {
sensorThingId = TellstickBindingConstants.SENSOR_THING_TYPE;
}
} else {
TellstickNetSensor sensor = (TellstickNetSensor) device;
if (sensor.isSensorOfType(LiveDataType.WINDAVERAGE)) {
if (sensor.isSensorOfType(LiveDataType.WINDAVERAGE) || sensor.isSensorOfType(LiveDataType.WINDDIRECTION)
|| sensor.isSensorOfType(LiveDataType.WINDGUST)) {
sensorThingId = TellstickBindingConstants.WINDSENSOR_THING_TYPE;
} else if (sensor.isSensorOfType(LiveDataType.RAINRATE)) {
} else if (sensor.isSensorOfType(LiveDataType.RAINRATE) || sensor.isSensorOfType(LiveDataType.RAINTOTAL)) {
sensorThingId = TellstickBindingConstants.RAINSENSOR_THING_TYPE;
} else if (sensor.isSensorOfType(LiveDataType.WATT)) {
sensorThingId = TellstickBindingConstants.POWERSENSOR_THING_TYPE;
Expand Down
Expand Up @@ -17,11 +17,21 @@
import java.math.BigDecimal;
import java.util.Calendar;

import javax.measure.quantity.Angle;
import javax.measure.quantity.Dimensionless;
import javax.measure.quantity.ElectricCurrent;
import javax.measure.quantity.Illuminance;
import javax.measure.quantity.Length;
import javax.measure.quantity.Power;
import javax.measure.quantity.Speed;
import javax.measure.quantity.Temperature;

import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.core.library.types.DateTimeType;
import org.eclipse.smarthome.core.library.types.DecimalType;
import org.eclipse.smarthome.core.library.types.PercentType;
import org.eclipse.smarthome.core.library.types.StringType;
import org.eclipse.smarthome.core.library.types.QuantityType;
import org.eclipse.smarthome.core.library.unit.SIUnits;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
Expand Down Expand Up @@ -205,11 +215,13 @@ public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {

private Device getDevice(TelldusBridgeHandler tellHandler, String deviceId) {
Device dev = null;
if (deviceId != null && isSensor()) {
dev = tellHandler.getSensor(deviceId);
} else if (deviceId != null) {
dev = tellHandler.getDevice(deviceId);
updateDeviceState(dev);
if (deviceId != null) {
if (isSensor()) {
dev = tellHandler.getSensor(deviceId);
} else {
dev = tellHandler.getDevice(deviceId);
updateDeviceState(dev);
}
}
return dev;
}
Expand Down Expand Up @@ -271,25 +283,25 @@ public void onDeviceStateChanged(Bridge bridge, Device device, TellstickEvent ev
private void updateSensorDataState(DataType dataType, String data) {
switch (dataType) {
case HUMIDITY:
updateState(humidityChannel, new DecimalType(data));
updateState(humidityChannel, new QuantityType<Dimensionless>(new BigDecimal(data), HUMIDITY_UNIT));
break;
case TEMPERATURE:
updateState(tempChannel, new DecimalType(data));
updateState(tempChannel, new QuantityType<Temperature>(new BigDecimal(data), SIUnits.CELSIUS));
break;
case RAINRATE:
updateState(rainRateChannel, new DecimalType(data));
updateState(rainRateChannel, new QuantityType<Length>(new BigDecimal(data), RAIN_UNIT));
break;
case RAINTOTAL:
updateState(raintTotChannel, new DecimalType(data));
updateState(raintTotChannel, new QuantityType<Length>(new BigDecimal(data), RAIN_UNIT));
break;
case WINDAVERAGE:
updateState(windAverageChannel, new DecimalType(data));
updateState(windAverageChannel, new QuantityType<Speed>(new BigDecimal(data), WIND_SPEED_UNIT_MS));
break;
case WINDDIRECTION:
updateState(windDirectionChannel, new StringType(data));
updateState(windDirectionChannel, new QuantityType<Angle>(new BigDecimal(data), WIND_DIRECTION_UNIT));
break;
case WINDGUST:
updateState(windGuestChannel, new DecimalType(data));
updateState(windGuestChannel, new QuantityType<Speed>(new BigDecimal(data), WIND_SPEED_UNIT_MS));
break;
default:
}
Expand All @@ -298,35 +310,41 @@ private void updateSensorDataState(DataType dataType, String data) {
private void updateSensorDataState(DataTypeValue dataType) {
switch (dataType.getName()) {
case HUMIDITY:
updateState(humidityChannel, new DecimalType(dataType.getValue()));
updateState(humidityChannel,
new QuantityType<Dimensionless>(new BigDecimal(dataType.getValue()), HUMIDITY_UNIT));
break;
case TEMPERATURE:
updateState(tempChannel, new DecimalType(dataType.getValue()));
updateState(tempChannel,
new QuantityType<Temperature>(new BigDecimal(dataType.getValue()), SIUnits.CELSIUS));
break;
case RAINRATE:
updateState(rainRateChannel, new DecimalType(dataType.getValue()));
updateState(rainRateChannel, new QuantityType<Length>(new BigDecimal(dataType.getValue()), RAIN_UNIT));
break;
case RAINTOTAL:
updateState(raintTotChannel, new DecimalType(dataType.getValue()));
updateState(raintTotChannel, new QuantityType<Length>(new BigDecimal(dataType.getValue()), RAIN_UNIT));
break;
case WINDAVERAGE:
updateState(windAverageChannel, new DecimalType(dataType.getValue()));
updateState(windAverageChannel,
new QuantityType<Speed>(new BigDecimal(dataType.getValue()), WIND_SPEED_UNIT_MS));
break;
case WINDDIRECTION:
updateState(windDirectionChannel, new StringType(dataType.getValue()));
updateState(windDirectionChannel,
new QuantityType<Angle>(new BigDecimal(dataType.getValue()), WIND_DIRECTION_UNIT));
break;
case WINDGUST:
updateState(windGuestChannel, new DecimalType(dataType.getValue()));
updateState(windGuestChannel,
new QuantityType<Speed>(new BigDecimal(dataType.getValue()), WIND_SPEED_UNIT_MS));
break;
case WATT:
if (dataType.getUnit() != null && dataType.getUnit().equals("A")) {
updateState(ampereChannel, new DecimalType(dataType.getValue()));
updateState(ampereChannel,
new QuantityType<ElectricCurrent>(new BigDecimal(dataType.getValue()), ELECTRIC_UNIT));
} else {
updateState(wattChannel, new DecimalType(dataType.getValue()));
updateState(wattChannel, new QuantityType<Power>(new BigDecimal(dataType.getValue()), POWER_UNIT));
}
break;
case LUMINATION:
updateState(luxChannel, new DecimalType(dataType.getValue()));
updateState(luxChannel, new QuantityType<Illuminance>(new DecimalType(dataType.getValue()), LUX_UNIT));
break;
default:
}
Expand Down
Expand Up @@ -22,7 +22,7 @@ public enum LiveDataType {
TEMPERATURE("temp"),
WINDAVERAGE("windaverage"),
WINDDIRECTION("winddirection"),
WINDGUST("temp"),
WINDGUST("windgust"),
RAINRATE("rainrate"),
RAINTOTAL("rainttotal"),
WATT("watt"),
Expand Down
Expand Up @@ -4,19 +4,6 @@
xsi:schemaLocation="https://openhab.org/schemas/config-description/v1.0.0
https://openhab.org/schemas/config-description-1.0.0.xsd">

<config-description uri="thing-type:tellstick:config">
<parameter name="interval" type="integer" min="1" max="86400">
<label>Interval</label>
<description>Refresh interval for positional data calculation in seconds.</description>
<default>300</default>
</parameter>
<parameter name="model" type="text">
<label>Model</label>
<description>The model used by a specific device.</description>
<required>false</required>
</parameter>
</config-description>

<config-description uri="thing-type:tellstick:sensor-config">
<parameter name="protocol" type="text">
<label>Protocol</label>
Expand Down
Expand Up @@ -49,6 +49,7 @@
</parameter>
</config-description>
</thing-type>

<thing-type id="switch">
<supported-bridge-type-refs>
<bridge-type-ref id="telldus-core" />
Expand Down Expand Up @@ -93,6 +94,7 @@
</parameter>
</config-description>
</thing-type>

<channel-type id="dimmer">
<item-type>Dimmer</item-type>
<label>Dimmer Value</label>
Expand Down

0 comments on commit e2b430b

Please sign in to comment.