core/tests/components/esphome/test_config_flow.py

342 lines
11 KiB
Python
Raw Normal View History

"""Test config flow."""
from collections import namedtuple
2021-01-01 21:31:56 +00:00
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from homeassistant import config_entries
from homeassistant.components.esphome import DOMAIN, DomainData
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT
from homeassistant.data_entry_flow import (
RESULT_TYPE_ABORT,
RESULT_TYPE_CREATE_ENTRY,
RESULT_TYPE_FORM,
)
from tests.common import MockConfigEntry
MockDeviceInfo = namedtuple("DeviceInfo", ["uses_password", "name"])
@pytest.fixture
def mock_client():
"""Mock APIClient."""
with patch("homeassistant.components.esphome.config_flow.APIClient") as mock_client:
2019-07-31 19:25:30 +00:00
def mock_constructor(loop, host, port, password, zeroconf_instance=None):
"""Fake the client constructor."""
mock_client.host = host
mock_client.port = port
mock_client.password = password
mock_client.zeroconf_instance = zeroconf_instance
return mock_client
mock_client.side_effect = mock_constructor
mock_client.connect = AsyncMock()
mock_client.disconnect = AsyncMock()
yield mock_client
@pytest.fixture(autouse=True)
def mock_api_connection_error():
"""Mock out the try login method."""
2019-07-31 19:25:30 +00:00
with patch(
"homeassistant.components.esphome.config_flow.APIConnectionError",
new_callable=lambda: OSError,
2019-07-31 19:25:30 +00:00
) as mock_error:
yield mock_error
@pytest.fixture(autouse=True)
def mock_setup_entry():
"""Mock setting up a config entry."""
with patch("homeassistant.components.esphome.async_setup_entry", return_value=True):
yield
async def test_user_connection_works(hass, mock_client):
"""Test we can finish a config flow."""
result = await hass.config_entries.flow.async_init(
2020-08-27 11:56:20 +00:00
"esphome",
context={"source": config_entries.SOURCE_USER},
2020-08-27 11:56:20 +00:00
data=None,
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user"
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test"))
result = await hass.config_entries.flow.async_init(
"esphome",
context={"source": config_entries.SOURCE_USER},
data={CONF_HOST: "127.0.0.1", CONF_PORT: 80},
)
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["data"] == {CONF_HOST: "127.0.0.1", CONF_PORT: 80, CONF_PASSWORD: ""}
2019-07-31 19:25:30 +00:00
assert result["title"] == "test"
assert len(mock_client.connect.mock_calls) == 1
assert len(mock_client.device_info.mock_calls) == 1
assert len(mock_client.disconnect.mock_calls) == 1
2019-07-31 19:25:30 +00:00
assert mock_client.host == "127.0.0.1"
assert mock_client.port == 80
2019-07-31 19:25:30 +00:00
assert mock_client.password == ""
2019-07-31 19:25:30 +00:00
async def test_user_resolve_error(hass, mock_api_connection_error, mock_client):
"""Test user step with IP resolve error."""
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 19:25:30 +00:00
with patch(
"homeassistant.components.esphome.config_flow.APIConnectionError",
new_callable=lambda: MockResolveError,
2019-07-31 19:25:30 +00:00
) as exc:
mock_client.device_info.side_effect = exc
result = await hass.config_entries.flow.async_init(
"esphome",
context={"source": config_entries.SOURCE_USER},
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
2019-07-31 19:25:30 +00:00
)
assert result["type"] == RESULT_TYPE_FORM
2019-07-31 19:25:30 +00:00
assert result["step_id"] == "user"
assert result["errors"] == {"base": "resolve_error"}
assert len(mock_client.connect.mock_calls) == 1
assert len(mock_client.device_info.mock_calls) == 1
assert len(mock_client.disconnect.mock_calls) == 1
2019-07-31 19:25:30 +00:00
async def test_user_connection_error(hass, mock_api_connection_error, mock_client):
"""Test user step with connection error."""
mock_client.device_info.side_effect = mock_api_connection_error
result = await hass.config_entries.flow.async_init(
"esphome",
context={"source": config_entries.SOURCE_USER},
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
)
assert result["type"] == RESULT_TYPE_FORM
2019-07-31 19:25:30 +00:00
assert result["step_id"] == "user"
assert result["errors"] == {"base": "connection_error"}
assert len(mock_client.connect.mock_calls) == 1
assert len(mock_client.device_info.mock_calls) == 1
assert len(mock_client.disconnect.mock_calls) == 1
async def test_user_with_password(hass, mock_client):
"""Test user step with password."""
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(True, "test"))
result = await hass.config_entries.flow.async_init(
"esphome",
context={"source": config_entries.SOURCE_USER},
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
)
assert result["type"] == RESULT_TYPE_FORM
2019-07-31 19:25:30 +00:00
assert result["step_id"] == "authenticate"
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_PASSWORD: "password1"}
)
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
2019-07-31 19:25:30 +00:00
assert result["data"] == {
CONF_HOST: "127.0.0.1",
CONF_PORT: 6053,
CONF_PASSWORD: "password1",
}
2019-07-31 19:25:30 +00:00
assert mock_client.password == "password1"
2019-07-31 19:25:30 +00:00
async def test_user_invalid_password(hass, mock_api_connection_error, mock_client):
"""Test user step with invalid password."""
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(True, "test"))
result = await hass.config_entries.flow.async_init(
"esphome",
context={"source": config_entries.SOURCE_USER},
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "authenticate"
mock_client.connect.side_effect = mock_api_connection_error
2019-07-31 19:25:30 +00:00
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_PASSWORD: "invalid"}
)
assert result["type"] == RESULT_TYPE_FORM
2019-07-31 19:25:30 +00:00
assert result["step_id"] == "authenticate"
assert result["errors"] == {"base": "invalid_auth"}
async def test_discovery_initiation(hass, mock_client):
"""Test discovery importing works."""
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test8266"))
service_info = {
2019-07-31 19:25:30 +00:00
"host": "192.168.43.183",
"port": 6053,
"hostname": "test8266.local.",
"properties": {},
}
flow = await hass.config_entries.flow.async_init(
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
)
result = await hass.config_entries.flow.async_configure(
flow["flow_id"], user_input={}
)
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
2019-07-31 19:25:30 +00:00
assert result["title"] == "test8266"
assert result["data"][CONF_HOST] == "192.168.43.183"
assert result["data"][CONF_PORT] == 6053
assert result["result"]
assert result["result"].unique_id == "test8266"
async def test_discovery_already_configured_hostname(hass, mock_client):
"""Test discovery aborts if already configured via hostname."""
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_HOST: "test8266.local", CONF_PORT: 6053, CONF_PASSWORD: ""},
)
entry.add_to_hass(hass)
service_info = {
2019-07-31 19:25:30 +00:00
"host": "192.168.43.183",
"port": 6053,
"hostname": "test8266.local.",
"properties": {},
}
result = await hass.config_entries.flow.async_init(
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
)
assert result["type"] == RESULT_TYPE_ABORT
2019-07-31 19:25:30 +00:00
assert result["reason"] == "already_configured"
assert entry.unique_id == "test8266"
async def test_discovery_already_configured_ip(hass, mock_client):
"""Test discovery aborts if already configured via static IP."""
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
)
entry.add_to_hass(hass)
service_info = {
2019-07-31 19:25:30 +00:00
"host": "192.168.43.183",
"port": 6053,
"hostname": "test8266.local.",
"properties": {"address": "192.168.43.183"},
}
result = await hass.config_entries.flow.async_init(
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
)
assert result["type"] == RESULT_TYPE_ABORT
2019-07-31 19:25:30 +00:00
assert result["reason"] == "already_configured"
assert entry.unique_id == "test8266"
async def test_discovery_already_configured_name(hass, mock_client):
"""Test discovery aborts if already configured via name."""
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
)
entry.add_to_hass(hass)
mock_entry_data = MagicMock()
2019-07-31 19:25:30 +00:00
mock_entry_data.device_info.name = "test8266"
domain_data = DomainData.get(hass)
domain_data.set_entry_data(entry, mock_entry_data)
service_info = {
"host": "192.168.43.184",
2019-07-31 19:25:30 +00:00
"port": 6053,
"hostname": "test8266.local.",
"properties": {"address": "test8266.local"},
}
result = await hass.config_entries.flow.async_init(
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
)
assert result["type"] == RESULT_TYPE_ABORT
2019-07-31 19:25:30 +00:00
assert result["reason"] == "already_configured"
assert entry.unique_id == "test8266"
assert entry.data[CONF_HOST] == "192.168.43.184"
async def test_discovery_duplicate_data(hass, mock_client):
"""Test discovery aborts if same mDNS packet arrives."""
service_info = {
2019-07-31 19:25:30 +00:00
"host": "192.168.43.183",
"port": 6053,
"hostname": "test8266.local.",
"properties": {"address": "test8266.local"},
}
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test8266"))
result = await hass.config_entries.flow.async_init(
"esphome", data=service_info, context={"source": config_entries.SOURCE_ZEROCONF}
)
assert result["type"] == RESULT_TYPE_FORM
2019-07-31 19:25:30 +00:00
assert result["step_id"] == "discovery_confirm"
result = await hass.config_entries.flow.async_init(
"esphome", data=service_info, context={"source": config_entries.SOURCE_ZEROCONF}
)
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_in_progress"
async def test_discovery_updates_unique_id(hass, mock_client):
"""Test a duplicate discovery host aborts and updates existing entry."""
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
)
entry.add_to_hass(hass)
service_info = {
"host": "192.168.43.183",
"port": 6053,
"hostname": "test8266.local.",
"properties": {"address": "test8266.local"},
}
result = await hass.config_entries.flow.async_init(
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
)
assert result["type"] == RESULT_TYPE_ABORT
2019-07-31 19:25:30 +00:00
assert result["reason"] == "already_configured"
assert entry.unique_id == "test8266"