Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle missing or incorrect device name and unique id for ESPHome during manual add #95678

Merged
merged 11 commits into from
Jul 2, 2023
Prev Previous commit
Next Next commit
add more coverage
  • Loading branch information
bdraco committed Jul 1, 2023
commit 6514c05cb527624c26a1e75ab7ec818ef6c1c3d6
72 changes: 62 additions & 10 deletions tests/components/esphome/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ async def test_user_sets_unique_id(
assert discovery_result["type"] == FlowResultType.FORM
assert discovery_result["step_id"] == "discovery_confirm"

discovery_result = await hass.config_entries.flow.async_configure(
discovery_result["flow_id"],
{},
)
assert discovery_result["type"] == FlowResultType.CREATE_ENTRY
assert discovery_result["data"] == {
CONF_HOST: "192.168.43.183",
CONF_PORT: 6053,
CONF_PASSWORD: "",
CONF_NOISE_PSK: "",
CONF_DEVICE_NAME: "test",
}

result = await hass.config_entries.flow.async_init(
"esphome",
context={"source": config_entries.SOURCE_USER},
Expand All @@ -153,16 +166,8 @@ async def test_user_sets_unique_id(
result["flow_id"],
{CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
)
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["data"] == {
CONF_HOST: "127.0.0.1",
CONF_PORT: 6053,
CONF_PASSWORD: "",
CONF_NOISE_PSK: "",
CONF_DEVICE_NAME: "test",
}

assert not hass.config_entries.flow.async_progress_by_handler(DOMAIN)
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "already_configured"


async def test_user_resolve_error(
Expand Down Expand Up @@ -190,6 +195,53 @@ async def test_user_resolve_error(
assert len(mock_client.disconnect.mock_calls) == 1


async def test_user_causes_zeroconf_to_abort(
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
) -> None:
"""Test that the user flow sets the unique id and aborts the zeroconf flow."""
service_info = zeroconf.ZeroconfServiceInfo(
host="192.168.43.183",
addresses=["192.168.43.183"],
hostname="test8266.local.",
name="mock_name",
port=6053,
properties={
"mac": "1122334455aa",
},
type="mock_type",
)
discovery_result = await hass.config_entries.flow.async_init(
"esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info
)

assert discovery_result["type"] == FlowResultType.FORM
assert discovery_result["step_id"] == "discovery_confirm"

result = await hass.config_entries.flow.async_init(
"esphome",
context={"source": config_entries.SOURCE_USER},
data=None,
)

assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "127.0.0.1", CONF_PORT: 6053},
)
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["data"] == {
CONF_HOST: "127.0.0.1",
CONF_PORT: 6053,
CONF_PASSWORD: "",
CONF_NOISE_PSK: "",
CONF_DEVICE_NAME: "test",
}

assert not hass.config_entries.flow.async_progress_by_handler(DOMAIN)


async def test_user_connection_error(
hass: HomeAssistant, mock_client, mock_zeroconf: None, mock_setup_entry: None
) -> None:
Expand Down