Add command to refresh auth (#20183)
This commit is contained in:
parent
136364f5db
commit
0fe5d567a2
2 changed files with 32 additions and 7 deletions
|
@ -316,15 +316,20 @@ def async_handle_google_actions(hass, cloud, payload):
|
||||||
|
|
||||||
|
|
||||||
@HANDLERS.register('cloud')
|
@HANDLERS.register('cloud')
|
||||||
@asyncio.coroutine
|
async def async_handle_cloud(hass, cloud, payload):
|
||||||
def async_handle_cloud(hass, cloud, payload):
|
|
||||||
"""Handle an incoming IoT message for cloud component."""
|
"""Handle an incoming IoT message for cloud component."""
|
||||||
action = payload['action']
|
action = payload['action']
|
||||||
|
|
||||||
if action == 'logout':
|
if action == 'logout':
|
||||||
yield from cloud.logout()
|
# Log out of Home Assistant Cloud
|
||||||
|
await cloud.logout()
|
||||||
_LOGGER.error("You have been logged out from Home Assistant cloud: %s",
|
_LOGGER.error("You have been logged out from Home Assistant cloud: %s",
|
||||||
payload['reason'])
|
payload['reason'])
|
||||||
|
elif action == 'refresh_auth':
|
||||||
|
# Refresh the auth token between now and payload['seconds']
|
||||||
|
hass.helpers.event.async_call_later(
|
||||||
|
random.randint(0, payload['seconds']),
|
||||||
|
lambda now: auth_api.check_token(cloud))
|
||||||
else:
|
else:
|
||||||
_LOGGER.warning("Received unknown cloud action: %s", action)
|
_LOGGER.warning("Received unknown cloud action: %s", action)
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,9 @@ from homeassistant.components.cloud import (
|
||||||
Cloud, iot, auth_api, MODE_DEV)
|
Cloud, iot, auth_api, MODE_DEV)
|
||||||
from homeassistant.components.cloud.const import (
|
from homeassistant.components.cloud.const import (
|
||||||
PREF_ENABLE_ALEXA, PREF_ENABLE_GOOGLE)
|
PREF_ENABLE_ALEXA, PREF_ENABLE_GOOGLE)
|
||||||
|
from homeassistant.util import dt as dt_util
|
||||||
from tests.components.alexa import test_smart_home as test_alexa
|
from tests.components.alexa import test_smart_home as test_alexa
|
||||||
from tests.common import mock_coro
|
from tests.common import mock_coro, async_fire_time_changed
|
||||||
|
|
||||||
from . import mock_cloud_prefs
|
from . import mock_cloud_prefs
|
||||||
|
|
||||||
|
@ -147,17 +148,36 @@ def test_handler_forwarding():
|
||||||
assert payload == 'payload'
|
assert payload == 'payload'
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def test_handling_core_messages_logout(hass, mock_cloud):
|
||||||
def test_handling_core_messages(hass, mock_cloud):
|
|
||||||
"""Test handling core messages."""
|
"""Test handling core messages."""
|
||||||
mock_cloud.logout.return_value = mock_coro()
|
mock_cloud.logout.return_value = mock_coro()
|
||||||
yield from iot.async_handle_cloud(hass, mock_cloud, {
|
await iot.async_handle_cloud(hass, mock_cloud, {
|
||||||
'action': 'logout',
|
'action': 'logout',
|
||||||
'reason': 'Logged in at two places.'
|
'reason': 'Logged in at two places.'
|
||||||
})
|
})
|
||||||
assert len(mock_cloud.logout.mock_calls) == 1
|
assert len(mock_cloud.logout.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
|
async def test_handling_core_messages_refresh_auth(hass, mock_cloud):
|
||||||
|
"""Test handling core messages."""
|
||||||
|
mock_cloud.hass = hass
|
||||||
|
with patch('random.randint', return_value=0) as mock_rand, patch(
|
||||||
|
'homeassistant.components.cloud.auth_api.check_token'
|
||||||
|
) as mock_check:
|
||||||
|
await iot.async_handle_cloud(hass, mock_cloud, {
|
||||||
|
'action': 'refresh_auth',
|
||||||
|
'seconds': 230,
|
||||||
|
})
|
||||||
|
async_fire_time_changed(hass, dt_util.utcnow())
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert len(mock_rand.mock_calls) == 1
|
||||||
|
assert mock_rand.mock_calls[0][1] == (0, 230)
|
||||||
|
|
||||||
|
assert len(mock_check.mock_calls) == 1
|
||||||
|
assert mock_check.mock_calls[0][1][0] is mock_cloud
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_cloud_getting_disconnected_by_server(mock_client, caplog, mock_cloud):
|
def test_cloud_getting_disconnected_by_server(mock_client, caplog, mock_cloud):
|
||||||
"""Test server disconnecting instance."""
|
"""Test server disconnecting instance."""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue