2018-03-17 03:27:05 +00:00
|
|
|
"""Test Hue bridge."""
|
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
|
2019-02-14 04:36:06 +00:00
|
|
|
import pytest
|
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
from homeassistant.components.hue import bridge, errors
|
2019-12-09 12:25:04 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
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()
|
2019-07-31 19:25:30 +00:00
|
|
|
entry.data = {"host": "1.2.3.4", "username": "mock-username"}
|
2018-03-30 03:15:40 +00:00
|
|
|
hue_bridge = bridge.HueBridge(hass, entry, False, False)
|
2018-03-17 03:27:05 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(bridge, "get_bridge", return_value=mock_coro(api)):
|
2018-03-30 03:15:40 +00:00
|
|
|
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
|
2019-04-18 05:13:03 +00:00
|
|
|
forward_entries = set(
|
2019-07-31 19:25:30 +00:00
|
|
|
c[1][1] for c in hass.config_entries.async_forward_entry_setup.mock_calls
|
|
|
|
)
|
2019-04-18 05:13:03 +00:00
|
|
|
assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 3
|
2019-07-31 19:25:30 +00:00
|
|
|
assert forward_entries == set(["light", "binary_sensor", "sensor"])
|
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()
|
2019-07-31 19:25:30 +00:00
|
|
|
entry.data = {"host": "1.2.3.4", "username": "mock-username"}
|
2018-03-30 03:15:40 +00:00
|
|
|
hue_bridge = bridge.HueBridge(hass, entry, False, False)
|
2018-03-17 03:27:05 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(bridge, "get_bridge", side_effect=errors.AuthenticationRequired):
|
2018-03-30 03:15:40 +00:00
|
|
|
assert await hue_bridge.async_setup() is False
|
2018-03-17 03:27:05 +00:00
|
|
|
|
2018-10-02 09:03:09 +00:00
|
|
|
assert len(hass.async_create_task.mock_calls) == 1
|
2018-03-30 03:15:40 +00:00
|
|
|
assert len(hass.config_entries.flow.async_init.mock_calls) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.config_entries.flow.async_init.mock_calls[0][2]["data"] == {
|
|
|
|
"host": "1.2.3.4"
|
2018-03-30 03:15:40 +00:00
|
|
|
}
|
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()
|
2019-07-31 19:25:30 +00:00
|
|
|
entry.data = {"host": "1.2.3.4", "username": "mock-username"}
|
2018-03-30 03:15:40 +00:00
|
|
|
hue_bridge = bridge.HueBridge(hass, entry, False, False)
|
2018-03-17 03:27:05 +00:00
|
|
|
|
2019-02-14 04:36:06 +00:00
|
|
|
with patch.object(
|
2019-07-31 19:25:30 +00:00
|
|
|
bridge, "get_bridge", side_effect=errors.CannotConnect
|
2019-02-14 04:36:06 +00:00
|
|
|
), pytest.raises(ConfigEntryNotReady):
|
|
|
|
await hue_bridge.async_setup()
|
2018-04-12 12:28:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_reset_if_entry_had_wrong_auth():
|
|
|
|
"""Test calling reset when the entry contained wrong auth."""
|
|
|
|
hass = Mock()
|
|
|
|
entry = Mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
entry.data = {"host": "1.2.3.4", "username": "mock-username"}
|
2018-04-12 12:28:54 +00:00
|
|
|
hue_bridge = bridge.HueBridge(hass, entry, False, False)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(bridge, "get_bridge", side_effect=errors.AuthenticationRequired):
|
2018-04-12 12:28:54 +00:00
|
|
|
assert await hue_bridge.async_setup() is False
|
|
|
|
|
2018-10-02 09:03:09 +00:00
|
|
|
assert len(hass.async_create_task.mock_calls) == 1
|
2018-04-12 12:28:54 +00:00
|
|
|
|
|
|
|
assert await hue_bridge.async_reset()
|
|
|
|
|
|
|
|
|
|
|
|
async def test_reset_unloads_entry_if_setup():
|
|
|
|
"""Test calling reset while the entry has been setup."""
|
|
|
|
hass = Mock()
|
|
|
|
entry = Mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
entry.data = {"host": "1.2.3.4", "username": "mock-username"}
|
2018-04-12 12:28:54 +00:00
|
|
|
hue_bridge = bridge.HueBridge(hass, entry, False, False)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(bridge, "get_bridge", return_value=mock_coro(Mock())):
|
2018-04-12 12:28:54 +00:00
|
|
|
assert await hue_bridge.async_setup() is True
|
|
|
|
|
|
|
|
assert len(hass.services.async_register.mock_calls) == 1
|
2019-04-18 05:13:03 +00:00
|
|
|
assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 3
|
2018-04-12 12:28:54 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.config_entries.async_forward_entry_unload.return_value = mock_coro(True)
|
2018-04-12 12:28:54 +00:00
|
|
|
assert await hue_bridge.async_reset()
|
|
|
|
|
2019-04-18 05:13:03 +00:00
|
|
|
assert len(hass.config_entries.async_forward_entry_unload.mock_calls) == 3
|
2018-04-12 12:28:54 +00:00
|
|
|
assert len(hass.services.async_remove.mock_calls) == 1
|
2019-10-28 15:45:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_handle_unauthorized():
|
|
|
|
"""Test handling an unauthorized error on update."""
|
|
|
|
hass = Mock()
|
|
|
|
entry = Mock()
|
|
|
|
entry.data = {"host": "1.2.3.4", "username": "mock-username"}
|
|
|
|
hue_bridge = bridge.HueBridge(hass, entry, False, False)
|
|
|
|
|
|
|
|
with patch.object(bridge, "get_bridge", return_value=mock_coro(Mock())):
|
|
|
|
assert await hue_bridge.async_setup() is True
|
|
|
|
|
|
|
|
assert hue_bridge.authorized is True
|
|
|
|
|
|
|
|
await hue_bridge.handle_unauthorized_error()
|
|
|
|
|
|
|
|
assert hue_bridge.authorized is False
|
|
|
|
assert len(hass.async_create_task.mock_calls) == 4
|
|
|
|
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"
|
|
|
|
}
|