From b7c70418734b079213afdf8f3ba6ab716598b425 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 4 Jan 2018 21:40:18 -0800 Subject: [PATCH] Add some tests to the cloud component (#11460) --- homeassistant/components/cloud/__init__.py | 2 +- homeassistant/components/cloud/iot.py | 4 +- tests/components/cloud/test_iot.py | 59 ++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/cloud/__init__.py b/homeassistant/components/cloud/__init__.py index 7f998311a6b..a3cedd38cd9 100644 --- a/homeassistant/components/cloud/__init__.py +++ b/homeassistant/components/cloud/__init__.py @@ -72,7 +72,7 @@ def async_setup(hass, config): kwargs[CONF_GOOGLE_ASSISTANT] = ASSISTANT_SCHEMA({}) kwargs[CONF_ALEXA] = alexa_sh.Config(**kwargs[CONF_ALEXA]) - kwargs['gass_should_expose'] = kwargs.pop(CONF_GOOGLE_ASSISTANT) + kwargs['gass_should_expose'] = kwargs.pop(CONF_GOOGLE_ASSISTANT)['filter'] cloud = hass.data[DOMAIN] = Cloud(hass, **kwargs) success = yield from cloud.initialize() diff --git a/homeassistant/components/cloud/iot.py b/homeassistant/components/cloud/iot.py index 789ffae2fed..a7ff30025c7 100644 --- a/homeassistant/components/cloud/iot.py +++ b/homeassistant/components/cloud/iot.py @@ -212,8 +212,8 @@ def async_handle_alexa(hass, cloud, payload): @HANDLERS.register('google_actions') @asyncio.coroutine -def async_handle_google_assistant(hass, cloud, payload): - """Handle an incoming IoT message for Google Assistant.""" +def async_handle_google_actions(hass, cloud, payload): + """Handle an incoming IoT message for Google Actions.""" result = yield from ga.async_handle_message(hass, cloud.gass_config, payload) return result diff --git a/tests/components/cloud/test_iot.py b/tests/components/cloud/test_iot.py index be5a93c9e47..e74d89c744d 100644 --- a/tests/components/cloud/test_iot.py +++ b/tests/components/cloud/test_iot.py @@ -5,7 +5,9 @@ from unittest.mock import patch, MagicMock, PropertyMock from aiohttp import WSMsgType, client_exceptions import pytest +from homeassistant.setup import async_setup_component from homeassistant.components.cloud import iot, auth_api +from tests.components.alexa import test_smart_home as test_alexa from tests.common import mock_coro @@ -36,6 +38,16 @@ def mock_cloud(): return MagicMock(subscription_expired=False) +@pytest.fixture +def cloud_instance(loop, hass): + """Instance of an initialized cloud class.""" + with patch('homeassistant.components.cloud.Cloud.initialize', + return_value=mock_coro(True)): + loop.run_until_complete(async_setup_component(hass, 'cloud', {})) + + yield hass.data['cloud'] + + @asyncio.coroutine def test_cloud_calling_handler(mock_client, mock_handle_message, mock_cloud): """Test we call handle message with correct info.""" @@ -254,3 +266,50 @@ def test_refresh_token_before_expiration_fails(hass, mock_cloud): assert len(mock_check_token.mock_calls) == 1 assert len(mock_create.mock_calls) == 1 + + +@asyncio.coroutine +def test_handler_alexa(hass, cloud_instance): + """Test handler Alexa.""" + hass.states.async_set( + 'switch.test', 'on', {'friendly_name': "Test switch"}) + + resp = yield from iot.async_handle_alexa( + hass, cloud_instance, + test_alexa.get_new_request('Alexa.Discovery', 'Discover')) + + endpoints = resp['event']['payload']['endpoints'] + + assert len(endpoints) == 1 + device = endpoints[0] + + assert device['description'] == 'switch.test' + assert device['friendlyName'] == 'Test switch' + assert device['manufacturerName'] == 'Home Assistant' + + +@asyncio.coroutine +def test_handler_google_actions(hass, cloud_instance): + """Test handler Google Actions.""" + hass.states.async_set( + 'switch.test', 'on', {'friendly_name': "Test switch"}) + + reqid = '5711642932632160983' + data = {'requestId': reqid, 'inputs': [{'intent': 'action.devices.SYNC'}]} + + with patch('homeassistant.components.cloud.Cloud._decode_claims', + return_value={'cognito:username': 'myUserName'}): + resp = yield from iot.async_handle_google_actions( + hass, cloud_instance, data) + + assert resp['requestId'] == reqid + payload = resp['payload'] + + assert payload['agentUserId'] == 'myUserName' + + devices = payload['devices'] + assert len(devices) == 1 + + device = devices[0] + assert device['id'] == 'switch.test' + assert device['name']['name'] == 'Test switch'