2018-03-17 03:27:05 +00:00
|
|
|
"""Test Hue bridge."""
|
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
from homeassistant.components.hue import bridge, errors
|
2018-03-17 03:27:05 +00:00
|
|
|
|
|
|
|
from tests.common import mock_coro
|
|
|
|
|
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
async def test_bridge_setup():
|
|
|
|
"""Test a successful setup."""
|
|
|
|
hass = Mock()
|
|
|
|
entry = Mock()
|
|
|
|
api = Mock()
|
|
|
|
entry.data = {'host': '1.2.3.4', 'username': 'mock-username'}
|
|
|
|
hue_bridge = bridge.HueBridge(hass, entry, False, False)
|
2018-03-17 03:27:05 +00:00
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
with patch.object(bridge, 'get_bridge', return_value=mock_coro(api)):
|
|
|
|
assert await hue_bridge.async_setup() is True
|
2018-03-17 03:27:05 +00:00
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
assert hue_bridge.api is api
|
|
|
|
assert len(hass.helpers.discovery.async_load_platform.mock_calls) == 1
|
|
|
|
assert hass.helpers.discovery.async_load_platform.mock_calls[0][1][2] == {
|
|
|
|
'host': '1.2.3.4'
|
|
|
|
}
|
2018-03-17 03:27:05 +00:00
|
|
|
|
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
async def test_bridge_setup_invalid_username():
|
|
|
|
"""Test we start config flow if username is no longer whitelisted."""
|
|
|
|
hass = Mock()
|
|
|
|
entry = Mock()
|
|
|
|
entry.data = {'host': '1.2.3.4', 'username': 'mock-username'}
|
|
|
|
hue_bridge = bridge.HueBridge(hass, entry, False, False)
|
2018-03-17 03:27:05 +00:00
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
with patch.object(bridge, 'get_bridge',
|
|
|
|
side_effect=errors.AuthenticationRequired):
|
|
|
|
assert await hue_bridge.async_setup() is False
|
2018-03-17 03:27:05 +00:00
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
assert len(hass.async_add_job.mock_calls) == 1
|
|
|
|
assert len(hass.config_entries.flow.async_init.mock_calls) == 1
|
|
|
|
assert hass.config_entries.flow.async_init.mock_calls[0][2]['data'] == {
|
|
|
|
'host': '1.2.3.4'
|
|
|
|
}
|
2018-03-17 03:27:05 +00:00
|
|
|
|
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
async def test_bridge_setup_timeout(hass):
|
|
|
|
"""Test we retry to connect if we cannot connect."""
|
|
|
|
hass = Mock()
|
|
|
|
entry = Mock()
|
|
|
|
entry.data = {'host': '1.2.3.4', 'username': 'mock-username'}
|
|
|
|
hue_bridge = bridge.HueBridge(hass, entry, False, False)
|
2018-03-17 03:27:05 +00:00
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
with patch.object(bridge, 'get_bridge', side_effect=errors.CannotConnect):
|
|
|
|
assert await hue_bridge.async_setup() is False
|
2018-03-17 03:27:05 +00:00
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
assert len(hass.helpers.event.async_call_later.mock_calls) == 1
|
|
|
|
# Assert we are going to wait 2 seconds
|
|
|
|
assert hass.helpers.event.async_call_later.mock_calls[0][1][0] == 2
|