Skip to content

Commit

Permalink
Merge pull request #58 from radionoise/feature/no-response-improved
Browse files Browse the repository at this point in the history
Disables accessory control when NO_RESPONSE was triggered
  • Loading branch information
Shaquu authored Mar 11, 2019
2 parents bb30b3d + 0ecc98b commit 3f53e08
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ Context info can be provided as part of the input message and will be available
}
```

## No Response

You can set accessory "No Response" status by sending "NO_RESPONSE" as a value for any available characteristic.

**Example**:

```json
{
"On": "NO_RESPONSE"
}
```

After "No Response" status was triggered, the accessory is marked accordingly when you try to control it or reopen Home.app.
Any subsequent update of any characteristic value will reset this status.

## FAQ

#### How can I generate Debug logs?
Expand Down
2 changes: 1 addition & 1 deletion lib/HAPServiceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ module.exports = function(RED) {
service.on("characteristic-change", ServiceUtils.onCharacteristicChange);

// Which characteristics are supported?
this.supported = CharacteristicUtils.getSupported(service);
this.supported = CharacteristicUtils.getSupportedAndSubscribeSet(service);

// Respond to inputs
this.on("input", ServiceUtils.onInput);
Expand Down
18 changes: 16 additions & 2 deletions lib/utils/CharacteristicUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = function(node) {
return characteristicProperties;
};

const getSupported = function (service) {
const getSupportedAndSubscribeSet = function (service) {
const supported = {read: [], write: []};

const allCharacteristics = service.characteristics.concat(
Expand All @@ -40,6 +40,15 @@ module.exports = function(node) {
.replace(/\./g, "_");
if (characteristic.props.perms.indexOf("pw") > -1) {
supported.read.push(cKey);

// Subscribe to 'get' event of readable characteristic
characteristic.on("get", function(callback, context) {
if (node.accessory.reachable === true) {
callback(null, characteristic.value);
} else {
callback("no response", null);
}
});
}

if (
Expand All @@ -48,6 +57,11 @@ module.exports = function(node) {
-2
) {
supported.write.push(cKey);

// Subscribe to 'set' event of writable characteristic
characteristic.on("set", function(newVal, callback) {
callback(node.accessory.reachable === true ? null : "no response");
});
}

//Allow for negative temperatures
Expand All @@ -61,6 +75,6 @@ module.exports = function(node) {

return {
load: load,
getSupported: getSupported
getSupportedAndSubscribeSet: getSupportedAndSubscribeSet
};
};
3 changes: 3 additions & 0 deletions lib/utils/ServiceUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,14 @@ module.exports = function(node) {
const noResponseMsg = "NO_RESPONSE";

if (msg.payload[key] === noResponseMsg) {
node.accessory.updateReachability(false);
characteristic.setValue(new Error(noResponseMsg));

return;
}

node.accessory.updateReachability(true);

if (context !== null) {
characteristic.setValue(msg.payload[key], undefined, context);
} else {
Expand Down

0 comments on commit 3f53e08

Please sign in to comment.