Skip to content

Commit 07ee781

Browse files
committed
feat(device): Add support for retrieavable state reporting; fix #77
1 parent 2560d79 commit 07ee781

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

connection.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,50 @@ module.exports = function (RED) {
269269
})
270270
}
271271

272+
this.handleReportState = function (deviceId, directiveRequest) {
273+
// EXAMPLE directiveRequest:
274+
// {
275+
// directive: {
276+
// header: {
277+
// namespace: 'Alexa',
278+
// name: 'ReportState',
279+
// payloadVersion: '3',
280+
// correlationToken: 'AAAAAAAAAQAwOfXmbhm...',
281+
// },
282+
// endpoint: {
283+
// endpointId: 'vshd-xxxxxxxxxxxxx',
284+
// },
285+
// payload: {},
286+
// },
287+
// }
288+
289+
const currentState = this.execCallbackForOne(deviceId, 'getLocalState')
290+
291+
if (!currentState) {
292+
this.logger(
293+
`no local state found for device ID ${deviceId}`,
294+
null,
295+
'warn'
296+
)
297+
return
298+
}
299+
300+
const currentProperties = buildPropertiesFromState(currentState).map(
301+
(prop) => {
302+
prop['changed'] = false
303+
return prop
304+
}
305+
)
306+
307+
this.triggerChangeReport({
308+
endpointId: deviceId,
309+
properties: currentProperties,
310+
causeType: 'STATE_REPORT',
311+
correlationToken: directiveRequest.directive.header.correlationToken,
312+
useRateLimiter: false,
313+
})
314+
}
315+
272316
this.handleDirectiveFromAlexa = function (deviceId, directiveRequest) {
273317
// EXAMPLE directiveRequest:
274318
// {
@@ -558,7 +602,11 @@ module.exports = function (RED) {
558602
// this.handleUpdateFromAlexa(deviceId, message)
559603
// } else
560604
if (topic.includes('/directive')) {
561-
this.handleDirectiveFromAlexa(deviceId, message)
605+
if (message.directive.header.name == 'ReportState') {
606+
this.handleReportState(deviceId, message)
607+
} else {
608+
this.handleDirectiveFromAlexa(deviceId, message)
609+
}
562610
} else {
563611
this.logger(
564612
'received device-related message that is not supported yet!',

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ __THANK YOU!__
157157

158158
1. Q: Can I run my own backend?
159159

160-
A: Yes! Check out the [backend repository](https://github.com/csuermann/virtual-smart-home/) and follow the instructions precisely.
160+
A: Yes! Check out the [backend repository](https://github.com/csuermann/virtual-smart-home/) and follow the instructions precisely. Keep in mind that it might be cheaper to keep using the official backend and donate some money so that I can keep the system up for you.
161161

162162
1. Q: Can you add feature X, please?
163163

0 commit comments

Comments
 (0)