Alexa proactive mode - unable to link alexa

Odd, but it doesn’t link for me even if I add long token and amended lambda function.

Not working for me with this version.
I also tried to delete everything and do it again.

Funny how cloud subscription got fixed in hours but this is not being looked by anyone.
This manual integration is completely broken.

1 Like

Have you tried linking without send events to Alexa and the config in homeassistant? I know I had it linking and then unlinked.
Are you using https on 443?
What version of homeassistant? What type of install?

Mine is 0 95.4 running in hassio in docker on a debian x64 host.

Glad it´s working! Did you also try without token in the environment? It should work (as long as you have homeassistant as oauth provider)

HA 0.95.4
Docker
My Home-Assistant runs on port 4443 instead of 443 because my provider blocks 80/443 access.

Ok guys,

Here is how I managed to get it working (some parts are from the original HAASKA):

Set Up Login With Amazon

  1. Sign in to the Amazon Developer Console.
  2. Click the link for “Login With Amazon” in the top navigation bar.
  3. Click the gold button that reads “Create a New Security Profile”.
  • Enter any security profile name you’d like, e.g. “haaska”
  • Write a short description, e.g. “haaska for my Home Assistant”
  • Add any url you’d like for the privacy notice, e.g. “https://myhass.example.com
  1. Click “Save” to continue.
  2. On your new Security Profile, click the gear button, and choose “Web Settings”.
  3. Make note of the Client ID, and Client Secret. Keep this window open for reference.

Linking Alexa Skills Kit to Login With Amazon

  1. Click the “Setup Account Linking” button at the bottom of the page.
  2. Enter the following:
Field Setting
Authorization URI https://www.amazon.com/ap/oa
Access Token URI https://api.amazon.com/auth/o2/token
Client ID The Client ID from your Login With Amazon window
Client Secret The Client Secret from your Login With Amazon window
Client Authentication Scheme HTTP Basic (Recommended)
Scope Click + Add Scope and enter: profile
Domain List Not used
Default Access Token Expiration Time Not used
  1. You will see a list of Redirect URIs. Please copy these down.
  2. Click Save at the top right.
  3. Go back to the Amazon Developer Console/Login with Amazon window. Click Edit at the bottom right.
  4. In “Allowed Return URLs” section, click “Add another” until you have three lines.
  5. On each line, add one of the Redirect URIs from the “Alexa Developer Console” window. Click Save.

And using this custom lambda I made:

#!/usr/bin/env python3.7
# coding: utf-8

# Copyright (c) 2019 Alexandre Leites
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os
import json
import logging
import ssl
from http.client import HTTPSConnection, RemoteDisconnected

_debug  = bool(os.environ.get('DEBUG'))
_logger = logging.getLogger('HomeAssistant-SmartHome')
_logger.setLevel(logging.DEBUG if _debug else logging.INFO)

def handle_event(event, num_attempt = 1):
    try:
        _logger.debug('Event: %s', event)
        host       = os.environ.get('HOMEASSISTANT_HOST')
        port       = os.environ.get('HOMEASSISTANT_PORT', default='443')
        token      = os.environ.get('HOMEASSISTANT_TOKEN')
        awsRegion  = os.environ.get("AWS_DEFAULT_REGION")
        sslVerify  = os.environ.get("SSL_VERIFY", default = True)
        
        sslContext = ssl.create_default_context()
        sslContext.verify_mode    = ssl.CERT_REQUIRED if sslVerify else ssl.CERT_NONE
        sslContext.check_hostname = True if sslVerify else False
        
        _logger.debug('Host: %s', host)
        _logger.debug('Port: %s', port)
        _logger.debug('Token: %s', token)
        _logger.debug('AWS Region: %s', awsRegion)
        _logger.debug('SSL Verify: %s', sslVerify)
        
        directive = event.get('directive')
        assert directive is not None, 'Malformatted request - missing directive'
        assert directive.get('header', {}).get('payloadVersion') == '3', 'Only support payloadVersion == 3'

        connection = HTTPSConnection(host, port, context = sslContext)
        headers = {
            'Authorization': 'Bearer {}'.format(token),
            'Content-Type': 'application/json',
            'User-Agent': 'HALambda in {}'.format(awsRegion)
        }
        
        connection.request('POST', '/api/alexa/smart_home', body = json.dumps(event).encode('utf8'), headers = headers)
        response = connection.getresponse()
        if response.status != 200:
            _logger.error(f'HomeAssistant error {response.status} {response.reason}')
            return None
        
        return json.loads(response.read())
    except Exception:
        _logger.info("HomeAssistant closed the connection.")
        connection.close()
        if num_attempt >= 3:
            return None
        
        _logger.debug(f"Attempt {num_attempt} failed. Retrying...")
        return handle_event(event, num_attempt + 1)

def event_handler(event, context):
    try:
        return handle_event(event)
    except Exception:
        _logger.exception('Internal error calling HomeAssistant')
        return None

Environment variables:
DEBUG: True or False (I use True to test)
HOMEASSISTANT_HOST: Your HTTPS External HA address (without the port)
HOMEASSISTANT_PORT: Port number (443 by default)
HOMEASSISTANT_TOKEN: Your LONG LIVED TOKEN
SSL_VERIFY: True or False (I use False)

