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
|
|
|
|
|
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
async def test_bridge_setup(hass):
|
2018-03-30 03:15:40 +00:00
|
|
|
"""Test a successful setup."""
|
|
|
|
entry = Mock()
|
2019-12-16 18:45:09 +00:00
|
|
|
api = Mock(initialize=mock_coro)
|
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-12-16 18:45:09 +00:00
|
|
|
with patch("aiohue.Bridge", return_value=api), patch.object(
|
|
|
|
hass.config_entries, "async_forward_entry_setup"
|
|
|
|
) as mock_forward:
|
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-12-16 18:45:09 +00:00
|
|
|
assert len(mock_forward.mock_calls) == 3
|
|
|
|
forward_entries = set(c[1][1] for c in mock_forward.mock_calls)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert forward_entries == set(["light", "binary_sensor", "sensor"])
|
2018-03-17 03:27:05 +00:00
|
|
|
|
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
async def test_bridge_setup_invalid_username(hass):
|
2018-03-30 03:15:40 +00:00
|
|
|
"""Test we start config flow if username is no longer whitelisted."""
|
|
|
|
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-12-16 18:45:09 +00:00
|
|
|
with patch.object(
|
|
|
|
bridge, "authenticate_bridge", side_effect=errors.AuthenticationRequired
|
|
|
|
), patch.object(
|
|
|
|
hass.config_entries.flow, "async_init", return_value=mock_coro()
|
|
|
|
) as mock_init:
|
2018-03-30 03:15:40 +00:00
|
|
|
assert await hue_bridge.async_setup() is False
|
2018-03-17 03:27:05 +00:00
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
assert len(mock_init.mock_calls) == 1
|
|
|
|
assert mock_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."""
|
|
|
|
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-12-16 18:45:09 +00:00
|
|
|
bridge, "authenticate_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
|
|
|
|
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
async def test_reset_if_entry_had_wrong_auth(hass):
|
2018-04-12 12:28:54 +00:00
|
|
|
"""Test calling reset when the entry contained wrong auth."""
|
|
|
|
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-12-16 18:45:09 +00:00
|
|
|
with patch.object(
|
|
|
|
bridge, "authenticate_bridge", side_effect=errors.AuthenticationRequired
|
|
|
|
), patch.object(bridge, "create_config_flow") as mock_create:
|
2018-04-12 12:28:54 +00:00
|
|
|
assert await hue_bridge.async_setup() is False
|
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
assert len(mock_create.mock_calls) == 1
|
2018-04-12 12:28:54 +00:00
|
|
|
|
|
|
|
assert await hue_bridge.async_reset()
|
|
|
|
|
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
async def test_reset_unloads_entry_if_setup(hass):
|
2018-04-12 12:28:54 +00:00
|
|
|
"""Test calling reset while the entry has been setup."""
|
|
|
|
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-12-16 18:45:09 +00:00
|
|
|
with patch.object(
|
|
|
|
bridge, "authenticate_bridge", return_value=mock_coro(Mock())
|
|
|
|
), patch("aiohue.Bridge", return_value=Mock()), patch.object(
|
|
|
|
hass.config_entries, "async_forward_entry_setup"
|
|
|
|
) as mock_forward:
|
2018-04-12 12:28:54 +00:00
|
|
|
assert await hue_bridge.async_setup() is True
|
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
assert len(hass.services.async_services()) == 1
|
|
|
|
assert len(mock_forward.mock_calls) == 3
|
2018-04-12 12:28:54 +00:00
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
with patch.object(
|
|
|
|
hass.config_entries, "async_forward_entry_unload", return_value=mock_coro(True)
|
|
|
|
) as mock_forward:
|
|
|
|
assert await hue_bridge.async_reset()
|
2018-04-12 12:28:54 +00:00
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
assert len(mock_forward.mock_calls) == 3
|
|
|
|
assert len(hass.services.async_services()) == 0
|
2019-10-28 15:45:08 +00:00
|
|
|
|
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
async def test_handle_unauthorized(hass):
|
2019-10-28 15:45:08 +00:00
|
|
|
"""Test handling an unauthorized error on update."""
|
2020-03-31 15:17:09 +00:00
|
|
|
entry = Mock(async_setup=Mock(return_value=mock_coro(Mock())))
|
2019-10-28 15:45:08 +00:00
|
|
|
entry.data = {"host": "1.2.3.4", "username": "mock-username"}
|
|
|
|
hue_bridge = bridge.HueBridge(hass, entry, False, False)
|
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
with patch.object(
|
|
|
|
bridge, "authenticate_bridge", return_value=mock_coro(Mock())
|
|
|
|
), patch("aiohue.Bridge", return_value=Mock()):
|
2019-10-28 15:45:08 +00:00
|
|
|
assert await hue_bridge.async_setup() is True
|
|
|
|
|
|
|
|
assert hue_bridge.authorized is True
|
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
with patch.object(bridge, "create_config_flow") as mock_create:
|
|
|
|
await hue_bridge.handle_unauthorized_error()
|
2019-10-28 15:45:08 +00:00
|
|
|
|
|
|
|
assert hue_bridge.authorized is False
|
2019-12-16 18:45:09 +00:00
|
|
|
assert len(mock_create.mock_calls) == 1
|
|
|
|
assert mock_create.mock_calls[0][1][1] == "1.2.3.4"
|