Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add msg.topic pull to force update from SmartThings #52

Merged
merged 1 commit into from Jun 20, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -114,8 +114,9 @@ Device changes are received through the webhook you set up at SmartThings Develo
Portal.

All device nodes can receive at its input a message with the ```msg.topic``` of
**update** to force the output of the current device state. This is useful when
**update** to force the output of the current saved device state. This is useful when
handling a request, for example.
If you want to pull the current status from SmartThings, you can send a message with the ```msg.topic``` of **pull**;

Some devices can have their state changed. You can turn on a light, change
level and color, or, open your door.
Expand Down
33 changes: 20 additions & 13 deletions smartthings/smartthings-battery.js
Expand Up @@ -48,19 +48,7 @@ module.exports = function(RED) {
this.reportState(send ,done);
};

if(this.conf && this.device){
const callback = (evt) => {
console.debug("BatteryDevice("+this.name+") Callback called");
console.debug(evt);
if(evt["name"] == "battery"){
this.setState({
value: parseFloat(evt["value"])
});
}
}

this.conf.registerCallback(this, this.device, callback);

this.pullState = function(){
this.conf.getDeviceStatus(this.device,"main/capabilities/battery").then( (status) => {
console.debug("BatteryDevice("+this.name+") Status Refreshed");
console.debug(status);
Expand All @@ -74,6 +62,21 @@ module.exports = function(RED) {
console.error("Ops... error getting device state (BatteryDevice)");
console.error(err);
});
};

if(this.conf && this.device){
const callback = (evt) => {
console.debug("BatteryDevice("+this.name+") Callback called");
console.debug(evt);
if(evt["name"] == "battery"){
this.setState({
value: parseFloat(evt["value"])
});
}
}

this.conf.registerCallback(this, this.device, callback);
this.pullState();

this.on('input', (msg, send, done) => {
send = send || function() { node.send.apply(node,arguments) };
Expand All @@ -84,6 +87,10 @@ module.exports = function(RED) {

if(msg && msg.topic !== undefined){
switch(msg.topic){
case "pull":
this.pullState();
break;

case "update":
this.reportState(send, done, msg);
break;
Expand Down
57 changes: 32 additions & 25 deletions smartthings/smartthings-color-temperature.js
Expand Up @@ -69,6 +69,33 @@ module.exports = function(RED) {
this.reportState(send, done);
};

this.pullState = function(){
this.conf.getDeviceStatus(this.device,"main").then( (status) => {
console.debug("ColorTemperatureDevice("+this.name+") Status Refreshed");

let state = {};

if(status["switch"] !== undefined && status["switch"]["switch"] !== undefined){
state.value = (status["switch"]["switch"]["value"].toLowerCase() === "on" ? 1 : 0);
}

if(status["switchLevel"] !== undefined && status["switchLevel"]["level"] !== undefined){
state.level = status["switchLevel"]["level"]["value"];
state.levelUnit = status["switchLevel"]["level"]["unit"];
}

if(status["colorTemperature"] !== undefined){
state.temperature = status["colorTemperature"]["colorTemperature"]["value"];
state.temperatureUnit = status["colorTemperature"]["colorTemperature"]["unit"];
}

this.setState(state);
}).catch( err => {
console.error("Ops... error getting device state (ColorTemperatureDevice)");
console.error(err);
});
};

if(this.conf && this.device){
const callback = (evt) => {
console.debug("ColorTemperatureDevice("+this.name+") Callback called");
Expand All @@ -95,31 +122,7 @@ module.exports = function(RED) {
}

this.conf.registerCallback(this, this.device, callback);

this.conf.getDeviceStatus(this.device,"main").then( (status) => {
console.debug("ColorTemperatureDevice("+this.name+") Status Refreshed");

let state = {};

if(status["switch"] !== undefined && status["switch"]["switch"] !== undefined){
state.value = (status["switch"]["switch"]["value"].toLowerCase() === "on" ? 1 : 0);
}

if(status["switchLevel"] !== undefined && status["switchLevel"]["level"] !== undefined){
state.level = status["switchLevel"]["level"]["value"];
state.levelUnit = status["switchLevel"]["level"]["unit"];
}

if(status["colorTemperature"] !== undefined){
state.temperature = status["colorTemperature"]["colorTemperature"]["value"];
state.temperatureUnit = status["colorTemperature"]["colorTemperature"]["unit"];
}

this.setState(state);
}).catch( err => {
console.error("Ops... error getting device state (ColorTemperatureDevice)");
console.error(err);
});
this.pullState();

this.on('input', (msg, send, done) => {
send = send || function() { node.send.apply(node,arguments) };
Expand All @@ -130,6 +133,10 @@ module.exports = function(RED) {

if(msg && msg.topic !== undefined){
switch(msg.topic){
case "pull":
this.pullState();
break;

case "update":
this.reportState(send, done, msg);
break;
Expand Down
67 changes: 37 additions & 30 deletions smartthings/smartthings-color.js
Expand Up @@ -158,6 +158,38 @@ module.exports = function(RED) {
this.reportState(send ,done);
};

this.pullState = function(){
this.conf.getDeviceStatus(this.device,"main").then( (status) => {
console.debug("ColorDevice("+this.name+") Status Refreshed");

let state = {};

if(status["switch"] !== undefined && status["switch"]["switch"] !== undefined){
state.value = (status["switch"]["switch"]["value"].toLowerCase() === "on" ? 1 : 0);
}

if(status["switchLevel"] !== undefined && status["switchLevel"]["level"] !== undefined){
state.level = status["switchLevel"]["level"]["value"];
state.levelUnit = status["switchLevel"]["level"]["unit"];
}

if(status["colorControl"] !== undefined){
state.hue = status["colorControl"]["hue"]["value"];
state.saturation = status["colorControl"]["saturation"]["value"];
}

if(status["colorTemperature"] !== undefined){
state.temperature = status["colorTemperature"]["colorTemperature"]["value"];
state.temperatureUnit = status["colorTemperature"]["colorTemperature"]["unit"];
}

this.setState(state);
}).catch( err => {
console.error("Ops... error getting device state (ColorDevice)");
console.error(err);
});
};

if(this.conf && this.device){
const callback = (evt) => {
console.debug("ColorDevice("+this.name+") Callback called");
Expand Down Expand Up @@ -194,36 +226,7 @@ module.exports = function(RED) {
}

this.conf.registerCallback(this, this.device, callback);

this.conf.getDeviceStatus(this.device,"main").then( (status) => {
console.debug("ColorDevice("+this.name+") Status Refreshed");

let state = {};

if(status["switch"] !== undefined && status["switch"]["switch"] !== undefined){
state.value = (status["switch"]["switch"]["value"].toLowerCase() === "on" ? 1 : 0);
}

if(status["switchLevel"] !== undefined && status["switchLevel"]["level"] !== undefined){
state.level = status["switchLevel"]["level"]["value"];
state.levelUnit = status["switchLevel"]["level"]["unit"];
}

if(status["colorControl"] !== undefined){
state.hue = status["colorControl"]["hue"]["value"];
state.saturation = status["colorControl"]["saturation"]["value"];
}

if(status["colorTemperature"] !== undefined){
state.temperature = status["colorTemperature"]["colorTemperature"]["value"];
state.temperatureUnit = status["colorTemperature"]["colorTemperature"]["unit"];
}

this.setState(state);
}).catch( err => {
console.error("Ops... error getting device state (ColorDevice)");
console.error(err);
});
this.pullState();

this.on('input', (msg, send, done) => {
send = send || function() { node.send.apply(node,arguments) };
Expand All @@ -233,6 +236,10 @@ module.exports = function(RED) {

if(msg && msg.topic !== undefined){
switch(msg.topic){
case "pull":
this.pullState();
break;

case "update":
this.reportState(send, done, msg);
break;
Expand Down
33 changes: 20 additions & 13 deletions smartthings/smartthings-humidity.js
Expand Up @@ -47,19 +47,7 @@ module.exports = function(RED) {
this.reportState(send, done);
};

if(this.conf && this.device){
const callback = (evt) => {
console.debug("HumidityDevice("+this.name+") Callback called");
console.debug(evt);
if(evt["name"] == "humidity"){
this.setState({
value: parseFloat(evt["value"])
});
}
}

this.conf.registerCallback(this, this.device, callback);

this.pullState = function(){
this.conf.getDeviceStatus(this.device,"main/capabilities/relativeHumidityMeasurement").then( (status) => {
console.debug("HumidityDevice("+this.name+") Status Refreshed");
console.debug(status);
Expand All @@ -73,6 +61,21 @@ module.exports = function(RED) {
console.error("Ops... error getting device state (HumidityDevice)");
console.error(err);
});
};

if(this.conf && this.device){
const callback = (evt) => {
console.debug("HumidityDevice("+this.name+") Callback called");
console.debug(evt);
if(evt["name"] == "humidity"){
this.setState({
value: parseFloat(evt["value"])
});
}
}

this.conf.registerCallback(this, this.device, callback);
this.pullState();

this.on('input', (msg, send, done) => {
send = send || function() { node.send.apply(node,arguments) };
Expand All @@ -82,6 +85,10 @@ module.exports = function(RED) {

if(msg && msg.topic !== undefined){
switch(msg.topic){
case "pull":
this.pullState();
break;

case "update":
this.reportState(send, done, msg);
break;
Expand Down
29 changes: 18 additions & 11 deletions smartthings/smartthings-illuminance.js
Expand Up @@ -46,17 +46,7 @@ module.exports = function(RED) {
this.reportStatus(send, done);
}

if(this.conf && this.device){
const callback = (evt) => {
console.debug("IlluminanceDevice("+this.name+") Callback called");
console.debug(evt);
if(evt["name"] == "illuminance"){
this.updateStatus(parseFloat(evt["value"]),evt["unit"]);
}
}

this.conf.registerCallback(this, this.device, callback);

this.pullStatus = function(){
this.conf.getDeviceStatus(this.device,"main/capabilities/illuminanceMeasurement").then( (status) => {
console.debug("IlluminanceDevice("+this.name+") Status Refreshed");
console.debug(status);
Expand All @@ -70,6 +60,19 @@ module.exports = function(RED) {
console.error("Ops... error getting device state (IlluminanceDevice)");
console.error(err);
});
}

if(this.conf && this.device){
const callback = (evt) => {
console.debug("IlluminanceDevice("+this.name+") Callback called");
console.debug(evt);
if(evt["name"] == "illuminance"){
this.updateStatus(parseFloat(evt["value"]),evt["unit"]);
}
}

this.conf.registerCallback(this, this.device, callback);
this.pullState();

this.on('input', (msg, send, done) => {
send = send || function() { node.send.apply(node,arguments) };
Expand All @@ -79,6 +82,10 @@ module.exports = function(RED) {

if(msg && msg.topic !== undefined){
switch(msg.topic){
case "pull":
this.pullStatus();
break;

case "update":
this.reportStatus(send, done, msg);
break;
Expand Down