Skip to content

Commit

Permalink
Major code refactoring
Browse files Browse the repository at this point in the history
* introduced new item configuration syntax using device metadata concept
* added new device capabilities for camera, door/window covering, fan, networking and vacuum
* improved security arm state error handling
* added input custom mappings support
* added playback stop and thermostat hold/fan capabilities support
* added new custom asset ids and semantic extensions
* added switch support to mode generic capability
* improved dimmer/rollershutter support for range value generic capability
* updated test suite increasing code coverage
* added support for ask-cli v2 using cloudformation deployer
* added github action to update repository locales on crowdin changes
* deprecated item sensor concept for decouple state support on specific capabilities
* removed item tag support for oh3.x
* removed alexa smarthome api v2 support

Signed-off-by: jsetton <jeremy.setton@gmail.com>
  • Loading branch information
jsetton committed Oct 24, 2021
1 parent 7472c94 commit 8bc9fe0
Show file tree
Hide file tree
Showing 470 changed files with 50,554 additions and 19,097 deletions.
9 changes: 0 additions & 9 deletions .ask/config

This file was deleted.

30 changes: 0 additions & 30 deletions .github/workflows/build.yml

This file was deleted.

57 changes: 57 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

defaults:
run:
working-directory: lambda

jobs:
test:
name: Testing
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x, 14.x]

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm ci

- name: Run tests
run: npm run test:coverage

- name: Upload coverage report
uses: actions/upload-artifact@v2
with:
name: coverage-report
path: lambda/coverage

codeql:
name: Code Analysis
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: javascript

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
35 changes: 35 additions & 0 deletions .github/workflows/locale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: locale

