Example #1
0
def test_include_filters(hass):
    """Test inclusion filters."""
    request = get_new_request('Alexa.Discovery', 'Discover')

    # setup test devices
    hass.states.async_set('switch.deny', 'on',
                          {'friendly_name': "Blocked switch"})

    hass.states.async_set('script.deny', 'off',
                          {'friendly_name': "Blocked script"})

    hass.states.async_set('automation.allow', 'off',
                          {'friendly_name': "Allowed automation"})

    hass.states.async_set('group.allow', 'off',
                          {'friendly_name': "Allowed group"})

    config = smart_home.Config(should_expose=entityfilter.generate_filter(
        include_domains=['automation', 'group'],
        include_entities=['script.deny'],
        exclude_domains=[],
        exclude_entities=[],
    ))

    msg = yield from smart_home.async_handle_message(hass, config, request)
    yield from hass.async_block_till_done()

    msg = msg['event']

    assert len(msg['payload']['endpoints']) == 3
Example #2
0
def test_exclude_filters(hass):
    """Test exclusion filters."""
    request = get_new_request('Alexa.Discovery', 'Discover')

    # setup test devices
    hass.states.async_set('switch.test', 'on',
                          {'friendly_name': "Test switch"})

    hass.states.async_set('script.deny', 'off',
                          {'friendly_name': "Blocked script"})

    hass.states.async_set('cover.deny', 'off',
                          {'friendly_name': "Blocked cover"})

    config = smart_home.Config(filter=entityfilter.generate_filter(
        include_domains=[],
        include_entities=[],
        exclude_domains=['script'],
        exclude_entities=['cover.deny'],
    ))

    msg = yield from smart_home.async_handle_message(hass, config, request)

    msg = msg['event']

    assert len(msg['payload']['endpoints']) == 1
Example #3
0
def test_entity_config(hass):
    """Test that we can configure things via entity config."""
    request = get_new_request('Alexa.Discovery', 'Discover')

    hass.states.async_set('light.test_1', 'on',
                          {'friendly_name': "Test light 1"})

    config = smart_home.Config(should_expose=lambda entity_id: True,
                               entity_config={
                                   'light.test_1': {
                                       'name': 'Config name',
                                       'display_categories': 'SWITCH',
                                       'description': 'Config description'
                                   }
                               })

    msg = yield from smart_home.async_handle_message(hass, config, request)

    assert 'event' in msg
    msg = msg['event']

    assert len(msg['payload']['endpoints']) == 1

    appliance = msg['payload']['endpoints'][0]
    assert appliance['endpointId'] == 'light#test_1'
    assert appliance['displayCategories'][0] == "SWITCH"
    assert appliance['friendlyName'] == "Config name"
    assert appliance['description'] == "Config description"
    assert len(appliance['capabilities']) == 1
    assert appliance['capabilities'][-1]['interface'] == \
        'Alexa.PowerController'
Example #4
0
def async_setup(hass, config):
    """Initialize the Home Assistant cloud."""
    if DOMAIN in config:
        kwargs = dict(config[DOMAIN])
    else:
        kwargs = {CONF_MODE: DEFAULT_MODE}

    alexa_conf = kwargs.pop(CONF_ALEXA, None) or ALEXA_SCHEMA({})

    if CONF_GOOGLE_ACTIONS not in kwargs:
        kwargs[CONF_GOOGLE_ACTIONS] = GACTIONS_SCHEMA({})

    kwargs[CONF_ALEXA] = alexa_sh.Config(
        should_expose=alexa_conf[CONF_FILTER],
        entity_config=alexa_conf.get(CONF_ENTITY_CONFIG),
    )

    cloud = hass.data[DOMAIN] = Cloud(hass, **kwargs)

    success = yield from cloud.initialize()

    if not success:
        return False

    yield from http_api.async_setup(hass)
    return True
Example #5
0
    def alexa_config(self) -> alexa_sh.Config:
        """Return Alexa config."""
        if not self._alexa_config:
            alexa_conf = self._alexa_user_config

            self._alexa_config = alexa_sh.Config(
                endpoint=None,
                async_get_access_token=None,
                should_expose=alexa_conf[CONF_FILTER],
                entity_config=alexa_conf.get(CONF_ENTITY_CONFIG),
            )

        return self._alexa_config
Example #6
0
def async_setup(hass, config):
    """Initialize the Home Assistant cloud."""
    if DOMAIN in config:
        kwargs = dict(config[DOMAIN])
    else:
        kwargs = {CONF_MODE: DEFAULT_MODE}

    alexa_conf = kwargs.pop(CONF_ALEXA, None) or ALEXA_SCHEMA({})

    if CONF_GOOGLE_ACTIONS not in kwargs:
        kwargs[CONF_GOOGLE_ACTIONS] = GACTIONS_SCHEMA({})

    kwargs[CONF_ALEXA] = alexa_sh.Config(
        should_expose=alexa_conf[CONF_FILTER],
        entity_config=alexa_conf.get(CONF_ENTITY_CONFIG),
    )

    cloud = hass.data[DOMAIN] = Cloud(hass, **kwargs)
    hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, cloud.async_start)
    yield from http_api.async_setup(hass)
    return True
Example #7
0
def async_setup(hass, config):
    """Initialize the Home Assistant cloud."""
    if DOMAIN in config:
        kwargs = config[DOMAIN]
    else:
        kwargs = {CONF_MODE: DEFAULT_MODE}

    if CONF_ALEXA not in kwargs:
        kwargs[CONF_ALEXA] = ALEXA_SCHEMA({})

    kwargs[CONF_ALEXA] = smart_home.Config(**kwargs[CONF_ALEXA])
    cloud = hass.data[DOMAIN] = Cloud(hass, **kwargs)

    @asyncio.coroutine
    def init_cloud(event):
        """Initialize connection."""
        yield from cloud.initialize()

    hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, init_cloud)

    yield from http_api.async_setup(hass)
    return True
Example #8
0
import asyncio
import json
from uuid import uuid4

import pytest

from homeassistant.const import (TEMP_FAHRENHEIT, STATE_LOCKED, STATE_UNLOCKED,
                                 STATE_UNKNOWN)
from homeassistant.setup import async_setup_component
from homeassistant.components import alexa
from homeassistant.components.alexa import smart_home
from homeassistant.helpers import entityfilter

from tests.common import async_mock_service

DEFAULT_CONFIG = smart_home.Config(should_expose=lambda entity_id: True)


def get_new_request(namespace, name, endpoint=None):
    """Generate a new API message."""
    raw_msg = {
        'directive': {
            'header': {
                'namespace': namespace,
                'name': name,
                'messageId': str(uuid4()),
                'correlationToken': str(uuid4()),
                'payloadVersion': '3',
            },
            'endpoint': {
                'scope': {
Example #9
0
"""Test for smart home alexa support."""
import asyncio
from uuid import uuid4

import pytest

from homeassistant.components.alexa import smart_home
from homeassistant.helpers import entityfilter

from tests.common import async_mock_service

DEFAULT_CONFIG = smart_home.Config(filter=lambda entity_id: True)


def get_new_request(namespace, name, endpoint=None):
    """Generate a new API message."""
    raw_msg = {
        'directive': {
            'header': {
                'namespace': namespace,
                'name': name,
                'messageId': str(uuid4()),
                'correlationToken': str(uuid4()),
                'payloadVersion': '3',
            },
            'endpoint': {
                'scope': {
                    'type': 'BearerToken',
                    'token': str(uuid4()),
                },
                'endpointId': endpoint,