From d993fc9a1e2aab579e676e0d0aae5580e1723b10 Mon Sep 17 00:00:00 2001 From: Claudio Chimera Date: Sat, 22 May 2021 19:00:48 +0200 Subject: [PATCH 1/5] Troubleshooting, doorbell, bugfix MotionSensor --- .github/dependabot.yml | 7 + .github/workflows/npm-publish.yml | 26 + README.md | 48 + alexa/alexa-adapter.js | 64 +- alexa/alexa-device.html | 2 +- alexa/alexa-device.js | 17 +- package-lock.json | 2011 ++++++++++++++--------------- package.json | 6 +- 8 files changed, 1104 insertions(+), 1077 deletions(-) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/npm-publish.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..2c7d170 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,7 @@ +version: 2 +updates: + # Maintain dependencies for GitHub Actions + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "daily" diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml new file mode 100644 index 0000000..e6933da --- /dev/null +++ b/.github/workflows/npm-publish.yml @@ -0,0 +1,26 @@ +name: NPM Publish + +on: + release: + types: [created] + +jobs: + publish-npm: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 12 + registry-url: https://registry.npmjs.org/ + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.npm_token}} + + update-nodered: + runs-on: ubuntu-latest + needs: [publish-npm] + steps: + - uses: Hacksore/node-red-flow@v2 + with: + repo: node-red-contrib-alexa-virtual-smarthome diff --git a/README.md b/README.md index 5bf373d..da6b4fc 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ - [Setup Instructions](#setup-instructions) - [The Config node](#the-config-node) - [The Device node](#the-device-node) +- [Troubleshooting](#troubleshooting) - [Credits](#credits) - [Copyright and license](#copyright-and-license) @@ -268,6 +269,53 @@ This is the "real" device node, configure the following info: * "Display categories": the display categories. See [Display categories](https://developer.amazon.com/en-US/docs/alexa/device-apis/alexa-discovery.html#display-categories) for more info. * "Interfaces": the interfaces supported by the device. See [Interfaces](https://developer.amazon.com/en-US/docs/alexa/device-apis/list-of-interfaces.html) for more info. +## Troubleshooting + +This is a checklist for the config: + +### Check the forward rule + +* Open your browser at "https://YOUR_DOMAIN.COM/alexa/oauth", You should get the message "Wrong client id". If not, check your port forwarding the reverse proxy config. + +### Check the lambda function + +* Enable the debug log in the Node-Red Alexa node configuration. +* Open your browser at [AWS lambda](https://eu-west-1.console.aws.amazon.com/lambda/home) +* Click on the "SmartHome" function +* Click on the "Execute Test" tab +* Paste the following message on the editor: + +``` +{ + "directive": { + "header": { + "namespace": "Test", + "name": "Test", + "messageId": "", + "payloadVersion": "3" + }, + "payload": { + "grant": { + "type": "OAuth2.AuthorizationCode", + "code": "Test" + }, + "grantee": { + "type": "BearerToken", + "token": "Test" + } + } + } +} +``` +* Click on "Execute test" button +* Click on detail, You should see the following response: +``` +{ + "ok": "ok" +} +``` + + ## Credits Parts of this README and large parts of the code comes from Amazon guide. diff --git a/alexa/alexa-adapter.js b/alexa/alexa-adapter.js index df66f83..24ba06c 100644 --- a/alexa/alexa-adapter.js +++ b/alexa/alexa-adapter.js @@ -854,7 +854,10 @@ module.exports = function (RED) { .then(pres => { if (node.config.verbose) node._debug("get_user_profile CCHI res " + JSON.stringify(pres)); if (node.config.emails.includes(pres.email)) { - if (node.config.verbose) node._debug(pres.email + ' OK'); + if (node.config.verbose) { + node._debug(pres.email + ' OK'); + node.error("Username " + pres.email + " authorized"); + } node.auth_code = { code: node.tokgen.generate(), expire_at: Date.now() + 60 * 2 * 1000 @@ -863,6 +866,7 @@ module.exports = function (RED) { node.redirect_to_amazon(ores, node.auth_code.code); } else { if (node.config.verbose) node._debug(pres.email + ' NOK'); + node.error("Username " + pres.email + " not authorized"); node.redirect_to_login_page(ores, true); } }) @@ -944,13 +948,54 @@ module.exports = function (RED) { // // // - report_state(endpointId) { - // https://developer.amazon.com/en-US/docs/alexa/smarthome/state-reporting-for-a-smart-home-skill.html - // https://developer.amazon.com/en-US/docs/alexa/smarthome/send-events-to-the-alexa-event-gateway.html + send_doorbell_press(cause) { + // https://developer.amazon.com/en-US/docs/alexa/device-apis/alexa-doorbelleventsource.html var node = this; - if (node.config.verbose) node._debug('report_state ' + endpointId); - const state = node.get_change_report(endpointId); - if (node.config.verbose) node._debug('report_state ' + JSON.stringify(state)); + if (node.config.verbose) node._debug('send_doorbell_press' + cause); + + node.get_access_token('evn') + .then(access_token => { + const state = { + context: {}, + event: { + header: { + namespace: "Alexa.DoorbellEventSource", + name: "DoorbellPress", + messageId: node.tokgen.generate(), + payloadVersion: "3" + }, + endpoint: { + scope: { + type: "BearerToken", + token: access_token + }, + endpointId: "appliance-001" + }, + payload: { + cause: { + type: cause || "PHYSICAL_INTERACTION" + }, + timestamp: new Date().toISOString() + } + }, + }; + if (node.config.verbose) node._debug('send_doorbell_press ' + JSON.stringify(state)); + superagent + .post(node.config.event_endpoint) + .set('Authorization', 'Bearer ' + access_token) + .send(state) + .end((err, res) => { + if (err) { + node.error('send_doorbell_press err ' + JSON.stringify(err)); + } else { + if (node.config.verbose) node._debug('send_doorbell_press res ' + JSON.stringify(res)); + } + }); + if (node.config.verbose) node._debug('send_doorbell_press sent'); + }) + .catch(err => { + node.error('send_doorbell_press get_access_token err ' + JSON.stringify(err)); + }) } // @@ -985,11 +1030,11 @@ module.exports = function (RED) { if (node.config.verbose) node._debug('send_change_report res ' + JSON.stringify(res)); } }); + if (node.config.verbose) node._debug('send_change_report sent'); }) .catch(err => { node.error('send_change_report get_access_token err ' + JSON.stringify(err)); }) - if (node.config.verbose) node._debug('send_change_report sent'); } // @@ -1107,6 +1152,7 @@ module.exports = function (RED) { } const oauth2_bearer_token = node.tokens.evn.access_token; const properties = node.devices[endpointId].getProperties(); + if (node.config.verbose) node._debug('endpointId ' + endpointId +' properties ' + JSON.stringify(properties)); if (changed_propertie_names && changed_propertie_names.length > 0) { properties.forEach(property => { if (changed_propertie_names && changed_propertie_names.includes(property.name)) { @@ -1186,7 +1232,7 @@ module.exports = function (RED) { if (node.config.verbose) node._debug("CCHI state " + JSON.stringify(state)); } } else { - // TODO all states + // TODO all states?? } } diff --git a/alexa/alexa-device.html b/alexa/alexa-device.html index 41f0968..a3df24d 100644 --- a/alexa/alexa-device.html +++ b/alexa/alexa-device.html @@ -186,7 +186,7 @@ -