2 Likes

Hi @alexslx

What happens if you replace your

         token = os.environ.get('HOMEASSISTANT_TOKEN')`

with

        directive = event.get('directive') 
        scope = directive.get('endpoint', {}).get('scope')
        if scope is None:
            scope = directive.get('payload', {}).get('grantee')
            
        if scope is None:
            scope = directive.get('payload', {}).get('scope')
           
        token = scope.get('token')
        if token is None:
            token = os.environ.get('HOMEASSISTANT_TOKEN')

And use the homeassistant oauth URIs (Careful do not use HTTP Basic)

If that doesn’t work for you it seem that it is neither a problem with the lambda funktion nor with the alexa smarthome component but rather with your oauth setup (maybe try setup a new homessistant admin user profile).

That was the previous version. It doesn’t work. Something is wrong with HA OAuth2 I think because it doesn’t even generate a lambda log on cloudwatch so probably amazon is not accepting the generated token, and don’t call the lambda. My HA Setup is pretty simple, one admin user and thats all.

That was my setup using HA OAuth2:
https://github.com/home-assistant/home-assistant/issues/24867#issuecomment-507446023

Now using the HAASKA method I can control my smart lights and such, just proactive mode is not working, but it is less issue than everything broken.

This actually worked for me after disabling ‘Send Alexa Events’. Following your instructions and using your Lambda code is the first time I’ve been able to successfully get the skill to link, so thank you for that!

The issue I appear to be having is now that I’m linked, Alexa doesn’t appear to discover anything at all on Home Assistant.

Previously, using the instructions from here > https://www.home-assistant.io/components/alexa.smart_home/ I was able to get the Lambda ‘test’ button discovery to function and return results. The issue was that the Skill absolutely refused to link. Now using your instructions I’m finally linked, but that test code doesn’t work and Alexa finds nothing when the discovery is run. What am I doing wrong?

Please supply your config. It took me over 2 weeks to finally get it working.

Have you rebooted homeassistant a couple of times since adding the config?
When testing in AWS Lambda do you see something similar to this?

@matt2005 did you use the method posted by @alexslx above, or did you follow the original instructions posted here > https://www.home-assistant.io/components/alexa.smart_home/

I used the original instructions but used the lambda function in my gist linked in the post above.
I first linked without send events setup in amaxon or homeassistant. This worked first time but it took a while to get the send events working. The fix was the lambda function.

@matt2005 you’re my savior! with you function now it’s working (at least for me :smiley: )

It seems like finally managed to make it work using the updated lambda. The “secret” for me was to reboot homeassistant after every disable/enable skill step in Alexa.

Glad you got it working.

I was having the same problem that the OP. After Alexa having worked for some time I was receiving the same message and Alexa app was not connecting to HA. I tried most of the things I found here but to no avail.

In the end I followed the guide I used first time (https://indomus.it/guide/integrare-gratuitamente-amazon-echo-alexa-con-home-assistant-via-haaska-e-aws/) and Alexa linked. After that I enabled sending events, re-linked, and everything is working fine.

Yes, I’m using the Long Lived Token, because any other way it wouldn’t link. And sorry, because the guide is in Italian (thanks inDomus, by the way), but being Spanish it’s not very difficult to understand it. The steps are very clear, anyway.

I’m having the same problem, I can’t link my Alexa skill no matter what I do. I’ve tried the instructions here (https://www.home-assistant.io/components/alexa.smart_home/), and I’ve tried using matt2005’s lambda function (which is posted here).

I’m at my wit’s end. Every time, I just get the message that “We were unable to link [SKILL NAME] at this time.”

If anyone has any advice, I’m all ears and would greatly appreciate any help.

I’m in the U.S., and I run Hassio on a Rasberry Pi 3b+. I use the DuckDNS addon, and have no problem making an https connection via the web using the external https domain name.

I removed the “cloud:” entry from my configuration.yaml file, and signed out of HA Cloud. In my configuration file I now have:

alexa:
smart_home:
endpoint: https://api.amazonalexa.com/v3/events
client_id: !secret alexa_mess_client_id
client_secret: !secret alexa_mess_secret

The secrets.yaml file list the client_id and client_secret without any quotation marks. I used the client_id and client_secret listed on the Permissions page of my skill in the Alexa Developer console.

I’m using matt2005’s lambda function from here, filling in the BASE_URL, DEBUG (set to yes), LONG_LIVED_ACCESS_TOKEN, and NOT_VERIFY_SSL (set to true). I am using Python 3.7 as Runtime, and when I test I get a successful result listing all of my devices.

Below are screenshots of my skill from the Alexa Developer console.



Can anyone see where I’ve gone wrong?

Have you rebooted several times? I know it’s odd but I had to reboot a few times for it to work.

I tried rebooting endless times.
Finally, gave up and tried Haaska, which seems to be working fine.
It was really frustrating that Amazon would not tell you why the accounts were not linking. Just stating “unable to do so at this time” is not helpful.

Thanks for the response.

One thing I’m learning on my HA journey is that there’s often more than one of way of doing things. Which is cool! :stuck_out_tongue:

Hi did you ever find a fix for this issue, I was getting the same problem when using the GIST code