Retry 11 Feb 2024 tawfik.daim@outlook.com 192.168.1.13 with dyararco.duckdns.org
Alexa Skills -> Create skill (e.g. Alexa13). SmartHome -> provision your own
AWS; Create role, AWS Service -> Lambda -> add 'AWSLambdaBasicExecutionRole' name it: AWSLambdaBasicExecutionRole-SmartHome
Create Lambda function (N. Virginia), Functions, Create Function, author from scratch - > function name (python 3.9 as run time) -> Expand default execution role and selected an existing role (AWSLambdaBasicExecutionRole-SmartHome)
Add trigger -> click Alexa Smart Home (get skill ID from Developer console) amzn1.ask.skill.f821d9fc-7788-4097-ae6b-98004f737c04
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import json
import logging
import urllib3
_debug = bool(os.environ.get('DEBUG'))
_logger = logging.getLogger('HomeAssistant-SmartHome')
_logger.setLevel(logging.DEBUG if _debug else logging.INFO)
_logger.debug('Event: %s', event)
base_url = os.environ.get('BASE_URL')
assert base_url is not None, 'Please set BASE_URL environment variable'
base_url = base_url.strip("/")
directive = event.get('directive')
assert directive is not None, 'Malformatted request - missing directive'
assert directive.get('header', {}).get('payloadVersion') == '3', \
'Only support payloadVersion == 3'
scope = directive.get('endpoint', {}).get('scope')
if scope is None:
# token is in grantee for Linking directive
scope = directive.get('payload', {}).get('grantee')
if scope is None:
# token is in payload for Discovery directive
scope = directive.get('payload', {}).get('scope')
assert scope is not None, 'Malformatted request - missing endpoint.scope'
assert scope.get('type') == 'BearerToken', 'Only support BearerToken'
token = scope.get('token')
if token is None and _debug:
token = os.environ.get('LONG_LIVED_ACCESS_TOKEN') # only for debug purpose
verify_ssl = not bool(os.environ.get('NOT_VERIFY_SSL'))
http = urllib3.PoolManager(
cert_reqs='CERT_REQUIRED' if verify_ssl else 'CERT_NONE',
timeout=urllib3.Timeout(connect=2.0, read=10.0)
)
response = http.request(
'POST',
'{}/api/alexa/smart_home'.format(base_url),
headers={
'Authorization': 'Bearer {}'.format(token),
'Content-Type': 'application/json',
},
body=json.dumps(event).encode('utf-8'),
)
if response.status >= 400:
return {
'event': {
'payload': {
'type': 'INVALID_AUTHORIZATION_CREDENTIAL'
if response.status in (401, 403) else 'INTERNAL_ERROR',
'message': response.data.decode("utf-8"),
}
}
}
_logger.debug('Response: %s', response.data.decode("utf-8"))
return json.loads(response.data.decode('utf-8'))
Navigate to Configuration-> Environment variables: (required) Key = BASEURL, Value = https://dyararco.duckdns.org (Do not include the trailing /). (optional) Key = NOT_VERIFY_SSL, Value = True. You can set this to True to ignore SSL issues, for example if you don’t have a valid SSL certificate or you are using a self-signed certificate.
(optional) Key = DEBUG, Value = True. Set this variable to log the debug message and to allow the LONG_LIVED_ACCESS_TOKEN
(optional, not recommend) Key = LONG_LIVED_ACCESSTOKEN,
in Alexa Developer Console developer console,
Your Skill ID: amzn1.ask.skill.ffc7776e-da07-4285-9ebf-d17ebb58d3bd
Default endpoint: arn:aws:lambda:us-east-1:815421264176:function:Lambda_HA_13
North America: arn:aws:lambda:us-east-1:815421264176:function:Lambda_HA_13
Your Web Authorization URI: https://dyararco.duckdns.org/auth/authorize
Access Token URI : https://dyararco.duckdns.org/auth/token
Your Client ID: https://pitangui.amazon.com/
Your secret: anything
Scope: smart_home
First run Test on Lambda function, result should be succesful and listing all devices from home assistant
then do account linking on Develop console
then account linking on alexa app
Open Alexa pp
go to your skills
go to Dev
skill should be listed e.g. Home Assistant _Pilot_1
sign-up Alex developer account developer.amazon.com
AWS management console, search for Iam, go to roles, create a new role (Lambda service -> next -> permissions -> tick 'AWSLambdaExecutionrule' -> create a role and name it "home_assistant_pilot_1_Lambda"
go to Alexa Developer console, create a new skill name it "HomeAssistant_Pilot_1", click on smart home skill, provision your own - leave this page open for now!!
in AWS set the region to one of the supported regions "North Virginia", search for lambda in the services
at top right create a new function, name it "HomeAssistant_Pilot_1", runtime select python 3.8, in execution role change the default execution role to the one we created (use an existing role, select it from there 'home_assistant_pilot_1_Lambda') -> create function
Add trigger, select Alexa then Alexa Smart Home -> then add the skill ID from developer console, click add
at AWS edit environment variable, add a new environment variable, name the key BASE_URL and value is the url to access home assistant from outside the home "https://automately.duckdns.org:8123" must have https
generate long access token at home assistant, call it Alexa, copy the random string it generates and add it to AWS console under 'configure test event' add this token as
"payload": {
"scope": {
"type": "BearerToken",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ"
### I have removed default config: line to test
it should be successful and listing the home assistant devices
copy the ARN value from top
Go back the Alexa Developer console from step 4 above, add the ARN to the Default End Point and click save.
Your Client ID get the US, usually second option listed next to Alexa Redirect URLs "only up to 3rd forward slash", currently its https://pitangui.amazon.com/
secret is not used so type anything
under scope, add scope and write smart_home
Code from github...https://gist.github.com/matt2005/744b5ef548cc13d88d0569eea65f5e5b
"""
Copyright 2019 Jason Hu
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import json
import logging
import urllib3
_debug = bool(os.environ.get('DEBUG'))
_logger = logging.getLogger('HomeAssistant-SmartHome')
_logger.setLevel(logging.DEBUG if _debug else logging.INFO)
Retry 11 Feb 2024 tawfik.daim@outlook.com 192.168.1.13 with dyararco.duckdns.org
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ import os import json import logging import urllib3
_debug = bool(os.environ.get('DEBUG'))
_logger = logging.getLogger('HomeAssistant-SmartHome') _logger.setLevel(logging.DEBUG if _debug else logging.INFO)
def lambda_handler(event, context): """Handle incoming Alexa directive."""
First run Test on Lambda function, result should be succesful and listing all devices from home assistant then do account linking on Develop console then account linking on alexa app
HOme assistant config file: alexa: smart_home: locale: en-US endpoint: https://api.amazonalexa.com/v3/events client_id: https://pitangui.amazon.com/ client_secret: anything filter: include_entities:
Instructions here...
https://www.youtube.com/watch?v=Ww2LI59IQ0A
smart home integration
alexa: smart_home:
Code from github...https://gist.github.com/matt2005/744b5ef548cc13d88d0569eea65f5e5b """ Copyright 2019 Jason Hu
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import json
import logging
import urllib3
_debug = bool(os.environ.get('DEBUG'))
_logger = logging.getLogger('HomeAssistant-SmartHome') _logger.setLevel(logging.DEBUG if _debug else logging.INFO)
def lambda_handler(event, context): """Handle incoming Alexa directive."""
Test code: https://www.home-assistant.io/integrations/alexa.smart_home/#test-the-lambda-function { "directive": { "header": { "namespace": "Alexa.Discovery", "name": "Discover", "payloadVersion": "3", "messageId": "1bd5d003-31b9-476f-ad03-71d471922820" }, "payload": { "scope": { "type": "BearerToken" } } } }