2018-12-17 01:29:32 +01:00
|
|
|
"""Test config flow."""
|
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2019-11-26 03:00:58 +01:00
|
|
|
from homeassistant.components.esphome import DATA_KEY, config_flow
|
2018-12-17 01:29:32 +01:00
|
|
|
|
2020-04-30 13:29:50 -07:00
|
|
|
from tests.async_mock import AsyncMock, MagicMock, patch
|
|
|
|
from tests.common import MockConfigEntry
|
2018-12-17 01:29:32 +01:00
|
|
|
|
2019-11-26 03:00:58 +01:00
|
|
|
MockDeviceInfo = namedtuple("DeviceInfo", ["uses_password", "name"])
|
2018-12-17 01:29:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_client():
|
|
|
|
"""Mock APIClient."""
|
2019-11-26 03:00:58 +01:00
|
|
|
with patch("homeassistant.components.esphome.config_flow.APIClient") as mock_client:
|
2019-07-31 12:25:30 -07:00
|
|
|
|
2018-12-17 01:29:32 +01:00
|
|
|
def mock_constructor(loop, host, port, password):
|
|
|
|
"""Fake the client constructor."""
|
|
|
|
mock_client.host = host
|
|
|
|
mock_client.port = port
|
|
|
|
mock_client.password = password
|
|
|
|
return mock_client
|
|
|
|
|
|
|
|
mock_client.side_effect = mock_constructor
|
2020-04-30 13:29:50 -07:00
|
|
|
mock_client.connect = AsyncMock()
|
|
|
|
mock_client.disconnect = AsyncMock()
|
2018-12-17 01:29:32 +01:00
|
|
|
|
|
|
|
yield mock_client
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def mock_api_connection_error():
|
|
|
|
"""Mock out the try login method."""
|
2019-07-31 12:25:30 -07:00
|
|
|
with patch(
|
2019-11-26 03:00:58 +01:00
|
|
|
"homeassistant.components.esphome.config_flow.APIConnectionError",
|
|
|
|
new_callable=lambda: OSError,
|
2019-07-31 12:25:30 -07:00
|
|
|
) as mock_error:
|
2018-12-17 01:29:32 +01:00
|
|
|
yield mock_error
|
|
|
|
|
|
|
|
|
2019-05-26 13:48:05 +02:00
|
|
|
def _setup_flow_handler(hass):
|
2018-12-17 01:29:32 +01:00
|
|
|
flow = config_flow.EsphomeFlowHandler()
|
|
|
|
flow.hass = hass
|
2019-05-26 13:48:05 +02:00
|
|
|
flow.context = {}
|
|
|
|
return flow
|
|
|
|
|
|
|
|
|
|
|
|
async def test_user_connection_works(hass, mock_client):
|
|
|
|
"""Test we can finish a config flow."""
|
|
|
|
flow = _setup_flow_handler(hass)
|
2018-12-17 01:29:32 +01:00
|
|
|
result = await flow.async_step_user(user_input=None)
|
2019-07-31 12:25:30 -07:00
|
|
|
assert result["type"] == "form"
|
2018-12-17 01:29:32 +01:00
|
|
|
|
2020-04-30 13:29:50 -07:00
|
|
|
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test"))
|
2018-12-17 01:29:32 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
result = await flow.async_step_user(user_input={"host": "127.0.0.1", "port": 80})
|
2018-12-17 01:29:32 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
assert result["type"] == "create_entry"
|
|
|
|
assert result["data"] == {"host": "127.0.0.1", "port": 80, "password": ""}
|
|
|
|
assert result["title"] == "test"
|
2018-12-17 01:29:32 +01:00
|
|
|
assert len(mock_client.connect.mock_calls) == 1
|
|
|
|
assert len(mock_client.device_info.mock_calls) == 1
|
2019-01-04 22:10:52 +01:00
|
|
|
assert len(mock_client.disconnect.mock_calls) == 1
|
2019-07-31 12:25:30 -07:00
|
|
|
assert mock_client.host == "127.0.0.1"
|
2018-12-17 01:29:32 +01:00
|
|
|
assert mock_client.port == 80
|
2019-07-31 12:25:30 -07:00
|
|
|
assert mock_client.password == ""
|
2018-12-17 01:29:32 +01:00
|
|
|
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
async def test_user_resolve_error(hass, mock_api_connection_error, mock_client):
|
2018-12-17 01:29:32 +01:00
|
|
|
"""Test user step with IP resolve error."""
|
2019-05-26 13:48:05 +02:00
|
|
|
flow = _setup_flow_handler(hass)
|
2018-12-17 01:29:32 +01:00
|
|
|
await flow.async_step_user(user_input=None)
|
|
|
|
|
|
|
|
class MockResolveError(mock_api_connection_error):
|
|
|
|
"""Create an exception with a specific error message."""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Initialize."""
|
|
|
|
super().__init__("Error resolving IP address")
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
with patch(
|
2019-11-26 03:00:58 +01:00
|
|
|
"homeassistant.components.esphome.config_flow.APIConnectionError",
|
|
|
|
new_callable=lambda: MockResolveError,
|
2019-07-31 12:25:30 -07:00
|
|
|
) as exc:
|
2018-12-17 01:29:32 +01:00
|
|
|
mock_client.device_info.side_effect = exc
|
2019-07-31 12:25:30 -07:00
|
|
|
result = await flow.async_step_user(
|
|
|
|
user_input={"host": "127.0.0.1", "port": 6053}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {"base": "resolve_error"}
|
2018-12-17 01:29:32 +01:00
|
|
|
assert len(mock_client.connect.mock_calls) == 1
|
|
|
|
assert len(mock_client.device_info.mock_calls) == 1
|
2019-01-04 22:10:52 +01:00
|
|
|
assert len(mock_client.disconnect.mock_calls) == 1
|
2018-12-17 01:29:32 +01:00
|
|
|
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
async def test_user_connection_error(hass, mock_api_connection_error, mock_client):
|
2018-12-17 01:29:32 +01:00
|
|
|
"""Test user step with connection error."""
|
2019-05-26 13:48:05 +02:00
|
|
|
flow = _setup_flow_handler(hass)
|
2018-12-17 01:29:32 +01:00
|
|
|
await flow.async_step_user(user_input=None)
|
|
|
|
|
|
|
|
mock_client.device_info.side_effect = mock_api_connection_error
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
result = await flow.async_step_user(user_input={"host": "127.0.0.1", "port": 6053})
|
2018-12-17 01:29:32 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {"base": "connection_error"}
|
2018-12-17 01:29:32 +01:00
|
|
|
assert len(mock_client.connect.mock_calls) == 1
|
|
|
|
assert len(mock_client.device_info.mock_calls) == 1
|
2019-01-04 22:10:52 +01:00
|
|
|
assert len(mock_client.disconnect.mock_calls) == 1
|
2018-12-17 01:29:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_user_with_password(hass, mock_client):
|
|
|
|
"""Test user step with password."""
|
2019-05-26 13:48:05 +02:00
|
|
|
flow = _setup_flow_handler(hass)
|
2018-12-17 01:29:32 +01:00
|
|
|
await flow.async_step_user(user_input=None)
|
|
|
|
|
2020-04-30 13:29:50 -07:00
|
|
|
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(True, "test"))
|
2018-12-17 01:29:32 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
result = await flow.async_step_user(user_input={"host": "127.0.0.1", "port": 6053})
|
2018-12-17 01:29:32 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "authenticate"
|
2018-12-17 01:29:32 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
result = await flow.async_step_authenticate(user_input={"password": "password1"})
|
2018-12-17 01:29:32 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
assert result["type"] == "create_entry"
|
|
|
|
assert result["data"] == {
|
|
|
|
"host": "127.0.0.1",
|
|
|
|
"port": 6053,
|
|
|
|
"password": "password1",
|
2018-12-17 01:29:32 +01:00
|
|
|
}
|
2019-07-31 12:25:30 -07:00
|
|
|
assert mock_client.password == "password1"
|
2018-12-17 01:29:32 +01:00
|
|
|
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
async def test_user_invalid_password(hass, mock_api_connection_error, mock_client):
|
2018-12-17 01:29:32 +01:00
|
|
|
"""Test user step with invalid password."""
|
2019-05-26 13:48:05 +02:00
|
|
|
flow = _setup_flow_handler(hass)
|
2018-12-17 01:29:32 +01:00
|
|
|
await flow.async_step_user(user_input=None)
|
|
|
|
|
2020-04-30 13:29:50 -07:00
|
|
|
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(True, "test"))
|
2018-12-17 01:29:32 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
await flow.async_step_user(user_input={"host": "127.0.0.1", "port": 6053})
|
2018-12-17 01:29:32 +01:00
|
|
|
mock_client.connect.side_effect = mock_api_connection_error
|
2019-07-31 12:25:30 -07:00
|
|
|
result = await flow.async_step_authenticate(user_input={"password": "invalid"})
|
|
|
|
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "authenticate"
|
|
|
|
assert result["errors"] == {"base": "invalid_password"}
|
2019-01-05 16:00:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_initiation(hass, mock_client):
|
|
|
|
"""Test discovery importing works."""
|
2019-05-26 13:48:05 +02:00
|
|
|
flow = _setup_flow_handler(hass)
|
2019-01-05 16:00:07 +01:00
|
|
|
service_info = {
|
2019-07-31 12:25:30 -07:00
|
|
|
"host": "192.168.43.183",
|
|
|
|
"port": 6053,
|
|
|
|
"hostname": "test8266.local.",
|
|
|
|
"properties": {},
|
2019-01-05 16:00:07 +01:00
|
|
|
}
|
|
|
|
|
2020-04-30 13:29:50 -07:00
|
|
|
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test8266"))
|
2019-01-05 16:00:07 +01:00
|
|
|
|
2019-05-23 08:55:08 +02:00
|
|
|
result = await flow.async_step_zeroconf(user_input=service_info)
|
2019-07-31 12:25:30 -07:00
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "discovery_confirm"
|
|
|
|
assert result["description_placeholders"]["name"] == "test8266"
|
|
|
|
assert flow.context["title_placeholders"]["name"] == "test8266"
|
2019-02-26 21:35:25 +01:00
|
|
|
|
|
|
|
result = await flow.async_step_discovery_confirm(user_input={})
|
2019-07-31 12:25:30 -07:00
|
|
|
assert result["type"] == "create_entry"
|
|
|
|
assert result["title"] == "test8266"
|
|
|
|
assert result["data"]["host"] == "test8266.local"
|
|
|
|
assert result["data"]["port"] == 6053
|
2019-01-05 16:00:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_already_configured_hostname(hass, mock_client):
|
|
|
|
"""Test discovery aborts if already configured via hostname."""
|
|
|
|
MockConfigEntry(
|
2019-07-31 12:25:30 -07:00
|
|
|
domain="esphome", data={"host": "test8266.local", "port": 6053, "password": ""}
|
2019-01-05 16:00:07 +01:00
|
|
|
).add_to_hass(hass)
|
|
|
|
|
2019-05-26 13:48:05 +02:00
|
|
|
flow = _setup_flow_handler(hass)
|
2019-01-05 16:00:07 +01:00
|
|
|
service_info = {
|
2019-07-31 12:25:30 -07:00
|
|
|
"host": "192.168.43.183",
|
|
|
|
"port": 6053,
|
|
|
|
"hostname": "test8266.local.",
|
|
|
|
"properties": {},
|
2019-01-05 16:00:07 +01:00
|
|
|
}
|
2019-05-23 08:55:08 +02:00
|
|
|
result = await flow.async_step_zeroconf(user_input=service_info)
|
2019-07-31 12:25:30 -07:00
|
|
|
assert result["type"] == "abort"
|
|
|
|
assert result["reason"] == "already_configured"
|
2019-01-05 16:00:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_already_configured_ip(hass, mock_client):
|
|
|
|
"""Test discovery aborts if already configured via static IP."""
|
|
|
|
MockConfigEntry(
|
2019-07-31 12:25:30 -07:00
|
|
|
domain="esphome", data={"host": "192.168.43.183", "port": 6053, "password": ""}
|
2019-01-05 16:00:07 +01:00
|
|
|
).add_to_hass(hass)
|
|
|
|
|
2019-05-26 13:48:05 +02:00
|
|
|
flow = _setup_flow_handler(hass)
|
2019-01-05 16:00:07 +01:00
|
|
|
service_info = {
|
2019-07-31 12:25:30 -07:00
|
|
|
"host": "192.168.43.183",
|
|
|
|
"port": 6053,
|
|
|
|
"hostname": "test8266.local.",
|
|
|
|
"properties": {"address": "192.168.43.183"},
|
2019-01-05 16:00:07 +01:00
|
|
|
}
|
2019-05-23 08:55:08 +02:00
|
|
|
result = await flow.async_step_zeroconf(user_input=service_info)
|
2019-07-31 12:25:30 -07:00
|
|
|
assert result["type"] == "abort"
|
|
|
|
assert result["reason"] == "already_configured"
|
2019-05-30 18:48:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_already_configured_name(hass, mock_client):
|
|
|
|
"""Test discovery aborts if already configured via name."""
|
|
|
|
entry = MockConfigEntry(
|
2019-07-31 12:25:30 -07:00
|
|
|
domain="esphome", data={"host": "192.168.43.183", "port": 6053, "password": ""}
|
2019-05-30 18:48:58 +02:00
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
mock_entry_data = MagicMock()
|
2019-07-31 12:25:30 -07:00
|
|
|
mock_entry_data.device_info.name = "test8266"
|
|
|
|
hass.data[DATA_KEY] = {entry.entry_id: mock_entry_data}
|
2019-05-30 18:48:58 +02:00
|
|
|
|
|
|
|
flow = _setup_flow_handler(hass)
|
|
|
|
service_info = {
|
2019-07-31 12:25:30 -07:00
|
|
|
"host": "192.168.43.183",
|
|
|
|
"port": 6053,
|
|
|
|
"hostname": "test8266.local.",
|
|
|
|
"properties": {"address": "test8266.local"},
|
2019-05-30 18:48:58 +02:00
|
|
|
}
|
|
|
|
result = await flow.async_step_zeroconf(user_input=service_info)
|
2019-07-31 12:25:30 -07:00
|
|
|
assert result["type"] == "abort"
|
|
|
|
assert result["reason"] == "already_configured"
|
2019-06-17 18:19:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_duplicate_data(hass, mock_client):
|
|
|
|
"""Test discovery aborts if same mDNS packet arrives."""
|
|
|
|
service_info = {
|
2019-07-31 12:25:30 -07:00
|
|
|
"host": "192.168.43.183",
|
|
|
|
"port": 6053,
|
|
|
|
"hostname": "test8266.local.",
|
|
|
|
"properties": {"address": "test8266.local"},
|
2019-06-17 18:19:40 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 13:29:50 -07:00
|
|
|
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test8266"))
|
2019-06-17 18:19:40 +02:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2019-07-31 12:25:30 -07:00
|
|
|
"esphome", data=service_info, context={"source": "zeroconf"}
|
2019-06-17 18:19:40 +02:00
|
|
|
)
|
2019-07-31 12:25:30 -07:00
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "discovery_confirm"
|
2019-06-17 18:19:40 +02:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2019-07-31 12:25:30 -07:00
|
|
|
"esphome", data=service_info, context={"source": "zeroconf"}
|
2019-06-17 18:19:40 +02:00
|
|
|
)
|
2019-07-31 12:25:30 -07:00
|
|
|
assert result["type"] == "abort"
|
|
|
|
assert result["reason"] == "already_configured"
|