Cloud: Websocket API to manage Google assistant entity config (#24153)

* Extract exposed devices function

* Add might_2fa info to trait

* Do not filter with should_expose in Google helper func

* Cloud: allow setting if Google entity is exposed

* Allow disabling 2FA via config

* Cloud: allow disabling 2FA

* Lint

* More changes

* Fix typing
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2019-05-29 08:39:12 -07:00
1 parent 85dfea1642
commit 6947f8cb2e
12 files changed
+346 -86

No files matched your search

+54
View File
@@ -2,9 +2,12 @@
from unittest.mock import patch, MagicMock
from aiohttp import web
import jwt
import pytest
from homeassistant.core import State
from homeassistant.setup import async_setup_component
from homeassistant.components.cloud import DOMAIN
from homeassistant.components.cloud.const import (
PREF_ENABLE_ALEXA, PREF_ENABLE_GOOGLE)
from tests.components.alexa import test_smart_home as test_alexa
@@ -19,6 +22,25 @@ def mock_cloud():
return MagicMock(subscription_expired=False)
@pytest.fixture
async def mock_cloud_setup(hass):
"""Set up the cloud."""
with patch('hass_nabucasa.Cloud.start', return_value=mock_coro()):
assert await async_setup_component(hass, 'cloud', {
'cloud': {}
})
@pytest.fixture
def mock_cloud_login(hass, mock_cloud_setup):
"""Mock cloud is logged in."""
hass.data[DOMAIN].id_token = jwt.encode({
'email': 'hello@home-assistant.io',
'custom:sub-exp': '2018-01-03',
'cognito:username': 'abcdefghjkl',
}, 'test')
async def test_handler_alexa(hass):
"""Test handler Alexa."""
hass.states.async_set(
@@ -197,3 +219,35 @@ async def test_webhook_msg(hass):
assert await received[0].json() == {
'hello': 'world'
}
async def test_google_config_expose_entity(
hass, mock_cloud_setup, mock_cloud_login):
"""Test Google config exposing entity method uses latest config."""
cloud_client = hass.data[DOMAIN].client
state = State('light.kitchen', 'on')
assert cloud_client.google_config.should_expose(state)
await cloud_client.prefs.async_update_google_entity_config(
entity_id='light.kitchen',
should_expose=False,
)
assert not cloud_client.google_config.should_expose(state)
async def test_google_config_should_2fa(
hass, mock_cloud_setup, mock_cloud_login):
"""Test Google config disabling 2FA method uses latest config."""
cloud_client = hass.data[DOMAIN].client
state = State('light.kitchen', 'on')
assert cloud_client.google_config.should_2fa(state)
await cloud_client.prefs.async_update_google_entity_config(
entity_id='light.kitchen',
disable_2fa=True,
)
assert not cloud_client.google_config.should_2fa(state)
+63 -3
View File
@@ -7,10 +7,13 @@ from jose import jwt
from hass_nabucasa.auth import Unauthenticated, UnknownError
from hass_nabucasa.const import STATE_CONNECTED
from homeassistant.core import State
from homeassistant.auth.providers import trusted_networks as tn_auth
from homeassistant.components.cloud.const import (
PREF_ENABLE_GOOGLE, PREF_ENABLE_ALEXA, PREF_GOOGLE_SECURE_DEVICES_PIN,
DOMAIN)
from homeassistant.components.google_assistant.helpers import (
GoogleEntity, Config)
from tests.common import mock_coro
@@ -32,7 +35,8 @@ def mock_cloud_login(hass, setup_api):
"""Mock cloud is logged in."""
hass.data[DOMAIN].id_token = jwt.encode({
'email': 'hello@home-assistant.io',
'custom:sub-exp': '2018-01-03'
'custom:sub-exp': '2018-01-03',
'cognito:username': 'abcdefghjkl',
}, 'test')
@@ -349,7 +353,15 @@ async def test_websocket_status(hass, hass_ws_client, mock_cloud_fixture,
'logged_in': True,
'email': 'hello@home-assistant.io',
'cloud': 'connected',
'prefs': mock_cloud_fixture,
'prefs': {
'alexa_enabled': True,
'cloud_user': None,
'cloudhooks': {},
'google_enabled': True,
'google_entity_configs': {},
'google_secure_devices_pin': None,
'remote_enabled': False,
},
'alexa_entities': {
'include_domains': [],
'include_entities': ['light.kitchen', 'switch.ac'],
@@ -363,7 +375,6 @@ async def test_websocket_status(hass, hass_ws_client, mock_cloud_fixture,
'exclude_domains': [],
'exclude_entities': [],
},
'google_domains': ['light'],
'remote_domain': None,
'remote_connected': False,
'remote_certificate': None,
@@ -689,3 +700,52 @@ async def test_enabling_remote_trusted_networks_other(
assert cloud.client.remote_autostart
assert len(mock_connect.mock_calls) == 1
async def test_list_google_entities(
hass, hass_ws_client, setup_api, mock_cloud_login):
"""Test that we can list Google entities."""
client = await hass_ws_client(hass)
entity = GoogleEntity(hass, Config(lambda *_: False), State(
'light.kitchen', 'on'
))
with patch('homeassistant.components.google_assistant.helpers'
'.async_get_entities', return_value=[entity]):
await client.send_json({
'id': 5,
'type': 'cloud/google_assistant/entities',
})
response = await client.receive_json()
assert response['success']
assert len(response['result']) == 1
assert response['result'][0] == {
'entity_id': 'light.kitchen',
'might_2fa': False,
'traits': ['action.devices.traits.OnOff'],
}
async def test_update_google_entity(
hass, hass_ws_client, setup_api, mock_cloud_login):
"""Test that we can update config of a Google entity."""
client = await hass_ws_client(hass)
await client.send_json({
'id': 5,
'type': 'cloud/google_assistant/entities/update',
'entity_id': 'light.kitchen',
'should_expose': False,
'override_name': 'updated name',
'aliases': ['lefty', 'righty'],
'disable_2fa': False,
})
response = await client.receive_json()
assert response['success']
prefs = hass.data[DOMAIN].client.prefs
assert prefs.google_entity_configs['light.kitchen'] == {
'should_expose': False,
'override_name': 'updated name',
'aliases': ['lefty', 'righty'],
'disable_2fa': False,
}