Add support for Automatic OAuth2 authentication (#8962)

* Add support for Automatic OAuth2 authentication

* Fix async conversion of configurator

* Rename method for async

* Use hass.components to get configurator component

* Fix typo

* Move session data to hidden directory

* Make configurator callback optional
This commit is contained in:
Adam Mills authored and Paulus Schoutsen committed 2017-08-13 22:37:50 -07:00
1 parent 8fcec03adf
commit 19d1d748d4
25 files changed
+314 -149

No files matched your search

@@ -7,12 +7,16 @@ import aioautomatic
from homeassistant.components.device_tracker.automatic import (
async_setup_scanner)
from tests.common import mock_http_component
_LOGGER = logging.getLogger(__name__)
@patch('aioautomatic.Client.create_session_from_password')
def test_invalid_credentials(mock_create_session, hass):
"""Test with invalid credentials."""
mock_http_component(hass)
@asyncio.coroutine
def get_session(*args, **kwargs):
"""Return the test session."""
@@ -34,8 +38,15 @@ def test_invalid_credentials(mock_create_session, hass):
@patch('aioautomatic.Client.create_session_from_password')
def test_valid_credentials(mock_create_session, hass):
@patch('aioautomatic.Client.ws_connect')
@patch('json.dump')
@patch('os.makedirs')
@patch('homeassistant.components.device_tracker.automatic.open', create=True)
def test_valid_credentials(mock_open, mock_os_makedirs, mock_json_dump,
mock_ws_connect, mock_create_session, hass):
"""Test with valid credentials."""
mock_http_component(hass)
session = MagicMock()
vehicle = MagicMock()
trip = MagicMock()
@@ -66,13 +77,21 @@ def test_valid_credentials(mock_create_session, hass):
return [trip]
mock_create_session.side_effect = get_session
session.ws_connect = MagicMock()
session.get_vehicles.side_effect = get_vehicles
session.get_trips.side_effect = get_trips
session.refresh_token = 'mock_refresh_token'
@asyncio.coroutine
def ws_connect():
return asyncio.Future(loop=hass.loop)
mock_ws_connect.side_effect = ws_connect
config = {
'platform': 'automatic',
'username': 'bad_username',
'password': 'bad_password',
'username': 'good_username',
'password': 'good_password',
'client_id': 'client_id',
'secret': 'client_secret',
'devices': None,
@@ -80,6 +99,8 @@ def test_valid_credentials(mock_create_session, hass):
result = hass.loop.run_until_complete(
async_setup_scanner(hass, config, mock_see))
hass.async_block_till_done()
assert result
assert mock_see.called
assert len(mock_see.mock_calls) == 2
@@ -89,3 +110,9 @@ def test_valid_credentials(mock_create_session, hass):
assert mock_see.mock_calls[0][2]['attributes'] == {'fuel_level': 45.6}
assert mock_see.mock_calls[0][2]['gps'] == (45.567, 34.345)
assert mock_see.mock_calls[0][2]['gps_accuracy'] == 5.6
assert mock_json_dump.called
assert len(mock_json_dump.mock_calls) == 1
assert mock_json_dump.mock_calls[0][1][0] == {
'refresh_token': 'mock_refresh_token'
}