Add unique id to esphome config flow (#34753)
This commit is contained in:
1 parent
799d98eaf0
commit
ba7391528f
4 files changed
+192
-103
No files matched your search
@@ -3,7 +3,13 @@ from collections import namedtuple
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.esphome import DATA_KEY, config_flow
|
||||
from homeassistant.components.esphome import DATA_KEY
|
||||
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.async_mock import AsyncMock, MagicMock, patch
|
||||
from tests.common import MockConfigEntry
|
||||
@@ -40,26 +46,27 @@ def mock_api_connection_error():
|
||||
yield mock_error
|
||||
|
||||
|
||||
def _setup_flow_handler(hass):
|
||||
flow = config_flow.EsphomeFlowHandler()
|
||||
flow.hass = hass
|
||||
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)
|
||||
result = await flow.async_step_user(user_input=None)
|
||||
assert result["type"] == "form"
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"esphome", context={"source": "user"}, 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 flow.async_step_user(user_input={"host": "127.0.0.1", "port": 80})
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"esphome",
|
||||
context={"source": "user"},
|
||||
data={CONF_HOST: "127.0.0.1", CONF_PORT: 80},
|
||||
)
|
||||
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["data"] == {"host": "127.0.0.1", "port": 80, "password": ""}
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"] == {CONF_HOST: "127.0.0.1", CONF_PORT: 80, CONF_PASSWORD: ""}
|
||||
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
|
||||
@@ -70,8 +77,6 @@ async def test_user_connection_works(hass, mock_client):
|
||||
|
||||
async def test_user_resolve_error(hass, mock_api_connection_error, mock_client):
|
||||
"""Test user step with IP resolve error."""
|
||||
flow = _setup_flow_handler(hass)
|
||||
await flow.async_step_user(user_input=None)
|
||||
|
||||
class MockResolveError(mock_api_connection_error):
|
||||
"""Create an exception with a specific error message."""
|
||||
@@ -85,13 +90,16 @@ async def test_user_resolve_error(hass, mock_api_connection_error, mock_client):
|
||||
new_callable=lambda: MockResolveError,
|
||||
) as exc:
|
||||
mock_client.device_info.side_effect = exc
|
||||
result = await flow.async_step_user(
|
||||
user_input={"host": "127.0.0.1", "port": 6053}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"esphome",
|
||||
context={"source": "user"},
|
||||
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
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
|
||||
@@ -99,16 +107,18 @@ async def test_user_resolve_error(hass, mock_api_connection_error, mock_client):
|
||||
|
||||
async def test_user_connection_error(hass, mock_api_connection_error, mock_client):
|
||||
"""Test user step with connection error."""
|
||||
flow = _setup_flow_handler(hass)
|
||||
await flow.async_step_user(user_input=None)
|
||||
|
||||
mock_client.device_info.side_effect = mock_api_connection_error
|
||||
|
||||
result = await flow.async_step_user(user_input={"host": "127.0.0.1", "port": 6053})
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"esphome",
|
||||
context={"source": "user"},
|
||||
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
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
|
||||
@@ -116,125 +126,159 @@ async def test_user_connection_error(hass, mock_api_connection_error, mock_clien
|
||||
|
||||
async def test_user_with_password(hass, mock_client):
|
||||
"""Test user step with password."""
|
||||
flow = _setup_flow_handler(hass)
|
||||
await flow.async_step_user(user_input=None)
|
||||
|
||||
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(True, "test"))
|
||||
|
||||
result = await flow.async_step_user(user_input={"host": "127.0.0.1", "port": 6053})
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"esphome",
|
||||
context={"source": "user"},
|
||||
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "authenticate"
|
||||
|
||||
result = await flow.async_step_authenticate(user_input={"password": "password1"})
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_PASSWORD: "password1"}
|
||||
)
|
||||
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"] == {
|
||||
"host": "127.0.0.1",
|
||||
"port": 6053,
|
||||
"password": "password1",
|
||||
CONF_HOST: "127.0.0.1",
|
||||
CONF_PORT: 6053,
|
||||
CONF_PASSWORD: "password1",
|
||||
}
|
||||
assert mock_client.password == "password1"
|
||||
|
||||
|
||||
async def test_user_invalid_password(hass, mock_api_connection_error, mock_client):
|
||||
"""Test user step with invalid password."""
|
||||
flow = _setup_flow_handler(hass)
|
||||
await flow.async_step_user(user_input=None)
|
||||
|
||||
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(True, "test"))
|
||||
|
||||
await flow.async_step_user(user_input={"host": "127.0.0.1", "port": 6053})
|
||||
mock_client.connect.side_effect = mock_api_connection_error
|
||||
result = await flow.async_step_authenticate(user_input={"password": "invalid"})
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"esphome",
|
||||
context={"source": "user"},
|
||||
data={CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "authenticate"
|
||||
|
||||
mock_client.connect.side_effect = mock_api_connection_error
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_PASSWORD: "invalid"}
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "authenticate"
|
||||
assert result["errors"] == {"base": "invalid_password"}
|
||||
|
||||
|
||||
async def test_discovery_initiation(hass, mock_client):
|
||||
"""Test discovery importing works."""
|
||||
flow = _setup_flow_handler(hass)
|
||||
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test8266"))
|
||||
|
||||
service_info = {
|
||||
"host": "192.168.43.183",
|
||||
"port": 6053,
|
||||
"hostname": "test8266.local.",
|
||||
"properties": {},
|
||||
}
|
||||
flow = await hass.config_entries.flow.async_init(
|
||||
"esphome", context={"source": "zeroconf"}, data=service_info
|
||||
)
|
||||
|
||||
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test8266"))
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow["flow_id"], user_input={}
|
||||
)
|
||||
|
||||
result = await flow.async_step_zeroconf(user_input=service_info)
|
||||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "discovery_confirm"
|
||||
assert result["description_placeholders"]["name"] == "test8266"
|
||||
assert flow.context["title_placeholders"]["name"] == "test8266"
|
||||
|
||||
result = await flow.async_step_discovery_confirm(user_input={})
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == "test8266"
|
||||
assert result["data"]["host"] == "test8266.local"
|
||||
assert result["data"]["port"] == 6053
|
||||
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."""
|
||||
MockConfigEntry(
|
||||
domain="esphome", data={"host": "test8266.local", "port": 6053, "password": ""}
|
||||
).add_to_hass(hass)
|
||||
entry = MockConfigEntry(
|
||||
domain="esphome",
|
||||
data={CONF_HOST: "test8266.local", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
||||
)
|
||||
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
flow = _setup_flow_handler(hass)
|
||||
service_info = {
|
||||
"host": "192.168.43.183",
|
||||
"port": 6053,
|
||||
"hostname": "test8266.local.",
|
||||
"properties": {},
|
||||
}
|
||||
result = await flow.async_step_zeroconf(user_input=service_info)
|
||||
assert result["type"] == "abort"
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"esphome", context={"source": "zeroconf"}, data=service_info
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
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."""
|
||||
MockConfigEntry(
|
||||
domain="esphome", data={"host": "192.168.43.183", "port": 6053, "password": ""}
|
||||
).add_to_hass(hass)
|
||||
entry = MockConfigEntry(
|
||||
domain="esphome",
|
||||
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
||||
)
|
||||
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
flow = _setup_flow_handler(hass)
|
||||
service_info = {
|
||||
"host": "192.168.43.183",
|
||||
"port": 6053,
|
||||
"hostname": "test8266.local.",
|
||||
"properties": {"address": "192.168.43.183"},
|
||||
}
|
||||
result = await flow.async_step_zeroconf(user_input=service_info)
|
||||
assert result["type"] == "abort"
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"esphome", context={"source": "zeroconf"}, data=service_info
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
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="esphome", data={"host": "192.168.43.183", "port": 6053, "password": ""}
|
||||
domain="esphome",
|
||||
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
mock_entry_data = MagicMock()
|
||||
mock_entry_data.device_info.name = "test8266"
|
||||
hass.data[DATA_KEY] = {entry.entry_id: mock_entry_data}
|
||||
|
||||
flow = _setup_flow_handler(hass)
|
||||
service_info = {
|
||||
"host": "192.168.43.183",
|
||||
"host": "192.168.43.184",
|
||||
"port": 6053,
|
||||
"hostname": "test8266.local.",
|
||||
"properties": {"address": "test8266.local"},
|
||||
}
|
||||
result = await flow.async_step_zeroconf(user_input=service_info)
|
||||
assert result["type"] == "abort"
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"esphome", context={"source": "zeroconf"}, data=service_info
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
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."""
|
||||
@@ -250,11 +294,36 @@ async def test_discovery_duplicate_data(hass, mock_client):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"esphome", data=service_info, context={"source": "zeroconf"}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "discovery_confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"esphome", data=service_info, context={"source": "zeroconf"}
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
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="esphome",
|
||||
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": "zeroconf"}, data=service_info
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
assert entry.unique_id == "test8266"
|
||||
Reference in new issue
Block a user