Convert Hue to use unique ID (#30000)

* Convert Hue to use unique ID

* Fix normalization

* Store/restore unique ID

* Fix tests
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2019-12-16 19:45:09 +01:00
1 parent 575eb48feb
commit 58b5833d64
12 files changed
+385 -440

No files matched your search

+53 -50
View File
@@ -9,107 +9,110 @@ from homeassistant.exceptions import ConfigEntryNotReady
from tests.common import mock_coro
async def test_bridge_setup():
async def test_bridge_setup(hass):
"""Test a successful setup."""
hass = Mock()
entry = Mock()
api = Mock()
api = Mock(initialize=mock_coro)
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(api)):
with patch("aiohue.Bridge", return_value=api), patch.object(
hass.config_entries, "async_forward_entry_setup"
) as mock_forward:
assert await hue_bridge.async_setup() is True
assert hue_bridge.api is api
forward_entries = set(
c[1][1] for c in hass.config_entries.async_forward_entry_setup.mock_calls
)
assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 3
assert len(mock_forward.mock_calls) == 3
forward_entries = set(c[1][1] for c in mock_forward.mock_calls)
assert forward_entries == set(["light", "binary_sensor", "sensor"])
async def test_bridge_setup_invalid_username():
async def test_bridge_setup_invalid_username(hass):
"""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)
with patch.object(bridge, "get_bridge", side_effect=errors.AuthenticationRequired):
assert await hue_bridge.async_setup() is False
assert len(hass.async_create_task.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"
}
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)
with patch.object(
bridge, "get_bridge", side_effect=errors.CannotConnect
bridge, "authenticate_bridge", side_effect=errors.AuthenticationRequired
), patch.object(
hass.config_entries.flow, "async_init", return_value=mock_coro()
) as mock_init:
assert await hue_bridge.async_setup() is False
assert len(mock_init.mock_calls) == 1
assert mock_init.mock_calls[0][2]["data"] == {"host": "1.2.3.4"}
async def test_bridge_setup_timeout(hass):
"""Test we retry to connect if we cannot connect."""
entry = Mock()
entry.data = {"host": "1.2.3.4", "username": "mock-username"}
hue_bridge = bridge.HueBridge(hass, entry, False, False)
with patch.object(
bridge, "authenticate_bridge", side_effect=errors.CannotConnect
), pytest.raises(ConfigEntryNotReady):
await hue_bridge.async_setup()
async def test_reset_if_entry_had_wrong_auth():
async def test_reset_if_entry_had_wrong_auth(hass):
"""Test calling reset when the entry contained wrong auth."""
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", side_effect=errors.AuthenticationRequired):
with patch.object(
bridge, "authenticate_bridge", side_effect=errors.AuthenticationRequired
), patch.object(bridge, "create_config_flow") as mock_create:
assert await hue_bridge.async_setup() is False
assert len(hass.async_create_task.mock_calls) == 1
assert len(mock_create.mock_calls) == 1
assert await hue_bridge.async_reset()
async def test_reset_unloads_entry_if_setup():
async def test_reset_unloads_entry_if_setup(hass):
"""Test calling reset while the entry has been setup."""
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())):
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:
assert await hue_bridge.async_setup() is True
assert len(hass.services.async_register.mock_calls) == 1
assert len(hass.config_entries.async_forward_entry_setup.mock_calls) == 3
assert len(hass.services.async_services()) == 1
assert len(mock_forward.mock_calls) == 3
hass.config_entries.async_forward_entry_unload.return_value = mock_coro(True)
assert await hue_bridge.async_reset()
with patch.object(
hass.config_entries, "async_forward_entry_unload", return_value=mock_coro(True)
) as mock_forward:
assert await hue_bridge.async_reset()
assert len(hass.config_entries.async_forward_entry_unload.mock_calls) == 3
assert len(hass.services.async_remove.mock_calls) == 1
assert len(mock_forward.mock_calls) == 3
assert len(hass.services.async_services()) == 0
async def test_handle_unauthorized():
async def test_handle_unauthorized(hass):
"""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())):
with patch.object(
bridge, "authenticate_bridge", return_value=mock_coro(Mock())
), patch("aiohue.Bridge", return_value=Mock()):
assert await hue_bridge.async_setup() is True
assert hue_bridge.authorized is True
await hue_bridge.handle_unauthorized_error()
with patch.object(bridge, "create_config_flow") as mock_create:
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"
}
assert len(mock_create.mock_calls) == 1
assert mock_create.mock_calls[0][1][1] == "1.2.3.4"
+97 -138
View File
@@ -6,50 +6,52 @@ import aiohue
import pytest
import voluptuous as vol
from homeassistant.components.hue import config_flow, const, errors
from homeassistant import data_entry_flow
from homeassistant.components.hue import config_flow, const
from tests.common import MockConfigEntry, mock_coro
async def test_flow_works(hass, aioclient_mock):
async def test_flow_works(hass):
"""Test config flow ."""
aioclient_mock.get(
const.API_NUPNP, json=[{"internalipaddress": "1.2.3.4", "id": "bla"}]
)
mock_bridge = Mock()
mock_bridge.host = "1.2.3.4"
mock_bridge.username = None
mock_bridge.config.name = "Mock Bridge"
mock_bridge.id = "aabbccddeeff"
async def mock_create_user(username):
mock_bridge.username = username
mock_bridge.create_user = mock_create_user
mock_bridge.initialize.return_value = mock_coro()
flow = config_flow.HueFlowHandler()
flow.hass = hass
flow.context = {}
await flow.async_step_init()
with patch("aiohue.Bridge") as mock_bridge:
with patch(
"homeassistant.components.hue.config_flow.discover_nupnp",
return_value=mock_coro([mock_bridge]),
):
result = await flow.async_step_init()
def mock_constructor(host, websession, username=None):
"""Fake the bridge constructor."""
mock_bridge.host = host
return mock_bridge
assert result["type"] == "form"
assert result["step_id"] == "link"
mock_bridge.side_effect = mock_constructor
mock_bridge.username = "username-abc"
mock_bridge.config.name = "Mock Bridge"
mock_bridge.config.bridgeid = "bridge-id-1234"
mock_bridge.create_user.return_value = mock_coro()
mock_bridge.initialize.return_value = mock_coro()
assert flow.context["unique_id"] == "aabbccddeeff"
result = await flow.async_step_link(user_input={})
assert mock_bridge.host == "1.2.3.4"
assert len(mock_bridge.create_user.mock_calls) == 1
assert len(mock_bridge.initialize.mock_calls) == 1
result = await flow.async_step_link(user_input={})
assert result["type"] == "create_entry"
assert result["title"] == "Mock Bridge"
assert result["data"] == {
"host": "1.2.3.4",
"bridge_id": "bridge-id-1234",
"username": "username-abc",
"username": "home-assistant#test-home",
}
assert len(mock_bridge.initialize.mock_calls) == 1
async def test_flow_no_discovered_bridges(hass, aioclient_mock):
"""Test config flow discovers no bridges."""
@@ -66,9 +68,12 @@ async def test_flow_all_discovered_bridges_exist(hass, aioclient_mock):
aioclient_mock.get(
const.API_NUPNP, json=[{"internalipaddress": "1.2.3.4", "id": "bla"}]
)
MockConfigEntry(domain="hue", data={"host": "1.2.3.4"}).add_to_hass(hass)
MockConfigEntry(
domain="hue", unique_id="bla", data={"host": "1.2.3.4"}
).add_to_hass(hass)
flow = config_flow.HueFlowHandler()
flow.hass = hass
flow.context = {}
result = await flow.async_step_init()
assert result["type"] == "abort"
@@ -81,6 +86,7 @@ async def test_flow_one_bridge_discovered(hass, aioclient_mock):
)
flow = config_flow.HueFlowHandler()
flow.hass = hass
flow.context = {}
result = await flow.async_step_init()
assert result["type"] == "form"
@@ -104,10 +110,10 @@ async def test_flow_two_bridges_discovered(hass, aioclient_mock):
assert result["step_id"] == "init"
with pytest.raises(vol.Invalid):
assert result["data_schema"]({"host": "0.0.0.0"})
assert result["data_schema"]({"id": "not-discovered"})
result["data_schema"]({"host": "1.2.3.4"})
result["data_schema"]({"host": "5.6.7.8"})
result["data_schema"]({"id": "bla"})
result["data_schema"]({"id": "beer"})
async def test_flow_two_bridges_discovered_one_new(hass, aioclient_mock):
@@ -119,14 +125,17 @@ async def test_flow_two_bridges_discovered_one_new(hass, aioclient_mock):
{"internalipaddress": "5.6.7.8", "id": "beer"},
],
)
MockConfigEntry(domain="hue", data={"host": "1.2.3.4"}).add_to_hass(hass)
MockConfigEntry(
domain="hue", unique_id="bla", data={"host": "1.2.3.4"}
).add_to_hass(hass)
flow = config_flow.HueFlowHandler()
flow.hass = hass
flow.context = {}
result = await flow.async_step_init()
assert result["type"] == "form"
assert result["step_id"] == "link"
assert flow.host == "5.6.7.8"
assert flow.bridge.host == "5.6.7.8"
async def test_flow_timeout_discovery(hass):
@@ -147,6 +156,7 @@ async def test_flow_link_timeout(hass):
"""Test config flow ."""
flow = config_flow.HueFlowHandler()
flow.hass = hass
flow.bridge = Mock()
with patch("aiohue.Bridge.create_user", side_effect=asyncio.TimeoutError):
result = await flow.async_step_link({})
@@ -160,9 +170,11 @@ async def test_flow_link_button_not_pressed(hass):
"""Test config flow ."""
flow = config_flow.HueFlowHandler()
flow.hass = hass
flow.bridge = Mock(
username=None, create_user=Mock(side_effect=aiohue.LinkButtonNotPressed)
)
with patch("aiohue.Bridge.create_user", side_effect=aiohue.LinkButtonNotPressed):
result = await flow.async_step_link({})
result = await flow.async_step_link({})
assert result["type"] == "form"
assert result["step_id"] == "link"
@@ -173,6 +185,7 @@ async def test_flow_link_unknown_host(hass):
"""Test config flow ."""
flow = config_flow.HueFlowHandler()
flow.hass = hass
flow.bridge = Mock()
with patch("aiohue.Bridge.create_user", side_effect=aiohue.RequestError):
result = await flow.async_step_link({})
@@ -188,16 +201,13 @@ async def test_bridge_ssdp(hass):
flow.hass = hass
flow.context = {}
with patch.object(
config_flow, "get_bridge", side_effect=errors.AuthenticationRequired
):
result = await flow.async_step_ssdp(
{
"host": "0.0.0.0",
"serial": "1234",
"manufacturerURL": config_flow.HUE_MANUFACTURERURL,
}
)
result = await flow.async_step_ssdp(
{
"host": "0.0.0.0",
"serial": "1234",
"manufacturerURL": config_flow.HUE_MANUFACTURERURL,
}
)
assert result["type"] == "form"
assert result["step_id"] == "link"
@@ -255,47 +265,22 @@ async def test_bridge_ssdp_espalexa(hass):
async def test_bridge_ssdp_already_configured(hass):
"""Test if a discovered bridge has already been configured."""
MockConfigEntry(domain="hue", data={"host": "0.0.0.0"}).add_to_hass(hass)
MockConfigEntry(
domain="hue", unique_id="1234", data={"host": "0.0.0.0"}
).add_to_hass(hass)
flow = config_flow.HueFlowHandler()
flow.hass = hass
flow.context = {}
result = await flow.async_step_ssdp(
{
"host": "0.0.0.0",
"serial": "1234",
"manufacturerURL": config_flow.HUE_MANUFACTURERURL,
}
)
assert result["type"] == "abort"
async def test_import_with_existing_config(hass):
"""Test importing a host with an existing config file."""
flow = config_flow.HueFlowHandler()
flow.hass = hass
flow.context = {}
bridge = Mock()
bridge.username = "username-abc"
bridge.config.bridgeid = "bridge-id-1234"
bridge.config.name = "Mock Bridge"
bridge.host = "0.0.0.0"
with patch.object(
config_flow, "_find_username_from_config", return_value="mock-user"
), patch.object(config_flow, "get_bridge", return_value=mock_coro(bridge)):
result = await flow.async_step_import({"host": "0.0.0.0", "path": "bla.conf"})
assert result["type"] == "create_entry"
assert result["title"] == "Mock Bridge"
assert result["data"] == {
"host": "0.0.0.0",
"bridge_id": "bridge-id-1234",
"username": "username-abc",
}
with pytest.raises(data_entry_flow.AbortFlow):
await flow.async_step_ssdp(
{
"host": "0.0.0.0",
"serial": "1234",
"manufacturerURL": config_flow.HUE_MANUFACTURERURL,
}
)
async def test_import_with_no_config(hass):
@@ -304,45 +289,12 @@ async def test_import_with_no_config(hass):
flow.hass = hass
flow.context = {}
with patch.object(
config_flow, "get_bridge", side_effect=errors.AuthenticationRequired
):
result = await flow.async_step_import({"host": "0.0.0.0"})
result = await flow.async_step_import({"host": "0.0.0.0"})
assert result["type"] == "form"
assert result["step_id"] == "link"
async def test_import_with_existing_but_invalid_config(hass):
"""Test importing a host with a config file with invalid username."""
flow = config_flow.HueFlowHandler()
flow.hass = hass
flow.context = {}
with patch.object(
config_flow, "_find_username_from_config", return_value="mock-user"
), patch.object(
config_flow, "get_bridge", side_effect=errors.AuthenticationRequired
):
result = await flow.async_step_import({"host": "0.0.0.0", "path": "bla.conf"})
assert result["type"] == "form"
assert result["step_id"] == "link"
async def test_import_cannot_connect(hass):
"""Test importing a host that we cannot conncet to."""
flow = config_flow.HueFlowHandler()
flow.hass = hass
flow.context = {}
with patch.object(config_flow, "get_bridge", side_effect=errors.CannotConnect):
result = await flow.async_step_import({"host": "0.0.0.0"})
assert result["type"] == "abort"
assert result["reason"] == "cannot_connect"
async def test_creating_entry_removes_entries_for_same_host_or_bridge(hass):
"""Test that we clean up entries for same host and bridge.
@@ -351,38 +303,45 @@ async def test_creating_entry_removes_entries_for_same_host_or_bridge(hass):
all existing entries that either have same IP or same bridge_id.
"""
orig_entry = MockConfigEntry(
domain="hue",
data={"host": "0.0.0.0", "bridge_id": "id-1234"},
unique_id="id-1234",
domain="hue", data={"host": "0.0.0.0", "username": "aaaa"}, unique_id="id-1234",
)
orig_entry.add_to_hass(hass)
MockConfigEntry(
domain="hue",
data={"host": "1.2.3.4", "bridge_id": "id-5678"},
unique_id="id-5678",
domain="hue", data={"host": "1.2.3.4", "username": "bbbb"}, unique_id="id-5678",
).add_to_hass(hass)
assert len(hass.config_entries.async_entries("hue")) == 2
bridge = Mock()
bridge.username = "username-abc"
bridge.config.bridgeid = "id-1234"
bridge.config.name = "Mock Bridge"
bridge.host = "0.0.0.0"
bridge.id = "id-1234"
with patch.object(
config_flow, "_find_username_from_config", return_value="mock-user"
), patch.object(config_flow, "get_bridge", return_value=mock_coro(bridge)):
with patch(
"aiohue.Bridge", return_value=bridge,
):
result = await hass.config_entries.flow.async_init(
"hue", data={"host": "2.2.2.2"}, context={"source": "import"}
)
assert result["type"] == "form"
assert result["step_id"] == "link"
with patch(
"homeassistant.components.hue.config_flow.authenticate_bridge",
return_value=mock_coro(),
), patch(
"homeassistant.components.hue.async_setup_entry",
side_effect=lambda _, _2: mock_coro(True),
):
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
assert result["type"] == "create_entry"
assert result["title"] == "Mock Bridge"
assert result["data"] == {
"host": "0.0.0.0",
"bridge_id": "id-1234",
"username": "username-abc",
}
entries = hass.config_entries.async_entries("hue")
@@ -398,17 +357,14 @@ async def test_bridge_homekit(hass):
flow.hass = hass
flow.context = {}
with patch.object(
config_flow, "get_bridge", side_effect=errors.AuthenticationRequired
):
result = await flow.async_step_homekit(
{
"host": "0.0.0.0",
"serial": "1234",
"manufacturerURL": config_flow.HUE_MANUFACTURERURL,
"properties": {"id": "aa:bb:cc:dd:ee:ff"},
}
)
result = await flow.async_step_homekit(
{
"host": "0.0.0.0",
"serial": "1234",
"manufacturerURL": config_flow.HUE_MANUFACTURERURL,
"properties": {"id": "aa:bb:cc:dd:ee:ff"},
}
)
assert result["type"] == "form"
assert result["step_id"] == "link"
@@ -416,12 +372,15 @@ async def test_bridge_homekit(hass):
async def test_bridge_homekit_already_configured(hass):
"""Test if a HomeKit discovered bridge has already been configured."""
MockConfigEntry(domain="hue", data={"host": "0.0.0.0"}).add_to_hass(hass)
MockConfigEntry(
domain="hue", unique_id="aabbccddeeff", data={"host": "0.0.0.0"}
).add_to_hass(hass)
flow = config_flow.HueFlowHandler()
flow.hass = hass
flow.context = {}
result = await flow.async_step_homekit({"host": "0.0.0.0"})
assert result["type"] == "abort"
with pytest.raises(data_entry_flow.AbortFlow):
await flow.async_step_homekit(
{"host": "0.0.0.0", "properties": {"id": "aa:bb:cc:dd:ee:ff"}}
)
+22 -38
View File
@@ -9,13 +9,10 @@ from tests.common import MockConfigEntry, mock_coro
async def test_setup_with_no_config(hass):
"""Test that we do not discover anything or try to set up a bridge."""
with patch.object(hass, "config_entries") as mock_config_entries, patch.object(
hue, "configured_hosts", return_value=[]
):
assert await async_setup_component(hass, hue.DOMAIN, {}) is True
assert await async_setup_component(hass, hue.DOMAIN, {}) is True
# No flows started
assert len(mock_config_entries.flow.mock_calls) == 0
assert len(hass.config_entries.flow.async_progress()) == 0
# No configs stored
assert hass.data[hue.DOMAIN] == {}
@@ -23,9 +20,9 @@ async def test_setup_with_no_config(hass):
async def test_setup_defined_hosts_known_auth(hass):
"""Test we don't initiate a config entry if config bridge is known."""
with patch.object(hass, "config_entries") as mock_config_entries, patch.object(
hue, "configured_hosts", return_value=["0.0.0.0"]
):
MockConfigEntry(domain="hue", data={"host": "0.0.0.0"}).add_to_hass(hass)
with patch.object(hue, "async_setup_entry", return_value=mock_coro(True)):
assert (
await async_setup_component(
hass,
@@ -34,7 +31,6 @@ async def test_setup_defined_hosts_known_auth(hass):
hue.DOMAIN: {
hue.CONF_BRIDGES: {
hue.CONF_HOST: "0.0.0.0",
hue.CONF_FILENAME: "bla.conf",
hue.CONF_ALLOW_HUE_GROUPS: False,
hue.CONF_ALLOW_UNREACHABLE: True,
}
@@ -45,13 +41,12 @@ async def test_setup_defined_hosts_known_auth(hass):
)
# Flow started for discovered bridge
assert len(mock_config_entries.flow.mock_calls) == 0
assert len(hass.config_entries.flow.async_progress()) == 0
# Config stored for domain.
assert hass.data[hue.DATA_CONFIGS] == {
"0.0.0.0": {
hue.CONF_HOST: "0.0.0.0",
hue.CONF_FILENAME: "bla.conf",
hue.CONF_ALLOW_HUE_GROUPS: False,
hue.CONF_ALLOW_UNREACHABLE: True,
}
@@ -60,40 +55,30 @@ async def test_setup_defined_hosts_known_auth(hass):
async def test_setup_defined_hosts_no_known_auth(hass):
"""Test we initiate config entry if config bridge is not known."""
with patch.object(hass, "config_entries") as mock_config_entries, patch.object(
hue, "configured_hosts", return_value=[]
):
mock_config_entries.flow.async_init.return_value = mock_coro()
assert (
await async_setup_component(
hass,
hue.DOMAIN,
{
hue.DOMAIN: {
hue.CONF_BRIDGES: {
hue.CONF_HOST: "0.0.0.0",
hue.CONF_FILENAME: "bla.conf",
hue.CONF_ALLOW_HUE_GROUPS: False,
hue.CONF_ALLOW_UNREACHABLE: True,
}
assert (
await async_setup_component(
hass,
hue.DOMAIN,
{
hue.DOMAIN: {
hue.CONF_BRIDGES: {
hue.CONF_HOST: "0.0.0.0",
hue.CONF_ALLOW_HUE_GROUPS: False,
hue.CONF_ALLOW_UNREACHABLE: True,
}
},
)
is True
}
},
)
is True
)
# Flow started for discovered bridge
assert len(mock_config_entries.flow.mock_calls) == 1
assert mock_config_entries.flow.mock_calls[0][2]["data"] == {
"host": "0.0.0.0",
"path": "bla.conf",
}
assert len(hass.config_entries.flow.async_progress()) == 1
# Config stored for domain.
assert hass.data[hue.DATA_CONFIGS] == {
"0.0.0.0": {
hue.CONF_HOST: "0.0.0.0",
hue.CONF_FILENAME: "bla.conf",
hue.CONF_ALLOW_HUE_GROUPS: False,
hue.CONF_ALLOW_UNREACHABLE: True,
}
@@ -126,7 +111,6 @@ async def test_config_passed_to_config_entry(hass):
hue.DOMAIN: {
hue.CONF_BRIDGES: {
hue.CONF_HOST: "0.0.0.0",
hue.CONF_FILENAME: "bla.conf",
hue.CONF_ALLOW_HUE_GROUPS: False,
hue.CONF_ALLOW_UNREACHABLE: True,
}
@@ -166,7 +150,7 @@ async def test_unload_entry(hass):
return_value=mock_coro(Mock()),
):
mock_bridge.return_value.async_setup.return_value = mock_coro(True)
mock_bridge.return_value.api.config = Mock()
mock_bridge.return_value.api.config = Mock(bridgeid="aabbccddeeff")
assert await async_setup_component(hass, hue.DOMAIN, {}) is True
assert len(mock_bridge.return_value.mock_calls) == 1