2018-12-17 00:29:32 +00:00
|
|
|
"""Test config flow."""
|
|
|
|
from collections import namedtuple
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2021-09-20 07:02:17 +00:00
|
|
|
from aioesphomeapi import (
|
|
|
|
APIConnectionError,
|
|
|
|
InvalidAuthAPIError,
|
|
|
|
InvalidEncryptionKeyAPIError,
|
|
|
|
RequiresEncryptionAPIError,
|
|
|
|
ResolveAPIError,
|
|
|
|
)
|
2018-12-17 00:29:32 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-04-25 09:27:40 +00:00
|
|
|
from homeassistant import config_entries
|
2021-11-15 19:26:50 +00:00
|
|
|
from homeassistant.components import zeroconf
|
2021-09-20 07:02:17 +00:00
|
|
|
from homeassistant.components.esphome import CONF_NOISE_PSK, DOMAIN, DomainData
|
2020-04-30 23:24:47 +00:00
|
|
|
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,
|
|
|
|
)
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2019-11-26 02:00:58 +00:00
|
|
|
MockDeviceInfo = namedtuple("DeviceInfo", ["uses_password", "name"])
|
2021-09-20 07:02:17 +00:00
|
|
|
VALID_NOISE_PSK = "bOFFzzvfpg5DB94DuBGLXD/hMnhpDKgP9UQyBulwWVU="
|
|
|
|
INVALID_NOISE_PSK = "lSYBYEjQI1bVL8s2Vask4YytGMj1f1epNtmoim2yuTM="
|
2018-12-17 00:29:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_client():
|
|
|
|
"""Mock APIClient."""
|
2019-11-26 02:00:58 +00:00
|
|
|
with patch("homeassistant.components.esphome.config_flow.APIClient") as mock_client:
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2021-09-20 07:02:17 +00:00
|
|
|
def mock_constructor(
|
2021-10-13 17:04:23 +00:00
|
|
|
host, port, password, zeroconf_instance=None, noise_psk=None
|
2021-09-20 07:02:17 +00:00
|
|
|
):
|
2018-12-17 00:29:32 +00:00
|
|
|
"""Fake the client constructor."""
|
|
|
|
mock_client.host = host
|
|
|
|
mock_client.port = port
|
|
|
|
mock_client.password = password
|
2020-09-04 19:01:41 +00:00
|
|
|
mock_client.zeroconf_instance = zeroconf_instance
|
2021-09-20 07:02:17 +00:00
|
|
|
mock_client.noise_psk = noise_psk
|
2018-12-17 00:29:32 +00:00
|
|
|
return mock_client
|
|
|
|
|
|
|
|
mock_client.side_effect = mock_constructor
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_client.connect = AsyncMock()
|
|
|
|
mock_client.disconnect = AsyncMock()
|
2018-12-17 00:29:32 +00:00
|
|
|
|
|
|
|
yield mock_client
|
|
|
|
|
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
@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
|
|
|
|
|
|
|
|
|
2021-09-01 20:38:00 +00:00
|
|
|
async def test_user_connection_works(hass, mock_client, mock_zeroconf):
|
2019-05-26 11:48:05 +00:00
|
|
|
"""Test we can finish a config flow."""
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2020-08-27 11:56:20 +00:00
|
|
|
"esphome",
|
2021-04-25 09:27:40 +00:00
|
|
|
context={"source": config_entries.SOURCE_USER},
|
2020-08-27 11:56:20 +00:00
|
|
|
data=None,
|
2020-04-30 23:24:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "user"
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test"))
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
2021-04-25 09:27:40 +00:00
|
|
|
context={"source": config_entries.SOURCE_USER},
|
2020-04-30 23:24:47 +00:00
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 80},
|
|
|
|
)
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
2021-09-20 07:02:17 +00:00
|
|
|
assert result["data"] == {
|
|
|
|
CONF_HOST: "127.0.0.1",
|
|
|
|
CONF_PORT: 80,
|
|
|
|
CONF_PASSWORD: "",
|
|
|
|
CONF_NOISE_PSK: "",
|
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["title"] == "test"
|
2020-04-30 23:24:47 +00:00
|
|
|
|
2018-12-17 00:29:32 +00:00
|
|
|
assert len(mock_client.connect.mock_calls) == 1
|
|
|
|
assert len(mock_client.device_info.mock_calls) == 1
|
2019-01-04 21:10:52 +00:00
|
|
|
assert len(mock_client.disconnect.mock_calls) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_client.host == "127.0.0.1"
|
2018-12-17 00:29:32 +00:00
|
|
|
assert mock_client.port == 80
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_client.password == ""
|
2021-09-20 07:02:17 +00:00
|
|
|
assert mock_client.noise_psk is None
|
2018-12-17 00:29:32 +00:00
|
|
|
|
|
|
|
|
2021-09-20 07:02:17 +00:00
|
|
|
async def test_user_resolve_error(hass, mock_client, mock_zeroconf):
|
2018-12-17 00:29:32 +00:00
|
|
|
"""Test user step with IP resolve error."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2019-11-26 02:00:58 +00:00
|
|
|
"homeassistant.components.esphome.config_flow.APIConnectionError",
|
2021-09-20 07:02:17 +00:00
|
|
|
new_callable=lambda: ResolveAPIError,
|
2019-07-31 19:25:30 +00:00
|
|
|
) as exc:
|
2018-12-17 00:29:32 +00:00
|
|
|
mock_client.device_info.side_effect = exc
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
2021-04-25 09:27:40 +00:00
|
|
|
context={"source": config_entries.SOURCE_USER},
|
2020-04-30 23:24:47 +00:00
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
|
2020-04-30 23:24:47 +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"}
|
2020-04-30 23:24:47 +00:00
|
|
|
|
2018-12-17 00:29:32 +00:00
|
|
|
assert len(mock_client.connect.mock_calls) == 1
|
|
|
|
assert len(mock_client.device_info.mock_calls) == 1
|
2019-01-04 21:10:52 +00:00
|
|
|
assert len(mock_client.disconnect.mock_calls) == 1
|
2018-12-17 00:29:32 +00:00
|
|
|
|
|
|
|
|
2021-09-20 07:02:17 +00:00
|
|
|
async def test_user_connection_error(hass, mock_client, mock_zeroconf):
|
2018-12-17 00:29:32 +00:00
|
|
|
"""Test user step with connection error."""
|
2021-09-20 07:02:17 +00:00
|
|
|
mock_client.device_info.side_effect = APIConnectionError
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
2021-04-25 09:27:40 +00:00
|
|
|
context={"source": config_entries.SOURCE_USER},
|
2020-04-30 23:24:47 +00:00
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 23:24:47 +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": "connection_error"}
|
2020-04-30 23:24:47 +00:00
|
|
|
|
2018-12-17 00:29:32 +00:00
|
|
|
assert len(mock_client.connect.mock_calls) == 1
|
|
|
|
assert len(mock_client.device_info.mock_calls) == 1
|
2019-01-04 21:10:52 +00:00
|
|
|
assert len(mock_client.disconnect.mock_calls) == 1
|
2018-12-17 00:29:32 +00:00
|
|
|
|
|
|
|
|
2021-09-01 20:38:00 +00:00
|
|
|
async def test_user_with_password(hass, mock_client, mock_zeroconf):
|
2018-12-17 00:29:32 +00:00
|
|
|
"""Test user step with password."""
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(True, "test"))
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
2021-04-25 09:27:40 +00:00
|
|
|
context={"source": config_entries.SOURCE_USER},
|
2020-04-30 23:24:47 +00:00
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
assert result["type"] == RESULT_TYPE_FORM
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["step_id"] == "authenticate"
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_PASSWORD: "password1"}
|
|
|
|
)
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["data"] == {
|
2020-04-30 23:24:47 +00:00
|
|
|
CONF_HOST: "127.0.0.1",
|
|
|
|
CONF_PORT: 6053,
|
|
|
|
CONF_PASSWORD: "password1",
|
2021-09-20 07:02:17 +00:00
|
|
|
CONF_NOISE_PSK: "",
|
2018-12-17 00:29:32 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_client.password == "password1"
|
2018-12-17 00:29:32 +00:00
|
|
|
|
|
|
|
|
2021-09-20 07:02:17 +00:00
|
|
|
async def test_user_invalid_password(hass, mock_client, mock_zeroconf):
|
2018-12-17 00:29:32 +00:00
|
|
|
"""Test user step with invalid password."""
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(True, "test"))
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
2021-04-25 09:27:40 +00:00
|
|
|
context={"source": config_entries.SOURCE_USER},
|
2020-04-30 23:24:47 +00:00
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "authenticate"
|
|
|
|
|
2021-09-20 07:02:17 +00:00
|
|
|
mock_client.connect.side_effect = InvalidAuthAPIError
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-04-30 23:24:47 +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"
|
2020-10-09 12:28:54 +00:00
|
|
|
assert result["errors"] == {"base": "invalid_auth"}
|
2019-01-05 15:00:07 +00:00
|
|
|
|
|
|
|
|
2021-09-20 07:02:17 +00:00
|
|
|
async def test_login_connection_error(hass, mock_client, mock_zeroconf):
|
|
|
|
"""Test user step with connection error on login attempt."""
|
|
|
|
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 = APIConnectionError
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_PASSWORD: "valid"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "authenticate"
|
|
|
|
assert result["errors"] == {"base": "connection_error"}
|
|
|
|
|
|
|
|
|
2021-09-01 20:38:00 +00:00
|
|
|
async def test_discovery_initiation(hass, mock_client, mock_zeroconf):
|
2019-01-05 15:00:07 +00:00
|
|
|
"""Test discovery importing works."""
|
2020-04-30 23:24:47 +00:00
|
|
|
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test8266"))
|
|
|
|
|
2021-11-15 19:26:50 +00:00
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
|
|
|
host="192.168.43.183",
|
|
|
|
port=6053,
|
|
|
|
hostname="test8266.local.",
|
|
|
|
properties={},
|
|
|
|
)
|
2020-04-30 23:24:47 +00:00
|
|
|
flow = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
|
2020-04-30 23:24:47 +00:00
|
|
|
)
|
2019-01-05 15:00:07 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
flow["flow_id"], user_input={}
|
|
|
|
)
|
2019-02-26 20:35:25 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["title"] == "test8266"
|
2020-04-30 23:24:47 +00:00
|
|
|
assert result["data"][CONF_HOST] == "192.168.43.183"
|
|
|
|
assert result["data"][CONF_PORT] == 6053
|
|
|
|
|
|
|
|
assert result["result"]
|
|
|
|
assert result["result"].unique_id == "test8266"
|
2019-01-05 15:00:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_already_configured_hostname(hass, mock_client):
|
|
|
|
"""Test discovery aborts if already configured via hostname."""
|
2020-04-30 23:24:47 +00:00
|
|
|
entry = MockConfigEntry(
|
2021-02-11 09:19:39 +00:00
|
|
|
domain=DOMAIN,
|
2020-04-30 23:24:47 +00:00
|
|
|
data={CONF_HOST: "test8266.local", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
|
|
|
)
|
|
|
|
|
|
|
|
entry.add_to_hass(hass)
|
2019-01-05 15:00:07 +00:00
|
|
|
|
2021-11-15 19:26:50 +00:00
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
|
|
|
host="192.168.43.183",
|
|
|
|
port=6053,
|
|
|
|
hostname="test8266.local.",
|
|
|
|
properties={},
|
|
|
|
)
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
|
2020-04-30 23:24:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == RESULT_TYPE_ABORT
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
2019-01-05 15:00:07 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
assert entry.unique_id == "test8266"
|
|
|
|
|
2019-01-05 15:00:07 +00:00
|
|
|
|
|
|
|
async def test_discovery_already_configured_ip(hass, mock_client):
|
|
|
|
"""Test discovery aborts if already configured via static IP."""
|
2020-04-30 23:24:47 +00:00
|
|
|
entry = MockConfigEntry(
|
2021-02-11 09:19:39 +00:00
|
|
|
domain=DOMAIN,
|
2020-04-30 23:24:47 +00:00
|
|
|
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
|
|
|
)
|
|
|
|
|
|
|
|
entry.add_to_hass(hass)
|
2019-01-05 15:00:07 +00:00
|
|
|
|
2021-11-15 19:26:50 +00:00
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
|
|
|
host="192.168.43.183",
|
|
|
|
port=6053,
|
|
|
|
hostname="test8266.local.",
|
|
|
|
properties={"address": "192.168.43.183"},
|
|
|
|
)
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
|
2020-04-30 23:24:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == RESULT_TYPE_ABORT
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
2019-05-30 16:48:58 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
assert entry.unique_id == "test8266"
|
|
|
|
|
2019-05-30 16:48:58 +00:00
|
|
|
|
|
|
|
async def test_discovery_already_configured_name(hass, mock_client):
|
|
|
|
"""Test discovery aborts if already configured via name."""
|
|
|
|
entry = MockConfigEntry(
|
2021-02-11 09:19:39 +00:00
|
|
|
domain=DOMAIN,
|
2020-04-30 23:24:47 +00:00
|
|
|
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
2019-05-30 16:48:58 +00:00
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
2020-04-30 23:24:47 +00:00
|
|
|
|
2019-05-30 16:48:58 +00:00
|
|
|
mock_entry_data = MagicMock()
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_entry_data.device_info.name = "test8266"
|
2021-06-29 22:06:24 +00:00
|
|
|
domain_data = DomainData.get(hass)
|
|
|
|
domain_data.set_entry_data(entry, mock_entry_data)
|
2019-05-30 16:48:58 +00:00
|
|
|
|
2021-11-15 19:26:50 +00:00
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
|
|
|
host="192.168.43.184",
|
|
|
|
port=6053,
|
|
|
|
hostname="test8266.local.",
|
|
|
|
properties={"address": "test8266.local"},
|
|
|
|
)
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
|
2020-04-30 23:24:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == RESULT_TYPE_ABORT
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
2019-06-17 16:19:40 +00:00
|
|
|
|
2020-04-30 23:24:47 +00:00
|
|
|
assert entry.unique_id == "test8266"
|
|
|
|
assert entry.data[CONF_HOST] == "192.168.43.184"
|
|
|
|
|
2019-06-17 16:19:40 +00:00
|
|
|
|
|
|
|
async def test_discovery_duplicate_data(hass, mock_client):
|
|
|
|
"""Test discovery aborts if same mDNS packet arrives."""
|
2021-11-15 19:26:50 +00:00
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
|
|
|
host="192.168.43.183",
|
|
|
|
port=6053,
|
|
|
|
hostname="test8266.local.",
|
|
|
|
properties={"address": "test8266.local"},
|
|
|
|
)
|
2019-06-17 16:19:40 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test8266"))
|
2019-06-17 16:19:40 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
"esphome", data=service_info, context={"source": config_entries.SOURCE_ZEROCONF}
|
2019-06-17 16:19:40 +00:00
|
|
|
)
|
2020-04-30 23:24:47 +00:00
|
|
|
assert result["type"] == RESULT_TYPE_FORM
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["step_id"] == "discovery_confirm"
|
2019-06-17 16:19:40 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
"esphome", data=service_info, context={"source": config_entries.SOURCE_ZEROCONF}
|
2019-06-17 16:19:40 +00:00
|
|
|
)
|
2020-04-30 23:24:47 +00:00
|
|
|
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(
|
2021-02-11 09:19:39 +00:00
|
|
|
domain=DOMAIN,
|
2020-04-30 23:24:47 +00:00
|
|
|
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
|
|
|
)
|
|
|
|
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2021-11-15 19:26:50 +00:00
|
|
|
service_info = zeroconf.ZeroconfServiceInfo(
|
|
|
|
host="192.168.43.183",
|
|
|
|
port=6053,
|
|
|
|
hostname="test8266.local.",
|
|
|
|
properties={"address": "test8266.local"},
|
|
|
|
)
|
2020-04-30 23:24:47 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
|
2020-04-30 23:24:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == RESULT_TYPE_ABORT
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
2020-04-30 23:24:47 +00:00
|
|
|
|
|
|
|
assert entry.unique_id == "test8266"
|
2021-09-20 07:02:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_user_requires_psk(hass, mock_client, mock_zeroconf):
|
|
|
|
"""Test user step with requiring encryption key."""
|
|
|
|
mock_client.device_info.side_effect = RequiresEncryptionAPIError
|
|
|
|
|
|
|
|
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"] == "encryption_key"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
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_encryption_key_valid_psk(hass, mock_client, mock_zeroconf):
|
|
|
|
"""Test encryption key step with valid key."""
|
|
|
|
|
|
|
|
mock_client.device_info.side_effect = RequiresEncryptionAPIError
|
|
|
|
|
|
|
|
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"] == "encryption_key"
|
|
|
|
|
|
|
|
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test"))
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_NOISE_PSK: VALID_NOISE_PSK}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["data"] == {
|
|
|
|
CONF_HOST: "127.0.0.1",
|
|
|
|
CONF_PORT: 6053,
|
|
|
|
CONF_PASSWORD: "",
|
|
|
|
CONF_NOISE_PSK: VALID_NOISE_PSK,
|
|
|
|
}
|
|
|
|
assert mock_client.noise_psk == VALID_NOISE_PSK
|
|
|
|
|
|
|
|
|
|
|
|
async def test_encryption_key_invalid_psk(hass, mock_client, mock_zeroconf):
|
|
|
|
"""Test encryption key step with invalid key."""
|
|
|
|
|
|
|
|
mock_client.device_info.side_effect = RequiresEncryptionAPIError
|
|
|
|
|
|
|
|
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"] == "encryption_key"
|
|
|
|
|
|
|
|
mock_client.device_info.side_effect = InvalidEncryptionKeyAPIError
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_NOISE_PSK: INVALID_NOISE_PSK}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "encryption_key"
|
|
|
|
assert result["errors"] == {"base": "invalid_psk"}
|
|
|
|
assert mock_client.noise_psk == INVALID_NOISE_PSK
|
|
|
|
|
|
|
|
|
|
|
|
async def test_reauth_initiation(hass, mock_client, mock_zeroconf):
|
|
|
|
"""Test reauth initiation shows form."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={
|
|
|
|
"source": config_entries.SOURCE_REAUTH,
|
|
|
|
"entry_id": entry.entry_id,
|
|
|
|
"unique_id": entry.unique_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert result["type"] == RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_reauth_confirm_valid(hass, mock_client, mock_zeroconf):
|
|
|
|
"""Test reauth initiation with valid PSK."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={
|
|
|
|
"source": config_entries.SOURCE_REAUTH,
|
|
|
|
"entry_id": entry.entry_id,
|
|
|
|
"unique_id": entry.unique_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test"))
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_NOISE_PSK: VALID_NOISE_PSK}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "reauth_successful"
|
|
|
|
assert entry.data[CONF_NOISE_PSK] == VALID_NOISE_PSK
|
|
|
|
|
|
|
|
|
|
|
|
async def test_reauth_confirm_invalid(hass, mock_client, mock_zeroconf):
|
|
|
|
"""Test reauth initiation with invalid PSK."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
"esphome",
|
|
|
|
context={
|
|
|
|
"source": config_entries.SOURCE_REAUTH,
|
|
|
|
"entry_id": entry.entry_id,
|
|
|
|
"unique_id": entry.unique_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_client.device_info.side_effect = InvalidEncryptionKeyAPIError
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_NOISE_PSK: INVALID_NOISE_PSK}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
assert result["errors"]
|
|
|
|
assert result["errors"]["base"] == "invalid_psk"
|