core/tests/components/tradfri/test_config_flow.py

230 lines
7.3 KiB
Python
Raw Normal View History

"""Test the Tradfri config flow."""
from unittest.mock import AsyncMock, patch
2021-01-01 21:31:56 +00:00
import pytest
from homeassistant import config_entries, data_entry_flow
from homeassistant.components import zeroconf
from homeassistant.components.tradfri import config_flow
from homeassistant.core import HomeAssistant
from . import TRADFRI_PATH
2020-04-25 21:32:55 +00:00
from tests.common import MockConfigEntry
@pytest.fixture(name="mock_auth")
def mock_auth_fixture():
"""Mock authenticate."""
with patch(f"{TRADFRI_PATH}.config_flow.authenticate") as auth:
yield auth
async def test_already_paired(hass: HomeAssistant, mock_entry_setup) -> None:
"""Test Gateway already paired."""
with patch(
f"{TRADFRI_PATH}.config_flow.APIFactory",
autospec=True,
) as mock_lib:
mock_it = AsyncMock()
mock_it.generate_psk.return_value = None
mock_lib.init.return_value = mock_it
result = await hass.config_entries.flow.async_init(
"tradfri", context={"source": config_entries.SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {"host": "123.123.123.123", "security_code": "abcd"}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] == {"base": "cannot_authenticate"}
async def test_user_connection_successful(
hass: HomeAssistant, mock_auth, mock_entry_setup
) -> None:
"""Test a successful connection."""
2020-04-25 21:32:55 +00:00
mock_auth.side_effect = lambda hass, host, code: {"host": host, "gateway_id": "bla"}
flow = await hass.config_entries.flow.async_init(
"tradfri", context={"source": config_entries.SOURCE_USER}
2019-07-31 19:25:30 +00:00
)
2019-07-31 19:25:30 +00:00
result = await hass.config_entries.flow.async_configure(
flow["flow_id"], {"host": "123.123.123.123", "security_code": "abcd"}
)
assert len(mock_entry_setup.mock_calls) == 1
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
2019-07-31 19:25:30 +00:00
assert result["result"].data == {
"host": "123.123.123.123",
"gateway_id": "bla",
}
async def test_user_connection_timeout(
hass: HomeAssistant, mock_auth, mock_entry_setup
) -> None:
"""Test a connection timeout."""
2019-07-31 19:25:30 +00:00
mock_auth.side_effect = config_flow.AuthError("timeout")
flow = await hass.config_entries.flow.async_init(
"tradfri", context={"source": config_entries.SOURCE_USER}
2019-07-31 19:25:30 +00:00
)
2019-07-31 19:25:30 +00:00
result = await hass.config_entries.flow.async_configure(
flow["flow_id"], {"host": "127.0.0.1", "security_code": "abcd"}
)
assert len(mock_entry_setup.mock_calls) == 0
assert result["type"] == data_entry_flow.FlowResultType.FORM
2019-07-31 19:25:30 +00:00
assert result["errors"] == {"base": "timeout"}
async def test_user_connection_bad_key(
hass: HomeAssistant, mock_auth, mock_entry_setup
) -> None:
"""Test a connection with bad key."""
2019-07-31 19:25:30 +00:00
mock_auth.side_effect = config_flow.AuthError("invalid_security_code")
flow = await hass.config_entries.flow.async_init(
"tradfri", context={"source": config_entries.SOURCE_USER}
2019-07-31 19:25:30 +00:00
)
2019-07-31 19:25:30 +00:00
result = await hass.config_entries.flow.async_configure(
flow["flow_id"], {"host": "127.0.0.1", "security_code": "abcd"}
)
assert len(mock_entry_setup.mock_calls) == 0
assert result["type"] == data_entry_flow.FlowResultType.FORM
2019-07-31 19:25:30 +00:00
assert result["errors"] == {"security_code": "invalid_security_code"}
async def test_discovery_connection(
hass: HomeAssistant, mock_auth, mock_entry_setup
) -> None:
"""Test a connection via discovery."""
2020-04-25 21:32:55 +00:00
mock_auth.side_effect = lambda hass, host, code: {"host": host, "gateway_id": "bla"}
flow = await hass.config_entries.flow.async_init(
2020-04-26 23:57:29 +00:00
"tradfri",
context={"source": config_entries.SOURCE_HOMEKIT},
data=zeroconf.ZeroconfServiceInfo(
host="123.123.123.123",
addresses=["123.123.123.123"],
hostname="mock_hostname",
name="mock_name",
port=None,
properties={zeroconf.ATTR_PROPERTIES_ID: "homekit-id"},
type="mock_type",
),
2019-07-31 19:25:30 +00:00
)
2019-07-31 19:25:30 +00:00
result = await hass.config_entries.flow.async_configure(
flow["flow_id"], {"security_code": "abcd"}
)
assert len(mock_entry_setup.mock_calls) == 1
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
2020-04-26 23:57:29 +00:00
assert result["result"].unique_id == "homekit-id"
2019-07-31 19:25:30 +00:00
assert result["result"].data == {
"host": "123.123.123.123",
"gateway_id": "bla",
}
async def test_discovery_duplicate_aborted(hass: HomeAssistant) -> None:
2020-04-26 23:57:29 +00:00
"""Test a duplicate discovery host aborts and updates existing entry."""
entry = MockConfigEntry(
domain="tradfri", data={"host": "some-host"}, unique_id="homekit-id"
)
entry.add_to_hass(hass)
flow = await hass.config_entries.flow.async_init(
2020-04-26 23:57:29 +00:00
"tradfri",
context={"source": config_entries.SOURCE_HOMEKIT},
data=zeroconf.ZeroconfServiceInfo(
host="new-host",
addresses=["new-host"],
hostname="mock_hostname",
name="mock_name",
port=None,
properties={zeroconf.ATTR_PROPERTIES_ID: "homekit-id"},
type="mock_type",
),
2019-07-31 19:25:30 +00:00
)
assert flow["type"] == data_entry_flow.FlowResultType.ABORT
2019-07-31 19:25:30 +00:00
assert flow["reason"] == "already_configured"
2020-04-26 23:57:29 +00:00
assert entry.data["host"] == "new-host"
async def test_duplicate_discovery(
hass: HomeAssistant, mock_auth, mock_entry_setup
) -> None:
"""Test a duplicate discovery in progress is ignored."""
result = await hass.config_entries.flow.async_init(
2020-04-26 23:57:29 +00:00
"tradfri",
context={"source": config_entries.SOURCE_HOMEKIT},
data=zeroconf.ZeroconfServiceInfo(
host="123.123.123.123",
addresses=["123.123.123.123"],
hostname="mock_hostname",
name="mock_name",
port=None,
properties={zeroconf.ATTR_PROPERTIES_ID: "homekit-id"},
type="mock_type",
),
2019-07-31 19:25:30 +00:00
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
result2 = await hass.config_entries.flow.async_init(
2020-04-26 23:57:29 +00:00
"tradfri",
context={"source": config_entries.SOURCE_HOMEKIT},
data=zeroconf.ZeroconfServiceInfo(
host="123.123.123.123",
addresses=["123.123.123.123"],
hostname="mock_hostname",
name="mock_name",
port=None,
properties={zeroconf.ATTR_PROPERTIES_ID: "homekit-id"},
type="mock_type",
),
2019-07-31 19:25:30 +00:00
)
assert result2["type"] == data_entry_flow.FlowResultType.ABORT
2020-04-26 23:57:29 +00:00
async def test_discovery_updates_unique_id(hass: HomeAssistant) -> None:
2020-04-26 23:57:29 +00:00
"""Test a duplicate discovery host aborts and updates existing entry."""
2020-08-27 11:56:20 +00:00
entry = MockConfigEntry(
domain="tradfri",
data={"host": "some-host"},
)
2020-04-26 23:57:29 +00:00
entry.add_to_hass(hass)
flow = await hass.config_entries.flow.async_init(
"tradfri",
context={"source": config_entries.SOURCE_HOMEKIT},
data=zeroconf.ZeroconfServiceInfo(
host="some-host",
addresses=["some-host"],
hostname="mock_hostname",
name="mock_name",
port=None,
properties={zeroconf.ATTR_PROPERTIES_ID: "homekit-id"},
type="mock_type",
),
2020-04-26 23:57:29 +00:00
)
assert flow["type"] == data_entry_flow.FlowResultType.ABORT
2020-04-26 23:57:29 +00:00
assert flow["reason"] == "already_configured"
assert entry.unique_id == "homekit-id"