Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -10,12 +10,9 @@ from tests.common import mock_coro
|
||||
|
||||
async def mock_cloud(hass, config=None):
|
||||
"""Mock cloud."""
|
||||
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()):
|
||||
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()
|
||||
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ from . import mock_cloud, mock_cloud_prefs
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_user_data():
|
||||
"""Mock os module."""
|
||||
with patch('hass_nabucasa.Cloud.write_user_info') as writer:
|
||||
with patch("hass_nabucasa.Cloud.write_user_info") as writer:
|
||||
yield writer
|
||||
|
||||
|
||||
@@ -40,8 +40,11 @@ async def mock_cloud_setup(hass):
|
||||
@pytest.fixture
|
||||
def mock_cloud_login(hass, mock_cloud_setup):
|
||||
"""Mock cloud is logged in."""
|
||||
hass.data[const.DOMAIN].id_token = jwt.encode({
|
||||
'email': 'hello@home-assistant.io',
|
||||
'custom:sub-exp': '2018-01-03',
|
||||
'cognito:username': 'abcdefghjkl',
|
||||
}, 'test')
|
||||
hass.data[const.DOMAIN].id_token = jwt.encode(
|
||||
{
|
||||
"email": "hello@home-assistant.io",
|
||||
"custom:sub-exp": "2018-01-03",
|
||||
"cognito:username": "abcdefghjkl",
|
||||
},
|
||||
"test",
|
||||
)
|
||||
@@ -10,17 +10,13 @@ from tests.common import mock_coro, async_fire_time_changed
|
||||
|
||||
async def test_alexa_config_expose_entity_prefs(hass, cloud_prefs):
|
||||
"""Test Alexa config should expose using prefs."""
|
||||
entity_conf = {
|
||||
'should_expose': False
|
||||
}
|
||||
await cloud_prefs.async_update(alexa_entity_configs={
|
||||
'light.kitchen': entity_conf
|
||||
})
|
||||
entity_conf = {"should_expose": False}
|
||||
await cloud_prefs.async_update(alexa_entity_configs={"light.kitchen": entity_conf})
|
||||
conf = alexa_config.AlexaConfig(hass, ALEXA_SCHEMA({}), cloud_prefs, None)
|
||||
|
||||
assert not conf.should_expose('light.kitchen')
|
||||
entity_conf['should_expose'] = True
|
||||
assert conf.should_expose('light.kitchen')
|
||||
assert not conf.should_expose("light.kitchen")
|
||||
entity_conf["should_expose"] = True
|
||||
assert conf.should_expose("light.kitchen")
|
||||
|
||||
|
||||
async def test_alexa_config_report_state(hass, cloud_prefs):
|
||||
@@ -31,8 +27,7 @@ async def test_alexa_config_report_state(hass, cloud_prefs):
|
||||
assert conf.should_report_state is False
|
||||
assert conf.is_reporting_states is False
|
||||
|
||||
with patch.object(conf, 'async_get_access_token',
|
||||
return_value=mock_coro("hello")):
|
||||
with patch.object(conf, "async_get_access_token", return_value=mock_coro("hello")):
|
||||
await cloud_prefs.async_update(alexa_report_state=True)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -57,11 +52,9 @@ def patch_sync_helper():
|
||||
to_update = []
|
||||
to_remove = []
|
||||
|
||||
with patch(
|
||||
'homeassistant.components.cloud.alexa_config.SYNC_DELAY', 0
|
||||
), patch(
|
||||
'homeassistant.components.cloud.alexa_config.AlexaConfig._sync_helper',
|
||||
side_effect=mock_coro
|
||||
with patch("homeassistant.components.cloud.alexa_config.SYNC_DELAY", 0), patch(
|
||||
"homeassistant.components.cloud.alexa_config.AlexaConfig._sync_helper",
|
||||
side_effect=mock_coro,
|
||||
) as mock_helper:
|
||||
yield to_update, to_remove
|
||||
|
||||
@@ -76,63 +69,62 @@ async def test_alexa_update_expose_trigger_sync(hass, cloud_prefs):
|
||||
|
||||
with patch_sync_helper() as (to_update, to_remove):
|
||||
await cloud_prefs.async_update_alexa_entity_config(
|
||||
entity_id='light.kitchen', should_expose=True
|
||||
entity_id="light.kitchen", should_expose=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
async_fire_time_changed(hass, utcnow())
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert to_update == ['light.kitchen']
|
||||
assert to_update == ["light.kitchen"]
|
||||
assert to_remove == []
|
||||
|
||||
with patch_sync_helper() as (to_update, to_remove):
|
||||
await cloud_prefs.async_update_alexa_entity_config(
|
||||
entity_id='light.kitchen', should_expose=False
|
||||
entity_id="light.kitchen", should_expose=False
|
||||
)
|
||||
await cloud_prefs.async_update_alexa_entity_config(
|
||||
entity_id='binary_sensor.door', should_expose=True
|
||||
entity_id="binary_sensor.door", should_expose=True
|
||||
)
|
||||
await cloud_prefs.async_update_alexa_entity_config(
|
||||
entity_id='sensor.temp', should_expose=True
|
||||
entity_id="sensor.temp", should_expose=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
async_fire_time_changed(hass, utcnow())
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert sorted(to_update) == ['binary_sensor.door', 'sensor.temp']
|
||||
assert to_remove == ['light.kitchen']
|
||||
assert sorted(to_update) == ["binary_sensor.door", "sensor.temp"]
|
||||
assert to_remove == ["light.kitchen"]
|
||||
|
||||
|
||||
async def test_alexa_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
|
||||
"""Test Alexa config responds to entity registry."""
|
||||
alexa_config.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, {
|
||||
'action': 'create',
|
||||
'entity_id': 'light.kitchen',
|
||||
})
|
||||
hass.bus.async_fire(
|
||||
EVENT_ENTITY_REGISTRY_UPDATED,
|
||||
{"action": "create", "entity_id": "light.kitchen"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert to_update == ['light.kitchen']
|
||||
assert to_update == ["light.kitchen"]
|
||||
assert to_remove == []
|
||||
|
||||
with patch_sync_helper() as (to_update, to_remove):
|
||||
hass.bus.async_fire(EVENT_ENTITY_REGISTRY_UPDATED, {
|
||||
'action': 'remove',
|
||||
'entity_id': 'light.kitchen',
|
||||
})
|
||||
hass.bus.async_fire(
|
||||
EVENT_ENTITY_REGISTRY_UPDATED,
|
||||
{"action": "remove", "entity_id": "light.kitchen"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert to_update == []
|
||||
assert to_remove == ['light.kitchen']
|
||||
assert to_remove == ["light.kitchen"]
|
||||
|
||||
with patch_sync_helper() as (to_update, to_remove):
|
||||
hass.bus.async_fire(EVENT_ENTITY_REGISTRY_UPDATED, {
|
||||
'action': 'update',
|
||||
'entity_id': 'light.kitchen',
|
||||
})
|
||||
hass.bus.async_fire(
|
||||
EVENT_ENTITY_REGISTRY_UPDATED,
|
||||
{"action": "update", "entity_id": "light.kitchen"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert to_update == []
|
||||
@@ -144,10 +136,14 @@ async def test_alexa_update_report_state(hass, cloud_prefs):
|
||||
alexa_config.AlexaConfig(hass, ALEXA_SCHEMA({}), cloud_prefs, None)
|
||||
|
||||
with patch(
|
||||
'homeassistant.components.cloud.alexa_config.AlexaConfig.'
|
||||
'async_sync_entities', side_effect=mock_coro) as mock_sync, patch(
|
||||
'homeassistant.components.cloud.alexa_config.'
|
||||
'AlexaConfig.async_enable_proactive_mode', side_effect=mock_coro):
|
||||
"homeassistant.components.cloud.alexa_config.AlexaConfig."
|
||||
"async_sync_entities",
|
||||
side_effect=mock_coro,
|
||||
) as mock_sync, patch(
|
||||
"homeassistant.components.cloud.alexa_config."
|
||||
"AlexaConfig.async_enable_proactive_mode",
|
||||
side_effect=mock_coro,
|
||||
):
|
||||
await cloud_prefs.async_update(alexa_report_state=True)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
@@ -8,37 +8,38 @@ from homeassistant.components.cloud.const import DISPATCHER_REMOTE_UPDATE
|
||||
async def test_remote_connection_sensor(hass):
|
||||
"""Test the remote connection sensor."""
|
||||
from homeassistant.components.cloud import binary_sensor as bin_sensor
|
||||
|
||||
bin_sensor.WAIT_UNTIL_CHANGE = 0
|
||||
|
||||
assert await async_setup_component(hass, 'cloud', {'cloud': {}})
|
||||
assert await async_setup_component(hass, "cloud", {"cloud": {}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get('binary_sensor.remote_ui') is None
|
||||
assert hass.states.get("binary_sensor.remote_ui") is None
|
||||
|
||||
# Fake connection/discovery
|
||||
org_cloud = hass.data['cloud']
|
||||
org_cloud = hass.data["cloud"]
|
||||
await org_cloud.iot._on_connect[-1]()
|
||||
|
||||
# Mock test env
|
||||
cloud = hass.data['cloud'] = Mock()
|
||||
cloud = hass.data["cloud"] = Mock()
|
||||
cloud.remote.certificate = None
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.remote_ui')
|
||||
state = hass.states.get("binary_sensor.remote_ui")
|
||||
assert state is not None
|
||||
assert state.state == 'unavailable'
|
||||
assert state.state == "unavailable"
|
||||
|
||||
cloud.remote.is_connected = False
|
||||
cloud.remote.certificate = object()
|
||||
hass.helpers.dispatcher.async_dispatcher_send(DISPATCHER_REMOTE_UPDATE, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.remote_ui')
|
||||
assert state.state == 'off'
|
||||
state = hass.states.get("binary_sensor.remote_ui")
|
||||
assert state.state == "off"
|
||||
|
||||
cloud.remote.is_connected = True
|
||||
hass.helpers.dispatcher.async_dispatcher_send(DISPATCHER_REMOTE_UPDATE, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('binary_sensor.remote_ui')
|
||||
assert state.state == 'on'
|
||||
state = hass.states.get("binary_sensor.remote_ui")
|
||||
assert state.state == "on"
|
||||
@@ -7,8 +7,7 @@ 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 homeassistant.components.cloud.const import PREF_ENABLE_ALEXA, PREF_ENABLE_GOOGLE
|
||||
from tests.components.alexa import test_smart_home as test_alexa
|
||||
from tests.common import mock_coro
|
||||
|
||||
@@ -23,203 +22,190 @@ def mock_cloud_inst():
|
||||
|
||||
async def test_handler_alexa(hass):
|
||||
"""Test handler Alexa."""
|
||||
hass.states.async_set(
|
||||
'switch.test', 'on', {'friendly_name': "Test switch"})
|
||||
hass.states.async_set(
|
||||
'switch.test2', 'on', {'friendly_name': "Test switch 2"})
|
||||
hass.states.async_set("switch.test", "on", {"friendly_name": "Test switch"})
|
||||
hass.states.async_set("switch.test2", "on", {"friendly_name": "Test switch 2"})
|
||||
|
||||
await mock_cloud(hass, {
|
||||
'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",
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
)
|
||||
|
||||
mock_cloud_prefs(hass)
|
||||
cloud = hass.data['cloud']
|
||||
cloud = hass.data["cloud"]
|
||||
|
||||
resp = await cloud.client.async_alexa_message(
|
||||
test_alexa.get_new_request('Alexa.Discovery', 'Discover'))
|
||||
test_alexa.get_new_request("Alexa.Discovery", "Discover")
|
||||
)
|
||||
|
||||
endpoints = resp['event']['payload']['endpoints']
|
||||
endpoints = resp["event"]["payload"]["endpoints"]
|
||||
|
||||
assert len(endpoints) == 1
|
||||
device = endpoints[0]
|
||||
|
||||
assert device['description'] == 'Config description'
|
||||
assert device['friendlyName'] == 'Config name'
|
||||
assert device['displayCategories'] == ['LIGHT']
|
||||
assert device['manufacturerName'] == 'Home Assistant'
|
||||
assert device["description"] == "Config description"
|
||||
assert device["friendlyName"] == "Config name"
|
||||
assert device["displayCategories"] == ["LIGHT"]
|
||||
assert device["manufacturerName"] == "Home Assistant"
|
||||
|
||||
|
||||
async def test_handler_alexa_disabled(hass, mock_cloud_fixture):
|
||||
"""Test handler Alexa when user has disabled it."""
|
||||
mock_cloud_fixture[PREF_ENABLE_ALEXA] = False
|
||||
cloud = hass.data['cloud']
|
||||
cloud = hass.data["cloud"]
|
||||
|
||||
resp = await cloud.client.async_alexa_message(
|
||||
test_alexa.get_new_request('Alexa.Discovery', 'Discover'))
|
||||
test_alexa.get_new_request("Alexa.Discovery", "Discover")
|
||||
)
|
||||
|
||||
assert resp['event']['header']['namespace'] == 'Alexa'
|
||||
assert resp['event']['header']['name'] == 'ErrorResponse'
|
||||
assert resp['event']['payload']['type'] == 'BRIDGE_UNREACHABLE'
|
||||
assert resp["event"]["header"]["namespace"] == "Alexa"
|
||||
assert resp["event"]["header"]["name"] == "ErrorResponse"
|
||||
assert resp["event"]["payload"]["type"] == "BRIDGE_UNREACHABLE"
|
||||
|
||||
|
||||
async def test_handler_google_actions(hass):
|
||||
"""Test handler Google Actions."""
|
||||
hass.states.async_set(
|
||||
'switch.test', 'on', {'friendly_name': "Test switch"})
|
||||
hass.states.async_set(
|
||||
'switch.test2', 'on', {'friendly_name': "Test switch 2"})
|
||||
hass.states.async_set(
|
||||
'group.all_locks', 'on', {'friendly_name': "Evil locks"})
|
||||
hass.states.async_set("switch.test", "on", {"friendly_name": "Test switch"})
|
||||
hass.states.async_set("switch.test2", "on", {"friendly_name": "Test switch 2"})
|
||||
hass.states.async_set("group.all_locks", "on", {"friendly_name": "Evil locks"})
|
||||
|
||||
await mock_cloud(hass, {
|
||||
'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",
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
)
|
||||
|
||||
mock_cloud_prefs(hass)
|
||||
cloud = hass.data['cloud']
|
||||
cloud = hass.data["cloud"]
|
||||
|
||||
reqid = '5711642932632160983'
|
||||
data = {'requestId': reqid, 'inputs': [{'intent': 'action.devices.SYNC'}]}
|
||||
reqid = "5711642932632160983"
|
||||
data = {"requestId": reqid, "inputs": [{"intent": "action.devices.SYNC"}]}
|
||||
|
||||
with patch(
|
||||
'hass_nabucasa.Cloud._decode_claims',
|
||||
return_value={'cognito:username': 'myUserName'}
|
||||
"hass_nabucasa.Cloud._decode_claims",
|
||||
return_value={"cognito:username": "myUserName"},
|
||||
):
|
||||
resp = await cloud.client.async_google_message(data)
|
||||
|
||||
assert resp['requestId'] == reqid
|
||||
payload = resp['payload']
|
||||
assert resp["requestId"] == reqid
|
||||
payload = resp["payload"]
|
||||
|
||||
assert payload['agentUserId'] == 'myUserName'
|
||||
assert payload["agentUserId"] == "myUserName"
|
||||
|
||||
devices = payload['devices']
|
||||
devices = payload["devices"]
|
||||
assert len(devices) == 1
|
||||
|
||||
device = devices[0]
|
||||
assert device['id'] == 'switch.test'
|
||||
assert device['name']['name'] == 'Config name'
|
||||
assert device['name']['nicknames'] == ['Config alias']
|
||||
assert device['type'] == 'action.devices.types.SWITCH'
|
||||
assert device['roomHint'] == 'living room'
|
||||
assert device["id"] == "switch.test"
|
||||
assert device["name"]["name"] == "Config name"
|
||||
assert device["name"]["nicknames"] == ["Config alias"]
|
||||
assert device["type"] == "action.devices.types.SWITCH"
|
||||
assert device["roomHint"] == "living room"
|
||||
|
||||
|
||||
async def test_handler_google_actions_disabled(hass, mock_cloud_fixture):
|
||||
"""Test handler Google Actions when user has disabled it."""
|
||||
mock_cloud_fixture[PREF_ENABLE_GOOGLE] = False
|
||||
|
||||
with patch('hass_nabucasa.Cloud.start', return_value=mock_coro()):
|
||||
assert await async_setup_component(hass, 'cloud', {})
|
||||
with patch("hass_nabucasa.Cloud.start", return_value=mock_coro()):
|
||||
assert await async_setup_component(hass, "cloud", {})
|
||||
|
||||
reqid = '5711642932632160983'
|
||||
data = {'requestId': reqid, 'inputs': [{'intent': 'action.devices.SYNC'}]}
|
||||
reqid = "5711642932632160983"
|
||||
data = {"requestId": reqid, "inputs": [{"intent": "action.devices.SYNC"}]}
|
||||
|
||||
cloud = hass.data['cloud']
|
||||
cloud = hass.data["cloud"]
|
||||
resp = await cloud.client.async_google_message(data)
|
||||
|
||||
assert resp['requestId'] == reqid
|
||||
assert resp['payload']['errorCode'] == 'deviceTurnedOff'
|
||||
assert resp["requestId"] == reqid
|
||||
assert resp["payload"]["errorCode"] == "deviceTurnedOff"
|
||||
|
||||
|
||||
async def test_webhook_msg(hass):
|
||||
"""Test webhook msg."""
|
||||
with patch('hass_nabucasa.Cloud.start', return_value=mock_coro()):
|
||||
setup = await async_setup_component(hass, 'cloud', {
|
||||
'cloud': {}
|
||||
})
|
||||
with patch("hass_nabucasa.Cloud.start", return_value=mock_coro()):
|
||||
setup = await async_setup_component(hass, "cloud", {"cloud": {}})
|
||||
assert setup
|
||||
cloud = hass.data['cloud']
|
||||
cloud = hass.data["cloud"]
|
||||
|
||||
await cloud.client.prefs.async_initialize()
|
||||
await cloud.client.prefs.async_update(cloudhooks={
|
||||
'hello': {
|
||||
'webhook_id': 'mock-webhook-id',
|
||||
'cloudhook_id': 'mock-cloud-id'
|
||||
await cloud.client.prefs.async_update(
|
||||
cloudhooks={
|
||||
"hello": {"webhook_id": "mock-webhook-id", "cloudhook_id": "mock-cloud-id"}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
received = []
|
||||
|
||||
async def handler(hass, webhook_id, request):
|
||||
"""Handle a webhook."""
|
||||
received.append(request)
|
||||
return web.json_response({'from': 'handler'})
|
||||
return web.json_response({"from": "handler"})
|
||||
|
||||
hass.components.webhook.async_register(
|
||||
'test', 'Test', 'mock-webhook-id', handler)
|
||||
hass.components.webhook.async_register("test", "Test", "mock-webhook-id", handler)
|
||||
|
||||
response = await cloud.client.async_webhook_message({
|
||||
'cloudhook_id': 'mock-cloud-id',
|
||||
'body': '{"hello": "world"}',
|
||||
'headers': {
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
'method': 'POST',
|
||||
'query': None,
|
||||
})
|
||||
response = await cloud.client.async_webhook_message(
|
||||
{
|
||||
"cloudhook_id": "mock-cloud-id",
|
||||
"body": '{"hello": "world"}',
|
||||
"headers": {"content-type": "application/json"},
|
||||
"method": "POST",
|
||||
"query": None,
|
||||
}
|
||||
)
|
||||
|
||||
assert response == {
|
||||
'status': 200,
|
||||
'body': '{"from": "handler"}',
|
||||
'headers': {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
"status": 200,
|
||||
"body": '{"from": "handler"}',
|
||||
"headers": {"Content-Type": "application/json"},
|
||||
}
|
||||
|
||||
assert len(received) == 1
|
||||
assert await received[0].json() == {
|
||||
'hello': 'world'
|
||||
}
|
||||
assert await received[0].json() == {"hello": "world"}
|
||||
|
||||
|
||||
async def test_google_config_expose_entity(
|
||||
hass, mock_cloud_setup, mock_cloud_login):
|
||||
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')
|
||||
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,
|
||||
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):
|
||||
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')
|
||||
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,
|
||||
entity_id="light.kitchen", disable_2fa=True
|
||||
)
|
||||
|
||||
assert not cloud_client.google_config.should_2fa(state)
|
||||
File diff suppressed because it is too large.
Load diff
@@ -9,8 +9,7 @@ from homeassistant.auth.const import GROUP_ID_ADMIN
|
||||
from homeassistant.components import cloud
|
||||
from homeassistant.components.cloud.const import DOMAIN
|
||||
from homeassistant.components.cloud.prefs import STORAGE_KEY
|
||||
from homeassistant.const import (
|
||||
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.setup import async_setup_component
|
||||
from tests.common import mock_coro
|
||||
|
||||
@@ -18,32 +17,36 @@ from tests.common import mock_coro
|
||||
async def test_constructor_loads_info_from_config(hass):
|
||||
"""Test non-dev mode loads info from SERVERS constant."""
|
||||
with patch("hass_nabucasa.Cloud.start", return_value=mock_coro()):
|
||||
result = await async_setup_component(hass, 'cloud', {
|
||||
'http': {},
|
||||
'cloud': {
|
||||
cloud.CONF_MODE: cloud.MODE_DEV,
|
||||
'cognito_client_id': 'test-cognito_client_id',
|
||||
'user_pool_id': 'test-user_pool_id',
|
||||
'region': 'test-region',
|
||||
'relayer': 'test-relayer',
|
||||
}
|
||||
})
|
||||
result = await async_setup_component(
|
||||
hass,
|
||||
"cloud",
|
||||
{
|
||||
"http": {},
|
||||
"cloud": {
|
||||
cloud.CONF_MODE: cloud.MODE_DEV,
|
||||
"cognito_client_id": "test-cognito_client_id",
|
||||
"user_pool_id": "test-user_pool_id",
|
||||
"region": "test-region",
|
||||
"relayer": "test-relayer",
|
||||
},
|
||||
},
|
||||
)
|
||||
assert result
|
||||
|
||||
cl = hass.data['cloud']
|
||||
cl = hass.data["cloud"]
|
||||
assert cl.mode == cloud.MODE_DEV
|
||||
assert cl.cognito_client_id == 'test-cognito_client_id'
|
||||
assert cl.user_pool_id == 'test-user_pool_id'
|
||||
assert cl.region == 'test-region'
|
||||
assert cl.relayer == 'test-relayer'
|
||||
assert cl.cognito_client_id == "test-cognito_client_id"
|
||||
assert cl.user_pool_id == "test-user_pool_id"
|
||||
assert cl.region == "test-region"
|
||||
assert cl.relayer == "test-relayer"
|
||||
|
||||
|
||||
async def test_remote_services(hass, mock_cloud_fixture, hass_read_only_user):
|
||||
"""Setup cloud component and test services."""
|
||||
cloud = hass.data[DOMAIN]
|
||||
|
||||
assert hass.services.has_service(DOMAIN, 'remote_connect')
|
||||
assert hass.services.has_service(DOMAIN, 'remote_disconnect')
|
||||
assert hass.services.has_service(DOMAIN, "remote_connect")
|
||||
assert hass.services.has_service(DOMAIN, "remote_disconnect")
|
||||
|
||||
with patch(
|
||||
"hass_nabucasa.remote.RemoteUI.connect", return_value=mock_coro()
|
||||
@@ -56,8 +59,7 @@ async def test_remote_services(hass, mock_cloud_fixture, hass_read_only_user):
|
||||
with patch(
|
||||
"hass_nabucasa.remote.RemoteUI.disconnect", return_value=mock_coro()
|
||||
) as mock_disconnect:
|
||||
await hass.services.async_call(
|
||||
DOMAIN, "remote_disconnect", blocking=True)
|
||||
await hass.services.async_call(DOMAIN, "remote_disconnect", blocking=True)
|
||||
|
||||
assert mock_disconnect.called
|
||||
assert not cloud.client.remote_autostart
|
||||
@@ -68,8 +70,9 @@ async def test_remote_services(hass, mock_cloud_fixture, hass_read_only_user):
|
||||
with patch(
|
||||
"hass_nabucasa.remote.RemoteUI.connect", return_value=mock_coro()
|
||||
) as mock_connect, pytest.raises(Unauthorized):
|
||||
await hass.services.async_call(DOMAIN, "remote_connect", blocking=True,
|
||||
context=non_admin_context)
|
||||
await hass.services.async_call(
|
||||
DOMAIN, "remote_connect", blocking=True, context=non_admin_context
|
||||
)
|
||||
|
||||
assert mock_connect.called is False
|
||||
|
||||
@@ -77,25 +80,21 @@ async def test_remote_services(hass, mock_cloud_fixture, hass_read_only_user):
|
||||
"hass_nabucasa.remote.RemoteUI.disconnect", return_value=mock_coro()
|
||||
) as mock_disconnect, pytest.raises(Unauthorized):
|
||||
await hass.services.async_call(
|
||||
DOMAIN, "remote_disconnect", blocking=True,
|
||||
context=non_admin_context)
|
||||
DOMAIN, "remote_disconnect", blocking=True, context=non_admin_context
|
||||
)
|
||||
|
||||
assert mock_disconnect.called is False
|
||||
|
||||
|
||||
async def test_startup_shutdown_events(hass, mock_cloud_fixture):
|
||||
"""Test if the cloud will start on startup event."""
|
||||
with patch(
|
||||
"hass_nabucasa.Cloud.start", return_value=mock_coro()
|
||||
) as mock_start:
|
||||
with patch("hass_nabucasa.Cloud.start", return_value=mock_coro()) as mock_start:
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_start.called
|
||||
|
||||
with patch(
|
||||
"hass_nabucasa.Cloud.stop", return_value=mock_coro()
|
||||
) as mock_stop:
|
||||
with patch("hass_nabucasa.Cloud.stop", return_value=mock_coro()) as mock_stop:
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -104,53 +103,51 @@ async def test_startup_shutdown_events(hass, mock_cloud_fixture):
|
||||
|
||||
async def test_setup_existing_cloud_user(hass, hass_storage):
|
||||
"""Test setup with API push default data."""
|
||||
user = await hass.auth.async_create_system_user('Cloud test')
|
||||
hass_storage[STORAGE_KEY] = {
|
||||
'version': 1,
|
||||
'data': {
|
||||
'cloud_user': user.id
|
||||
}
|
||||
}
|
||||
with patch('hass_nabucasa.Cloud.start', return_value=mock_coro()):
|
||||
result = await async_setup_component(hass, 'cloud', {
|
||||
'http': {},
|
||||
'cloud': {
|
||||
cloud.CONF_MODE: cloud.MODE_DEV,
|
||||
'cognito_client_id': 'test-cognito_client_id',
|
||||
'user_pool_id': 'test-user_pool_id',
|
||||
'region': 'test-region',
|
||||
'relayer': 'test-relayer',
|
||||
}
|
||||
})
|
||||
user = await hass.auth.async_create_system_user("Cloud test")
|
||||
hass_storage[STORAGE_KEY] = {"version": 1, "data": {"cloud_user": user.id}}
|
||||
with patch("hass_nabucasa.Cloud.start", return_value=mock_coro()):
|
||||
result = await async_setup_component(
|
||||
hass,
|
||||
"cloud",
|
||||
{
|
||||
"http": {},
|
||||
"cloud": {
|
||||
cloud.CONF_MODE: cloud.MODE_DEV,
|
||||
"cognito_client_id": "test-cognito_client_id",
|
||||
"user_pool_id": "test-user_pool_id",
|
||||
"region": "test-region",
|
||||
"relayer": "test-relayer",
|
||||
},
|
||||
},
|
||||
)
|
||||
assert result
|
||||
|
||||
assert hass_storage[STORAGE_KEY]['data']['cloud_user'] == user.id
|
||||
assert hass_storage[STORAGE_KEY]["data"]["cloud_user"] == user.id
|
||||
|
||||
|
||||
async def test_setup_invalid_cloud_user(hass, hass_storage):
|
||||
"""Test setup with API push default data."""
|
||||
hass_storage[STORAGE_KEY] = {
|
||||
'version': 1,
|
||||
'data': {
|
||||
'cloud_user': 'non-existing'
|
||||
}
|
||||
}
|
||||
with patch('hass_nabucasa.Cloud.start', return_value=mock_coro()):
|
||||
result = await async_setup_component(hass, 'cloud', {
|
||||
'http': {},
|
||||
'cloud': {
|
||||
cloud.CONF_MODE: cloud.MODE_DEV,
|
||||
'cognito_client_id': 'test-cognito_client_id',
|
||||
'user_pool_id': 'test-user_pool_id',
|
||||
'region': 'test-region',
|
||||
'relayer': 'test-relayer',
|
||||
}
|
||||
})
|
||||
hass_storage[STORAGE_KEY] = {"version": 1, "data": {"cloud_user": "non-existing"}}
|
||||
with patch("hass_nabucasa.Cloud.start", return_value=mock_coro()):
|
||||
result = await async_setup_component(
|
||||
hass,
|
||||
"cloud",
|
||||
{
|
||||
"http": {},
|
||||
"cloud": {
|
||||
cloud.CONF_MODE: cloud.MODE_DEV,
|
||||
"cognito_client_id": "test-cognito_client_id",
|
||||
"user_pool_id": "test-user_pool_id",
|
||||
"region": "test-region",
|
||||
"relayer": "test-relayer",
|
||||
},
|
||||
},
|
||||
)
|
||||
assert result
|
||||
|
||||
assert hass_storage[STORAGE_KEY]['data']['cloud_user'] != 'non-existing'
|
||||
assert hass_storage[STORAGE_KEY]["data"]["cloud_user"] != "non-existing"
|
||||
cloud_user = await hass.auth.async_get_user(
|
||||
hass_storage[STORAGE_KEY]['data']['cloud_user']
|
||||
hass_storage[STORAGE_KEY]["data"]["cloud_user"]
|
||||
)
|
||||
|
||||
assert cloud_user
|
||||
@@ -159,27 +156,26 @@ async def test_setup_invalid_cloud_user(hass, hass_storage):
|
||||
|
||||
async def test_setup_setup_cloud_user(hass, hass_storage):
|
||||
"""Test setup with API push default data."""
|
||||
hass_storage[STORAGE_KEY] = {
|
||||
'version': 1,
|
||||
'data': {
|
||||
'cloud_user': None
|
||||
}
|
||||
}
|
||||
with patch('hass_nabucasa.Cloud.start', return_value=mock_coro()):
|
||||
result = await async_setup_component(hass, 'cloud', {
|
||||
'http': {},
|
||||
'cloud': {
|
||||
cloud.CONF_MODE: cloud.MODE_DEV,
|
||||
'cognito_client_id': 'test-cognito_client_id',
|
||||
'user_pool_id': 'test-user_pool_id',
|
||||
'region': 'test-region',
|
||||
'relayer': 'test-relayer',
|
||||
}
|
||||
})
|
||||
hass_storage[STORAGE_KEY] = {"version": 1, "data": {"cloud_user": None}}
|
||||
with patch("hass_nabucasa.Cloud.start", return_value=mock_coro()):
|
||||
result = await async_setup_component(
|
||||
hass,
|
||||
"cloud",
|
||||
{
|
||||
"http": {},
|
||||
"cloud": {
|
||||
cloud.CONF_MODE: cloud.MODE_DEV,
|
||||
"cognito_client_id": "test-cognito_client_id",
|
||||
"user_pool_id": "test-user_pool_id",
|
||||
"region": "test-region",
|
||||
"relayer": "test-relayer",
|
||||
},
|
||||
},
|
||||
)
|
||||
assert result
|
||||
|
||||
cloud_user = await hass.auth.async_get_user(
|
||||
hass_storage[STORAGE_KEY]['data']['cloud_user']
|
||||
hass_storage[STORAGE_KEY]["data"]["cloud_user"]
|
||||
)
|
||||
|
||||
assert cloud_user
|
||||
@@ -188,20 +184,21 @@ async def test_setup_setup_cloud_user(hass, hass_storage):
|
||||
|
||||
async def test_on_connect(hass, mock_cloud_fixture):
|
||||
"""Test cloud on connect triggers."""
|
||||
cl = hass.data['cloud']
|
||||
cl = hass.data["cloud"]
|
||||
|
||||
assert len(cl.iot._on_connect) == 4
|
||||
|
||||
assert len(hass.states.async_entity_ids('binary_sensor')) == 0
|
||||
assert len(hass.states.async_entity_ids("binary_sensor")) == 0
|
||||
|
||||
assert 'async_setup' in str(cl.iot._on_connect[-1])
|
||||
assert "async_setup" in str(cl.iot._on_connect[-1])
|
||||
await cl.iot._on_connect[-1]()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_entity_ids('binary_sensor')) == 1
|
||||
assert len(hass.states.async_entity_ids("binary_sensor")) == 1
|
||||
|
||||
with patch('homeassistant.helpers.discovery.async_load_platform',
|
||||
side_effect=mock_coro) as mock_load:
|
||||
with patch(
|
||||
"homeassistant.helpers.discovery.async_load_platform", side_effect=mock_coro
|
||||
) as mock_load:
|
||||
await cl.iot._on_connect[-1]()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
@@ -6,24 +6,21 @@ from homeassistant.components.cloud import utils
|
||||
|
||||
def test_serialize_text():
|
||||
"""Test serializing a text response."""
|
||||
response = web.Response(status=201, text='Hello')
|
||||
response = web.Response(status=201, text="Hello")
|
||||
assert utils.aiohttp_serialize_response(response) == {
|
||||
'status': 201,
|
||||
'body': 'Hello',
|
||||
'headers': {'Content-Type': 'text/plain; charset=utf-8'},
|
||||
"status": 201,
|
||||
"body": "Hello",
|
||||
"headers": {"Content-Type": "text/plain; charset=utf-8"},
|
||||
}
|
||||
|
||||
|
||||
def test_serialize_body_str():
|
||||
"""Test serializing a response with a str as body."""
|
||||
response = web.Response(status=201, body='Hello')
|
||||
response = web.Response(status=201, body="Hello")
|
||||
assert utils.aiohttp_serialize_response(response) == {
|
||||
'status': 201,
|
||||
'body': 'Hello',
|
||||
'headers': {
|
||||
'Content-Length': '5',
|
||||
'Content-Type': 'text/plain; charset=utf-8'
|
||||
},
|
||||
"status": 201,
|
||||
"body": "Hello",
|
||||
"headers": {"Content-Length": "5", "Content-Type": "text/plain; charset=utf-8"},
|
||||
}
|
||||
|
||||
|
||||
@@ -31,20 +28,19 @@ def test_serialize_body_None():
|
||||
"""Test serializing a response with a str as body."""
|
||||
response = web.Response(status=201, body=None)
|
||||
assert utils.aiohttp_serialize_response(response) == {
|
||||
'status': 201,
|
||||
'body': None,
|
||||
'headers': {
|
||||
},
|
||||
"status": 201,
|
||||
"body": None,
|
||||
"headers": {},
|
||||
}
|
||||
|
||||
|
||||
def test_serialize_body_bytes():
|
||||
"""Test serializing a response with a str as body."""
|
||||
response = web.Response(status=201, body=b'Hello')
|
||||
response = web.Response(status=201, body=b"Hello")
|
||||
assert utils.aiohttp_serialize_response(response) == {
|
||||
'status': 201,
|
||||
'body': 'Hello',
|
||||
'headers': {},
|
||||
"status": 201,
|
||||
"body": "Hello",
|
||||
"headers": {},
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +48,7 @@ def test_serialize_json():
|
||||
"""Test serializing a JSON response."""
|
||||
response = web.json_response({"how": "what"})
|
||||
assert utils.aiohttp_serialize_response(response) == {
|
||||
'status': 200,
|
||||
'body': '{"how": "what"}',
|
||||
'headers': {'Content-Type': 'application/json; charset=utf-8'},
|
||||
"status": 200,
|
||||
"body": '{"how": "what"}',
|
||||
"headers": {"Content-Type": "application/json; charset=utf-8"},
|
||||
}
|
||||
Reference in new issue
Block a user