on:
push:
branches: [ main ]
paths: [ resources/locales/** ]

jobs:
locale:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '12.x'

- name: Update skill locale
run: node tools/updateSkillLocale.js

- name: Check for changes to commit
id: changes
run: test -n "$(git status -suno)"
continue-on-error: true

- name: Commit and push changes
if: steps.changes.outcome == 'success'
run: |
git config user.name openhab-bot
git config user.email bot@openhab.org
git commit -a -m "New locale updates"
git push
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/lambda/*/catalog.json
/lambda/*/config.js
ssl/
.ask
lambda/ssl/
skill-package/accountLinking.json

# VS Code
.vscode

# Logs
Expand Down
166 changes: 98 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Amazon Alexa Smart Home skill for openHAB 2
# openHAB Skill for Amazon Alexa

[![Build Status](https://github.com/openhab/openhab-alexa/workflows/build/badge.svg?branch=main)](https://github.com/openhab/openhab-alexa/actions?query=workflow%3Abuild)
[![CI](https://github.com/openhab/openhab-alexa/actions/workflows/ci.yml/badge.svg)](https://github.com/openhab/openhab-alexa/actions/workflows/ci.yml)
[![Crowdin](https://badges.crowdin.net/openhab-alexa/localized.svg)](https://crowdin.com/project/openhab-alexa)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)

This is a nodejs / lambda application that connects the Alexa Smart Home API to a user's openHAB instance, either directly or through the openHAB Cloud service (preferred). The Smart Home API is not a general skill API, it allows the user to bypass using a application wake work and instead ask Alexa to perform a smart home action like "Alexa turn lights on"

Expand All @@ -13,7 +14,7 @@ This document describes how to configure and deploy the skill for development or

## Skill Usage

For end-user documentation and general usage, see the [Usage](USAGE.md) page for examples and instructions on configuring items for Alexa within openHAB.
For end-user documentation and general usage, see the [Usage](USAGE.md) page for examples and instructions on configuring items for Amazon Alexa within openHAB.

## Requirements

Expand All @@ -23,30 +24,22 @@ You need an [AWS account](https://aws.amazon.com) and an [Amazon developer accou

In order to use the ASK CLI features to automatically deploy and manage your Lambda skill, ensure that you have AWS credentials set up with the appropriate permissions on the computer you are installing ASK CLI, as described in [this documentation](https://developer.amazon.com/docs/smapi/manage-credentials-with-ask-cli.html).

You will have to install the latest [ASK CLI](https://developer.amazon.com/docs/smapi/quick-start-alexa-skills-kit-command-line-interface.html) v1.x, and then initialize it:
You will have to install the latest [ASK CLI](https://developer.amazon.com/docs/smapi/quick-start-alexa-skills-kit-command-line-interface.html), and then configure it:
```
$ npm install -g ask-cli@^1.0.0
$ ask init
$ npm install -g ask-cli
$ ask configure
```

By default, the ASK CLI deploys the lambda function in the `us-east-1` region. You will need to change your [default region](https://docs.aws.amazon.com/general/latest/gr/rande.html#lambda_region) based on the skill language you are planning to use. You should refer to the table below, based on the [smart home multi-languages development guidelines](https://developer.amazon.com/docs/smarthome/develop-smart-home-skills-in-multiple-languages.html#deploy):
By default, the ASK CLI deploys the skill resources in the `us-east-1` region. You will need to change your deploy region based on the skill language you are planning to use. You should refer to the table below, based on the [smart home multi-languages development guidelines](https://developer.amazon.com/docs/smarthome/develop-smart-home-skills-in-multiple-languages.html#deploy):

| Skill Language | Endpoint Region | Lambda Region |
| Skill Language | Endpoint Region | Deploy Region |
| -------------- | --------------- | ------------- |
| English (CA), English (US), French (CA), Portuguese (BR),<br>Spanish (MX), Spanish (US) | North America | us-east-1 |
| English (UK), French (FR), German, Italian, Spanish (ES) | Europe | eu-west-1 |
| English (IN), Hindi (IN) | India | eu-west-1 |
| English (AU), Japanese | Far East | us-west-2 |
| English (CA), English (US), French (CA), Portuguese (BR),<br>Spanish (MX), Spanish (US) | North America | `us-east-1` |
| English (UK), French (FR), German, Italian, Spanish (ES) | Europe | `eu-west-1` |
| English (IN), Hindi (IN) | India | `eu-west-1` |
| English (AU), Japanese | Far East | `us-west-2` |

To change your default region, you will need to add the `region` parameter to the AWS credentials file located in your home directory, under the AWS profile name associated during the ASK CLI profile initialization. The default profile name is `ask_cli_default`.

```
$ cat ~/.aws/credentials
[ask_cli_default]
aws_access_key_id=<accessKeyId>
aws_secret_access_key=<secretAccessKey>
region=us-east-1
```
To change your deploy region, update the `awsRegion` skill infrastructure user config parameter in [`ask-resources.json`](ask-resources.json).

### OAuth2 Provider

Expand All @@ -64,15 +57,15 @@ $ mongo
And use the following settings during the skill account linking deployment step instead:

* Authorization URL: `https://openhab.myserver.com/oauth2/authorize`
* Access Token URL: `https://openhab.myserver.com/oauth2/token`
* Client ID: `alexa-skill`
* Scope: `alexa`
* Access Token URI: `https://openhab.myserver.com/oauth2/token`

### openHAB Server

The openHAB server you are trying to control with the skill needs to be accessible online as an AWS service endpoint. You can either use [myopenHAB.org](http://myopenHAB.org) cloud service or point the skill directly to your server URL. If going with the latter, make sure to use a valid SSL certificate. It is highly recommended to use [Let's Encrypt](https://letsencrypt.org) to validate your certificates.

To configure the server path and credentials, you will need to setup the application configuration in `lambda/smarthome/config.js` using the sample file. Set the server rest path in the `baseURL` property if not using myopenHAB.org cloud service. The app can access an openHAB installation using three different types of authentication: SSL client certificate, basic (user/password) or bearer (OAuth2 token). For certificate authentication, place the client certificate in `lambda/smarthome/ssl/client.pfx` or set the `certFile` property with the certificate path, and optionally, set the `certPass` property with the certificate passphrase. For basic authentication, set the `user` and `pass` properties, otherwise a bearer token will be used.
To configure the server path and credentials, you will need to setup your backend configuration in [`lambda/config.js`](lambda/config.js). Set your server path in the `baseURL` property if not using myopenHAB.org cloud service. The Lambda function can access an openHAB server using three different types of authentication: SSL client certificate, basic (user/password) or bearer (OAuth2 token). For certificate authentication, place the client certificate in `lambda/ssl/client.pfx` or set the `certFile` property with the certificate path, and optionally, set the `certPass` property with the certificate passphrase. For basic authentication, set the `user` and `pass` properties, otherwise a bearer token will be used.

## Deployment Steps

Expand All @@ -86,36 +79,54 @@ To configure the server path and credentials, you will need to setup the applica
2. Deploy the skill and the lambda function in one step:
```
$ ask deploy
Profile for the deployment: [default]
-------------------- Create Skill Project --------------------
Skill Id: <skillId>
Skill deployment finished.
[Warn]: No runtime and handler settings found for alexaUsage "smartHome/default" when creating Lambda function. CLI will use "nodejs10.x" and "index.handler" as the Runtime and Handler to create Lambda. You can update the runtime and handler for the target Lambda in the project config and deploy again if you want to set differently.
Lambda deployment finished.
Lambda function(s) created:
[Lambda ARN] <lambdaArn>
[Info]: No in-skill product to be deployed.
[Warn]: Skill api domain "smartHome" can not be enabled. Skipping the enablement.
```
Deploy configuration loaded from ask-resources.json
Deploy project for profile [default]
3. Setup skill account linking using the skill id displayed in previous step and your OAuth2 provider configuration:
```
$ ask api create-account-linking -s <skillId>
? Authorization URL: https://www.amazon.com/ap/oa
? Client ID: <clientId>
? Scopes(separate by comma): profile
? Domains(separate by comma):
? Authorization Grant Type: AUTH_CODE
? Access Token URI: https://api.amazon.com/auth/o2/token
? Client Secret: [hidden]
? Client Authentication Scheme: HTTP_BASIC
? Optional* Default Access Token Expiration Time In Seconds:
? Optional* Reciprocal Access Token Url:
? Optional* RedirectUrls for App-to-App Account Linking (separated by comma):
Account linking created successfully.
==================== Deploy Skill Metadata ====================
Skill package deployed successfully.
Skill ID: <skillId>
==================== Build Skill Code ====================
Skill code built successfully.
Code for region default built to <skillPath>/.ask/lambda/build.zip successfully with build flow NodeJsNpmBuildFlow.
==================== Deploy Skill Infrastructure ====================
✔ Deploy Alexa skill infrastructure for region "default"
The api endpoints of skill.json have been updated from the skill infrastructure deploy results.
Skill infrastructures deployed successfully through @ask-cli/cfn-deployer.
==================== Enable Skill ====================
[Warn]: CliWarn: Skill api domain "smartHome" cannot be enabled. Skip the enable process.
```

4. Enable skill with account linking:
3. Setup the skill account linking using the `skillId` displayed in previous step and your OAuth2 provider configuration:
1. Create skill account linking request file:
```
$ cat skill-package/accountLinking.json
{
"accountLinkingRequest": {
"skipOnEnablement": "false",
"type": "AUTH_CODE",
"authorizationUrl": "https://www.amazon.com/ap/oa",
"accessTokenUrl": "https://api.amazon.com/auth/o2/token",
"accessTokenScheme": "HTTP_BASIC"
"clientId": "<clientId>",
"clientSecret": "<clientSecret>",
"scopes": [
"profile"
],
}
}
```

2. Update the skill account linking information:
```
$ ask smapi update-account-linking-info -s <skillId> --account-linking-request file:skill-package/accountLinking.json
Command executed successfully!
```

4. Enable the skill on your Alexa account:
* Go to your [Alexa skill console](https://alexa.amazon.com/spa/index.html#skills/your-skills/?ref-suffix=ysa_gw)
* Click on the "openHAB" skill under the "Dev Skills" tab
* Click "Enable" and go through the account linking process
Expand All @@ -127,33 +138,52 @@ To configure the server path and credentials, you will need to setup the applica
$ git pull
```

2. If updating from v2 version:
1. Copy relevant settings from `config.js` (v2) to `lambda/smarthome/config.js` (v3).
2. If updating from ASK CLI v1:
1. Copy relevant settings from `lambda/smarthome/config.js` (ask-cli-v1) to [`lambda/config.js`](lambda/config.js) (ask-cli-v2). If configuring `baseURL`, be aware that it should now point to your server root level and not the rest endpoint. Additionally, move your client certificate to `lambda/ssl` if using that authentication method.

2. Add current skill id to ask config by using the skill id listed under your [Alexa developer console](https://developer.amazon.com/alexa/console/ask).
2. Create the `.ask` hidden directory and `ask-states.json` states file adding the Skill ID listed under your [Alexa developer console](https://developer.amazon.com/alexa/console/ask). This will prevent duplicate skills from being created under your account.
```
$ cat .ask/config
$ cat .ask/ask-states.json
{
"deploy_settings": {
"askcliStatesVersion": "2020-03-31",
"profiles": {
"default": {
"skill_id": "<skillId>",
"was_cloned": false,
"merge": {}
"skillId": "<skillId>"
}
}
}
```

3. Delete existing function and logs using the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html), or via your [AWS Console](https://console.aws.amazon.com) to prevent conflicts with the CloudFormation stack deployment.
```
$ aws lambda delete-function --function-name alexa-openhab
$ aws logs delete-log-group --log-group-name /aws/lambda/alexa-openhab
```

4. Remove old folder environment.
```
$ rm -rf lambda/smarthome
```

3. Deploy the skill and the lambda function in one step:
```
$ ask deploy [--force] (Force deployment if lambda manual configs were applied)
Profile for the deployment: [default]
-------------------- Update Skill Project --------------------
Skill Id: <skillId>
Skill deployment finished.
Lambda deployment finished.
Lambda function(s) updated:
[Lambda ARN] <lambdaArn>
[Info]: No in-skill product to be deployed.
[Warn]: Skill api domain "smartHome" can not be enabled. Skipping the enablement.
$ ask deploy
Deploy configuration loaded from ask-resources.json
Deploy project for profile [default]
==================== Deploy Skill Metadata ====================
Skill package deployed successfully.
Skill ID: <skillId>
==================== Build Skill Code ====================
Skill code built successfully.
Code for region default built to <skillPath>/.ask/lambda/build.zip successfully with build flow NodeJsNpmBuildFlow.
==================== Deploy Skill Infrastructure ====================
✔ Deploy Alexa skill infrastructure for region "default"
The api endpoints of skill.json have been updated from the skill infrastructure deploy results.
Skill infrastructures deployed successfully through @ask-cli/cfn-deployer.
==================== Enable Skill ====================
[Warn]: CliWarn: Skill api domain "smartHome" cannot be enabled. Skip the enable process.
```
Loading

0 comments on commit 8bc9fe0

Please sign in to comment.