Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Fix for #2249; Add handling of REFRESH command and introduce lastMoti… #2277

Merged
merged 1 commit into from
Oct 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@

<channels>
<channel id="motionDetection" typeId="motionDetection"/>
<channel id="lastMotionDetected" typeId="lastMotionDetected"/>
</channels>

<config-description>
Expand Down Expand Up @@ -144,11 +145,16 @@
<state readOnly="true"></state>
</channel-type>

<channel-type id="lastMotionDetected" advanced="true">
<item-type>DateTime</item-type>
<label>Last Activity (Date/Time)</label>
<description>Date/time when last motion was detected</description>
</channel-type>

<channel-type id="lastChangedAt" advanced="true">
<item-type>DateTime</item-type>
<label>Last Activity (Date/Time)</label>
<description>Date/time when the state last changed</description>
<category>Date</category>
</channel-type>

<channel-type id="lastOnFor" advanced="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Devices support some of the following channels:

| Channel Type ID | Item Type | Description |
|-----------------|------------------------|--------------|----------------- |------------- |
| motionDetection | Switch | On if motion is detected, off otherwise. (Motion Sensor only) |
| lastMotionDetected | DateTime | Representing the Date and Time when the last motion was detected. (Motion Sensor only) |
| state | Switch | This channel controls the actual binary State of a Device or represents Motion Detection. |
| lastChangedAt | DateTime | Representing the Date and Time the device was last turned on or of. |
| lastOnFor | Number | Time in seconds an Insight device was last turned on for. |
Expand Down Expand Up @@ -78,6 +80,7 @@ demo.items:
Switch DemoSwitch { channel="wemo:socket:Switch1:state" }
Switch LightSwitch { channel="wemo:lightswitch:Lightswitch1:state" }
Switch MotionSensor { channel="wemo:Motion:Sensor1:motionDetection" }
Switch MotionDetected { channel="wemo:Motion:Sensor1:lastMotionDetected" }
Number InsightPower { channel="wemo:insight:Insight1:currentPower" }
Number InsightLastOn { channel="wemo:insight:Insight1:lastOnFor" }
Number InsightToday { channel="wemo:insight:Insight1:onToday" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class WemoBindingConstants {
// List of all Channel ids
public final static String CHANNEL_STATE = "state";
public final static String CHANNEL_MOTIONDETECTION = "motionDetection";
public final static String CHANNEL_LASTMOTIONDETECTED = "lastMotionDetected";
public final static String CHANNEL_LASTCHANGEDAT = "lastChangedAt";
public final static String CHANNEL_LASTONFOR = "lastOnFor";
public final static String CHANNEL_ONTODAY = "onToday";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.RefreshType;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.io.transport.upnp.UpnpIOParticipant;
import org.eclipse.smarthome.io.transport.upnp.UpnpIOService;
Expand Down Expand Up @@ -152,7 +153,14 @@ public void dispose() {
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
logger.trace("Command '{}' received for channel '{}'", command, channelUID);
if (channelUID.getId().equals(CHANNEL_STATE)) {

if (command instanceof RefreshType) {
try {
updateWemoState();
} catch (Exception e) {
logger.debug("Exception during poll : {}", e);
}
} else if (channelUID.getId().equals(CHANNEL_STATE)) {
if (command instanceof OnOffType) {

try {
Expand Down Expand Up @@ -317,6 +325,10 @@ public void onValueReceived(String variable, String value, String service) {
if (state != null) {
if (getThing().getThingTypeUID().getId().equals("motion")) {
updateState(CHANNEL_MOTIONDETECTION, state);
if (state.equals(OnOffType.ON)) {
State lastMotionDetected = new DateTimeType();
updateState(CHANNEL_LASTMOTIONDETECTED, lastMotionDetected);
}
} else {
updateState(CHANNEL_STATE, state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.RefreshType;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.io.transport.upnp.UpnpIOParticipant;
import org.eclipse.smarthome.io.transport.upnp.UpnpIOService;
Expand Down Expand Up @@ -187,100 +188,110 @@ private synchronized WemoBridgeHandler getWemoBridgeHandler() {
@Override
public void handleCommand(ChannelUID channelUID, Command command) {

Configuration configuration = getConfig();
configuration.get(DEVICE_ID);
if (command instanceof RefreshType) {
try {
getDeviceState();
} catch (Exception e) {
logger.debug("Exception during poll : {}", e);
}
} else {

WemoBridgeHandler wemoBridge = getWemoBridgeHandler();
if (wemoBridge == null) {
logger.debug("wemoBridgeHandler not found, cannot handle command");
return;
}
String devUDN = "uuid:" + wemoBridge.getThing().getConfiguration().get(UDN).toString();
logger.trace("WeMo Bridge to send command to : {}", devUDN);

String value = null;
String capability = null;
switch (channelUID.getId()) {
case CHANNEL_BRIGHTNESS:
capability = "10008";
if (command instanceof PercentType) {
int newBrightness = ((PercentType) command).intValue();
logger.trace("wemoLight received Value {}", newBrightness);
int value1 = Math.round(newBrightness * 255 / 100);
value = value1 + ":0";
currentBrightness = newBrightness;
} else if (command instanceof OnOffType) {
Configuration configuration = getConfig();
configuration.get(DEVICE_ID);

WemoBridgeHandler wemoBridge = getWemoBridgeHandler();
if (wemoBridge == null) {
logger.debug("wemoBridgeHandler not found, cannot handle command");
return;
}
String devUDN = "uuid:" + wemoBridge.getThing().getConfiguration().get(UDN).toString();
logger.trace("WeMo Bridge to send command to : {}", devUDN);

String value = null;
String capability = null;
switch (channelUID.getId()) {
case CHANNEL_BRIGHTNESS:
capability = "10008";
if (command instanceof PercentType) {
int newBrightness = ((PercentType) command).intValue();
logger.trace("wemoLight received Value {}", newBrightness);
int value1 = Math.round(newBrightness * 255 / 100);
value = value1 + ":0";
currentBrightness = newBrightness;
} else if (command instanceof OnOffType) {
switch (command.toString()) {
case "ON":
value = "255:0";
break;
case "OFF":
value = "0:0";
break;
}
} else if (command instanceof IncreaseDecreaseType) {
int newBrightness;
switch (command.toString()) {
case "INCREASE":
currentBrightness = currentBrightness + DIM_STEPSIZE;
newBrightness = Math.round(currentBrightness * 255 / 100);
if (newBrightness > 255) {
newBrightness = 255;
}
value = newBrightness + ":0";
break;
case "DECREASE":
currentBrightness = currentBrightness - DIM_STEPSIZE;
newBrightness = Math.round(currentBrightness * 255 / 100);
if (newBrightness < 0) {
newBrightness = 0;
}
value = newBrightness + ":0";
break;
}
}
break;
case CHANNEL_STATE:
capability = "10006";
switch (command.toString()) {
case "ON":
value = "255:0";
value = "1";
break;
case "OFF":
value = "0:0";
value = "0";
break;
}
} else if (command instanceof IncreaseDecreaseType) {
int newBrightness;
switch (command.toString()) {
case "INCREASE":
currentBrightness = currentBrightness + DIM_STEPSIZE;
newBrightness = Math.round(currentBrightness * 255 / 100);
if (newBrightness > 255) {
newBrightness = 255;
}
value = newBrightness + ":0";
break;
case "DECREASE":
currentBrightness = currentBrightness - DIM_STEPSIZE;
newBrightness = Math.round(currentBrightness * 255 / 100);
if (newBrightness < 0) {
newBrightness = 0;
break;
}
try {
String soapHeader = "\"urn:Belkin:service:bridge:1#SetDeviceStatus\"";
String content = "<?xml version=\"1.0\"?>"
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
+ "<s:Body>" + "<u:SetDeviceStatus xmlns:u=\"urn:Belkin:service:bridge:1\">"
+ "<DeviceStatusList>"
+ "&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;DeviceStatus&gt;&lt;DeviceID&gt;"
+ wemoLightID
+ "&lt;/DeviceID&gt;&lt;IsGroupAction&gt;NO&lt;/IsGroupAction&gt;&lt;CapabilityID&gt;"
+ capability + "&lt;/CapabilityID&gt;&lt;CapabilityValue&gt;" + value
+ "&lt;/CapabilityValue&gt;&lt;/DeviceStatus&gt;" + "</DeviceStatusList>"
+ "</u:SetDeviceStatus>" + "</s:Body>" + "</s:Envelope>";

String wemoURL = getWemoURL();

if (wemoURL != null && capability != null && value != null) {
String wemoCallResponse = WemoHttpCall.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
if (capability != null && capability.equals("10008") && value != null) {
OnOffType binaryState = null;
binaryState = value.equals("0") ? OnOffType.OFF : OnOffType.ON;
if (binaryState != null) {
updateState(CHANNEL_STATE, binaryState);
}
value = newBrightness + ":0";
break;
}
}
break;
case CHANNEL_STATE:
capability = "10006";
switch (command.toString()) {
case "ON":
value = "1";
break;
case "OFF":
value = "0";
break;
}
break;
}
try {
String soapHeader = "\"urn:Belkin:service:bridge:1#SetDeviceStatus\"";
String content = "<?xml version=\"1.0\"?>"
+ "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
+ "<s:Body>" + "<u:SetDeviceStatus xmlns:u=\"urn:Belkin:service:bridge:1\">" + "<DeviceStatusList>"
+ "&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;DeviceStatus&gt;&lt;DeviceID&gt;"
+ wemoLightID + "&lt;/DeviceID&gt;&lt;IsGroupAction&gt;NO&lt;/IsGroupAction&gt;&lt;CapabilityID&gt;"
+ capability + "&lt;/CapabilityID&gt;&lt;CapabilityValue&gt;" + value
+ "&lt;/CapabilityValue&gt;&lt;/DeviceStatus&gt;" + "</DeviceStatusList>" + "</u:SetDeviceStatus>"
+ "</s:Body>" + "</s:Envelope>";

String wemoURL = getWemoURL();

if (wemoURL != null) {
String wemoCallResponse = WemoHttpCall.executeCall(wemoURL, soapHeader, content);
if (wemoCallResponse != null) {
if (capability != null && capability.equals("10008") && value != null) {
OnOffType binaryState = null;
binaryState = value.equals("0") ? OnOffType.OFF : OnOffType.ON;
if (binaryState != null) {
updateState(CHANNEL_STATE, binaryState);
}
}
}
} catch (Exception e) {
throw new RuntimeException("Could not send command to WeMo Bridge", e);
}
} catch (Exception e) {
throw new RuntimeException("Could not send command to WeMo Bridge", e);
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.RefreshType;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.io.transport.upnp.UpnpIOParticipant;
import org.eclipse.smarthome.io.transport.upnp.UpnpIOService;
Expand Down Expand Up @@ -144,7 +145,14 @@ public void dispose() {
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
logger.trace("Command '{}' received for channel '{}'", command, channelUID);
if (channelUID.getId().equals(CHANNEL_RELAY)) {

if (command instanceof RefreshType) {
try {
updateWemoState();
} catch (Exception e) {
logger.debug("Exception during poll : {}", e);
}
} else if (channelUID.getId().equals(CHANNEL_RELAY)) {
if (command instanceof OnOffType) {

try {
Expand Down