Skip to content

Instantly share code, notes, and snippets.

@bitglue
Last active January 17, 2019 02:34
Show Gist options
  • Save bitglue/66ca023b3c3c1a5b267bdd1d9b3ee48f to your computer and use it in GitHub Desktop.
Save bitglue/66ca023b3c3c1a5b267bdd1d9b3ee48f to your computer and use it in GitHub Desktop.
Alexa v3 Smart Home payload on home-assistant

Haaska implements the deprecated v2 Smart Home API, which can no longer be selected for new skills without some hackery.

Home Assistant Cloud is an alternative, but it's only available in the US.

Fortunately, the Home Assistant developers are awesome and all the logic is in Home Assistant, not hidden in the cloud. It's pretty easy to expose it via an HTTP POST interface which is easily integrated with a Lambda function.

1. Create a view to make an HTTP POST interface to Smart Home actions

Add to homeassistant/components/alexa/smart_home.py

@ha.callback
def async_setup(hass, config):
    hass.http.register_view(SmartHomeView)


class SmartHomeView(http.HomeAssistantView):
    url = '/api/alexa_smart_home'
    name = 'wtf:isthis'

    @asyncio.coroutine
    def post(self, request):
        """Handle Alexa."""
        hass = request.app['hass']
        message = yield from request.json()

        _LOGGER.debug("Received Alexa request: %s", message)

        response = yield from async_handle_message(hass, Config(lambda entity_id: True), message)
        return b'' if response is None else self.json(response)

And in async_setup in homeassistant/components/alexa/__init__.py, call the setup function for the view above:

smart_home.async_setup(hass, None)

Restart hass and you should be able to post to http://YOUR_HASS_IP/api/alexa_smart_home. Try a discovery message.

2. Make a Lambda function to bridge the gap

If you've already set up haaska, a quick and dirty solution is to replace the event handler with:

def event_handler(event, context):
    config = Configuration('config.json')
    if config.debug:
        logger.setLevel(logging.DEBUG)
    ha = HomeAssistant(config)

    return ha.post('alexa_smart_home', event, wait=True).json()

3. Profit

I'll be making PRs for these things as soon as I learn enough about home-assistant to remove some of the hacks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment