Clean up Google Config (#24663)

* Clean up Google Config

* Lint

* pylint

* pylint2
This commit is contained in:
Paulus Schoutsen authored and Pascal Vizeli committed 2019-06-21 11:17:21 +02:00
1 parent d468d0f71b
commit 78b7ed0ebe
12 files changed
+460 -387

No files matched your search

+9 -11
View File
@@ -1,24 +1,22 @@
"""Tests for the cloud component."""
from unittest.mock import patch
from homeassistant.setup import async_setup_component
from homeassistant.components import cloud
from homeassistant.components.cloud import const
from jose import jwt
from tests.common import mock_coro
def mock_cloud(hass, config={}):
async def mock_cloud(hass, config=None):
"""Mock cloud."""
with patch('hass_nabucasa.Cloud.start', return_value=mock_coro()):
assert hass.loop.run_until_complete(async_setup_component(
hass, cloud.DOMAIN, {
'cloud': config
}))
hass.data[cloud.DOMAIN]._decode_claims = \
lambda token: jwt.get_unverified_claims(token)
assert await async_setup_component(
hass, cloud.DOMAIN, {
'cloud': config or {}
})
cloud_inst = hass.data['cloud']
with patch('hass_nabucasa.Cloud.run_executor', return_value=mock_coro()):
await cloud_inst.start()
def mock_cloud_prefs(hass, prefs={}):
+1 -1
View File
@@ -18,7 +18,7 @@ def mock_user_data():
@pytest.fixture
def mock_cloud_fixture(hass):
"""Fixture for cloud component."""
mock_cloud(hass)
hass.loop.run_until_complete(mock_cloud(hass))
return mock_cloud_prefs(hass)
+35 -45
View File
@@ -9,7 +9,7 @@ import pytest
from homeassistant.core import State
from homeassistant.setup import async_setup_component
from homeassistant.components.cloud import (
DOMAIN, ALEXA_SCHEMA, client)
DOMAIN, ALEXA_SCHEMA, alexa_config)
from homeassistant.components.cloud.const import (
PREF_ENABLE_ALEXA, PREF_ENABLE_GOOGLE)
from homeassistant.util.dt import utcnow
@@ -17,11 +17,11 @@ from homeassistant.helpers.entity_registry import EVENT_ENTITY_REGISTRY_UPDATED
from tests.components.alexa import test_smart_home as test_alexa
from tests.common import mock_coro, async_fire_time_changed
from . import mock_cloud_prefs
from . import mock_cloud_prefs, mock_cloud
@pytest.fixture
def mock_cloud():
def mock_cloud_inst():
"""Mock cloud class."""
return MagicMock(subscription_expired=False)
@@ -29,10 +29,7 @@ def mock_cloud():
@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': {}
})
await mock_cloud(hass)
@pytest.fixture
@@ -52,24 +49,20 @@ async def test_handler_alexa(hass):
hass.states.async_set(
'switch.test2', 'on', {'friendly_name': "Test switch 2"})
with patch('hass_nabucasa.Cloud.start', return_value=mock_coro()):
setup = await async_setup_component(hass, 'cloud', {
'cloud': {
'alexa': {
'filter': {
'exclude_entities': 'switch.test2'
},
'entity_config': {
'switch.test': {
'name': 'Config name',
'description': 'Config description',
'display_categories': 'LIGHT'
}
}
await mock_cloud(hass, {
'alexa': {
'filter': {
'exclude_entities': 'switch.test2'
},
'entity_config': {
'switch.test': {
'name': 'Config name',
'description': 'Config description',
'display_categories': 'LIGHT'
}
}
})
assert setup
}
})
mock_cloud_prefs(hass)
cloud = hass.data['cloud']
@@ -110,24 +103,20 @@ async def test_handler_google_actions(hass):
hass.states.async_set(
'group.all_locks', 'on', {'friendly_name': "Evil locks"})
with patch('hass_nabucasa.Cloud.start', return_value=mock_coro()):
setup = await async_setup_component(hass, 'cloud', {
'cloud': {
'google_actions': {
'filter': {
'exclude_entities': 'switch.test2'
},
'entity_config': {
'switch.test': {
'name': 'Config name',
'aliases': 'Config alias',
'room': 'living room'
}
}
await mock_cloud(hass, {
'google_actions': {
'filter': {
'exclude_entities': 'switch.test2'
},
'entity_config': {
'switch.test': {
'name': 'Config name',
'aliases': 'Config alias',
'room': 'living room'
}
}
})
assert setup
}
})
mock_cloud_prefs(hass)
cloud = hass.data['cloud']
@@ -265,7 +254,7 @@ async def test_alexa_config_expose_entity_prefs(hass, cloud_prefs):
await cloud_prefs.async_update(alexa_entity_configs={
'light.kitchen': entity_conf
})
conf = client.AlexaConfig(hass, ALEXA_SCHEMA({}), cloud_prefs, None)
conf = alexa_config.AlexaConfig(hass, ALEXA_SCHEMA({}), cloud_prefs, None)
assert not conf.should_expose('light.kitchen')
entity_conf['should_expose'] = True
@@ -274,7 +263,7 @@ async def test_alexa_config_expose_entity_prefs(hass, cloud_prefs):
async def test_alexa_config_report_state(hass, cloud_prefs):
"""Test Alexa config should expose using prefs."""
conf = client.AlexaConfig(hass, ALEXA_SCHEMA({}), cloud_prefs, None)
conf = alexa_config.AlexaConfig(hass, ALEXA_SCHEMA({}), cloud_prefs, None)
assert cloud_prefs.alexa_report_state is False
assert conf.should_report_state is False
@@ -307,9 +296,9 @@ def patch_sync_helper():
to_remove = []
with patch(
'homeassistant.components.cloud.client.SYNC_DELAY', 0
'homeassistant.components.cloud.alexa_config.SYNC_DELAY', 0
), patch(
'homeassistant.components.cloud.client.AlexaConfig._sync_helper',
'homeassistant.components.cloud.alexa_config.AlexaConfig._sync_helper',
side_effect=mock_coro
) as mock_helper:
yield to_update, to_remove
@@ -321,7 +310,7 @@ def patch_sync_helper():
async def test_alexa_update_expose_trigger_sync(hass, cloud_prefs):
"""Test Alexa config responds to updating exposed entities."""
client.AlexaConfig(hass, ALEXA_SCHEMA({}), cloud_prefs, None)
alexa_config.AlexaConfig(hass, ALEXA_SCHEMA({}), cloud_prefs, None)
with patch_sync_helper() as (to_update, to_remove):
await cloud_prefs.async_update_alexa_entity_config(
@@ -354,7 +343,8 @@ async def test_alexa_update_expose_trigger_sync(hass, cloud_prefs):
async def test_alexa_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
"""Test Alexa config responds to entity registry."""
client.AlexaConfig(hass, ALEXA_SCHEMA({}), cloud_prefs, hass.data['cloud'])
alexa_config.AlexaConfig(
hass, ALEXA_SCHEMA({}), cloud_prefs, hass.data['cloud'])
with patch_sync_helper() as (to_update, to_remove):
hass.bus.async_fire(EVENT_ENTITY_REGISTRY_UPDATED, {
+8 -6
View File
@@ -14,10 +14,11 @@ 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)
GoogleEntity)
from homeassistant.components.alexa.entities import LightCapabilities
from tests.common import mock_coro
from tests.components.google_assistant import MockConfig
from . import mock_cloud, mock_cloud_prefs
@@ -45,7 +46,7 @@ def mock_cloud_login(hass, setup_api):
@pytest.fixture(autouse=True)
def setup_api(hass, aioclient_mock):
"""Initialize HTTP API."""
mock_cloud(hass, {
hass.loop.run_until_complete(mock_cloud(hass, {
'mode': 'development',
'cognito_client_id': 'cognito_client_id',
'user_pool_id': 'user_pool_id',
@@ -63,7 +64,7 @@ def setup_api(hass, aioclient_mock):
'include_entities': ['light.kitchen', 'switch.ac']
}
}
})
}))
return mock_cloud_prefs(hass)
@@ -709,9 +710,10 @@ 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'
))
entity = GoogleEntity(
hass, MockConfig(should_expose=lambda *_: False), State(
'light.kitchen', 'on'
))
with patch('homeassistant.components.google_assistant.helpers'
'.async_get_entities', return_value=[entity]):
await client.send_json({
+29 -2
View File
@@ -1,6 +1,33 @@
"""Tests for the Google Assistant integration."""
from homeassistant.components.google_assistant import helpers
class MockConfig(helpers.AbstractConfig):
"""Fake config that always exposes everything."""
def __init__(self, *, secure_devices_pin=None, should_expose=None,
entity_config=None):
"""Initialize config."""
self._should_expose = should_expose
self._secure_devices_pin = secure_devices_pin
self._entity_config = entity_config or {}
@property
def secure_devices_pin(self):
"""Return secure devices pin."""
return self._secure_devices_pin
@property
def entity_config(self):
"""Return secure devices pin."""
return self._entity_config
def should_expose(self, state):
"""Expose it all."""
return self._should_expose is None or self._should_expose(state)
BASIC_CONFIG = MockConfig()
DEMO_DEVICES = [{
'id':
@@ -11,7 +11,7 @@ from homeassistant.components.climate.const import (
ATTR_MIN_TEMP, ATTR_MAX_TEMP, STATE_HEAT, SUPPORT_OPERATION_MODE
)
from homeassistant.components.google_assistant import (
const, trait, helpers, smart_home as sh,
const, trait, smart_home as sh,
EVENT_COMMAND_RECEIVED, EVENT_QUERY_RECEIVED, EVENT_SYNC_RECEIVED)
from homeassistant.components.demo.binary_sensor import DemoBinarySensor
from homeassistant.components.demo.cover import DemoCover
@@ -23,9 +23,8 @@ from homeassistant.helpers import device_registry
from tests.common import (mock_device_registry, mock_registry,
mock_area_registry, mock_coro)
BASIC_CONFIG = helpers.Config(
should_expose=lambda state: True,
)
from . import BASIC_CONFIG, MockConfig
REQ_ID = 'ff36a3cc-ec34-11e6-b1a0-64510650abcf'
@@ -57,7 +56,7 @@ async def test_sync_message(hass):
# Excluded via config
hass.states.async_set('light.not_expose', 'on')
config = helpers.Config(
config = MockConfig(
should_expose=lambda state: state.entity_id != 'light.not_expose',
entity_config={
'light.demo_light': {
@@ -145,7 +144,7 @@ async def test_sync_in_area(hass, registries):
light.entity_id = entity.entity_id
await light.async_update_ha_state()
config = helpers.Config(
config = MockConfig(
should_expose=lambda _: True,
entity_config={}
)
@@ -29,10 +29,8 @@ from homeassistant.const import (
from homeassistant.core import State, DOMAIN as HA_DOMAIN, EVENT_CALL_SERVICE
from homeassistant.util import color
from tests.common import async_mock_service, mock_coro
from . import BASIC_CONFIG, MockConfig
BASIC_CONFIG = helpers.Config(
should_expose=lambda state: True,
)
REQ_ID = 'ff36a3cc-ec34-11e6-b1a0-64510650abcf'
@@ -42,8 +40,7 @@ BASIC_DATA = helpers.RequestData(
REQ_ID,
)
PIN_CONFIG = helpers.Config(
should_expose=lambda state: True,
PIN_CONFIG = MockConfig(
secure_devices_pin='1234'
)
@@ -927,7 +924,7 @@ async def test_lock_unlock_unlock(hass):
# Test with 2FA override
with patch('homeassistant.components.google_assistant.helpers'
'.Config.should_2fa', return_value=False):
'.AbstractConfig.should_2fa', return_value=False):
await trt.execute(
trait.COMMAND_LOCKUNLOCK, BASIC_DATA, {'lock': False}, {})
assert len(calls) == 2